Merge 46da485481517ad5edb2dcf97c3590354cf62c8e into 5f4f9643258dc2a65e684b63f12c8d543c936c67

This commit is contained in:
Mike Ma 2026-05-08 21:34:08 -05:00 committed by GitHub
commit 5db6f45f4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 10 deletions

View File

@ -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.

View File

@ -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