From 598cf87ca8980f1a87c1c02f3e9dbb5a62bedce2 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Fri, 14 Mar 2025 14:33:17 -0400 Subject: [PATCH] Update for healthcheck and dev env --- cmd/api/healthcheck_test.go | 43 +++++++++++++++++++++++++++++++++++++ cmd/api/main_test.go | 15 ------------- 2 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 cmd/api/healthcheck_test.go delete mode 100644 cmd/api/main_test.go diff --git a/cmd/api/healthcheck_test.go b/cmd/api/healthcheck_test.go new file mode 100644 index 0000000..252b406 --- /dev/null +++ b/cmd/api/healthcheck_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "bytes" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestHealthRoute(t *testing.T) { + respRec := httptest.NewRecorder() + + r, err := http.NewRequest(http.MethodGet, "/v1/healthcheck", nil) + if err != nil { + t.Fatal(err) + } + cfg := config{env: "test"} + + app := application{config: cfg, logger: slog.New(slog.NewTextHandler(io.Discard, nil))} + + health := http.HandlerFunc(app.HealthCheckHandler) + health.ServeHTTP(respRec, r) + + resp := respRec.Result() + + if resp.StatusCode != http.StatusOK { + t.Fatalf("Got status code %q, wanted status code %q", resp.StatusCode, http.StatusOK) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatal(err) + } + + body = bytes.TrimSpace(body) + + if !strings.Contains(string(body), "environment: test") { + t.Fatalf("Did not find: %q in response body", "environment: test") + } +} diff --git a/cmd/api/main_test.go b/cmd/api/main_test.go deleted file mode 100644 index 9f79512..0000000 --- a/cmd/api/main_test.go +++ /dev/null @@ -1,15 +0,0 @@ -package main - -import ( - "testing" -) - -func TestRun(t *testing.T) { - // buffer := bytes.Buffer{} - // run(&buffer) - // got := buffer.String() - // want := "Hello API\n" - // if got != want { - // t.Errorf("got %q, want %q", got, want) - // } -}