You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
903 B
Go

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")
}
}