Merge f9beb8845190b8d6acb5edf928e040a520c46f01 into d75fcd4c9ab260e5225de590f1f0f8c0e0e12d11

This commit is contained in:
Raju Ahmed 2026-06-02 16:43:34 +00:00 committed by GitHub
commit b62d4b94e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 0 deletions

View File

@ -1185,6 +1185,18 @@ func (c *Context) Render(code int, r render.Render) {
return
}
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)
}
}
}
if err := r.Render(c.Writer); err != nil {
// Pushing error to c.Errors
_ = c.Error(err)

View File

@ -1425,6 +1425,43 @@ func TestContextRenderNoContentData(t *testing.T) {
assert.Equal(t, "text/csv", w.Header().Get("Content-Type"))
}
// Test multiple JSON writes in debug mode
func TestContextRenderMultipleJSON(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
oldMode := Mode()
defer SetMode(oldMode)
SetMode(DebugMode)
output := captureOutput(t, func() {
c.JSON(http.StatusOK, H{"foo": "bar"})
c.JSON(http.StatusOK, H{"baz": "qux"})
})
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, output, "[WARNING] Response body already written")
assert.Contains(t, output, "status code 200")
}
// Test multiple SSE writes in debug mode
func TestContextRenderMultipleSSE(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
oldMode := Mode()
defer SetMode(oldMode)
SetMode(DebugMode)
output := captureOutput(t, func() {
c.SSEvent("message", "test1")
c.SSEvent("message", "test2")
})
assert.Equal(t, http.StatusOK, w.Code)
assert.NotContains(t, output, "[WARNING] Response body already written")
}
func TestContextRenderSSE(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)