mirror of
https://github.com/gin-gonic/gin.git
synced 2026-04-29 23:23:18 +08:00
Merge a2792d87ddb088eed4110f98c7fbc86468b92a0c into d3ffc9985281dcf4d3bef604cce4e662b1a327a6
This commit is contained in:
commit
70dde2f5d8
@ -17,7 +17,10 @@ const (
|
|||||||
defaultStatus = http.StatusOK
|
defaultStatus = http.StatusOK
|
||||||
)
|
)
|
||||||
|
|
||||||
var errHijackAlreadyWritten = errors.New("gin: response body already written")
|
var (
|
||||||
|
errHijackAlreadyWritten = errors.New("gin: response body already written")
|
||||||
|
errHijackNotSupported = errors.New("gin: underlying ResponseWriter does not implement http.Hijacker")
|
||||||
|
)
|
||||||
|
|
||||||
// ResponseWriter ...
|
// ResponseWriter ...
|
||||||
type ResponseWriter interface {
|
type ResponseWriter interface {
|
||||||
@ -117,12 +120,25 @@ 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()
|
if hijacker, ok := w.ResponseWriter.(http.Hijacker); ok {
|
||||||
|
return hijacker.Hijack()
|
||||||
|
}
|
||||||
|
return nil, nil, errHijackNotSupported
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloseNotify implements the http.CloseNotifier interface.
|
// CloseNotify implements the http.CloseNotifier interface.
|
||||||
|
//
|
||||||
|
// If the underlying ResponseWriter doesn't implement http.CloseNotifier
|
||||||
|
// (e.g. httptest.NewRecorder), the returned channel will never fire.
|
||||||
|
// Use Request.Context().Done() to observe client disconnects instead.
|
||||||
|
//
|
||||||
|
// Deprecated: the CloseNotifier interface predates Go's context package.
|
||||||
|
// New code should use Request.Context instead.
|
||||||
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 make(chan bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flush implements the http.Flusher interface.
|
// Flush implements the http.Flusher interface.
|
||||||
|
|||||||
@ -113,15 +113,12 @@ 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)
|
require.ErrorIs(t, err, errHijackNotSupported)
|
||||||
})
|
|
||||||
assert.True(t, w.Written())
|
assert.True(t, w.Written())
|
||||||
|
|
||||||
assert.Panics(t, func() {
|
ch := w.CloseNotify()
|
||||||
w.CloseNotify()
|
assert.NotNil(t, ch)
|
||||||
})
|
|
||||||
|
|
||||||
w.Flush()
|
w.Flush()
|
||||||
}
|
}
|
||||||
@ -315,3 +312,48 @@ func TestPusherWithoutPusher(t *testing.T) {
|
|||||||
pusher := w.Pusher()
|
pusher := w.Pusher()
|
||||||
assert.Nil(t, pusher, "Expected pusher to be nil")
|
assert.Nil(t, pusher, "Expected pusher to be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// mockCloseNotifier is an http.ResponseWriter that implements http.CloseNotifier.
|
||||||
|
type mockCloseNotifier struct {
|
||||||
|
*httptest.ResponseRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockCloseNotifier) CloseNotify() <-chan bool {
|
||||||
|
return make(chan bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCloseNotifyWithCloseNotifier(t *testing.T) {
|
||||||
|
rw := &mockCloseNotifier{ResponseRecorder: httptest.NewRecorder()}
|
||||||
|
w := &responseWriter{}
|
||||||
|
w.reset(rw)
|
||||||
|
|
||||||
|
ch := w.CloseNotify()
|
||||||
|
assert.NotNil(t, ch, "Expected CloseNotify channel to be non-nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCloseNotifyWithoutCloseNotifier(t *testing.T) {
|
||||||
|
// httptest.NewRecorder does not implement http.CloseNotifier
|
||||||
|
rw := httptest.NewRecorder()
|
||||||
|
w := &responseWriter{}
|
||||||
|
w.reset(rw)
|
||||||
|
|
||||||
|
ch := w.CloseNotify()
|
||||||
|
assert.NotNil(t, ch, "Expected non-nil channel when CloseNotifier is not supported")
|
||||||
|
select {
|
||||||
|
case <-ch:
|
||||||
|
t.Fatal("channel should never fire when CloseNotifier is not supported")
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHijackWithoutHijacker(t *testing.T) {
|
||||||
|
// httptest.NewRecorder does not implement http.Hijacker
|
||||||
|
rw := httptest.NewRecorder()
|
||||||
|
w := &responseWriter{}
|
||||||
|
w.reset(rw)
|
||||||
|
|
||||||
|
conn, buf, err := w.Hijack()
|
||||||
|
assert.Nil(t, conn)
|
||||||
|
assert.Nil(t, buf)
|
||||||
|
require.ErrorIs(t, err, errHijackNotSupported)
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user