mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-05 02:18:15 +08:00
Merge 7b5727c6db42a1ce8207f2cb1ad9e7522191bb51 into d3ffc9985281dcf4d3bef604cce4e662b1a327a6
This commit is contained in:
commit
6a21a2dd36
@ -1158,6 +1158,15 @@ func (c *Context) Render(code int, r render.Render) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.Writer.Written() && IsDebugging() {
|
||||||
|
// Skip warning for SSE and streaming responses (status code -1)
|
||||||
|
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 {
|
if err := r.Render(c.Writer); err != nil {
|
||||||
// Pushing error to c.Errors
|
// Pushing error to c.Errors
|
||||||
_ = c.Error(err)
|
_ = c.Error(err)
|
||||||
|
|||||||
@ -1424,6 +1424,43 @@ func TestContextRenderNoContentData(t *testing.T) {
|
|||||||
assert.Equal(t, "text/csv", w.Header().Get("Content-Type"))
|
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 := os.Getenv("GIN_MODE")
|
||||||
|
defer os.Setenv("GIN_MODE", 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 := os.Getenv("GIN_MODE")
|
||||||
|
defer os.Setenv("GIN_MODE", 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) {
|
func TestContextRenderSSE(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user