Inconsist behavior between http.Response.Body and httpmock.Response.Body
Ray-Eldath opened this issue · 4 comments
Ray-Eldath commented
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
.
maxatome commented
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))
Ray-Eldath commented
Thanks, I'll check it out. 😉
maxatome commented
Hi @Ray-Eldath, #116 should fix the problem, could you check on your side please?
Ray-Eldath commented
Yes, now the second read returns empty string, as consistent with http.Response. 😸