package server import ( "net/http" "net/http/httptest" "strings" "testing" "github.com/prometheus/client_golang/prometheus/promhttp" ) // TestNoMetricsRecordedWithoutMetricsUsername checks that with no metrics // username set the router records nothing about the requests it serves. // /metrics is not served then, so the process-wide Prometheus registry is // read directly. TestMaintenanceModeKeepsOtherRoutes records into the same // registry, but never a GET /robots.txt. func TestNoMetricsRecordedWithoutMetricsUsername(t *testing.T) { t.Parallel() s := newTestServer(t) s.ServeHTTP(httptest.NewRecorder(), httptest.NewRequestWithContext( t.Context(), http.MethodGet, "/robots.txt", nil)) rec := httptest.NewRecorder() promhttp.Handler().ServeHTTP(rec, httptest.NewRequestWithContext( t.Context(), http.MethodGet, "/metrics", nil)) if strings.Contains(rec.Body.String(), `handler="/robots.txt"`) { t.Errorf("with no metrics username GET /robots.txt was recorded:\n%s", rec.Body.String()) } }