package proxy_test import ( "net/http" "sync/atomic" "testing" "sneak.berlin/go/smallwebwaf/internal/proxy" "sneak.berlin/go/smallwebwaf/internal/requestlog" ) func TestHealthEndpointIsAnsweredBeforeAnyCheck(t *testing.T) { t.Parallel() var calls atomic.Int32 app := startApp(t, func(http.ResponseWriter, *http.Request) { calls.Add(1) }) // With a limit of one request a minute, any request counted before // the last one would have it refused. addr, out := startProxy(t, app.URL, map[string]string{rateLimitPerMinute: "1"}) const ( healthChecks = 3 contentType = "text/plain; charset=utf-8" ) for range healthChecks { got := get(t, addr, proxy.HealthPath) wantStatus(t, got, http.StatusOK) if string(got.body) != "ok\n" || got.header.Get("Content-Type") != contentType { t.Errorf("health endpoint answered %q with Content-Type %q, want ok "+ "with %q", got.body, got.header.Get("Content-Type"), contentType) } } wantStatus(t, get(t, addr, "/"), http.StatusOK) lines := out.requestLines(t, healthChecks+1) for _, line := range lines[:healthChecks] { wantLine(t, line, http.StatusOK, requestlog.ActionAdmin) if line.ResponseContentType != contentType { t.Errorf("health check's log line has response_content_type %q, "+ "want %q", line.ResponseContentType, contentType) } } wantLine(t, lines[healthChecks], http.StatusOK, requestlog.ActionForward) if calls.Load() != 1 { t.Errorf("the app was called %d times, want once", calls.Load()) } }