diff --git a/context.go b/context.go index c42459ff..562ef95b 100644 --- a/context.go +++ b/context.go @@ -1133,6 +1133,10 @@ func (c *Context) Render(code int, r render.Render) { return } + if c.Writer.Written() { + 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) diff --git a/debug_test.go b/debug_test.go index dab02133..8ddc4410 100644 --- a/debug_test.go +++ b/debug_test.go @@ -11,6 +11,7 @@ import ( "io" "log" "net/http" + "net/http/httptest" "os" "strings" "sync" @@ -178,3 +179,19 @@ func TestGetMinVer(t *testing.T) { _, e = getMinVer("go1.1.1.1") require.Error(t, e) } + +func TestRenderWarnsOnMultipleWrites(t *testing.T) { + re := captureOutput(t, func() { + SetMode(DebugMode) + w := httptest.NewRecorder() + c, _ := CreateTestContext(w) + + // First write + c.JSON(http.StatusOK, H{"first": "response"}) + // Second write should trigger warning + c.JSON(http.StatusOK, H{"second": "response"}) + + SetMode(TestMode) + }) + assert.Contains(t, re, "[WARNING] Response body already written") +}