Sha256: 93e23855245cea931d1902f9b9c5a25306c922d384d172ee60bb8e4bb5a9cffa

Contents?: true

Size: 1.17 KB

Versions: 7

Compression:

Stored size: 1.17 KB

Contents

# This code is entierly copied from VCR (http://github.com/myronmarston/vcr) by courtesy of Myron Marston

# A Net::HTTP response that has already been read raises an IOError when #read_body
# is called with a destination string or block.
#
# This causes a problem when VCR records a response--it reads the body before yielding
# the response, and if the code that is consuming the HTTP requests uses #read_body, it
# can cause an error.
#
# This is a bit of a hack, but it allows a Net::HTTP response to be "re-read"
# after it has aleady been read.  This attemps to preserve the behavior of
# #read_body, acting just as if it had never been read.


module Net
  module WebMockHTTPResponse
    def read_body(dest = nil, &block)
      return super if @__read_body_previously_called
      return @body if dest.nil? && block.nil?
      raise ArgumentError.new("both arg and block given for HTTP method") if dest && block
      return nil if @body.nil?

      dest ||= ::Net::ReadAdapter.new(block)
      dest << @body
      @body = dest
    ensure
      # allow subsequent calls to #read_body to proceed as normal, without our hack...
      @__read_body_previously_called = true
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
webmock-1.7.6 lib/webmock/http_lib_adapters/net_http_response.rb
webmock-1.7.5 lib/webmock/http_lib_adapters/net_http_response.rb
webmock-1.7.4 lib/webmock/http_lib_adapters/net_http_response.rb
webmock-1.7.3 lib/webmock/http_lib_adapters/net_http_response.rb
webmock-1.7.2 lib/webmock/http_lib_adapters/net_http_response.rb
webmock-1.7.1 lib/webmock/http_lib_adapters/net_http_response.rb
webmock-1.7.0 lib/webmock/http_lib_adapters/net_http_response.rb