mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 04:57:07 +08:00
refine: return error instead of panic when response writer doesn't implement hijacker
This commit is contained in:
parent
cc4e11438c
commit
44b40e45b4
@ -6,6 +6,7 @@ package gin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -109,7 +110,11 @@ 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, errors.New("response writer does not support Hijack")
|
||||||
|
}
|
||||||
|
return hijacker.Hijack()
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloseNotify implements the http.CloseNotifier interface.
|
// CloseNotify implements the http.CloseNotifier interface.
|
||||||
|
@ -111,10 +111,8 @@ func TestResponseWriterHijack(t *testing.T) {
|
|||||||
writer.reset(testWriter)
|
writer.reset(testWriter)
|
||||||
w := ResponseWriter(writer)
|
w := ResponseWriter(writer)
|
||||||
|
|
||||||
assert.Panics(t, func() {
|
|
||||||
_, _, err := w.Hijack()
|
_, _, err := w.Hijack()
|
||||||
require.NoError(t, err)
|
assert.Equal(t, "response writer does not support Hijack", err.Error())
|
||||||
})
|
|
||||||
assert.True(t, w.Written())
|
assert.True(t, w.Written())
|
||||||
|
|
||||||
assert.Panics(t, func() {
|
assert.Panics(t, func() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user