MCPcopy
hub / github.com/grafana/dskit / TestDisabledSuccessfulRequestsLogging

Function TestDisabledSuccessfulRequestsLogging

middleware/logging_test.go:61–106  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

59}
60
61func TestDisabledSuccessfulRequestsLogging(t *testing.T) {
62 for _, tc := range []struct {
63 err error
64 disableLog bool
65 logContains string
66 }{
67 {
68 err: nil,
69 disableLog: false,
70 }, {
71 err: nil,
72 disableLog: true,
73 logContains: "",
74 },
75 } {
76 buf := bytes.NewBuffer(nil)
77
78 loggingMiddleware := Log{
79 Log: log.NewGoKitWithWriter(log.LogfmtFormat, buf),
80 DisableRequestSuccessLog: tc.disableLog,
81 }
82
83 handler := func(w http.ResponseWriter, _ *http.Request) {
84 _, err := io.WriteString(w, "<html><body>Hello World!</body></html>")
85 require.NoError(t, err) //nolint:errcheck
86 }
87 loggingHandler := loggingMiddleware.Wrap(http.HandlerFunc(handler))
88
89 req := httptest.NewRequest("GET", "http://example.com/foo", nil)
90 recorder := httptest.NewRecorder()
91
92 w := errorWriter{
93 err: tc.err,
94 w: recorder,
95 }
96 loggingHandler.ServeHTTP(w, req)
97 content := buf.String()
98
99 if !tc.disableLog {
100 require.Contains(t, content, "GET http://example.com/foo (200)")
101 } else {
102 require.NotContains(t, content, "(200)")
103 require.Empty(t, content)
104 }
105 }
106}
107
108func TestLoggingRequestsAtInfoLevel(t *testing.T) {
109 for _, tc := range []struct {

Callers

nothing calls this directly

Calls 4

WrapMethod · 0.95
NewGoKitWithWriterFunction · 0.92
StringMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected