lib/webmock/response.rb in webmock-3.17.1 vs lib/webmock/response.rb in webmock-3.18.0
- old
+ new
@@ -12,12 +12,15 @@
end
end
class Response
def initialize(options = {})
- if options.is_a?(IO) || options.is_a?(String)
+ case options
+ when IO, StringIO
self.options = read_raw_response(options)
+ when String
+ self.options = read_raw_response(StringIO.new(options))
else
self.options = options
end
end
@@ -118,17 +121,12 @@
else
raise InvalidBody, "must be one of: #{valid_types}. '#{@body.class}' given"
end
end
- def read_raw_response(raw_response)
- if raw_response.is_a?(IO)
- string = raw_response.read
- raw_response.close
- raw_response = string
- end
- socket = ::Net::BufferedIO.new(raw_response)
+ def read_raw_response(io)
+ socket = ::Net::BufferedIO.new(io)
response = ::Net::HTTPResponse.read_new(socket)
transfer_encoding = response.delete('transfer-encoding') #chunks were already read by curl
response.reading_body(socket, true) {}
options = {}
@@ -136,9 +134,11 @@
response.each_header {|name, value| options[:headers][name] = value}
options[:headers]['transfer-encoding'] = transfer_encoding if transfer_encoding
options[:body] = response.read_body
options[:status] = [response.code.to_i, response.message]
options
+ ensure
+ socket.close
end
InvalidBody = Class.new(StandardError)
end