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

Function TestRecover

httpmw/recover_test.go:15–72  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13)
14
15func TestRecover(t *testing.T) {
16 t.Parallel()
17
18 handler := func(isPanic, _ bool) http.Handler {
19 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20 if isPanic {
21 panic("Oh no!")
22 }
23
24 w.WriteHeader(http.StatusOK)
25 })
26 }
27
28 cases := []struct {
29 Name string
30 Code int
31 Panic bool
32 Hijack bool
33 }{
34 {
35 Name: "OK",
36 Code: http.StatusOK,
37 Panic: false,
38 Hijack: false,
39 },
40 {
41 Name: "Panic",
42 Code: http.StatusInternalServerError,
43 Panic: true,
44 Hijack: false,
45 },
46 {
47 Name: "Hijack",
48 Code: 0,
49 Panic: true,
50 Hijack: true,
51 },
52 }
53
54 for _, c := range cases {
55 t.Run(c.Name, func(t *testing.T) {
56 t.Parallel()
57
58 var (
59 log = testutil.Logger(t)
60 r = httptest.NewRequest("GET", "/", nil)
61 w = &tracing.StatusWriter{
62 ResponseWriter: httptest.NewRecorder(),
63 Hijacked: c.Hijack,
64 }
65 )
66
67 httpmw.Recover(log)(handler(c.Panic, c.Hijack)).ServeHTTP(w, r)
68
69 require.Equal(t, c.Code, w.Status)
70 })
71 }
72}

Callers

nothing calls this directly

Calls 7

LoggerFunction · 0.92
RecoverFunction · 0.92
RunMethod · 0.65
handlerFunction · 0.50
WriteHeaderMethod · 0.45
ServeHTTPMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected