mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-08 03:31:07 +08:00
Compare commits
5 Commits
10bd477157
...
4bbb2a902b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4bbb2a902b | ||
|
|
63dd3e60ca | ||
|
|
84a97ef73e | ||
|
|
81c4110cb4 | ||
|
|
24cea05888 |
11
context.go
11
context.go
@ -865,6 +865,11 @@ func (c *Context) ShouldBindJSON(obj any) error {
|
||||
return c.ShouldBindWith(obj, binding.JSON)
|
||||
}
|
||||
|
||||
// ShouldBindMsgPack is a shortcut for c.ShouldBindWith(obj, binding.MsgPack).
|
||||
func (c *Context) ShouldBindMsgPack(obj interface{}) error {
|
||||
return c.ShouldBindWith(obj, binding.MsgPack)
|
||||
}
|
||||
|
||||
// ShouldBindXML is a shortcut for c.ShouldBindWith(obj, binding.XML).
|
||||
// It works like ShouldBindJSON but binds the request body as XML data.
|
||||
func (c *Context) ShouldBindXML(obj any) error {
|
||||
@ -1188,6 +1193,12 @@ func (c *Context) JSON(code int, obj any) {
|
||||
c.Render(code, render.JSON{Data: obj})
|
||||
}
|
||||
|
||||
// MsgPack serializes the given struct as MsgPack into the response body.
|
||||
// It also sets the Content-Type as "application/msgpack".
|
||||
func (c *Context) MsgPack(code int, obj interface{}) {
|
||||
c.Render(code, render.MsgPack{Data: obj})
|
||||
}
|
||||
|
||||
// AsciiJSON serializes the given struct as JSON into the response body with unicode to ASCII string.
|
||||
// It also sets the Content-Type as "application/json".
|
||||
func (c *Context) AsciiJSON(code int, obj any) {
|
||||
|
||||
@ -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