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

Function TestBadWriteLogging

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

Source from the content-addressed store, hash-verified

19)
20
21func TestBadWriteLogging(t *testing.T) {
22 for _, tc := range []struct {
23 err error
24 logContains []string
25 }{{
26 err: context.Canceled,
27 logContains: []string{"debug", "request cancelled: context canceled"},
28 }, {
29 err: errors.New("yolo"),
30 logContains: []string{"warn", "error: yolo"},
31 }, {
32 err: nil,
33 logContains: []string{"debug", "GET http://example.com/foo (200)"},
34 }} {
35 buf := bytes.NewBuffer(nil)
36 logger := log.NewGoKitWithWriter(log.LogfmtFormat, buf)
37
38 loggingMiddleware := Log{
39 Log: logger,
40 }
41 handler := func(w http.ResponseWriter, _ *http.Request) {
42 _, _ = io.WriteString(w, "<html><body>Hello World!</body></html>")
43 }
44 loggingHandler := loggingMiddleware.Wrap(http.HandlerFunc(handler))
45
46 req := httptest.NewRequest("GET", "http://example.com/foo", nil)
47 recorder := httptest.NewRecorder()
48
49 w := errorWriter{
50 err: tc.err,
51 w: recorder,
52 }
53 loggingHandler.ServeHTTP(w, req)
54
55 for _, content := range tc.logContains {
56 require.True(t, bytes.Contains(buf.Bytes(), []byte(content)))
57 }
58 }
59}
60
61func TestDisabledSuccessfulRequestsLogging(t *testing.T) {
62 for _, tc := range []struct {

Callers

nothing calls this directly

Calls 3

WrapMethod · 0.95
NewGoKitWithWriterFunction · 0.92
ServeHTTPMethod · 0.45

Tested by

no test coverage detected