Sha256: c04669e652202e2223756a79ecabba08013dffb5bb3c6ff5ee795215702030d9

Contents?: true

Size: 1.27 KB

Versions: 13

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

# 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)
      if !(defined?(@__read_body_previously_called).nil?) && @__read_body_previously_called
        return super
      end
      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.dup
      @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

13 entries across 13 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/webmock-3.23.1/lib/webmock/http_lib_adapters/net_http_response.rb
webmock-3.24.0 lib/webmock/http_lib_adapters/net_http_response.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/webmock-3.23.1/lib/webmock/http_lib_adapters/net_http_response.rb
webmock-3.23.0 lib/webmock/http_lib_adapters/net_http_response.rb
webmock-3.22.0 lib/webmock/http_lib_adapters/net_http_response.rb
webmock-3.21.2 lib/webmock/http_lib_adapters/net_http_response.rb
webmock-3.21.1 lib/webmock/http_lib_adapters/net_http_response.rb
webmock-3.21.0 lib/webmock/http_lib_adapters/net_http_response.rb
webmock-3.20.0 lib/webmock/http_lib_adapters/net_http_response.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/webmock-3.19.1/lib/webmock/http_lib_adapters/net_http_response.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/webmock-3.19.1/lib/webmock/http_lib_adapters/net_http_response.rb
webmock-3.19.1 lib/webmock/http_lib_adapters/net_http_response.rb
webmock-3.19.0 lib/webmock/http_lib_adapters/net_http_response.rb