jarcoal / httpmock

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

Add ContentLength property in mocked http.Response #20

Closed browny closed 5 years ago

browny commented 8 years ago

When using httpmock with grequests http client, the grequests.Response.String() method will fail to read http response body because the property ContentLength of the mocked http.Response is 0. (When ContentLength == 0, grequests doesn't copy http response body to its own Response, refer to here)

According to the godoc of http.Response, I think make it as -1 would be good.

// ContentLength records the length of the associated content. The // value -1 indicates that the length is unknown. Unless Request.Method // is "HEAD", values >= 0 indicate that the given number of bytes may // be read from Body.

thanks :)

dlebech commented 7 years ago

@browny Thanks for the PR! I have two requests:

  1. Given that you have access to the actual response body when setting this value, I think it would be better to set the correct ContentLength based on the length of the string and the byte slice.
  2. If you could also add a test that asserts the correct length and possibly test this with either something like io.CopyN() or simply resp.Body.Len() == resp.ContentLength. Something along those lines would be awesome :smiley: