MCPcopy
hub / github.com/gin-gonic/gin / TestPanicInHandler

Function TestPanicInHandler

recovery_test.go:50–75  ·  recovery_test.go::TestPanicInHandler

TestPanicInHandler assert that panic has been recovered.

(t *testing.T)

Source from the content-addressed store, hash-verified

48
49// TestPanicInHandler assert that panic has been recovered.
50func TestPanicInHandler(t *testing.T) {
51 buffer := new(strings.Builder)
52 router := New()
53 router.Use(RecoveryWithWriter(buffer))
54 router.GET("/recovery", func(_ *Context) {
55 panic("Oops, Houston, we have a problem")
56 })
57 // RUN
58 w := PerformRequest(router, http.MethodGet, "/recovery")
59 // TEST
60 assert.Equal(t, http.StatusInternalServerError, w.Code)
61 assert.Contains(t, buffer.String(), "panic recovered")
62 assert.Contains(t, buffer.String(), "Oops, Houston, we have a problem")
63 assert.Contains(t, buffer.String(), t.Name())
64 assert.NotContains(t, buffer.String(), "GET /recovery")
65
66 // Debug mode prints the request
67 SetMode(DebugMode)
68 // RUN
69 w = PerformRequest(router, http.MethodGet, "/recovery")
70 // TEST
71 assert.Equal(t, http.StatusInternalServerError, w.Code)
72 assert.Contains(t, buffer.String(), "GET /recovery")
73
74 SetMode(TestMode)
75}
76
77// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.
78func TestPanicWithAbort(t *testing.T) {

Callers

nothing calls this directly

Calls 8

NewFunction · 0.85
RecoveryWithWriterFunction · 0.85
PerformRequestFunction · 0.85
SetModeFunction · 0.85
UseMethod · 0.65
GETMethod · 0.65
StringMethod · 0.65
NameMethod · 0.65

Tested by

no test coverage detected