Skip to content

Commit 137226a

Browse files
committed
User proper test table for TestFailIfBodySizeTooLarge
1 parent 0ad871e commit 137226a

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

prober/http_test.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -851,17 +851,36 @@ func TestFailIfBodySizeTooLarge(t *testing.T) {
851851
}))
852852
defer ts.Close()
853853

854-
for _, failIfBodyTooLarge := range []bool{true, false} {
855-
registry := prometheus.NewRegistry()
856-
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
857-
defer cancel()
858-
result := ProbeHTTP(testCTX, ts.URL,
859-
config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, BodySizeLimit: bodySizeLimit, FailIfBodyTooLarge: &failIfBodyTooLarge}}, registry, promslog.NewNopLogger())
860-
if result && failIfBodyTooLarge {
861-
t.Fatal("Fail if body size too large succeeded unexpectedly")
862-
} else if !result && !failIfBodyTooLarge {
863-
t.Fatal("Dont't fail if body too large failed unexpectedly")
864-
}
854+
for title, tc := range map[string]struct {
855+
Config config.Module
856+
URL string
857+
Success bool
858+
MessageExpected bool
859+
}{
860+
"Read failure and message due to exeeded body size expected": {
861+
Config: config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, BodySizeLimit: bodySizeLimit, FailIfBodyTooLarge: true}},
862+
Success: false,
863+
MessageExpected: true,
864+
},
865+
"No read failure or message due to exeeded body size expected": {
866+
Config: config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{IPProtocolFallback: true, BodySizeLimit: bodySizeLimit, FailIfBodyTooLarge: false}},
867+
Success: true,
868+
MessageExpected: false,
869+
},
870+
} {
871+
t.Run(title, func(t *testing.T) {
872+
recorder := logRecorder{next: promslog.NewNopLogger()}
873+
registry := prometheus.NewRegistry()
874+
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
875+
defer cancel()
876+
result := ProbeHTTP(testCTX, ts.URL, tc.Config, registry, slog.New(&recorder))
877+
if result != tc.Success {
878+
t.Fatalf("Expected success=%v, got=%v", tc.Success, result)
879+
}
880+
if seen := recorder.msgs["Failed to read HTTP response body"]; seen != tc.MessageExpected {
881+
t.Fatalf("Read failure message expected=%v, seen=%v", tc.MessageExpected, seen)
882+
}
883+
})
865884
}
866885
}
867886

0 commit comments

Comments
 (0)