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

Function TestCustomRecoveryWithWriter

recovery_test.go:155–187  ·  recovery_test.go::TestCustomRecoveryWithWriter
(t *testing.T)

Source from the content-addressed store, hash-verified

153}
154
155func TestCustomRecoveryWithWriter(t *testing.T) {
156 errBuffer := new(strings.Builder)
157 buffer := new(strings.Builder)
158 router := New()
159 handleRecovery := func(c *Context, err any) {
160 errBuffer.WriteString(err.(string))
161 c.AbortWithStatus(http.StatusBadRequest)
162 }
163 router.Use(CustomRecoveryWithWriter(buffer, handleRecovery))
164 router.GET("/recovery", func(_ *Context) {
165 panic("Oops, Houston, we have a problem")
166 })
167 // RUN
168 w := PerformRequest(router, http.MethodGet, "/recovery")
169 // TEST
170 assert.Equal(t, http.StatusBadRequest, w.Code)
171 assert.Contains(t, buffer.String(), "panic recovered")
172 assert.Contains(t, buffer.String(), "Oops, Houston, we have a problem")
173 assert.Contains(t, buffer.String(), t.Name())
174 assert.NotContains(t, buffer.String(), "GET /recovery")
175
176 // Debug mode prints the request
177 SetMode(DebugMode)
178 // RUN
179 w = PerformRequest(router, http.MethodGet, "/recovery")
180 // TEST
181 assert.Equal(t, http.StatusBadRequest, w.Code)
182 assert.Contains(t, buffer.String(), "GET /recovery")
183
184 assert.Equal(t, strings.Repeat("Oops, Houston, we have a problem", 2), errBuffer.String())
185
186 SetMode(TestMode)
187}
188
189func TestCustomRecovery(t *testing.T) {
190 errBuffer := new(strings.Builder)

Callers

nothing calls this directly

Calls 10

NewFunction · 0.85
CustomRecoveryWithWriterFunction · 0.85
PerformRequestFunction · 0.85
SetModeFunction · 0.85
AbortWithStatusMethod · 0.80
WriteStringMethod · 0.65
UseMethod · 0.65
GETMethod · 0.65
StringMethod · 0.65
NameMethod · 0.65

Tested by

no test coverage detected