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

Function TestLoggingRequestsAtInfoLevel

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

Source from the content-addressed store, hash-verified

106}
107
108func TestLoggingRequestsAtInfoLevel(t *testing.T) {
109 for _, tc := range []struct {
110 err error
111 logContains []string
112 }{{
113 err: context.Canceled,
114 logContains: []string{"info", "request cancelled: context canceled"},
115 }, {
116 err: nil,
117 logContains: []string{"info", "GET http://example.com/foo (200)"},
118 }} {
119 buf := bytes.NewBuffer(nil)
120
121 loggingMiddleware := Log{
122 Log: log.NewGoKitWithWriter(log.LogfmtFormat, buf),
123 LogRequestAtInfoLevel: true,
124 }
125 handler := func(w http.ResponseWriter, _ *http.Request) {
126 _, _ = io.WriteString(w, "<html><body>Hello World!</body></html>")
127 }
128 loggingHandler := loggingMiddleware.Wrap(http.HandlerFunc(handler))
129
130 req := httptest.NewRequest("GET", "http://example.com/foo", nil)
131 recorder := httptest.NewRecorder()
132
133 w := errorWriter{
134 err: tc.err,
135 w: recorder,
136 }
137 loggingHandler.ServeHTTP(w, req)
138
139 for _, content := range tc.logContains {
140 require.True(t, bytes.Contains(buf.Bytes(), []byte(content)))
141 }
142 }
143}
144
145func TestLoggingRequestWithExcludedHeaders(t *testing.T) {
146 defaultHeaders := []string{"Authorization", "Cookie", "X-Csrf-Token", "X-Grafana-Id", "X-Access-Token"}

Callers

nothing calls this directly

Calls 3

WrapMethod · 0.95
NewGoKitWithWriterFunction · 0.92
ServeHTTPMethod · 0.45

Tested by

no test coverage detected