Skip to content
Browse files

Ensure character encoding of gzip content-encoded responses is binary…

…. Issue #168
  • Loading branch information...
1 parent da79456 commit 9eee397268a4e982c552885e8c077d3949742b17 @drbrain drbrain committed Nov 22, 2011
View
1 lib/mechanize/http/agent.rb
@@ -695,6 +695,7 @@ def response_content_encoding response, body_io
begin
zio = Zlib::GzipReader.new body_io
out_io = Tempfile.new 'mechanize-decode'
+ out_io.binmode
until zio.eof? do
out_io.write zio.read 16384
View
4 lib/mechanize/test_case.rb
@@ -43,6 +43,10 @@ def fake_page agent = @mech
Mechanize::Page.new uri, response, html, 200, agent
end
+ def have_encoding?
+ Object.const_defined? :Encoding
+ end
+
def html_page body
uri = URI 'http://example/'
Mechanize::Page.new uri, { 'content-type' => 'text/html' }, body, 200, @mech
View
20 test/test_mechanize_http_agent.rb
@@ -601,6 +601,26 @@ def @res.content_length() nil end
assert_equal 'part', body.read
end
+ def test_response_content_encoding_gzip_encoding_bad
+ def @res.content_length() 24 end
+ @res.instance_variable_set(:@header,
+ 'content-encoding' => %w[gzip],
+ 'content-type' => 'text/html; charset=UTF-8')
+
+ # "test\xB2"
+ body_io = StringIO.new \
+ "\037\213\b\000*+\314N\000\003+I-.\331\004\000x\016\003\376\005\000\000\000"
+
+ body = @agent.response_content_encoding @res, body_io
+
+ expected = "test\xB2"
+ expected.force_encoding Encoding::BINARY if have_encoding?
+
+ content = body.read
+ assert_equal expected, content
+ assert_equal Encoding::BINARY, content.encoding if have_encoding?
+ end
+
def test_response_content_encoding_none
def @res.content_length() 4 end
@res.instance_variable_set :@header, 'content-encoding' => %w[none]
View
4 test/test_mechanize_page_encoding.rb
@@ -168,8 +168,8 @@ def test_parser_encoding_equals_overwrites_force_default_encoding
end
def test_parser_encoding_when_searching_elements
- skip "Encoding not implemented" unless Object.const_defined? :Encoding
-
+ skip "Encoding not implemented" unless have_encoding?
+
body = '<span id="latin1">hi</span>'
page = util_page body, 'content-type' => 'text/html,charset=ISO-8859-1'

0 comments on commit 9eee397

Please sign in to comment.
Something went wrong with that request. Please try again.