@@ -542,6 +542,7 @@ func testRequestWithBody(t *testing.T, verb, path string) {
542
542
testRequestWithBodyBodyTooBig ,
543
543
testRequestWithBodyEmptyBody ,
544
544
testRequestWithBodyFormEncodedBody ,
545
+ testRequestWithBodyMultiPartBodyFiles ,
545
546
testRequestWithBodyFormEncodedBodyNoContentType ,
546
547
testRequestWithBodyInvalidFormEncodedBody ,
547
548
testRequestWithBodyInvalidJSON ,
@@ -816,6 +817,47 @@ func testRequestWithBodyMultiPartBody(t *testing.T, verb, path string) {
816
817
}
817
818
}
818
819
820
+ func testRequestWithBodyMultiPartBodyFiles (t * testing.T , verb , path string ) {
821
+ var body bytes.Buffer
822
+ mw := multipart .NewWriter (& body )
823
+
824
+ // Add a file to the multipart request
825
+ part , _ := mw .CreateFormFile ("fieldname" , "filename" )
826
+ part .Write ([]byte ("hello world" ))
827
+ mw .Close ()
828
+
829
+ r , _ := http .NewRequest (verb , path , bytes .NewReader (body .Bytes ()))
830
+ r .Header .Set ("Content-Type" , mw .FormDataContentType ())
831
+ w := httptest .NewRecorder ()
832
+ app .ServeHTTP (w , r )
833
+
834
+ assertStatusCode (t , w , http .StatusOK )
835
+ assertContentType (t , w , jsonContentType )
836
+
837
+ var resp * bodyResponse
838
+ err := json .Unmarshal (w .Body .Bytes (), & resp )
839
+ if err != nil {
840
+ t .Fatalf ("failed to unmarshal body %#v from JSON: %s" , w .Body .String (), err )
841
+ }
842
+
843
+ if len (resp .Args ) > 0 {
844
+ t .Fatalf ("expected no query params, got %#v" , resp .Args )
845
+ }
846
+
847
+ // verify that the file we added is present in the `files` attribute of the
848
+ // response, with the field as key and content as value
849
+ wantFiles := map [string ][]string {
850
+ "fieldname" : {"hello world" },
851
+ }
852
+ if ! reflect .DeepEqual (resp .Files , wantFiles ) {
853
+ t .Fatalf ("want resp.Files = %#v, got %#v" , wantFiles , resp .Files )
854
+ }
855
+
856
+ if resp .Method != verb {
857
+ t .Fatalf ("expected method to be %s, got %s" , verb , resp .Method )
858
+ }
859
+ }
860
+
819
861
func testRequestWithBodyInvalidFormEncodedBody (t * testing.T , verb , path string ) {
820
862
r , _ := http .NewRequest (verb , path , strings .NewReader ("%ZZ" ))
821
863
r .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
0 commit comments