Commit 09965460 authored by Geoff Simmons's avatar Geoff Simmons

REST API: fix and test HEAD /v1/pems

parent 54459835
...@@ -281,14 +281,34 @@ func TestAllPems(t *testing.T) { ...@@ -281,14 +281,34 @@ func TestAllPems(t *testing.T) {
if !barFound { if !barFound {
t.Errorf("GET /v1/pems did not return bar: %#v", allInfo) t.Errorf("GET /v1/pems did not return bar: %#v", allInfo)
} }
if rr.Header().Get("Content-Length") != strconv.Itoa(rr.Body.Len()) { bodyLen := strconv.Itoa(rr.Body.Len())
t.Errorf("GET /v1/pems Content-Length: got %s want %d", if rr.Header().Get("Content-Length") != bodyLen {
rr.Header().Get("Content-Length"), rr.Body.Len()) t.Errorf("GET /v1/pems Content-Length: got %s want %s",
rr.Header().Get("Content-Length"), bodyLen)
} }
if rr.Header().Get("Content-Type") != jsonContentType { if rr.Header().Get("Content-Type") != jsonContentType {
t.Errorf("GET /v1/pems Content-Type: got %s want %s", t.Errorf("GET /v1/pems Content-Type: got %s want %s",
rr.Header().Get("Content-Type"), jsonContentType) rr.Header().Get("Content-Type"), jsonContentType)
} }
req = httptest.NewRequest(http.MethodHead, "/v1/pems", nil)
rr = httptest.NewRecorder()
hndlr.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("HEAD /v1/pems status: got %d want %d", rr.Code,
http.StatusOK)
}
if rr.Body.Len() != 0 {
t.Errorf("HEAD /v1/pems body len: got %d want 0", rr.Body.Len())
}
if rr.Header().Get("Content-Length") != bodyLen {
t.Errorf("HEAD /v1/pems Content-Length: got %s want %s",
rr.Header().Get("Content-Length"), bodyLen)
}
if rr.Header().Get("Content-Type") != jsonContentType {
t.Errorf("HEAD /v1/pems Content-Type: got %s want %s",
rr.Header().Get("Content-Type"), jsonContentType)
}
} }
func TestAllPems405(t *testing.T) { func TestAllPems405(t *testing.T) {
......
...@@ -266,10 +266,15 @@ func (h *pemsHndlr) allPems( ...@@ -266,10 +266,15 @@ func (h *pemsHndlr) allPems(
// XXX LastModified and ETag // XXX LastModified and ETag
resp.Header().Set("Content-Type", jsonContentType) resp.Header().Set("Content-Type", jsonContentType)
resp.Header().Set("Content-Length", strconv.Itoa(len(jsonBytes))) resp.Header().Set("Content-Length", strconv.Itoa(len(jsonBytes)))
n, err := resp.Write(jsonBytes) n := 0
if err != nil { if req.Method == http.MethodGet {
h.log.Errorf("%s %s: cannot write body: %v", req.Method, n, err = resp.Write(jsonBytes)
req.RequestURI, err) if err != nil {
h.log.Errorf("%s %s: cannot write body: %v", req.Method,
req.RequestURI, err)
}
} else {
resp.WriteHeader(http.StatusOK)
} }
reqLog(h.log, req, now, http.StatusOK, n) reqLog(h.log, req, now, http.StatusOK, n)
} }
......
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