class HTTP::Response
def to_webmock
def to_webmock webmock_response = ::WebMock::Response.new webmock_response.status = [status.to_i, reason] webmock_response.body = body.to_s # This call is used to reset the body of the response to enable it to be streamed if necessary. # The `body.to_s` call above reads the body, which allows WebMock to trigger any registered callbacks. # However, once the body is read to_s, it cannot be streamed again and attempting to do so # will raise a "HTTP::StateError: body has already been consumed" error. # To avoid this error, we replace the original body with a new one. # The new body has its @stream attribute set to new Streamer, instead of the original Connection. # Unfortunately, it's not possible to reset the original body to its initial streaming state. # Therefore, this replacement is the best workaround currently available. reset_body_to_allow_it_to_be_streamed!(webmock_response) webmock_response.headers = headers.to_h webmock_response end