@@ -3,6 +3,8 @@ package httpmock_test
33import (
44 "encoding/xml"
55 "errors"
6+ "fmt"
7+ "io"
68 "io/ioutil"
79 "net/http"
810 "path/filepath"
@@ -468,41 +470,83 @@ func TestNewErrorResponder(t *testing.T) {
468470 }
469471}
470472
471- func TestRewindResponse (t * testing.T ) {
472- body := []byte ("hello world" )
473- status := 200
474- responses := []* http.Response {
475- NewBytesResponse (status , body ),
476- NewStringResponse (status , string (body )),
477- }
473+ func TestResponseBody (t * testing.T ) {
474+ const (
475+ body = "hello world"
476+ status = 200
477+ )
478478
479- for _ , response := range responses {
480- data , err := ioutil .ReadAll (response .Body )
481- if err != nil {
482- t .Fatal (err )
483- }
479+ t .Run ("http.Response" , func (t * testing.T ) {
480+ for i , response := range []* http.Response {
481+ NewBytesResponse (status , []byte (body )),
482+ NewStringResponse (status , body ),
483+ } {
484+ t .Run (fmt .Sprintf ("resp #%d" , i ), func (t * testing.T ) {
485+ data , err := ioutil .ReadAll (response .Body )
486+ if err != nil {
487+ t .Error (err )
488+ return
489+ }
484490
485- if string (data ) != string (body ) {
486- t .FailNow ()
487- }
491+ if string (data ) != string (body ) {
492+ t .Errorf ("body mismatch: %q ≠ %q" , data , body )
493+ return
494+ }
488495
489- if response .StatusCode != status {
490- t .FailNow ()
491- }
496+ if response .StatusCode != status {
497+ t .Errorf ("status mismatch: %d ≠ %d" , response .StatusCode , status )
498+ return
499+ }
492500
493- data , err = ioutil .ReadAll (response .Body )
494- if err != nil {
495- t .Fatal (err )
501+ var buf [1 ]byte
502+ _ , err = response .Body .Read (buf [:])
503+ if err == nil {
504+ t .Errorf ("Next Read() should produce an error" )
505+ return
506+ }
507+ if err != io .EOF {
508+ t .Errorf ("Next Read() should io.EOF" )
509+ }
510+ })
496511 }
512+ })
497513
498- if string (data ) != string (body ) {
499- t .FailNow ()
500- }
514+ t .Run ("Responder" , func (t * testing.T ) {
515+ for i , responder := range []Responder {
516+ NewBytesResponder (200 , []byte (body )),
517+ NewStringResponder (200 , body ),
518+ } {
519+ t .Run (fmt .Sprintf ("resp #%d" , i ), func (t * testing.T ) {
520+ req , _ := http .NewRequest ("GET" , "http://foo.bar" , nil )
521+ response , err := responder (req )
522+ if err != nil {
523+ t .Error (err )
524+ return
525+ }
526+
527+ data , err := ioutil .ReadAll (response .Body )
528+ if err != nil {
529+ t .Error (err )
530+ return
531+ }
501532
502- if response .StatusCode != status {
503- t .FailNow ()
533+ if string (data ) != string (body ) {
534+ t .Errorf ("body mismatch: %q ≠ %q" , data , body )
535+ return
536+ }
537+
538+ var buf [1 ]byte
539+ _ , err = response .Body .Read (buf [:])
540+ if err == nil {
541+ t .Errorf ("Next Read() should produce an error" )
542+ return
543+ }
544+ if err != io .EOF {
545+ t .Errorf ("Next Read() should io.EOF" )
546+ }
547+ })
504548 }
505- }
549+ })
506550}
507551
508552func TestResponder (t * testing.T ) {
0 commit comments