(t *testing.T)
| 106 | } |
| 107 | |
| 108 | func 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 | |
| 145 | func TestLoggingRequestWithExcludedHeaders(t *testing.T) { |
| 146 | defaultHeaders := []string{"Authorization", "Cookie", "X-Csrf-Token", "X-Grafana-Id", "X-Access-Token"} |
nothing calls this directly
no test coverage detected