66 "compress/gzip"
77 "compress/zlib"
88 "context"
9+ "encoding/base64"
910 "encoding/json"
1011 "errors"
1112 "fmt"
@@ -544,6 +545,7 @@ func testRequestWithBody(t *testing.T, verb, path string) {
544545 testRequestWithBodyMultiPartBody ,
545546 testRequestWithBodyQueryParams ,
546547 testRequestWithBodyQueryParamsAndBody ,
548+ testRequestWithBodyBinaryBody ,
547549 }
548550 for _ , testFunc := range testFuncs {
549551 testFunc := testFunc
@@ -555,6 +557,57 @@ func testRequestWithBody(t *testing.T, verb, path string) {
555557 }
556558}
557559
560+ func testRequestWithBodyBinaryBody (t * testing.T , verb string , path string ) {
561+ tests := []struct {
562+ contentType string
563+ requestBody string
564+ }{
565+ {"application/octet-stream" , "encodeMe" },
566+ {"image/png" , "encodeMe-png" },
567+ {"image/webp" , "encodeMe-webp" },
568+ {"image/jpeg" , "encodeMe-jpeg" },
569+ {"unknown" , "encodeMe-unknown" },
570+ }
571+ for _ , test := range tests {
572+ test := test
573+ t .Run ("content type/" + test .contentType , func (t * testing.T ) {
574+ t .Parallel ()
575+
576+ testBody := bytes .NewReader ([]byte (test .requestBody ))
577+
578+ r , _ := http .NewRequest (verb , path , testBody )
579+ r .Header .Set ("Content-Type" , test .contentType )
580+ w := httptest .NewRecorder ()
581+ app .ServeHTTP (w , r )
582+
583+ assertStatusCode (t , w , http .StatusOK )
584+ assertContentType (t , w , jsonContentType )
585+
586+ var resp * bodyResponse
587+ err := json .Unmarshal (w .Body .Bytes (), & resp )
588+ if err != nil {
589+ t .Fatalf ("failed to unmarshal body %s from JSON: %s" , w .Body , err )
590+ }
591+
592+ expected := "data:" + test .contentType + ";base64," + base64 .StdEncoding .EncodeToString ([]byte (test .requestBody ))
593+
594+ if resp .Data != expected {
595+ t .Fatalf ("expected binary encoded response data: %#v got %#v" , expected , resp .Data )
596+ }
597+ if resp .JSON != nil {
598+ t .Fatalf ("expected nil response json, got %#v" , resp .JSON )
599+ }
600+
601+ if len (resp .Args ) > 0 {
602+ t .Fatalf ("expected no query params, got %#v" , resp .Args )
603+ }
604+ if len (resp .Form ) > 0 {
605+ t .Fatalf ("expected no form data, got %#v" , resp .Form )
606+ }
607+ })
608+ }
609+ }
610+
558611func testRequestWithBodyEmptyBody (t * testing.T , verb string , path string ) {
559612 tests := []struct {
560613 contentType string
@@ -681,8 +734,10 @@ func testRequestWithBodyFormEncodedBodyNoContentType(t *testing.T, verb, path st
681734 if len (resp .Form ) != 0 {
682735 t .Fatalf ("expected no form values, got %d" , len (resp .Form ))
683736 }
684- if string (resp .Data ) != params .Encode () {
685- t .Fatalf ("response data mismatch, %#v != %#v" , string (resp .Data ), params .Encode ())
737+ // Because we did not set an content type, httpbin will return the base64 encoded data.
738+ expectedBody := "data:application/octet-stream;base64," + base64 .StdEncoding .EncodeToString ([]byte (params .Encode ()))
739+ if string (resp .Data ) != expectedBody {
740+ t .Fatalf ("response data mismatch, %#v != %#v" , string (resp .Data ), expectedBody )
686741 }
687742}
688743
0 commit comments