Skip to content

Commit

Permalink
optimize: prebind without content-length
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Oct 31, 2023
1 parent e02c162 commit e135aff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions pkg/app/server/binding/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1556,3 +1556,30 @@ func Benchmark_Binding(b *testing.B) {
}
}
}

func TestBind_BindProtobufWithoutContentLength(t *testing.T) {
data := testdata.HertzReq{Name: "hertz"}
body, err := proto.Marshal(&data)
if err != nil {
t.Fatal(err)
}
req := newMockRequest().
SetRequestURI("http://foobar.com").
SetProtobufContentType().
SetBody(body)

req.Req.Header.Del("Content-Length")
result := testdata.HertzReq{}
err = DefaultBinder().BindAndValidate(req.Req, &result, nil)
if err != nil {
t.Error(err)
}
assert.DeepEqual(t, "hertz", result.Name)

result = testdata.HertzReq{}
err = DefaultBinder().BindProtobuf(req.Req, &result)
if err != nil {
t.Error(err)
}
assert.DeepEqual(t, "hertz", result.Name)
}
2 changes: 1 addition & 1 deletion pkg/app/server/binding/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (b *defaultBinder) Bind(req *protocol.Request, v interface{}, params param.

// best effort binding
func (b *defaultBinder) preBindBody(req *protocol.Request, v interface{}) error {
if req.Header.ContentLength() <= 0 {
if len(req.Body()) == 0 {
return nil
}
ct := bytesconv.B2s(req.Header.ContentType())
Expand Down

0 comments on commit e135aff

Please sign in to comment.