mirror of
https://github.com/labstack/echo.git
synced 2025-07-05 00:58:47 +02:00
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
This commit is contained in:
committed by
GitHub
parent
3b017855b4
commit
0368ed87f2
2
bind.go
2
bind.go
@ -67,7 +67,7 @@ func (b *DefaultBinder) BindQueryParams(c Context, i interface{}) error {
|
|||||||
// See MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseMultipartForm
|
// See MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseMultipartForm
|
||||||
func (b *DefaultBinder) BindBody(c Context, i interface{}) (err error) {
|
func (b *DefaultBinder) BindBody(c Context, i interface{}) (err error) {
|
||||||
req := c.Request()
|
req := c.Request()
|
||||||
if req.ContentLength <= 0 {
|
if req.ContentLength == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
26
bind_test.go
26
bind_test.go
@ -13,6 +13,7 @@ import (
|
|||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -941,6 +942,7 @@ func TestDefaultBinder_BindBody(t *testing.T) {
|
|||||||
givenMethod string
|
givenMethod string
|
||||||
givenContentType string
|
givenContentType string
|
||||||
whenNoPathParams bool
|
whenNoPathParams bool
|
||||||
|
whenChunkedBody bool
|
||||||
whenBindTarget interface{}
|
whenBindTarget interface{}
|
||||||
expect interface{}
|
expect interface{}
|
||||||
expectError string
|
expectError string
|
||||||
@ -1061,12 +1063,30 @@ func TestDefaultBinder_BindBody(t *testing.T) {
|
|||||||
expectError: "code=415, message=Unsupported Media Type",
|
expectError: "code=415, message=Unsupported Media Type",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "ok, JSON POST bind to struct with: path + query + http.NoBody",
|
name: "nok, JSON POST with http.NoBody",
|
||||||
givenURL: "/api/real_node/endpoint?node=xxx",
|
givenURL: "/api/real_node/endpoint?node=xxx",
|
||||||
givenMethod: http.MethodPost,
|
givenMethod: http.MethodPost,
|
||||||
givenContentType: MIMEApplicationJSON,
|
givenContentType: MIMEApplicationJSON,
|
||||||
givenContent: http.NoBody,
|
givenContent: http.NoBody,
|
||||||
expect: &Node{ID: 0, Node: ""},
|
expect: &Node{ID: 0, Node: ""},
|
||||||
|
expectError: "code=400, message=EOF, internal=EOF",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ok, JSON POST with empty body",
|
||||||
|
givenURL: "/api/real_node/endpoint?node=xxx",
|
||||||
|
givenMethod: http.MethodPost,
|
||||||
|
givenContentType: MIMEApplicationJSON,
|
||||||
|
givenContent: strings.NewReader(""),
|
||||||
|
expect: &Node{ID: 0, Node: ""},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ok, JSON POST bind to struct with: path + query + chunked body",
|
||||||
|
givenURL: "/api/real_node/endpoint?node=xxx",
|
||||||
|
givenMethod: http.MethodPost,
|
||||||
|
givenContentType: MIMEApplicationJSON,
|
||||||
|
givenContent: httputil.NewChunkedReader(strings.NewReader("18\r\n" + `{"id": 1, "node": "zzz"}` + "\r\n0\r\n")),
|
||||||
|
whenChunkedBody: true,
|
||||||
|
expect: &Node{ID: 1, Node: "zzz"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1083,6 +1103,10 @@ func TestDefaultBinder_BindBody(t *testing.T) {
|
|||||||
case MIMEApplicationJSON:
|
case MIMEApplicationJSON:
|
||||||
req.Header.Set(HeaderContentType, MIMEApplicationJSON)
|
req.Header.Set(HeaderContentType, MIMEApplicationJSON)
|
||||||
}
|
}
|
||||||
|
if tc.whenChunkedBody {
|
||||||
|
req.ContentLength = -1
|
||||||
|
req.TransferEncoding = append(req.TransferEncoding, "chunked")
|
||||||
|
}
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
c := e.NewContext(req, rec)
|
c := e.NewContext(req, rec)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user