Merge 1182339849d2712dc7d90aac8f28c86a8f57a8de into 5f4f9643258dc2a65e684b63f12c8d543c936c67

This commit is contained in:
Sai Asish Y 2026-05-09 10:23:28 +08:00 committed by GitHub
commit 1a0b1664f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 9 deletions

View File

@ -117,12 +117,19 @@ func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if w.size < 0 { if w.size < 0 {
w.size = 0 w.size = 0
} }
return w.ResponseWriter.(http.Hijacker).Hijack() hijacker, ok := w.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, http.ErrNotSupported
}
return hijacker.Hijack()
} }
// CloseNotify implements the http.CloseNotifier interface. // CloseNotify implements the http.CloseNotifier interface.
func (w *responseWriter) CloseNotify() <-chan bool { func (w *responseWriter) CloseNotify() <-chan bool {
return w.ResponseWriter.(http.CloseNotifier).CloseNotify() if cn, ok := w.ResponseWriter.(http.CloseNotifier); ok {
return cn.CloseNotify()
}
return nil
} }
// Flush implements the http.Flusher interface. // Flush implements the http.Flusher interface.

View File

@ -113,15 +113,16 @@ func TestResponseWriterHijack(t *testing.T) {
writer.reset(testWriter) writer.reset(testWriter)
w := ResponseWriter(writer) w := ResponseWriter(writer)
assert.Panics(t, func() { // httptest.ResponseRecorder doesn't implement http.Hijacker; return
_, _, err := w.Hijack() // http.ErrNotSupported instead of panicking (#4638).
require.NoError(t, err) conn, buf, err := w.Hijack()
}) assert.Nil(t, conn)
assert.Nil(t, buf)
require.ErrorIs(t, err, http.ErrNotSupported)
assert.True(t, w.Written()) assert.True(t, w.Written())
assert.Panics(t, func() { // CloseNotify on a non-CloseNotifier returns nil instead of panicking.
w.CloseNotify() assert.Nil(t, w.CloseNotify())
})
w.Flush() w.Flush()
} }