Skip to content

Commit 25b82c6

Browse files
authored
Merge pull request #116 from jarcoal/response-body
fix: be consistent between net/http and httpmock Response.Body
2 parents 8c2c5b7 + 8247e93 commit 25b82c6

2 files changed

Lines changed: 73 additions & 33 deletions

File tree

response.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -458,15 +458,11 @@ func (d *dummyReadCloser) setup() {
458458

459459
func (d *dummyReadCloser) Read(p []byte) (n int, err error) {
460460
d.setup()
461-
n, err = d.body.Read(p)
462-
if err == io.EOF {
463-
d.body.Seek(0, 0) // nolint: errcheck
464-
}
465-
return n, err
461+
return d.body.Read(p)
466462
}
467463

468464
func (d *dummyReadCloser) Close() error {
469465
d.setup()
470-
d.body.Seek(0, 0) // nolint: errcheck
466+
d.body.Seek(0, io.SeekEnd) // nolint: errcheck
471467
return nil
472468
}

response_test.go

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package httpmock_test
33
import (
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

508552
func TestResponder(t *testing.T) {

0 commit comments

Comments
 (0)