diff --git a/cmd/placeholder-name/main.go b/cmd/placeholder-name/main.go index 39d548d7..71ef3c23 100644 --- a/cmd/placeholder-name/main.go +++ b/cmd/placeholder-name/main.go @@ -6,9 +6,10 @@ SPDX-License-Identifier: Apache-2.0 package main import ( - "github.com/suzerain-io/placeholder-name/pkg/handlers" "log" "net/http" + + "github.com/suzerain-io/placeholder-name/pkg/handlers" ) func main() { diff --git a/pkg/handlers/handlers.go b/pkg/handlers/handlers.go index 2e722edf..fce170b5 100644 --- a/pkg/handlers/handlers.go +++ b/pkg/handlers/handlers.go @@ -7,7 +7,7 @@ package handlers import "net/http" -const JsonMimeType = "application/json; charset=utf-8" +const JSONMimeType = "application/json; charset=utf-8" const HeaderNameContentType = "Content-Type" func New() http.Handler { diff --git a/pkg/handlers/healthz_handler.go b/pkg/handlers/healthz_handler.go index 791070f9..9e34407e 100644 --- a/pkg/handlers/healthz_handler.go +++ b/pkg/handlers/healthz_handler.go @@ -19,7 +19,7 @@ type healthzHandler struct{} func (h healthzHandler) ServeHTTP(responseWriter http.ResponseWriter, _ *http.Request) { response := healthzResponse{"OK"} js, _ := json.Marshal(response) - responseWriter.Header().Set(HeaderNameContentType, JsonMimeType) + responseWriter.Header().Set(HeaderNameContentType, JSONMimeType) _, _ = responseWriter.Write(js) } diff --git a/pkg/handlers/healthz_handler_test.go b/pkg/handlers/healthz_handler_test.go index b04d0990..a79950c8 100644 --- a/pkg/handlers/healthz_handler_test.go +++ b/pkg/handlers/healthz_handler_test.go @@ -6,23 +6,37 @@ SPDX-License-Identifier: Apache-2.0 package handlers_test import ( - "github.com/stretchr/testify/require" - "github.com/suzerain-io/placeholder-name/pkg/handlers" + "context" "io/ioutil" "net/http" "net/http/httptest" "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/suzerain-io/placeholder-name/pkg/handlers" ) func TestHealthzReturnsOkWithJsonBody(t *testing.T) { expect := require.New(t) server := httptest.NewServer(handlers.New()) defer server.Close() - client := http.Client{} - response, err := client.Get(server.URL + "/healthz") + // Create a request context with a short timeout. + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) + defer cancel() + // Initialize an HTTP GET request to /healthz + req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL+"/healthz", nil) expect.NoError(err) + + // Perform the request and assert that we received a response. + response, err := http.DefaultClient.Do(req) + expect.NoError(err) + defer response.Body.Close() + + // Assert that we got an HTTP 200 with the correct content type and JSON body. expect.Equal(http.StatusOK, response.StatusCode) expect.Equal("application/json; charset=utf-8", response.Header.Get("content-type")) body, err := ioutil.ReadAll(response.Body)