mirror of
https://github.com/gin-gonic/gin.git
synced 2026-01-15 20:17:09 +08:00
Merge 6cba6e6d8a0bd9675c19d9d6af53330c0d196319 into 52ecf029bd2e9b4d2652f96dd2b753f8bc6b6e95
This commit is contained in:
commit
4fe6ab0d80
@ -68,6 +68,9 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
||||
}
|
||||
}
|
||||
}
|
||||
if e, ok := err.(error); ok && errors.Is(e, http.ErrAbortHandler) {
|
||||
brokenPipe = true
|
||||
}
|
||||
if logger != nil {
|
||||
const stackSkip = 3
|
||||
if brokenPipe {
|
||||
|
||||
@ -142,6 +142,30 @@ func TestPanicWithBrokenPipe(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestPanicWithAbortHandler asserts that recovery handles http.ErrAbortHandler as broken pipe
|
||||
func TestPanicWithAbortHandler(t *testing.T) {
|
||||
const expectCode = 204
|
||||
|
||||
var buf strings.Builder
|
||||
router := New()
|
||||
router.Use(RecoveryWithWriter(&buf))
|
||||
router.GET("/recovery", func(c *Context) {
|
||||
// Start writing response
|
||||
c.Header("X-Test", "Value")
|
||||
c.Status(expectCode)
|
||||
|
||||
// Panic with ErrAbortHandler which should be treated as broken pipe
|
||||
panic(http.ErrAbortHandler)
|
||||
})
|
||||
// RUN
|
||||
w := PerformRequest(router, http.MethodGet, "/recovery")
|
||||
// TEST
|
||||
assert.Equal(t, expectCode, w.Code)
|
||||
out := buf.String()
|
||||
assert.Contains(t, out, "net/http: abort Handler")
|
||||
assert.NotContains(t, out, "panic recovered")
|
||||
}
|
||||
|
||||
func TestCustomRecoveryWithWriter(t *testing.T) {
|
||||
errBuffer := new(strings.Builder)
|
||||
buffer := new(strings.Builder)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user