Commit b6cd2f4f authored by Geoff Simmons's avatar Geoff Simmons

Set an Allow header in 405 responses.

parent 9fe1e308
...@@ -52,6 +52,8 @@ var ( ...@@ -52,6 +52,8 @@ var (
http.MethodGet: struct{}{}, http.MethodGet: struct{}{},
http.MethodHead: struct{}{}, http.MethodHead: struct{}{},
} }
allowHealthz = "GET, HEAD"
allowedPems = map[string]struct{}{ allowedPems = map[string]struct{}{
http.MethodGet: struct{}{}, http.MethodGet: struct{}{},
http.MethodHead: struct{}{}, http.MethodHead: struct{}{},
...@@ -59,6 +61,7 @@ var ( ...@@ -59,6 +61,7 @@ var (
http.MethodPost: struct{}{}, http.MethodPost: struct{}{},
http.MethodPut: struct{}{}, http.MethodPut: struct{}{},
} }
allowPems = "GET, HEAD, PUT, POST, DELETE"
) )
// Date format for Common Log Format // Date format for Common Log Format
...@@ -100,6 +103,7 @@ func (h *healthzHndlr) ServeHTTP(resp http.ResponseWriter, req *http.Request) { ...@@ -100,6 +103,7 @@ func (h *healthzHndlr) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
status = http.StatusNotFound status = http.StatusNotFound
} else if _, ok := allowedHealthz[req.Method]; !ok { } else if _, ok := allowedHealthz[req.Method]; !ok {
status = http.StatusMethodNotAllowed status = http.StatusMethodNotAllowed
resp.Header().Set("Allow", allowHealthz)
} else { } else {
status = http.StatusNoContent status = http.StatusNoContent
resp.Header().Del("Content-Length") resp.Header().Del("Content-Length")
...@@ -147,6 +151,7 @@ func (h *pemsHndlr) ServeHTTP(resp http.ResponseWriter, req *http.Request) { ...@@ -147,6 +151,7 @@ func (h *pemsHndlr) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
if _, ok := allowedPems[req.Method]; !ok { if _, ok := allowedPems[req.Method]; !ok {
status = http.StatusMethodNotAllowed status = http.StatusMethodNotAllowed
resp.Header().Set("Allow", allowPems)
resp.WriteHeader(status) resp.WriteHeader(status)
reqLog(h.log, req, now, status, bytes) reqLog(h.log, req, now, status, bytes)
return return
......
...@@ -122,6 +122,11 @@ func TestHealthz405(t *testing.T) { ...@@ -122,6 +122,11 @@ func TestHealthz405(t *testing.T) {
t.Errorf("%s /v1/healthz status: got %d want %d", t.Errorf("%s /v1/healthz status: got %d want %d",
method, rr.Code, http.StatusMethodNotAllowed) method, rr.Code, http.StatusMethodNotAllowed)
} }
if rr.Result().Header.Get("Allow") != allowHealthz {
t.Errorf("%s /v1/healthz Allow: got %s want %s",
method, rr.Result().Header.Get("Allow"),
allowHealthz)
}
if rr.Result().Header.Get("Content-Length") != "0" { if rr.Result().Header.Get("Content-Length") != "0" {
t.Errorf("%s /v1/healthz Content-Length: got %s want 0", t.Errorf("%s /v1/healthz Content-Length: got %s want 0",
method, rr.Result().Header. method, rr.Result().Header.
...@@ -753,6 +758,11 @@ func TestPem405(t *testing.T) { ...@@ -753,6 +758,11 @@ func TestPem405(t *testing.T) {
t.Errorf("%s /v1/pems/ns/name status: got %d want %d", t.Errorf("%s /v1/pems/ns/name status: got %d want %d",
method, rr.Code, http.StatusMethodNotAllowed) method, rr.Code, http.StatusMethodNotAllowed)
} }
if rr.Result().Header.Get("Allow") != allowPems {
t.Errorf("%s /v1/pems/ns/name Allow: got %s want %s",
method, rr.Result().Header.Get("Allow"),
allowPems)
}
if rr.Result().Header.Get("Content-Length") != "0" { if rr.Result().Header.Get("Content-Length") != "0" {
t.Errorf("%s /v1/pems/ns/name Content-Length: "+ t.Errorf("%s /v1/pems/ns/name Content-Length: "+
"got %s want 0", method, "got %s want 0", method,
......
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