mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-06 20:18:19 +08:00
Merge 46da485481517ad5edb2dcf97c3590354cf62c8e into 5f4f9643258dc2a65e684b63f12c8d543c936c67
This commit is contained in:
commit
5db6f45f4f
@ -114,15 +114,22 @@ func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
if w.size > 0 {
|
||||
return nil, nil, errHijackAlreadyWritten
|
||||
}
|
||||
hijacker, ok := w.ResponseWriter.(http.Hijacker)
|
||||
if !ok {
|
||||
return nil, nil, http.ErrNotSupported
|
||||
}
|
||||
if w.size < 0 {
|
||||
w.size = 0
|
||||
}
|
||||
return w.ResponseWriter.(http.Hijacker).Hijack()
|
||||
return hijacker.Hijack()
|
||||
}
|
||||
|
||||
// CloseNotify implements the http.CloseNotifier interface.
|
||||
func (w *responseWriter) CloseNotify() <-chan bool {
|
||||
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
|
||||
if notifier, ok := w.ResponseWriter.(http.CloseNotifier); ok {
|
||||
return notifier.CloseNotify()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Flush implements the http.Flusher interface.
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -113,19 +114,36 @@ func TestResponseWriterHijack(t *testing.T) {
|
||||
writer.reset(testWriter)
|
||||
w := ResponseWriter(writer)
|
||||
|
||||
assert.Panics(t, func() {
|
||||
_, _, err := w.Hijack()
|
||||
require.NoError(t, err)
|
||||
})
|
||||
assert.True(t, w.Written())
|
||||
_, _, err := w.Hijack()
|
||||
require.ErrorIs(t, err, http.ErrNotSupported)
|
||||
assert.False(t, w.Written())
|
||||
|
||||
assert.Panics(t, func() {
|
||||
w.CloseNotify()
|
||||
})
|
||||
assert.Nil(t, w.CloseNotify())
|
||||
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
func TestResponseWriterHijackWithTimeoutHandler(t *testing.T) {
|
||||
var hijackErr error
|
||||
var closeNotify <-chan bool
|
||||
var written bool
|
||||
|
||||
handler := http.TimeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
writer := &responseWriter{}
|
||||
writer.reset(w)
|
||||
|
||||
_, _, hijackErr = writer.Hijack()
|
||||
closeNotify = writer.CloseNotify()
|
||||
written = writer.Written()
|
||||
}), time.Second, "")
|
||||
|
||||
handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
|
||||
|
||||
require.ErrorIs(t, hijackErr, http.ErrNotSupported)
|
||||
assert.Nil(t, closeNotify)
|
||||
assert.False(t, written)
|
||||
}
|
||||
|
||||
type mockHijacker struct {
|
||||
*httptest.ResponseRecorder
|
||||
hijacked bool
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user