(t *testing.T)
| 14 | ) |
| 15 | |
| 16 | func TestBadResponseLoggingWriter(t *testing.T) { |
| 17 | for _, tc := range []struct { |
| 18 | statusCode int |
| 19 | data string |
| 20 | expected string |
| 21 | }{ |
| 22 | {http.StatusOK, "", ""}, |
| 23 | {http.StatusOK, "some data", ""}, |
| 24 | {http.StatusUnprocessableEntity, "unprocessable", ""}, |
| 25 | {http.StatusInternalServerError, "", ""}, |
| 26 | {http.StatusInternalServerError, "bad juju", "bad juju\n"}, |
| 27 | } { |
| 28 | w := httptest.NewRecorder() |
| 29 | var buf bytes.Buffer |
| 30 | wrapped := newBadResponseLoggingWriter(w, &buf) |
| 31 | switch { |
| 32 | case tc.data == "": |
| 33 | wrapped.WriteHeader(tc.statusCode) |
| 34 | case tc.statusCode < 300 && tc.data != "": |
| 35 | wrapped.WriteHeader(tc.statusCode) |
| 36 | _, err := wrapped.Write([]byte(tc.data)) |
| 37 | require.NoError(t, err) |
| 38 | default: |
| 39 | http.Error(wrapped, tc.data, tc.statusCode) |
| 40 | } |
| 41 | if wrapped.getStatusCode() != tc.statusCode { |
| 42 | t.Errorf("Wrong status code: have %d want %d", wrapped.getStatusCode(), tc.statusCode) |
| 43 | } |
| 44 | data := buf.String() |
| 45 | if data != tc.expected { |
| 46 | t.Errorf("Wrong data: have %q want %q", data, tc.expected) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // nonFlushingResponseWriter implements http.ResponseWriter but does not implement http.Flusher |
| 52 | type nonFlushingResponseWriter struct{} |
nothing calls this directly
no test coverage detected