Permalink
Browse files

Merge pull request #126 from andreas/master

Fix PatronAdapter for Patron v0.4.15
  • Loading branch information...
2 parents fcea4b9 + eb2e94d commit 2ec3f5c7f3ffe56dc951b86c9010992a35759ff8 @bblimke committed Aug 27, 2011
Showing with 58 additions and 9 deletions.
  1. +15 −8 lib/webmock/http_lib_adapters/patron_adapter.rb
  2. +38 −0 spec/patron_spec.rb
  3. +5 −1 spec/patron_spec_helper.rb
@@ -25,7 +25,7 @@ def handle_request_with_webmock(req)
WebMock::HttpLibAdapters::PatronAdapter.
handle_file_name(req, webmock_response)
res = WebMock::HttpLibAdapters::PatronAdapter.
- build_patron_response(webmock_response)
+ build_patron_response(webmock_response, default_response_charset)
WebMock::CallbackRegistry.invoke_callbacks(
{:lib => :patron}, request_signature, webmock_response)
res
@@ -98,15 +98,22 @@ def self.build_request_signature(req)
request_signature
end
- def self.build_patron_response(webmock_response)
+ def self.build_patron_response(webmock_response, default_response_charset)
raise ::Patron::TimeoutError if webmock_response.should_timeout
webmock_response.raise_error_if_any
- res = ::Patron::Response.new
- res.instance_variable_set(:@body, webmock_response.body)
- res.instance_variable_set(:@status, webmock_response.status[0])
- res.instance_variable_set(:@status_line, webmock_response.status[1])
- res.instance_variable_set(:@headers, webmock_response.headers)
- res
+
+ header_fields = (webmock_response.headers || []).map { |(k, vs)| Array(vs).map { |v| "#{k}: #{v}" } }.flatten
+ status_line = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}"
+ header_data = ([status_line] + header_fields).join("\r\n")
+
+ ::Patron::Response.new(
+ "",
+ webmock_response.status[0],
+ 0,
+ header_data,
+ webmock_response.body,
+ default_response_charset
+ )
end
def self.build_webmock_response(patron_response)
View
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'webmock_shared'
@@ -84,6 +86,42 @@
stub_http_request(:copy, "www.example.com/abc").with(:headers => {'Destination' => "/def"})
@sess.copy("/abc", "/def")
end
+
+ if /^1\.9/ === RUBY_VERSION
+ describe "handling encoding same way as patron" do
+ around(:each) do |example|
+ @encoding = Encoding.default_internal
+ Encoding.default_internal = "UTF-8"
+ example.run
+ Encoding.default_internal = @encoding
+ end
+
+ it "should encode body based on charset in headers" do
+ stub_request(:get, "www.example.com").
+ to_return(:headers => {'Content-Type' => 'text/html; charset=iso-8859-1'},
+ :body => "Øl".encode("iso-8859-1"))
+
+ @sess.get("").body.encoding.should == Encoding.default_internal
+ end
+
+ it "should encode body based on encoding-attribute in body" do
+ stub_request(:get, "www.example.com").
+ to_return(:body => "<?xml encoding=\"iso-8859-1\">Øl</xml>".encode("iso-8859-1"))
+
+
+ @sess.get("").body.encoding.should == Encoding.default_internal
+ end
+
+ it "should encode body based on Session#default_response_charset" do
+ stub_request(:get, "www.example.com").
+ to_return(:body => "Øl".encode("iso-8859-1"))
+
+ @sess.default_response_charset = "iso-8859-1"
+
+ @sess.get("").body.encoding.should == Encoding.default_internal
+ end
+ end
+ end
end
end
end
@@ -23,11 +23,15 @@ def http_request(method, uri, options = {}, &block)
headers[k] = v
end
end
+
+ status_line_pattern = %r(\AHTTP/(\d+\.\d+)\s+(\d\d\d)\s*([^\r\n]+)?)
+ message = response.status_line.match(status_line_pattern)[3] || ""
+
OpenStruct.new({
:body => response.body,
:headers => WebMock::Util::Headers.normalize_headers(headers),
:status => response.status.to_s,
- :message => response.status_line
+ :message => message
})
end

0 comments on commit 2ec3f5c

Please sign in to comment.