Skip to content

Commit

Permalink
fix: MiddlewareHandlerResponse writes additional information
Browse files Browse the repository at this point in the history
  • Loading branch information
wlynxg committed Jan 9, 2025
1 parent 80f57d1 commit 75d6348
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion net/ghttp/ghttp_middleware_handler_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func MiddlewareHandlerResponse(r *Request) {
r.Middleware.Next()

// There's custom buffer content, it then exits current handler.
if r.Response.BufferLength() > 0 {
if r.Response.BufferLength() > 0 || r.Response.Writer.BytesWritten() > 0 {
return
}

Expand Down
26 changes: 26 additions & 0 deletions net/ghttp/ghttp_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package ghttp_test
import (
"context"
"fmt"
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -630,3 +631,28 @@ func Test_Issue4047(t *testing.T) {
t.Assert(s.Logger(), nil)
})
}

// https://github.com/gogf/gf/issues/4108
func Test_Issue4108(t *testing.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.GET("/", func(r *ghttp.Request) {
r.Response.Writer.Write([]byte("hello"))
})
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)

gtest.C(t, func(t *gtest.T) {
client := g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))

rsp, err := client.Get(ctx, "/")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusOK)
t.Assert(rsp.ReadAllString(), "hello")
})
}

0 comments on commit 75d6348

Please sign in to comment.