mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-06 20:18:19 +08:00
response_writer: keep Written() false when Hijack is unsupported
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
This commit is contained in:
parent
1182339849
commit
adbdcceff3
@ -114,13 +114,13 @@ func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
if w.size > 0 {
|
||||
return nil, nil, errHijackAlreadyWritten
|
||||
}
|
||||
if w.size < 0 {
|
||||
w.size = 0
|
||||
}
|
||||
hijacker, ok := w.ResponseWriter.(http.Hijacker)
|
||||
if !ok {
|
||||
return nil, nil, http.ErrNotSupported
|
||||
}
|
||||
if w.size < 0 {
|
||||
w.size = 0
|
||||
}
|
||||
return hijacker.Hijack()
|
||||
}
|
||||
|
||||
|
||||
@ -114,12 +114,14 @@ func TestResponseWriterHijack(t *testing.T) {
|
||||
w := ResponseWriter(writer)
|
||||
|
||||
// httptest.ResponseRecorder doesn't implement http.Hijacker; return
|
||||
// http.ErrNotSupported instead of panicking (#4638).
|
||||
// http.ErrNotSupported instead of panicking (#4638). On unsupported the
|
||||
// writer state stays untouched so the handler can still emit a normal
|
||||
// HTTP response as a fallback.
|
||||
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.False(t, w.Written())
|
||||
|
||||
// CloseNotify on a non-CloseNotifier returns nil instead of panicking.
|
||||
assert.Nil(t, w.CloseNotify())
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user