jarcoal/httpmock

Inconsist behavior between http.Response.Body and httpmock.Response.Body

Ray-Eldath opened this issue · 4 comments

httpmock.RegisterResponder("GET", "http://127.0.0.1:12350/test_01",
	httpmock.NewStringResponder(200, "test"))
httpmock.Activate()

body, _ := http.Get("http://127.0.0.1:12350/test_01")
bytes, _ := ioutil.ReadAll(body.Body)
fmt.Println(string(bytes))

bytes2, _ := ioutil.ReadAll(body.Body)
fmt.Println(string(bytes2))

httpmock.Deactivate()

This will print test twice, but use a real server it will only print once since you can't read twice from a http.Body.

Hi, it seems 5203e0b is no longer needed since a88cbab. I have to do some tests to check potential edge cases.

You can work this problem around by providing your own response body:

resp := httpmock.NewStringResponse(200, "")
resp.Body = io.NopCloser(strings.NewReader("test"))
httpmock.RegisterResponder("GET", "http://127.0.0.1:12350/test_01",
    httpmock.ResponderFromResponse(resp))

https://play.golang.org/p/UEajs9YzgQ5

Thanks, I'll check it out. 😉

Hi @Ray-Eldath, #116 should fix the problem, could you check on your side please?

Yes, now the second read returns empty string, as consistent with http.Response. 😸