(t *testing.T)
| 16 | ) |
| 17 | |
| 18 | func TestStatusWriter(t *testing.T) { |
| 19 | t.Parallel() |
| 20 | |
| 21 | t.Run("WriteHeader", func(t *testing.T) { |
| 22 | t.Parallel() |
| 23 | |
| 24 | var ( |
| 25 | rec = httptest.NewRecorder() |
| 26 | w = &tracing.StatusWriter{ResponseWriter: rec} |
| 27 | ) |
| 28 | |
| 29 | w.WriteHeader(http.StatusOK) |
| 30 | require.Equal(t, http.StatusOK, w.Status) |
| 31 | // Validate that the code is written to the underlying Response. |
| 32 | require.Equal(t, http.StatusOK, rec.Code) |
| 33 | }) |
| 34 | |
| 35 | t.Run("WriteHeaderTwice", func(t *testing.T) { |
| 36 | t.Parallel() |
| 37 | |
| 38 | var ( |
| 39 | rec = httptest.NewRecorder() |
| 40 | w = &tracing.StatusWriter{ResponseWriter: rec} |
| 41 | code = http.StatusNotFound |
| 42 | ) |
| 43 | |
| 44 | w.WriteHeader(code) |
| 45 | w.WriteHeader(http.StatusOK) |
| 46 | // Validate that we only record the first status code. |
| 47 | require.Equal(t, code, w.Status) |
| 48 | // Validate that the code is written to the underlying Response. |
| 49 | require.Equal(t, code, rec.Code) |
| 50 | }) |
| 51 | |
| 52 | t.Run("WriteNoHeader", func(t *testing.T) { |
| 53 | t.Parallel() |
| 54 | var ( |
| 55 | rec = httptest.NewRecorder() |
| 56 | w = &tracing.StatusWriter{ResponseWriter: rec} |
| 57 | body = []byte("hello") |
| 58 | ) |
| 59 | |
| 60 | _, err := w.Write(body) |
| 61 | require.NoError(t, err) |
| 62 | |
| 63 | // Should set the status to OK. |
| 64 | require.Equal(t, http.StatusOK, w.Status) |
| 65 | // We don't record the body for codes <400. |
| 66 | require.Equal(t, []byte(nil), w.ResponseBody()) |
| 67 | require.Equal(t, body, rec.Body.Bytes()) |
| 68 | }) |
| 69 | |
| 70 | t.Run("WriteAfterHeader", func(t *testing.T) { |
| 71 | t.Parallel() |
| 72 | var ( |
| 73 | rec = httptest.NewRecorder() |
| 74 | w = &tracing.StatusWriter{ResponseWriter: rec} |
| 75 | body = []byte("hello") |
nothing calls this directly
no test coverage detected