diff --git a/net/ghttp/ghttp_z_unit_issue_test.go b/net/ghttp/ghttp_z_unit_issue_test.go index e14a38eed46..f8b2069e2ec 100644 --- a/net/ghttp/ghttp_z_unit_issue_test.go +++ b/net/ghttp/ghttp_z_unit_issue_test.go @@ -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") + }) +} diff --git a/net/ghttp/internal/response/response_buffer_writer.go b/net/ghttp/internal/response/response_buffer_writer.go index f1f65c88e19..8b334d79049 100644 --- a/net/ghttp/internal/response/response_buffer_writer.go +++ b/net/ghttp/internal/response/response_buffer_writer.go @@ -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 {