@@ -13,6 +13,7 @@ import (
13
13
"mime/multipart"
14
14
"net/http"
15
15
"net/http/httptest"
16
+ "net/http/httputil"
16
17
"net/url"
17
18
"reflect"
18
19
"strconv"
@@ -941,6 +942,7 @@ func TestDefaultBinder_BindBody(t *testing.T) {
941
942
givenMethod string
942
943
givenContentType string
943
944
whenNoPathParams bool
945
+ whenChunkedBody bool
944
946
whenBindTarget interface {}
945
947
expect interface {}
946
948
expectError string
@@ -1061,12 +1063,30 @@ func TestDefaultBinder_BindBody(t *testing.T) {
1061
1063
expectError : "code=415, message=Unsupported Media Type" ,
1062
1064
},
1063
1065
{
1064
- name : "ok , JSON POST bind to struct with: path + query + http.NoBody" ,
1066
+ name : "nok , JSON POST with http.NoBody" ,
1065
1067
givenURL : "/api/real_node/endpoint?node=xxx" ,
1066
1068
givenMethod : http .MethodPost ,
1067
1069
givenContentType : MIMEApplicationJSON ,
1068
1070
givenContent : http .NoBody ,
1069
1071
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 \n 0\r \n " )),
1088
+ whenChunkedBody : true ,
1089
+ expect : & Node {ID : 1 , Node : "zzz" },
1070
1090
},
1071
1091
}
1072
1092
@@ -1083,6 +1103,10 @@ func TestDefaultBinder_BindBody(t *testing.T) {
1083
1103
case MIMEApplicationJSON :
1084
1104
req .Header .Set (HeaderContentType , MIMEApplicationJSON )
1085
1105
}
1106
+ if tc .whenChunkedBody {
1107
+ req .ContentLength = - 1
1108
+ req .TransferEncoding = append (req .TransferEncoding , "chunked" )
1109
+ }
1086
1110
rec := httptest .NewRecorder ()
1087
1111
c := e .NewContext (req , rec )
1088
1112
0 commit comments