Commit cc35fbcb authored by Geoff Simmons's avatar Geoff Simmons

REST API validates condtional requests for /v1/pems (I-M-S only).

parent 82f18bed
......@@ -250,20 +250,37 @@ func (h *pemsHndlr) allPems(
}
// XXX check Accept
// XXX I-M-S and I-N-M
allInfo, err := h.files.GetAllFileInfo()
if err != nil {
h.errorResponse(resp, req, now, http.StatusInternalServerError,
errPemsReadErr, err)
return
}
resp.Header().Set("Last-Modified",
h.files.ModTime().UTC().Format(http.TimeFormat))
ims := req.Header.Get("If-Modified-Since")
if ims != "" {
since, err := time.Parse(http.TimeFormat, ims)
if err != nil {
h.log.Errorf("%s %s: cannot parse If-Modified-Since "+
"\"%s\": %v", req.Method, req.RequestURI, ims,
err)
} else if !h.files.ModTime().UTC().Truncate(time.Second).
After(since.UTC()) {
resp.Header().Del("Content-Length")
resp.WriteHeader(http.StatusNotModified)
reqLog(h.log, req, now, http.StatusNotModified, 0)
return
}
}
jsonBytes, err := json.Marshal(allInfo)
if err != nil {
h.errorResponse(resp, req, now, http.StatusInternalServerError,
errPemsJSONMarshal, err)
return
}
// XXX LastModified and ETag
resp.Header().Set("Content-Type", jsonContentType)
resp.Header().Set("Content-Length", strconv.Itoa(len(jsonBytes)))
n := 0
......
......@@ -880,3 +880,86 @@ func TestValidatePem(t *testing.T) {
t.Error("GET /v1/pems/ns1/cafe body len: got 0 want > 0")
}
}
func TestValidateAllPems(t *testing.T) {
client := fake.NewSimpleClientset()
lister := setupSecretLister(client)
getter := crt.NewGetter(lister)
files, err := pem.NewFiles(basedir, -1, getter)
if err != nil {
t.Fatalf("NewFiles(): %v", err)
}
files.Files["ns1/cafe"] = cafeFile
hndlr := &pemsHndlr{
log: &logrus.Logger{Out: ioutil.Discard},
files: files,
crtGetter: getter,
}
req := httptest.NewRequest(http.MethodGet, "/v1/pems", nil)
rr := httptest.NewRecorder()
hndlr.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("GET /v1/pems status: got %d want %d", rr.Code,
http.StatusOK)
}
mtime, err := time.Parse(http.TimeFormat,
rr.Header().Get("Last-Modified"))
if err != nil {
t.Fatalf("GET /v1/pems: cannot parse Last-Modified %s: %v",
rr.Header().Get("Last-Modified"), err)
}
if !mtime.Truncate(time.Second).Equal(
files.ModTime().Truncate(time.Second)) {
t.Errorf("GET /v1/pems Last-Modified: got %s want %s",
rr.Header().Get("Last-Modified"), files.ModTime())
}
req = httptest.NewRequest(http.MethodGet, "/v1/pems", nil)
rr = httptest.NewRecorder()
req.Header.Set("If-Modified-Since",
files.ModTime().UTC().Format(http.TimeFormat))
hndlr.ServeHTTP(rr, req)
if rr.Code != http.StatusNotModified {
t.Errorf("GET /v1/pems status: got %d want %d", rr.Code,
http.StatusNotModified)
}
if rr.Body.Len() > 0 {
t.Errorf("GET /v1/pems body len: got %d want 0", rr.Body.Len())
}
if rr.Header().Get("Content-Type") != "" {
t.Errorf("GET /v1/pems Content-Type: got %s want empty",
rr.Header().Get("Content-Type"))
}
if rr.Header().Get("Content-Length") != "" {
t.Errorf("GET /v1/pems Content-Length: "+
"got %s want empty", rr.Header().Get("Content-Length"))
}
req = httptest.NewRequest(http.MethodGet, "/v1/pems", nil)
rr = httptest.NewRecorder()
req.Header.Set("If-Modified-Since",
files.ModTime().Add(time.Second).Format(http.TimeFormat))
hndlr.ServeHTTP(rr, req)
if rr.Code != http.StatusNotModified {
t.Errorf("GET /v1/pems status: got %d want %d", rr.Code,
http.StatusNotModified)
}
if rr.Body.Len() > 0 {
t.Errorf("GET /v1/pems body len: got %d want 0", rr.Body.Len())
}
req = httptest.NewRequest(http.MethodGet, "/v1/pems", nil)
rr = httptest.NewRecorder()
req.Header.Set("If-Modified-Since",
files.ModTime().Add(-1*time.Second).Format(http.TimeFormat))
hndlr.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("GET /v1/pems status: got %d want %d", rr.Code,
http.StatusOK)
}
if rr.Body.Len() == 0 {
t.Error("GET /v1/pems body len: got 0 want > 0")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment