Skip to content

Commit 0368ed8

Browse files
authored
Add Conditions to Ensure Bind Succeeds with Transfer-Encoding: chunked (#2717)
* Add conditions to ensure Bind succeeds with `Transfer-Encoding: chunked`. * Revert the ContentLength conditions for BindBody
1 parent 3b01785 commit 0368ed8

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

bind.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (b *DefaultBinder) BindQueryParams(c Context, i interface{}) error {
6767
// See MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseMultipartForm
6868
func (b *DefaultBinder) BindBody(c Context, i interface{}) (err error) {
6969
req := c.Request()
70-
if req.ContentLength <= 0 {
70+
if req.ContentLength == 0 {
7171
return
7272
}
7373

bind_test.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"mime/multipart"
1414
"net/http"
1515
"net/http/httptest"
16+
"net/http/httputil"
1617
"net/url"
1718
"reflect"
1819
"strconv"
@@ -941,6 +942,7 @@ func TestDefaultBinder_BindBody(t *testing.T) {
941942
givenMethod string
942943
givenContentType string
943944
whenNoPathParams bool
945+
whenChunkedBody bool
944946
whenBindTarget interface{}
945947
expect interface{}
946948
expectError string
@@ -1061,12 +1063,30 @@ func TestDefaultBinder_BindBody(t *testing.T) {
10611063
expectError: "code=415, message=Unsupported Media Type",
10621064
},
10631065
{
1064-
name: "ok, JSON POST bind to struct with: path + query + http.NoBody",
1066+
name: "nok, JSON POST with http.NoBody",
10651067
givenURL: "/api/real_node/endpoint?node=xxx",
10661068
givenMethod: http.MethodPost,
10671069
givenContentType: MIMEApplicationJSON,
10681070
givenContent: http.NoBody,
10691071
expect: &Node{ID: 0, Node: ""},
1072+
expectError: "code=400, message=EOF, internal=EOF",
1073+
},
1074+
{
1075+
name: "ok, JSON POST with empty body",
1076+
givenURL: "/api/real_node/endpoint?node=xxx",
1077+
givenMethod: http.MethodPost,
1078+
givenContentType: MIMEApplicationJSON,
1079+
givenContent: strings.NewReader(""),
1080+
expect: &Node{ID: 0, Node: ""},
1081+
},
1082+
{
1083+
name: "ok, JSON POST bind to struct with: path + query + chunked body",
1084+
givenURL: "/api/real_node/endpoint?node=xxx",
1085+
givenMethod: http.MethodPost,
1086+
givenContentType: MIMEApplicationJSON,
1087+
givenContent: httputil.NewChunkedReader(strings.NewReader("18\r\n" + `{"id": 1, "node": "zzz"}` + "\r\n0\r\n")),
1088+
whenChunkedBody: true,
1089+
expect: &Node{ID: 1, Node: "zzz"},
10701090
},
10711091
}
10721092

@@ -1083,6 +1103,10 @@ func TestDefaultBinder_BindBody(t *testing.T) {
10831103
case MIMEApplicationJSON:
10841104
req.Header.Set(HeaderContentType, MIMEApplicationJSON)
10851105
}
1106+
if tc.whenChunkedBody {
1107+
req.ContentLength = -1
1108+
req.TransferEncoding = append(req.TransferEncoding, "chunked")
1109+
}
10861110
rec := httptest.NewRecorder()
10871111
c := e.NewContext(req, rec)
10881112

0 commit comments

Comments
 (0)