Is there a way to verify the body of an http.Response has been closed?

I am attempting to write some unit test to make sure a method that handles some http.Response calls response.Body.Close() once it is done reading such body.

You can replace the Body field with a io.ReadCloser that sets a flag when Close has been called, if the response is passed as a parameter to the method.

Other than that, the best way is source inspection. If the function creates and uses the Response in its entirety, defer a Close() call after it’s been verified to not be nil an error (certain errors return a non-nil, but closed body, Response).


Here is an example of what @riking suggested

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.