MCPcopy Index your code
hub / github.com/coder/coder / TestStatusWriter

Function TestStatusWriter

coderd/tracing/status_writer_test.go:18–175  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

16)
17
18func 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")

Callers

nothing calls this directly

Calls 15

WriteHeaderMethod · 0.95
WriteMethod · 0.95
ResponseBodyMethod · 0.95
HijackMethod · 0.95
UnwrapMethod · 0.95
StatusWriterMiddlewareFunction · 0.92
SetWriteDeadlineMethod · 0.80
RunMethod · 0.65
ReadMethod · 0.65
AddMethod · 0.65
CleanupMethod · 0.65
ContextMethod · 0.65

Tested by

no test coverage detected