TestPanicWithBrokenPipe asserts that recovery specifically handles writing responses to broken pipes
(t *testing.T)
| 96 | // TestPanicWithBrokenPipe asserts that recovery specifically handles |
| 97 | // writing responses to broken pipes |
| 98 | func 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 |
| 132 | func TestPanicWithAbortHandler(t *testing.T) { |
nothing calls this directly
no test coverage detected