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

Function TestPanicWithBrokenPipe

recovery_test.go:98–129  ·  view source on GitHub ↗

TestPanicWithBrokenPipe asserts that recovery specifically handles writing responses to broken pipes

(t *testing.T)

Source from the content-addressed store, hash-verified

96// TestPanicWithBrokenPipe asserts that recovery specifically handles
97// writing responses to broken pipes
98func TestPanicWithBrokenPipe(t *testing.T) {
99 const expectCode = 204
100
101 expectErrnos := []syscall.Errno{
102 syscall.EPIPE,
103 syscall.ECONNRESET,
104 }
105
106 for _, errno := range expectErrnos {
107 t.Run("Recovery from "+errno.Error(), func(t *testing.T) {
108 var buf strings.Builder
109
110 router := New()
111 router.Use(RecoveryWithWriter(&buf))
112 router.GET("/recovery", func(c *Context) {
113 // Start writing response
114 c.Header("X-Test", "Value")
115 c.Status(expectCode)
116
117 // Oops. Client connection closed
118 e := &net.OpError{Err: &os.SyscallError{Err: errno}}
119 panic(e)
120 })
121 // RUN
122 w := PerformRequest(router, http.MethodGet, "/recovery")
123 // TEST
124 assert.Equal(t, expectCode, w.Code)
125 assert.Contains(t, strings.ToLower(buf.String()), errno.Error())
126 assert.NotContains(t, strings.ToLower(buf.String()), "[Recovery]")
127 })
128 }
129}
130
131// TestPanicWithAbortHandler asserts that recovery handles http.ErrAbortHandler as broken pipe
132func TestPanicWithAbortHandler(t *testing.T) {

Callers

nothing calls this directly

Calls 10

NewFunction · 0.85
RecoveryWithWriterFunction · 0.85
PerformRequestFunction · 0.85
RunMethod · 0.80
UseMethod · 0.65
GETMethod · 0.65
StatusMethod · 0.65
StringMethod · 0.65
ErrorMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected