Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Add support for HTTP Method HEAD #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func TestTCPDialCheck(t *testing.T) {
}

func TestHTTPGetCheck(t *testing.T) {
assert.NoError(t, HTTPGetCheck("https://heptio.com", 5*time.Second)())
assert.Error(t, HTTPGetCheck("http://heptio.com", 5*time.Second)(), "redirect should fail")
assert.Error(t, HTTPGetCheck("https://heptio.com/nonexistent", 5*time.Second)(), "404 should fail")
assert.NoError(t, HTTPGetCheck("https://heptio.cloud.vmware.com", 5*time.Second)())
assert.Error(t, HTTPGetCheck("http://heptio.cloud.vmware.com", 5*time.Second)(), "redirect should fail")
assert.Error(t, HTTPGetCheck("https://heptio.cloud.vmware.com/nonexistent", 5*time.Second)(), "404 should fail")
}

func TestDatabasePingCheck(t *testing.T) {
Expand Down
7 changes: 6 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func (s *basicHandler) collectChecks(checks map[string]Check, resultsOut map[str
}

func (s *basicHandler) handle(w http.ResponseWriter, r *http.Request, checks ...map[string]Check) {
if r.Method != http.MethodGet {

if r.Method != http.MethodGet && r.Method != http.MethodHead {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
Expand All @@ -88,6 +89,10 @@ func (s *basicHandler) handle(w http.ResponseWriter, r *http.Request, checks ...
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)

if r.Method == http.MethodHead {
return
}

// unless ?full=1, return an empty body. Kubernetes only cares about the
// HTTP status code, so we won't waste bytes on the full body.
if r.URL.Query().Get("full") != "1" {
Expand Down
36 changes: 36 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,42 @@ func TestNewHandler(t *testing.T) {
expect: http.StatusServiceUnavailable,
expectBody: "{}\n",
},
{
name: "HEAD with no checks, /live should succeed",
method: "HEAD",
path: "/live",
live: true,
ready: true,
expect: http.StatusOK,
expectBody: "",
},
{
name: "HEAD with no checks, /ready should succeed",
method: "HEAD",
path: "/ready",
live: true,
ready: true,
expect: http.StatusOK,
expectBody: "",
},
{
name: "HEAD with a failing liveness check, /live should fail",
method: "HEAD",
path: "/live",
live: false,
ready: true,
expect: http.StatusServiceUnavailable,
expectBody: "",
},
{
name: "HEAD with a failing liveness check, /ready should fail",
method: "HEAD",
path: "/ready",
live: false,
ready: true,
expect: http.StatusServiceUnavailable,
expectBody: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down