Sha256: 1ec61d2ade3676da9560b188998f9436ce8d6af3991f830cb100a42840594a10

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

# 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 VCR
  module Net
    module HTTPResponse
      def self.extended(response)
        response.instance_variable_set(:@__read_body_previously_called, false)
      end

      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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vcr-2.0.0.rc1 lib/vcr/extensions/net_http_response.rb
vcr-2.0.0.beta2 lib/vcr/extensions/net_http_response.rb
vcr-2.0.0.beta1 lib/vcr/extensions/net_http_response.rb
vcr-1.11.3 lib/vcr/extensions/net_http_response.rb
vcr-1.11.2 lib/vcr/extensions/net_http_response.rb