From 037af751fa428fde5beb426bcbbfb06526fdd440 Mon Sep 17 00:00:00 2001 From: Daniel Teunis Date: Tue, 15 Nov 2022 00:00:23 +0100 Subject: [PATCH] Add HTTP request body test This commit adds a test for specifying a request body for HTTP probes. --- prober/http_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/prober/http_test.go b/prober/http_test.go index 69fcceefc..430b2b9a3 100644 --- a/prober/http_test.go +++ b/prober/http_test.go @@ -22,6 +22,7 @@ import ( "crypto/x509" "encoding/pem" "fmt" + "io/ioutil" "net/http" "net/http/httptest" "net/textproto" @@ -1405,3 +1406,28 @@ func TestSkipResolvePhase(t *testing.T) { checkMetrics(expectedMetrics, mfs, t) }) } + +func TestBody(t *testing.T) { + body := "Test Body" + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, err := ioutil.ReadAll(r.Body) + if err != nil { + t.Fatalf("Body test failed unexpectedly.") + } + if string(b) != body { + t.Fatalf("Body test failed unexpectedly.") + } + })) + defer ts.Close() + + registry := prometheus.NewRegistry() + testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{ + IPProtocolFallback: true, + Body: body, + }}, registry, log.NewNopLogger()) + if !result { + t.Fatalf("Body test failed unexpectedly.") + } +}