TestPanicInHandler assert that panic has been recovered.
(t *testing.T)
| 48 | |
| 49 | // TestPanicInHandler assert that panic has been recovered. |
| 50 | func 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. |
| 78 | func TestPanicWithAbort(t *testing.T) { |
nothing calls this directly
no test coverage detected