From 3c28f41f5b880cdbc33555931a8ce8fe1a5b0500 Mon Sep 17 00:00:00 2001 From: hso Date: Tue, 17 Mar 2026 09:55:26 +0900 Subject: [PATCH] fix: Remove deprecated http.CloseNotifier, use Request.Context().Done() in Stream Summary --- context.go | 2 +- context_test.go | 24 ++++++++++++------------ response_writer.go | 6 ------ response_writer_test.go | 5 ----- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/context.go b/context.go index 5174033e..1d0a1f49 100644 --- a/context.go +++ b/context.go @@ -1327,7 +1327,7 @@ func (c *Context) SSEvent(name string, message any) { // indicates "Is client disconnected in middle of stream" func (c *Context) Stream(step func(w io.Writer) bool) bool { w := c.Writer - clientGone := w.CloseNotify() + clientGone := c.Request.Context().Done() for { select { case <-clientGone: diff --git a/context_test.go b/context_test.go index ef60379d..96369a30 100644 --- a/context_test.go +++ b/context_test.go @@ -3023,27 +3023,26 @@ func TestContextRenderDataFromReaderNoHeaders(t *testing.T) { type TestResponseRecorder struct { *httptest.ResponseRecorder - closeChannel chan bool -} - -func (r *TestResponseRecorder) CloseNotify() <-chan bool { - return r.closeChannel + cancel context.CancelFunc } func (r *TestResponseRecorder) closeClient() { - r.closeChannel <- true + r.cancel() } -func CreateTestResponseRecorder() *TestResponseRecorder { +func CreateTestResponseRecorder() (*TestResponseRecorder, *http.Request) { + ctx, cancel := context.WithCancel(context.Background()) + req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/", nil) return &TestResponseRecorder{ httptest.NewRecorder(), - make(chan bool, 1), - } + cancel, + }, req } func TestContextStream(t *testing.T) { - w := CreateTestResponseRecorder() + w, req := CreateTestResponseRecorder() c, _ := CreateTestContext(w) + c.Request = req stopStream := true c.Stream(func(w io.Writer) bool { @@ -3061,8 +3060,9 @@ func TestContextStream(t *testing.T) { } func TestContextStreamWithClientGone(t *testing.T) { - w := CreateTestResponseRecorder() + w, req := CreateTestResponseRecorder() c, _ := CreateTestContext(w) + c.Request = req c.Stream(func(writer io.Writer) bool { defer func() { @@ -3079,7 +3079,7 @@ func TestContextStreamWithClientGone(t *testing.T) { } func TestContextResetInHandler(t *testing.T) { - w := CreateTestResponseRecorder() + w, _ := CreateTestResponseRecorder() c, _ := CreateTestContext(w) c.handlers = []HandlerFunc{ diff --git a/response_writer.go b/response_writer.go index 9035e6f1..9b2c4614 100644 --- a/response_writer.go +++ b/response_writer.go @@ -24,7 +24,6 @@ type ResponseWriter interface { http.ResponseWriter http.Hijacker http.Flusher - http.CloseNotifier // Status returns the HTTP response status code of the current request. Status() int @@ -120,11 +119,6 @@ func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { return w.ResponseWriter.(http.Hijacker).Hijack() } -// CloseNotify implements the http.CloseNotifier interface. -func (w *responseWriter) CloseNotify() <-chan bool { - return w.ResponseWriter.(http.CloseNotifier).CloseNotify() -} - // Flush implements the http.Flusher interface. func (w *responseWriter) Flush() { w.WriteHeaderNow() diff --git a/response_writer_test.go b/response_writer_test.go index dfc1d2c6..2be7536b 100644 --- a/response_writer_test.go +++ b/response_writer_test.go @@ -26,7 +26,6 @@ var ( _ http.ResponseWriter = ResponseWriter(&responseWriter{}) _ http.Hijacker = ResponseWriter(&responseWriter{}) _ http.Flusher = ResponseWriter(&responseWriter{}) - _ http.CloseNotifier = ResponseWriter(&responseWriter{}) ) func init() { @@ -119,10 +118,6 @@ func TestResponseWriterHijack(t *testing.T) { }) assert.True(t, w.Written()) - assert.Panics(t, func() { - w.CloseNotify() - }) - w.Flush() }