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

Function TestBadResponseLoggingWriter

middleware/response_test.go:16–49  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func 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
52type nonFlushingResponseWriter struct{}

Callers

nothing calls this directly

Calls 7

ErrorfMethod · 0.80
getStatusCodeMethod · 0.65
StringMethod · 0.65
WriteHeaderMethod · 0.45
WriteMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected