From f9beb8845190b8d6acb5edf928e040a520c46f01 Mon Sep 17 00:00:00 2001 From: Raju Ahmed Date: Tue, 2 Jun 2026 22:43:11 +0600 Subject: [PATCH] refactor: incorporate Copilot review suggestions --- context.go | 7 +++++-- context_test.go | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/context.go b/context.go index dc6f8e4a..6d9fe6d7 100644 --- a/context.go +++ b/context.go @@ -1158,8 +1158,11 @@ func (c *Context) Render(code int, r render.Render) { return } - if c.Writer.Written() && IsDebugging() { - // Skip warning for SSE and streaming responses (status code -1) + if c.Writer.Size() > 0 && IsDebugging() { + // Skip this warning when: + // - status code is -1, which is used for special/streaming-style renders (including some + // non-streaming helpers like redirects) where multiple writes are expected, and + // - the renderer is an SSE event, since Server-Sent Events are sent as a stream. if code != -1 { if _, ok := r.(sse.Event); !ok { debugPrint("[WARNING] Response body already written. Attempting to write again with status code %d", code) diff --git a/context_test.go b/context_test.go index f83a57df..7b1cbf51 100644 --- a/context_test.go +++ b/context_test.go @@ -1429,8 +1429,8 @@ func TestContextRenderMultipleJSON(t *testing.T) { w := httptest.NewRecorder() c, _ := CreateTestContext(w) - oldMode := os.Getenv("GIN_MODE") - defer os.Setenv("GIN_MODE", oldMode) + oldMode := Mode() + defer SetMode(oldMode) SetMode(DebugMode) output := captureOutput(t, func() { @@ -1448,8 +1448,8 @@ func TestContextRenderMultipleSSE(t *testing.T) { w := httptest.NewRecorder() c, _ := CreateTestContext(w) - oldMode := os.Getenv("GIN_MODE") - defer os.Setenv("GIN_MODE", oldMode) + oldMode := Mode() + defer SetMode(oldMode) SetMode(DebugMode) output := captureOutput(t, func() {