Skip to content

Commit

Permalink
fix: 304 status code return error (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Duslia authored Jun 30, 2023
1 parent c2745b9 commit 06e557a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
31 changes: 31 additions & 0 deletions factory/http2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,34 @@ func TestTrailer(t *testing.T) {
}
}
}

func TestBodyNotAllowedStatus(t *testing.T) {
var acceptCount int32 = 0
h := server.New(
server.WithHostPorts(":8894"),
server.WithH2C(true),
server.WithOnAccept(func(conn net.Conn) context.Context {
atomic.AddInt32(&acceptCount, 1)
return context.Background()
}))

// register http2 server factory
h.AddProtocol("h2", NewServerFactory())

h.POST("/", func(c context.Context, ctx *app.RequestContext) {
ctx.Data(304, "application/json", []byte("test data"))
})
go h.Spin()
time.Sleep(time.Second)

c, _ := client.NewClient()
c.SetClientFactory(NewClientFactory(config.WithAllowHTTP(true)))
req, rsp := protocol.AcquireRequest(), protocol.AcquireResponse()
req.SetMethod("POST")
req.SetRequestURI("http://127.0.0.1:8894")

err := c.Do(context.Background(), req, rsp)
assert.Nil(t, err)
assert.DeepEqual(t, rsp.StatusCode(), 304)
assert.DeepEqual(t, len(rsp.Body()), 0)
}
7 changes: 1 addition & 6 deletions response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,7 @@ func (w *responseWriter) write(lenData int, dataB []byte, dataS string) (n int,
if rws == nil {
panic("Write called after Handler finished")
}
if !rws.wroteHeader {
w.WriteHeader(200)
}
if !bodyAllowedForStatus(rws.status) {
return 0, http.ErrBodyNotAllowed
}

rws.wroteBytes += int64(len(dataB)) + int64(len(dataS)) // only one can be set
if rws.sentContentLen != 0 && rws.wroteBytes > rws.sentContentLen {
// TODO: send a RST_STREAM
Expand Down

0 comments on commit 06e557a

Please sign in to comment.