Skip to content

Commit

Permalink
fix(net/ghttp): BufferWriter.Flush writes additional information afte…
Browse files Browse the repository at this point in the history
…r custom response wrote (#4116)
  • Loading branch information
wlynxg authored Jan 22, 2025
1 parent e0f7348 commit 99f0fb1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions net/ghttp/ghttp_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,3 +656,25 @@ func Test_Issue4108(t *testing.T) {
t.Assert(rsp.ReadAllString(), "hello")
})
}

// https://github.com/gogf/gf/issues/4115
func Test_Issue4115(t *testing.T) {
s := g.Server(guid.S())
s.Use(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")
})
}
2 changes: 1 addition & 1 deletion net/ghttp/internal/response/response_buffer_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (w *BufferWriter) Flush() {
w.Writer.WriteHeader(w.Status)
}
// Default status text output.
if w.Status != http.StatusOK && w.buffer.Len() == 0 {
if w.Status != http.StatusOK && w.buffer.Len() == 0 && w.Writer.BytesWritten() == 0 {
w.buffer.WriteString(http.StatusText(w.Status))
}
if w.buffer.Len() > 0 {
Expand Down

0 comments on commit 99f0fb1

Please sign in to comment.