jarcoal / httpmock

HTTP mocking for Golang
http://godoc.org/github.com/jarcoal/httpmock
MIT License
1.93k stars 103 forks source link

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

Closed Ray-Eldath closed 2 years ago

Ray-Eldath commented 3 years ago
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 3 years ago

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

Ray-Eldath commented 3 years ago

Thanks, I'll check it out. :wink:

maxatome commented 2 years ago

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

Ray-Eldath commented 2 years ago

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