Skip to content

Commit

Permalink
Add HTTP request body test
Browse files Browse the repository at this point in the history
This commit adds a test for specifying a request body for HTTP probes.
  • Loading branch information
danteu committed Nov 14, 2022
1 parent 0e044cf commit 037af75
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions prober/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/textproto"
Expand Down Expand Up @@ -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.")
}
}

0 comments on commit 037af75

Please sign in to comment.