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

Function TestPanicClean

recovery_test.go:18–47  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

16)
17
18func TestPanicClean(t *testing.T) {
19 buffer := new(strings.Builder)
20 router := New()
21 password := "my-super-secret-password"
22 router.Use(RecoveryWithWriter(buffer))
23 router.GET("/recovery", func(c *Context) {
24 c.AbortWithStatus(http.StatusBadRequest)
25 panic("Oops, Houston, we have a problem")
26 })
27 // RUN
28 w := PerformRequest(router, http.MethodGet, "/recovery",
29 header{
30 Key: "Host",
31 Value: "www.google.com",
32 },
33 header{
34 Key: "Authorization",
35 Value: "Bearer " + password,
36 },
37 header{
38 Key: "Content-Type",
39 Value: "application/json",
40 },
41 )
42 // TEST
43 assert.Equal(t, http.StatusBadRequest, w.Code)
44
45 // Check the buffer does not have the secret key
46 assert.NotContains(t, buffer.String(), password)
47}
48
49// TestPanicInHandler assert that panic has been recovered.
50func TestPanicInHandler(t *testing.T) {

Callers

nothing calls this directly

Calls 7

NewFunction · 0.85
RecoveryWithWriterFunction · 0.85
PerformRequestFunction · 0.85
AbortWithStatusMethod · 0.80
UseMethod · 0.65
GETMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected