1 package testutil 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 ) 8 9 // NewFakeExporter starts an httptest.Server that responds to any request with 10 // the provided Prometheus exposition-format body and Content-Type 11 // "text/plain; version=0.0.4". The server is automatically closed when the 12 // test finishes via t.Cleanup. 13 func NewFakeExporter(t *testing.T, body string) *httptest.Server { 14 t.Helper() 15 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { 16 w.Header().Set("Content-Type", "text/plain; version=0.0.4") 17 _, _ = w.Write([]byte(body)) 18 })) 19 t.Cleanup(srv.Close) 20 return srv 21 } 22