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

Function TestCustomRecovery

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

Source from the content-addressed store, hash-verified

187}
188
189func TestCustomRecovery(t *testing.T) {
190 errBuffer := new(strings.Builder)
191 buffer := new(strings.Builder)
192 router := New()
193 DefaultErrorWriter = buffer
194 handleRecovery := func(c *Context, err any) {
195 errBuffer.WriteString(err.(string))
196 c.AbortWithStatus(http.StatusBadRequest)
197 }
198 router.Use(CustomRecovery(handleRecovery))
199 router.GET("/recovery", func(_ *Context) {
200 panic("Oops, Houston, we have a problem")
201 })
202 // RUN
203 w := PerformRequest(router, http.MethodGet, "/recovery")
204 // TEST
205 assert.Equal(t, http.StatusBadRequest, w.Code)
206 assert.Contains(t, buffer.String(), "panic recovered")
207 assert.Contains(t, buffer.String(), "Oops, Houston, we have a problem")
208 assert.Contains(t, buffer.String(), t.Name())
209 assert.NotContains(t, buffer.String(), "GET /recovery")
210
211 // Debug mode prints the request
212 SetMode(DebugMode)
213 // RUN
214 w = PerformRequest(router, http.MethodGet, "/recovery")
215 // TEST
216 assert.Equal(t, http.StatusBadRequest, w.Code)
217 assert.Contains(t, buffer.String(), "GET /recovery")
218
219 assert.Equal(t, strings.Repeat("Oops, Houston, we have a problem", 2), errBuffer.String())
220
221 SetMode(TestMode)
222}
223
224func TestRecoveryWithWriterWithCustomRecovery(t *testing.T) {
225 errBuffer := new(strings.Builder)

Callers

nothing calls this directly

Calls 10

NewFunction · 0.85
CustomRecoveryFunction · 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