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) {
if !barFound {
t.Errorf("GET /v1/pems did not return bar: %#v", allInfo)
}
if rr.Header().Get("Content-Length") != strconv.Itoa(rr.Body.Len()) {
t.Errorf("GET /v1/pems Content-Length: got %s want %d",
rr.Header().Get("Content-Length"), rr.Body.Len())
bodyLen := strconv.Itoa(rr.Body.Len())
if rr.Header().Get("Content-Length") != bodyLen {
t.Errorf("GET /v1/pems Content-Length: got %s want %s",
rr.Header().Get("Content-Length"), bodyLen)
}
if rr.Header().Get("Content-Type") != jsonContentType {
t.Errorf("GET /v1/pems Content-Type: got %s want %s",
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) {
......
......@@ -266,10 +266,15 @@ func (h *pemsHndlr) allPems(
// XXX LastModified and ETag
resp.Header().Set("Content-Type", jsonContentType)
resp.Header().Set("Content-Length", strconv.Itoa(len(jsonBytes)))
n, err := resp.Write(jsonBytes)
if err != nil {
h.log.Errorf("%s %s: cannot write body: %v", req.Method,
req.RequestURI, err)
n := 0
if req.Method == http.MethodGet {
n, err = resp.Write(jsonBytes)
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)
}
......
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