lib/webmock/response.rb in webmock-0.8.2 vs lib/webmock/response.rb in webmock-0.9.0
- old
+ new
@@ -1,11 +1,21 @@
+#compatibility with Ruby 1.9.2 preview1 to allow reading raw responses
+class StringIO
+ alias_method :read_nonblock, :sysread
+end
+
module WebMock
+
class Response
attr_reader :options
def initialize(options = {})
- self.options = options
+ if options.is_a?(IO) || options.is_a?(String)
+ self.options = read_raw_response(options)
+ else
+ self.options = options
+ end
@options[:headers] = Util::Headers.normalize_headers(@options[:headers]) unless @options[:headers].is_a?(Proc)
end
def headers
@options[:headers]
@@ -37,16 +47,37 @@
end
def ==(other)
options == other.options
end
+
+ private
def stringify_body!
if @options[:body].is_a?(IO)
io = @options[:body]
@options[:body] = io.read
io.close
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)
+ response = Net::HTTPResponse.read_new(socket)
+ transfer_encoding = response.delete('transfer-encoding') #chunks were already read by curl
+ response.reading_body(socket, true) {}
+
+ options = {}
+ options[:headers] = {}
+ response.each_header {|name, value| options[:headers][name] = value}
+ options[:headers]['transfer-encoding'] = transfer_encoding if transfer_encoding
+ options[:body] = response.read_body
+ options
end
end
end