gock uses a ioutil.NopCloser for its response bodies:
|
// createReadCloser creates an io.ReadCloser from a byte slice that is suitable for use as an |
|
// http response body. |
|
func createReadCloser(body []byte) io.ReadCloser { |
|
return ioutil.NopCloser(bytes.NewReader(body)) |
|
} |
This makes it very hard to catch faulty code which would try to read on a response's body after it's been closed:
func TestHTTP(t *testing.T) {
gock.Intercept()
gock.Observe(gock.DumpRequest)
gock.New("https://httpbin.org").
Get("/get").
Reply(200).
JSON(map[string]string{"foo": "bar"})
resp, err := http.Get("https://httpbin.org/get")
require.NoError(t, err)
resp.Body.Close()
body := map[string]string{}
require.NoError(t, json.NewDecoder(resp.Body).Decode(&body)) // this should fail
assert.Equal(t, map[string]string{"foo": "bar"}, body)
}
gock uses a
ioutil.NopCloserfor its response bodies:gock/responder.go
Lines 107 to 111 in 0c43888
This makes it very hard to catch faulty code which would try to read on a response's body after it's been closed: