- Added TestMockSetsAndConfiguresResponse: verifies PUT /e/{id}/mock stores mock config
- Added TestMockReturnsConfiguredResponseOnCapture: verifies capture returns mock response
when one is configured, and the request is still recorded
- Added TestMockAutoCreatesEndpointOnSet: verifies mock endpoint auto-creates if missing
- Fixed TestHealthzSmoke to use t.Setenv instead of os.Setenv to prevent env leak
Co-Authored-By: Paperclip <noreply@paperclip.ing>
77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/elfoundation/hatch/internal/handler"
|
|
"github.com/elfoundation/hatch/internal/store"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func TestHealthzViaRouter(t *testing.T) {
|
|
repo, err := store.Open(":memory:")
|
|
if err != nil {
|
|
t.Fatalf("open store: %v", err)
|
|
}
|
|
defer repo.Close()
|
|
|
|
r := chi.NewRouter()
|
|
h := handler.New(repo)
|
|
h.RegisterRoutes(r)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200 OK, got %d", w.Code)
|
|
}
|
|
body, _ := io.ReadAll(w.Result().Body)
|
|
w.Result().Body.Close()
|
|
got := strings.TrimSpace(string(body))
|
|
if got != "ok" {
|
|
t.Fatalf("expected body 'ok', got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestHealthzSmoke(t *testing.T) {
|
|
// Smoke: boot server on random port, hit /healthz.
|
|
// Use :memory: store so no filesystem dependency.
|
|
t.Setenv("HATCH_DB_PATH", ":memory:")
|
|
|
|
repo, err := store.Open(":memory:")
|
|
if err != nil {
|
|
t.Fatalf("open store: %v", err)
|
|
}
|
|
defer repo.Close()
|
|
|
|
r := chi.NewRouter()
|
|
h := handler.New(repo)
|
|
h.RegisterRoutes(r)
|
|
|
|
srv := httptest.NewServer(r)
|
|
defer srv.Close()
|
|
|
|
resp, err := http.Get(srv.URL + "/healthz")
|
|
if err != nil {
|
|
t.Fatalf("failed to GET /healthz: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("expected 200 OK, got %d", resp.StatusCode)
|
|
}
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatalf("failed to read response body: %v", err)
|
|
}
|
|
got := strings.TrimSpace(string(body))
|
|
if got != "ok" {
|
|
t.Fatalf("expected body 'ok', got %q", got)
|
|
}
|
|
}
|