httputil: add explicit healthcheck handler (#354)

Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
This commit is contained in:
Bobby DeSimone 2019-10-07 17:38:28 -07:00 committed by GitHub
parent c0bcab5171
commit bca5caf77a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 13 deletions

View file

@ -0,0 +1,37 @@
package httputil
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHealthCheck(t *testing.T) {
t.Parallel()
tests := []struct {
name string
method string
wantStatus int
}{
{"good - Get", http.MethodGet, http.StatusOK},
{"good - Head", http.MethodHead, http.StatusOK},
{"bad - Options", http.MethodOptions, http.StatusMethodNotAllowed},
{"bad - Put", http.MethodPut, http.StatusMethodNotAllowed},
{"bad - Post", http.MethodPost, http.StatusMethodNotAllowed},
{"bad - route miss", http.MethodGet, http.StatusOK},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := httptest.NewRequest(tt.method, "/", nil)
w := httptest.NewRecorder()
HealthCheck(w, r)
if w.Code != tt.wantStatus {
t.Errorf("code differs. got %d want %d body: %s", w.Code, tt.wantStatus, w.Body.String())
}
})
}
}