Merge 44b40e45b451a38b6872bd0d81d6c5bd82fc2834 into cf4775283ec30cda685355b5016c5abd2a56884e

This commit is contained in:
Griffin 2025-06-21 12:43:20 +08:00 committed by GitHub
commit 896acf0d6b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 5 deletions

View File

@ -6,6 +6,7 @@ package gin
import (
"bufio"
"errors"
"io"
"net"
"net/http"
@ -109,7 +110,11 @@ func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if w.size < 0 {
w.size = 0
}
return w.ResponseWriter.(http.Hijacker).Hijack()
hijacker, ok := w.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("response writer does not support Hijack")
}
return hijacker.Hijack()
}
// CloseNotify implements the http.CloseNotifier interface.

View File

@ -111,10 +111,8 @@ func TestResponseWriterHijack(t *testing.T) {
writer.reset(testWriter)
w := ResponseWriter(writer)
assert.Panics(t, func() {
_, _, err := w.Hijack()
require.NoError(t, err)
})
_, _, err := w.Hijack()
assert.Equal(t, "response writer does not support Hijack", err.Error())
assert.True(t, w.Written())
assert.Panics(t, func() {