(t *testing.T)
| 9 | ) |
| 10 | |
| 11 | func TestLoggerMiddleware(t *testing.T) { |
| 12 | handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 13 | w.WriteHeader(http.StatusOK) |
| 14 | w.Write([]byte("test")) |
| 15 | }) |
| 16 | var out strings.Builder |
| 17 | |
| 18 | w := httptest.NewRecorder() |
| 19 | req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 20 | req.Header.Add("X-Real-Ip", "127.0.0.1") |
| 21 | logHandler := withLogging(nil, &out)(handler) |
| 22 | logHandler.ServeHTTP(w, req) |
| 23 | |
| 24 | if w.Code != http.StatusOK { |
| 25 | t.Errorf("expected %d, got %d", http.StatusOK, w.Code) |
| 26 | } |
| 27 | const expectedPiece = `127.0.0.1 GET "/"` |
| 28 | if !strings.Contains(out.String(), expectedPiece) { |
| 29 | t.Errorf("expected %q, got %q", expectedPiece, out.String()) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestLoggerMiddlewareWithPanic(t *testing.T) { |
| 34 | handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
nothing calls this directly
no test coverage detected