mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 04:08:15 +08:00
Merge 4330ea04c012dfba7f232c9d5612bc3c11495c83 into 9968c4bf9d5a99edc3eee2c068a4c9160ece8915
This commit is contained in:
commit
ba2dc6b0c8
@ -68,6 +68,12 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
||||
}
|
||||
}
|
||||
}
|
||||
if e, ok := err.(error); ok {
|
||||
// ErrAbortHandler should be treated as broken pipe too.
|
||||
if errors.Is(e, http.ErrAbortHandler) {
|
||||
brokenPipe = true
|
||||
}
|
||||
}
|
||||
if logger != nil {
|
||||
const stackSkip = 3
|
||||
if brokenPipe {
|
||||
|
@ -142,6 +142,28 @@ 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)
|
||||
assert.Contains(t, buf.String(), "net/http: abort Handler")
|
||||
}
|
||||
|
||||
func TestCustomRecoveryWithWriter(t *testing.T) {
|
||||
errBuffer := new(strings.Builder)
|
||||
buffer := new(strings.Builder)
|
||||
|
Loading…
x
Reference in New Issue
Block a user