9
9
"mime/multipart"
10
10
"net/http"
11
11
"net/http/httptest"
12
+ "net/url"
12
13
"reflect"
13
14
"strconv"
14
15
"strings"
@@ -187,19 +188,26 @@ func TestToMultipleFields(t *testing.T) {
187
188
188
189
func TestBindJSON (t * testing.T ) {
189
190
assert := assert .New (t )
190
- testBindOkay (assert , strings .NewReader (userJSON ), MIMEApplicationJSON )
191
+ testBindOkay (assert , strings .NewReader (userJSON ), nil , MIMEApplicationJSON )
192
+ testBindOkay (assert , strings .NewReader (userJSON ), dummyQuery , MIMEApplicationJSON )
193
+ testBindArrayOkay (assert , strings .NewReader (usersJSON ), nil , MIMEApplicationJSON )
194
+ testBindArrayOkay (assert , strings .NewReader (usersJSON ), dummyQuery , MIMEApplicationJSON )
191
195
testBindError (assert , strings .NewReader (invalidContent ), MIMEApplicationJSON , & json.SyntaxError {})
192
196
testBindError (assert , strings .NewReader (userJSONInvalidType ), MIMEApplicationJSON , & json.UnmarshalTypeError {})
193
197
}
194
198
195
199
func TestBindXML (t * testing.T ) {
196
200
assert := assert .New (t )
197
201
198
- testBindOkay (assert , strings .NewReader (userXML ), MIMEApplicationXML )
202
+ testBindOkay (assert , strings .NewReader (userXML ), nil , MIMEApplicationXML )
203
+ testBindOkay (assert , strings .NewReader (userXML ), dummyQuery , MIMEApplicationXML )
204
+ testBindArrayOkay (assert , strings .NewReader (userXML ), nil , MIMEApplicationXML )
205
+ testBindArrayOkay (assert , strings .NewReader (userXML ), dummyQuery , MIMEApplicationXML )
199
206
testBindError (assert , strings .NewReader (invalidContent ), MIMEApplicationXML , errors .New ("" ))
200
207
testBindError (assert , strings .NewReader (userXMLConvertNumberError ), MIMEApplicationXML , & strconv.NumError {})
201
208
testBindError (assert , strings .NewReader (userXMLUnsupportedTypeError ), MIMEApplicationXML , & xml.SyntaxError {})
202
- testBindOkay (assert , strings .NewReader (userXML ), MIMETextXML )
209
+ testBindOkay (assert , strings .NewReader (userXML ), nil , MIMETextXML )
210
+ testBindOkay (assert , strings .NewReader (userXML ), dummyQuery , MIMETextXML )
203
211
testBindError (assert , strings .NewReader (invalidContent ), MIMETextXML , errors .New ("" ))
204
212
testBindError (assert , strings .NewReader (userXMLConvertNumberError ), MIMETextXML , & strconv.NumError {})
205
213
testBindError (assert , strings .NewReader (userXMLUnsupportedTypeError ), MIMETextXML , & xml.SyntaxError {})
@@ -208,7 +216,8 @@ func TestBindXML(t *testing.T) {
208
216
func TestBindForm (t * testing.T ) {
209
217
assert := assert .New (t )
210
218
211
- testBindOkay (assert , strings .NewReader (userForm ), MIMEApplicationForm )
219
+ testBindOkay (assert , strings .NewReader (userForm ), nil , MIMEApplicationForm )
220
+ testBindOkay (assert , strings .NewReader (userForm ), dummyQuery , MIMEApplicationForm )
212
221
e := New ()
213
222
req := httptest .NewRequest (http .MethodPost , "/" , strings .NewReader (userForm ))
214
223
rec := httptest .NewRecorder ()
@@ -336,14 +345,16 @@ func TestBindUnmarshalTextPtr(t *testing.T) {
336
345
}
337
346
338
347
func TestBindMultipartForm (t * testing.T ) {
339
- body := new (bytes.Buffer )
340
- mw := multipart .NewWriter (body )
348
+ bodyBuffer := new (bytes.Buffer )
349
+ mw := multipart .NewWriter (bodyBuffer )
341
350
mw .WriteField ("id" , "1" )
342
351
mw .WriteField ("name" , "Jon Snow" )
343
352
mw .Close ()
353
+ body := bodyBuffer .Bytes ()
344
354
345
355
assert := assert .New (t )
346
- testBindOkay (assert , body , mw .FormDataContentType ())
356
+ testBindOkay (assert , bytes .NewReader (body ), nil , mw .FormDataContentType ())
357
+ testBindOkay (assert , bytes .NewReader (body ), dummyQuery , mw .FormDataContentType ())
347
358
}
348
359
349
360
func TestBindUnsupportedMediaType (t * testing.T ) {
@@ -547,9 +558,13 @@ func assertBindTestStruct(a *assert.Assertions, ts *bindTestStruct) {
547
558
a .Equal ("" , ts .GetCantSet ())
548
559
}
549
560
550
- func testBindOkay (assert * assert.Assertions , r io.Reader , ctype string ) {
561
+ func testBindOkay (assert * assert.Assertions , r io.Reader , query url. Values , ctype string ) {
551
562
e := New ()
552
- req := httptest .NewRequest (http .MethodPost , "/" , r )
563
+ path := "/"
564
+ if len (query ) > 0 {
565
+ path += "?" + query .Encode ()
566
+ }
567
+ req := httptest .NewRequest (http .MethodPost , path , r )
553
568
rec := httptest .NewRecorder ()
554
569
c := e .NewContext (req , rec )
555
570
req .Header .Set (HeaderContentType , ctype )
@@ -561,6 +576,25 @@ func testBindOkay(assert *assert.Assertions, r io.Reader, ctype string) {
561
576
}
562
577
}
563
578
579
+ func testBindArrayOkay (assert * assert.Assertions , r io.Reader , query url.Values , ctype string ) {
580
+ e := New ()
581
+ path := "/"
582
+ if len (query ) > 0 {
583
+ path += "?" + query .Encode ()
584
+ }
585
+ req := httptest .NewRequest (http .MethodPost , path , r )
586
+ rec := httptest .NewRecorder ()
587
+ c := e .NewContext (req , rec )
588
+ req .Header .Set (HeaderContentType , ctype )
589
+ u := []user {}
590
+ err := c .Bind (& u )
591
+ if assert .NoError (err ) {
592
+ assert .Equal (1 , len (u ))
593
+ assert .Equal (1 , u [0 ].ID )
594
+ assert .Equal ("Jon Snow" , u [0 ].Name )
595
+ }
596
+ }
597
+
564
598
func testBindError (assert * assert.Assertions , r io.Reader , ctype string , expectedInternal error ) {
565
599
e := New ()
566
600
req := httptest .NewRequest (http .MethodPost , "/" , r )
@@ -679,15 +713,16 @@ func TestDefaultBinder_BindToStructFromMixedSources(t *testing.T) {
679
713
expect : & Opts {ID : 0 , Node : "xxx" }, // query binding has already modified bind target
680
714
expectError : "code=400, message=Unmarshal type error: expected=echo.Opts, got=array, field=, offset=1, internal=json: cannot unmarshal array into Go value of type echo.Opts" ,
681
715
},
682
- { // binding query params interferes with body. b.BindBody() should be used to bind only body to slice
683
- name : "nok , GET query params bind failure - trying to bind json array to slice " ,
716
+ { // query param is ignored as we do not know where exactly to bind it in slice
717
+ name : "ok , GET bind to struct slice, ignore query param " ,
684
718
givenMethod : http .MethodGet ,
685
719
givenURL : "/api/real_node/endpoint?node=xxx" ,
686
720
givenContent : strings .NewReader (`[{"id": 1}]` ),
687
721
whenNoPathParams : true ,
688
722
whenBindTarget : & []Opts {},
689
- expect : & []Opts {},
690
- expectError : "code=400, message=binding element must be a struct, internal=binding element must be a struct" ,
723
+ expect : & []Opts {
724
+ {ID : 1 , Node : "" },
725
+ },
691
726
},
692
727
{ // binding query params interferes with body. b.BindBody() should be used to bind only body to slice
693
728
name : "ok, POST binding to slice should not be affected query params types" ,
@@ -699,14 +734,15 @@ func TestDefaultBinder_BindToStructFromMixedSources(t *testing.T) {
699
734
expect : & []Opts {{ID : 1 }},
700
735
expectError : "" ,
701
736
},
702
- { // binding path params interferes with body. b.BindBody() should be used to bind only body to slice
703
- name : "nok , GET path params bind failure - trying to bind json array to slice " ,
737
+ { // path param is ignored as we do not know where exactly to bind it in slice
738
+ name : "ok , GET bind to struct slice, ignore path param " ,
704
739
givenMethod : http .MethodGet ,
705
740
givenURL : "/api/real_node/endpoint?node=xxx" ,
706
741
givenContent : strings .NewReader (`[{"id": 1}]` ),
707
742
whenBindTarget : & []Opts {},
708
- expect : & []Opts {},
709
- expectError : "code=400, message=binding element must be a struct, internal=binding element must be a struct" ,
743
+ expect : & []Opts {
744
+ {ID : 1 , Node : "" },
745
+ },
710
746
},
711
747
{
712
748
name : "ok, GET body bind json array to slice" ,
0 commit comments