6
6
"compress/gzip"
7
7
"compress/zlib"
8
8
"context"
9
+ "encoding/base64"
9
10
"encoding/json"
10
11
"errors"
11
12
"fmt"
@@ -544,6 +545,7 @@ func testRequestWithBody(t *testing.T, verb, path string) {
544
545
testRequestWithBodyMultiPartBody ,
545
546
testRequestWithBodyQueryParams ,
546
547
testRequestWithBodyQueryParamsAndBody ,
548
+ testRequestWithBodyBinaryBody ,
547
549
}
548
550
for _ , testFunc := range testFuncs {
549
551
testFunc := testFunc
@@ -555,6 +557,57 @@ func testRequestWithBody(t *testing.T, verb, path string) {
555
557
}
556
558
}
557
559
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
+
558
611
func testRequestWithBodyEmptyBody (t * testing.T , verb string , path string ) {
559
612
tests := []struct {
560
613
contentType string
@@ -681,8 +734,10 @@ func testRequestWithBodyFormEncodedBodyNoContentType(t *testing.T, verb, path st
681
734
if len (resp .Form ) != 0 {
682
735
t .Fatalf ("expected no form values, got %d" , len (resp .Form ))
683
736
}
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 )
686
741
}
687
742
}
688
743
0 commit comments