Improve unit tests for InstallPathHandler

When adding InstallPathHandler it was suggested to follow-up with an improvement to the unit tests.
This commit is contained in:
Jacob Tanenbaum 2018-05-21 11:09:13 -04:00
parent 080739a12a
commit 1a0eb8c7b6

View File

@ -74,20 +74,20 @@ func TestInstallPathHandler(t *testing.T) {
} }
func TestMulitipleChecks(t *testing.T) { func testMultipleChecks(path string, t *testing.T) {
tests := []struct { tests := []struct {
path string path string
expectedResponse string expectedResponse string
expectedStatus int expectedStatus int
addBadCheck bool addBadCheck bool
}{ }{
{"/healthz?verbose", "[+]ping ok\nhealthz check passed\n", http.StatusOK, false}, {"?verbose", "[+]ping ok\nhealthz check passed\n", http.StatusOK, false},
{"/healthz/ping", "ok", http.StatusOK, false}, {"/ping", "ok", http.StatusOK, false},
{"/healthz", "ok", http.StatusOK, false}, {"", "ok", http.StatusOK, false},
{"/healthz?verbose", "[+]ping ok\n[-]bad failed: reason withheld\nhealthz check failed\n", http.StatusInternalServerError, true}, {"?verbose", "[+]ping ok\n[-]bad failed: reason withheld\nhealthz check failed\n", http.StatusInternalServerError, true},
{"/healthz/ping", "ok", http.StatusOK, true}, {"/ping", "ok", http.StatusOK, true},
{"/healthz/bad", "internal server error: this will fail\n", http.StatusInternalServerError, true}, {"/bad", "internal server error: this will fail\n", http.StatusInternalServerError, true},
{"/healthz", "[+]ping ok\n[-]bad failed: reason withheld\nhealthz check failed\n", http.StatusInternalServerError, true}, {"", "[+]ping ok\n[-]bad failed: reason withheld\nhealthz check failed\n", http.StatusInternalServerError, true},
} }
for i, test := range tests { for i, test := range tests {
@ -98,8 +98,13 @@ func TestMulitipleChecks(t *testing.T) {
return errors.New("this will fail") return errors.New("this will fail")
})) }))
} }
if path == "" {
InstallHandler(mux, checks...) InstallHandler(mux, checks...)
req, err := http.NewRequest("GET", fmt.Sprintf("http://example.com%v", test.path), nil) path = "/healthz"
} else {
InstallPathHandler(mux, path, checks...)
}
req, err := http.NewRequest("GET", fmt.Sprintf("http://example.com%s%v", path, test.path), nil)
if err != nil { if err != nil {
t.Fatalf("case[%d] Unexpected error: %v", i, err) t.Fatalf("case[%d] Unexpected error: %v", i, err)
} }
@ -114,6 +119,14 @@ func TestMulitipleChecks(t *testing.T) {
} }
} }
func TestMultipleChecks(t *testing.T) {
testMultipleChecks("", t)
}
func TestMultiplePathChecks(t *testing.T) {
testMultipleChecks("/ready", t)
}
func TestCheckerNames(t *testing.T) { func TestCheckerNames(t *testing.T) {
n1 := "n1" n1 := "n1"
n2 := "n2" n2 := "n2"