Skip to content

Commit

Permalink
meta content-type tag now overrides Page#content_type (GH #114)
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode committed Jun 15, 2011
1 parent 21d3f18 commit 042b07c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lib/mechanize/page.rb
Expand Up @@ -24,6 +24,7 @@ def initialize(uri=nil, response=nil, body=nil, code=nil, mech=nil)
raise Mechanize::ContentTypeError, response['content-type'] unless
response['content-type'] =~ /^(text\/html)|(application\/xhtml\+xml)/i

@meta_content_type = nil
@encoding = nil
@encodings = [nil]
raise 'no' if mech and not Mechanize === mech
Expand All @@ -49,8 +50,9 @@ def initialize(uri=nil, response=nil, body=nil, code=nil, mech=nil)
elsif meta =~ /http-equiv\s*=\s*(["'])?content-type\1/i
meta =~ /content=(["'])?(.*?)\1/i

encoding = charset $2
@meta_content_type = $2

encoding = charset $2
@encodings << encoding if encoding
end
end
Expand Down Expand Up @@ -151,7 +153,7 @@ def canonical_uri

# Get the content type
def content_type
response['content-type']
@meta_content_type || response['content-type']
end

# Search through the page like HPricot
Expand Down
33 changes: 30 additions & 3 deletions test/test_mechanize_page.rb
Expand Up @@ -52,15 +52,42 @@ def util_page body = @body, res = @res
Mechanize::Page.new @uri, res, body, 200, @agent
end

def test_initialize_content_type
assert Mechanize::Page.new nil, 'content-type' => 'application/xhtml+xml'
assert Mechanize::Page.new nil, 'content-type' => 'text/html'
def test_initialize_supported_content_type
page = Mechanize::Page.new nil, 'content-type' => 'application/xhtml+xml'
assert page
assert_equal 'application/xhtml+xml', page.content_type

page = Mechanize::Page.new nil, 'content-type' => 'text/html'
assert page
assert_equal 'text/html', page.content_type

page = Mechanize::Page.new nil, 'content-type' => 'application/xhtml+xml;charset=utf-8'
assert page
assert_equal 'application/xhtml+xml;charset=utf-8', page.content_type

page = Mechanize::Page.new nil, 'content-type' => 'text/html;charset=utf-8'
assert page
assert_equal 'text/html;charset=utf-8', page.content_type
end

def test_initialize_unsupported_content_type
e = assert_raises Mechanize::ContentTypeError do
Mechanize::Page.new nil, 'content-type' => 'text/plain'
end

assert_equal 'text/plain', e.content_type

e = assert_raises Mechanize::ContentTypeError do
Mechanize::Page.new nil, 'content-type' => 'text/plain;charset=utf-8'
end

assert_equal 'text/plain;charset=utf-8', e.content_type
end

def test_override_content_type
page = Mechanize::Page.new nil, {'content-type' => 'text/html'}, WINDOWS_1255
assert page
assert_equal 'text/html; charset=windows-1255', page.content_type
end

def test_canonical_uri
Expand Down

0 comments on commit 042b07c

Please sign in to comment.