aai10-mechanize 2.0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +6 -0
- data/.gitignore +9 -0
- data/CHANGELOG.rdoc +652 -0
- data/EXAMPLES.rdoc +187 -0
- data/FAQ.rdoc +11 -0
- data/GUIDE.rdoc +163 -0
- data/LICENSE.rdoc +20 -0
- data/Manifest.txt +172 -0
- data/README.rdoc +63 -0
- data/Rakefile +36 -0
- data/aai10-mechanize.gemspec +20 -0
- data/examples/flickr_upload.rb +22 -0
- data/examples/mech-dump.rb +5 -0
- data/examples/proxy_req.rb +7 -0
- data/examples/rubyforge.rb +20 -0
- data/examples/spider.rb +21 -0
- data/lib/mechanize.rb +664 -0
- data/lib/mechanize/content_type_error.rb +14 -0
- data/lib/mechanize/cookie.rb +116 -0
- data/lib/mechanize/cookie_jar.rb +202 -0
- data/lib/mechanize/element_matcher.rb +35 -0
- data/lib/mechanize/file.rb +80 -0
- data/lib/mechanize/file_connection.rb +17 -0
- data/lib/mechanize/file_request.rb +26 -0
- data/lib/mechanize/file_response.rb +74 -0
- data/lib/mechanize/file_saver.rb +37 -0
- data/lib/mechanize/form.rb +478 -0
- data/lib/mechanize/form/button.rb +9 -0
- data/lib/mechanize/form/check_box.rb +11 -0
- data/lib/mechanize/form/field.rb +44 -0
- data/lib/mechanize/form/file_upload.rb +23 -0
- data/lib/mechanize/form/image_button.rb +20 -0
- data/lib/mechanize/form/multi_select_list.rb +83 -0
- data/lib/mechanize/form/option.rb +49 -0
- data/lib/mechanize/form/radio_button.rb +48 -0
- data/lib/mechanize/form/select_list.rb +40 -0
- data/lib/mechanize/headers.rb +25 -0
- data/lib/mechanize/history.rb +83 -0
- data/lib/mechanize/http.rb +3 -0
- data/lib/mechanize/http/agent.rb +738 -0
- data/lib/mechanize/inspect.rb +88 -0
- data/lib/mechanize/monkey_patch.rb +37 -0
- data/lib/mechanize/page.rb +408 -0
- data/lib/mechanize/page/base.rb +8 -0
- data/lib/mechanize/page/frame.rb +27 -0
- data/lib/mechanize/page/image.rb +30 -0
- data/lib/mechanize/page/label.rb +20 -0
- data/lib/mechanize/page/link.rb +82 -0
- data/lib/mechanize/page/meta_refresh.rb +56 -0
- data/lib/mechanize/pluggable_parsers.rb +101 -0
- data/lib/mechanize/redirect_limit_reached_error.rb +16 -0
- data/lib/mechanize/redirect_not_get_or_head_error.rb +19 -0
- data/lib/mechanize/response_code_error.rb +22 -0
- data/lib/mechanize/response_read_error.rb +27 -0
- data/lib/mechanize/robots_disallowed_error.rb +29 -0
- data/lib/mechanize/unsupported_scheme_error.rb +8 -0
- data/lib/mechanize/util.rb +113 -0
- data/test/data/htpasswd +1 -0
- data/test/data/server.crt +16 -0
- data/test/data/server.csr +12 -0
- data/test/data/server.key +15 -0
- data/test/data/server.pem +15 -0
- data/test/helper.rb +175 -0
- data/test/htdocs/alt_text.html +10 -0
- data/test/htdocs/bad_form_test.html +9 -0
- data/test/htdocs/button.jpg +0 -0
- data/test/htdocs/canonical_uri.html +9 -0
- data/test/htdocs/dir with spaces/foo.html +1 -0
- data/test/htdocs/empty_form.html +6 -0
- data/test/htdocs/file_upload.html +26 -0
- data/test/htdocs/find_link.html +41 -0
- data/test/htdocs/form_multi_select.html +16 -0
- data/test/htdocs/form_multival.html +37 -0
- data/test/htdocs/form_no_action.html +18 -0
- data/test/htdocs/form_no_input_name.html +16 -0
- data/test/htdocs/form_select.html +16 -0
- data/test/htdocs/form_select_all.html +16 -0
- data/test/htdocs/form_select_none.html +17 -0
- data/test/htdocs/form_select_noopts.html +10 -0
- data/test/htdocs/form_set_fields.html +14 -0
- data/test/htdocs/form_test.html +188 -0
- data/test/htdocs/frame_referer_test.html +10 -0
- data/test/htdocs/frame_test.html +30 -0
- data/test/htdocs/google.html +13 -0
- data/test/htdocs/iframe_test.html +16 -0
- data/test/htdocs/index.html +6 -0
- data/test/htdocs/link with space.html +5 -0
- data/test/htdocs/meta_cookie.html +11 -0
- data/test/htdocs/no_title_test.html +6 -0
- data/test/htdocs/nofollow.html +9 -0
- data/test/htdocs/noindex.html +9 -0
- data/test/htdocs/norobots.html +8 -0
- data/test/htdocs/rails_3_encoding_hack_form_test.html +27 -0
- data/test/htdocs/rel_nofollow.html +8 -0
- data/test/htdocs/relative/tc_relative_links.html +21 -0
- data/test/htdocs/robots.html +8 -0
- data/test/htdocs/robots.txt +2 -0
- data/test/htdocs/tc_bad_charset.html +9 -0
- data/test/htdocs/tc_bad_links.html +5 -0
- data/test/htdocs/tc_base_images.html +10 -0
- data/test/htdocs/tc_base_link.html +8 -0
- data/test/htdocs/tc_blank_form.html +11 -0
- data/test/htdocs/tc_charset.html +6 -0
- data/test/htdocs/tc_checkboxes.html +19 -0
- data/test/htdocs/tc_encoded_links.html +5 -0
- data/test/htdocs/tc_field_precedence.html +11 -0
- data/test/htdocs/tc_follow_meta.html +8 -0
- data/test/htdocs/tc_form_action.html +48 -0
- data/test/htdocs/tc_images.html +8 -0
- data/test/htdocs/tc_links.html +18 -0
- data/test/htdocs/tc_meta_in_body.html +9 -0
- data/test/htdocs/tc_no_attributes.html +16 -0
- data/test/htdocs/tc_pretty_print.html +17 -0
- data/test/htdocs/tc_radiobuttons.html +17 -0
- data/test/htdocs/tc_referer.html +16 -0
- data/test/htdocs/tc_relative_links.html +19 -0
- data/test/htdocs/tc_textarea.html +23 -0
- data/test/htdocs/test_bad_encoding.html +52 -0
- data/test/htdocs/test_click.html +11 -0
- data/test/htdocs/unusual______.html +5 -0
- data/test/servlets.rb +402 -0
- data/test/ssl_server.rb +48 -0
- data/test/test_cookies.rb +129 -0
- data/test/test_form_action.rb +52 -0
- data/test/test_form_as_hash.rb +59 -0
- data/test/test_form_button.rb +46 -0
- data/test/test_frames.rb +34 -0
- data/test/test_headers.rb +33 -0
- data/test/test_history.rb +118 -0
- data/test/test_history_added.rb +16 -0
- data/test/test_html_unscape_forms.rb +46 -0
- data/test/test_if_modified_since.rb +20 -0
- data/test/test_images.rb +19 -0
- data/test/test_mechanize.rb +852 -0
- data/test/test_mechanize_cookie.rb +345 -0
- data/test/test_mechanize_cookie_jar.rb +433 -0
- data/test/test_mechanize_file.rb +53 -0
- data/test/test_mechanize_file_request.rb +19 -0
- data/test/test_mechanize_file_response.rb +21 -0
- data/test/test_mechanize_form.rb +576 -0
- data/test/test_mechanize_form_check_box.rb +37 -0
- data/test/test_mechanize_form_encoding.rb +120 -0
- data/test/test_mechanize_form_field.rb +21 -0
- data/test/test_mechanize_form_image_button.rb +12 -0
- data/test/test_mechanize_form_textarea.rb +51 -0
- data/test/test_mechanize_http_agent.rb +697 -0
- data/test/test_mechanize_link.rb +84 -0
- data/test/test_mechanize_page_encoding.rb +147 -0
- data/test/test_mechanize_page_link.rb +382 -0
- data/test/test_mechanize_page_meta_refresh.rb +115 -0
- data/test/test_mechanize_redirect_not_get_or_head_error.rb +18 -0
- data/test/test_mechanize_subclass.rb +22 -0
- data/test/test_mechanize_util.rb +92 -0
- data/test/test_multi_select.rb +118 -0
- data/test/test_no_attributes.rb +13 -0
- data/test/test_option.rb +18 -0
- data/test/test_pluggable_parser.rb +136 -0
- data/test/test_post_form.rb +37 -0
- data/test/test_pretty_print.rb +22 -0
- data/test/test_radiobutton.rb +75 -0
- data/test/test_redirect_limit_reached.rb +39 -0
- data/test/test_redirect_ok.rb +25 -0
- data/test/test_referer.rb +81 -0
- data/test/test_relative_links.rb +40 -0
- data/test/test_request.rb +13 -0
- data/test/test_response_code.rb +53 -0
- data/test/test_robots.rb +72 -0
- data/test/test_save_file.rb +48 -0
- data/test/test_scheme.rb +48 -0
- data/test/test_select.rb +119 -0
- data/test/test_select_all.rb +15 -0
- data/test/test_select_none.rb +15 -0
- data/test/test_select_noopts.rb +18 -0
- data/test/test_set_fields.rb +44 -0
- data/test/test_ssl_server.rb +20 -0
- metadata +360 -0
@@ -0,0 +1,84 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
class TestMechanizeLink < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@agent = Mechanize.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_click
|
10
|
+
page = @agent.get("http://localhost/frame_test.html")
|
11
|
+
link = page.link_with(:text => "Form Test")
|
12
|
+
|
13
|
+
assert_equal('Form Test', link.text)
|
14
|
+
page = link.click
|
15
|
+
assert_equal("http://localhost/form_test.html",
|
16
|
+
@agent.history.last.uri.to_s)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_click_base
|
20
|
+
page = @agent.get("http://google.com/tc_base_link.html")
|
21
|
+
page = page.links.first.click
|
22
|
+
assert @agent.visited?("http://localhost/index.html")
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_click_unsupported_scheme
|
26
|
+
page = @agent.get("http://google.com/tc_links.html")
|
27
|
+
link = page.link_with(:text => 'javascript link')
|
28
|
+
assert_raises Mechanize::UnsupportedSchemeError do
|
29
|
+
link.click
|
30
|
+
end
|
31
|
+
|
32
|
+
@agent.scheme_handlers['javascript'] = lambda { |my_link, my_page|
|
33
|
+
URI.parse('http://localhost/tc_links.html')
|
34
|
+
}
|
35
|
+
|
36
|
+
link.click
|
37
|
+
|
38
|
+
# HACK no assertion
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_text_alt_text
|
42
|
+
page = @agent.get("http://localhost/alt_text.html")
|
43
|
+
assert_equal(5, page.links.length)
|
44
|
+
assert_equal(1, page.meta_refresh.length)
|
45
|
+
|
46
|
+
assert_equal '', page.meta_refresh.first.text
|
47
|
+
assert_equal 'alt text', page.link_with(:href => 'alt_text.html').text
|
48
|
+
assert_equal '', page.link_with(:href => 'no_alt_text.html').text
|
49
|
+
assert_equal 'no image', page.link_with(:href => 'no_image.html').text
|
50
|
+
assert_equal '', page.link_with(:href => 'no_text.html').text
|
51
|
+
assert_equal '', page.link_with(:href => 'nil_alt_text.html').text
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_uri_escaped
|
55
|
+
doc = Nokogiri::HTML::Document.new
|
56
|
+
|
57
|
+
node = Nokogiri::XML::Node.new('foo', doc)
|
58
|
+
node['href'] = 'http://foo.bar/%20baz'
|
59
|
+
|
60
|
+
link = Mechanize::Page::Link.new(node, nil, nil)
|
61
|
+
|
62
|
+
assert_equal 'http://foo.bar/%20baz', link.uri.to_s
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_uri_no_path
|
66
|
+
page = @agent.get("http://localhost/relative/tc_relative_links.html")
|
67
|
+
page = page.link_with(:text => 'just the query string').click
|
68
|
+
assert_equal('http://localhost/relative/tc_relative_links.html?a=b',
|
69
|
+
page.uri.to_s)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_uri_weird
|
73
|
+
doc = Nokogiri::HTML::Document.new
|
74
|
+
|
75
|
+
node = Nokogiri::XML::Node.new('foo', doc)
|
76
|
+
node['href'] = 'http://foo.bar/ baz'
|
77
|
+
|
78
|
+
link = Mechanize::Page::Link.new(node, nil, nil)
|
79
|
+
|
80
|
+
assert_equal 'http://foo.bar/%20baz', link.uri.to_s
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
@@ -0,0 +1,147 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'helper'
|
3
|
+
require 'cgi'
|
4
|
+
|
5
|
+
# tests for Page encoding and charset and parsing
|
6
|
+
|
7
|
+
class TestMechanizePageEncoding < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
MECH_ASCII_ENCODING = Mechanize::Util::NEW_RUBY_ENCODING ? 'US-ASCII' : 'ISO-8859-1'
|
10
|
+
|
11
|
+
def setup
|
12
|
+
@agent = Mechanize.new
|
13
|
+
@uri = URI('http://localhost/')
|
14
|
+
@response_headers = { 'content-type' => 'text/html' }
|
15
|
+
@body = '<title>hi</title>'
|
16
|
+
end
|
17
|
+
|
18
|
+
def util_page body = @body, headers = @response_headers
|
19
|
+
body.force_encoding Encoding::BINARY if body.respond_to? :force_encoding
|
20
|
+
Mechanize::Page.new @uri, headers, body, 200, @agent
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_page_charset
|
24
|
+
charset = Mechanize::Page.charset 'text/html;charset=vAlue'
|
25
|
+
assert_equal 'vAlue', charset
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_page_charset_upcase
|
29
|
+
charset = Mechanize::Page.charset 'TEXT/HTML;CHARSET=UTF-8'
|
30
|
+
assert_equal 'UTF-8', charset
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_page_semicolon
|
34
|
+
charset = Mechanize::Page.charset 'text/html;charset=UTF-8;'
|
35
|
+
assert_equal 'UTF-8', charset
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_page_charset_no_chaset_token
|
39
|
+
charset = Mechanize::Page.charset 'text/html'
|
40
|
+
assert_nil charset
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_page_charset_returns_nil_when_charset_says_none
|
44
|
+
charset = Mechanize::Page.charset 'text/html;charset=none'
|
45
|
+
|
46
|
+
assert_nil charset
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_page_charset_multiple
|
50
|
+
charset = Mechanize::Page.charset 'text/html;charset=111;charset=222'
|
51
|
+
|
52
|
+
assert_equal '111', charset
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_page_response_header_charset
|
56
|
+
headers = {'content-type' => 'text/html;charset=HEADER'}
|
57
|
+
charsets = Mechanize::Page.response_header_charset(headers)
|
58
|
+
|
59
|
+
assert_equal ['HEADER'], charsets
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_page_response_header_charset_no_token
|
63
|
+
headers = {'content-type' => 'text/html'}
|
64
|
+
charsets = Mechanize::Page.response_header_charset(headers)
|
65
|
+
|
66
|
+
assert_equal [], charsets
|
67
|
+
|
68
|
+
headers = {'X-My-Header' => 'hello'}
|
69
|
+
charsets = Mechanize::Page.response_header_charset(headers)
|
70
|
+
|
71
|
+
assert_equal [], charsets
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_response_header_charset
|
75
|
+
page = util_page nil, {'content-type' => 'text/html;charset=HEADER'}
|
76
|
+
|
77
|
+
assert_equal ['HEADER'], page.response_header_charset
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_page_meta_charset
|
81
|
+
body = '<meta http-equiv="content-type" content="text/html;charset=META">'
|
82
|
+
charsets = Mechanize::Page.meta_charset(body)
|
83
|
+
|
84
|
+
assert_equal ['META'], charsets
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_page_meta_charset_is_empty_when_no_charset_meta
|
88
|
+
body = '<meta http-equiv="refresh" content="5; url=index.html">'
|
89
|
+
charsets = Mechanize::Page.meta_charset(body)
|
90
|
+
assert_equal [], charsets
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_meta_charset
|
94
|
+
body = '<meta http-equiv="content-type" content="text/html;charset=META">'
|
95
|
+
page = util_page body
|
96
|
+
|
97
|
+
assert_equal ['META'], page.meta_charset
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_detected_encoding
|
101
|
+
page = util_page
|
102
|
+
|
103
|
+
assert_equal MECH_ASCII_ENCODING, page.detected_encoding
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_encodings
|
107
|
+
response = {'content-type' => 'text/html;charset=HEADER'}
|
108
|
+
body = '<meta http-equiv="content-type" content="text/html;charset=META">'
|
109
|
+
@agent.default_encoding = 'DEFAULT'
|
110
|
+
page = util_page body, response
|
111
|
+
|
112
|
+
assert_equal true, page.encodings.include?('HEADER')
|
113
|
+
assert_equal true, page.encodings.include?('META')
|
114
|
+
assert_equal true, page.encodings.include?(MECH_ASCII_ENCODING)
|
115
|
+
assert_equal true, page.encodings.include?('DEFAULT')
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_parser_with_default_encoding
|
119
|
+
# pre test
|
120
|
+
assert_equal false, util_page.encodings.include?('Windows-1252')
|
121
|
+
|
122
|
+
@agent.default_encoding = 'Windows-1252'
|
123
|
+
page = util_page
|
124
|
+
|
125
|
+
assert_equal true, page.encodings.include?('Windows-1252')
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_parser_force_default_encoding
|
129
|
+
@agent.default_encoding = 'Windows-1252'
|
130
|
+
@agent.force_default_encoding = true
|
131
|
+
page = util_page
|
132
|
+
|
133
|
+
assert page.encodings.include? 'Windows-1252'
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_parser_encoding_equals_overwrites_force_default_encoding
|
137
|
+
@agent.default_encoding = 'Windows-1252'
|
138
|
+
@agent.force_default_encoding = true
|
139
|
+
page = util_page
|
140
|
+
|
141
|
+
assert_equal 'Windows-1252', page.encoding
|
142
|
+
|
143
|
+
page.encoding = 'ISO-8859-2'
|
144
|
+
|
145
|
+
assert_equal 'ISO-8859-2', page.encoding
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,382 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'helper'
|
4
|
+
require 'cgi'
|
5
|
+
|
6
|
+
class TestMechanizePage < MiniTest::Unit::TestCase
|
7
|
+
|
8
|
+
WINDOWS_1255 = <<-HTML
|
9
|
+
<meta http-equiv="content-type" content="text/html; charset=windows-1255">
|
10
|
+
<title>hi</title>
|
11
|
+
HTML
|
12
|
+
|
13
|
+
BAD = <<-HTML
|
14
|
+
<meta http-equiv="content-type" content="text/html; charset=windows-1255">
|
15
|
+
<title>Bia\xB3ystok</title>
|
16
|
+
HTML
|
17
|
+
BAD.force_encoding Encoding::BINARY if defined? Encoding
|
18
|
+
|
19
|
+
SJIS_TITLE = "\x83\x65\x83\x58\x83\x67"
|
20
|
+
|
21
|
+
SJIS_AFTER_TITLE = <<-HTML
|
22
|
+
<title>#{SJIS_TITLE}</title>
|
23
|
+
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
|
24
|
+
HTML
|
25
|
+
|
26
|
+
SJIS_AFTER_TITLE.force_encoding Encoding::BINARY if defined? Encoding
|
27
|
+
|
28
|
+
SJIS_BAD_AFTER_TITLE = <<-HTML
|
29
|
+
<title>#{SJIS_TITLE}</title>
|
30
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
31
|
+
HTML
|
32
|
+
|
33
|
+
SJIS_BAD_AFTER_TITLE.force_encoding Encoding::BINARY if defined? Encoding
|
34
|
+
|
35
|
+
UTF8_TITLE = 'テスト'
|
36
|
+
UTF8 = <<-HTML
|
37
|
+
<title>#{UTF8_TITLE}</title>
|
38
|
+
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
|
39
|
+
HTML
|
40
|
+
|
41
|
+
ENCODING_ERROR_CLASS = Nokogiri::XML::SyntaxError
|
42
|
+
|
43
|
+
def setup
|
44
|
+
@agent = Mechanize.new
|
45
|
+
@uri = URI('http://example')
|
46
|
+
@res = { 'content-type' => 'text/html' }
|
47
|
+
@body = '<title>hi</title>'
|
48
|
+
end
|
49
|
+
|
50
|
+
def util_page body = @body, res = @res
|
51
|
+
body.force_encoding Encoding::BINARY if body.respond_to? :force_encoding
|
52
|
+
Mechanize::Page.new @uri, res, body, 200, @agent
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_initialize_supported_content_type
|
56
|
+
page = Mechanize::Page.new nil, 'content-type' => 'application/xhtml+xml'
|
57
|
+
assert page
|
58
|
+
assert_equal 'application/xhtml+xml', page.content_type
|
59
|
+
|
60
|
+
page = Mechanize::Page.new nil, 'content-type' => 'text/html'
|
61
|
+
assert page
|
62
|
+
assert_equal 'text/html', page.content_type
|
63
|
+
|
64
|
+
page = Mechanize::Page.new nil, 'content-type' => 'application/xhtml+xml;charset=utf-8'
|
65
|
+
assert page
|
66
|
+
assert_equal 'application/xhtml+xml;charset=utf-8', page.content_type
|
67
|
+
|
68
|
+
page = Mechanize::Page.new nil, 'content-type' => 'text/html;charset=utf-8'
|
69
|
+
assert page
|
70
|
+
assert_equal 'text/html;charset=utf-8', page.content_type
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_initialize_unsupported_content_type
|
74
|
+
e = assert_raises Mechanize::ContentTypeError do
|
75
|
+
Mechanize::Page.new nil, 'content-type' => 'text/plain'
|
76
|
+
end
|
77
|
+
|
78
|
+
assert_equal 'text/plain', e.content_type
|
79
|
+
|
80
|
+
e = assert_raises Mechanize::ContentTypeError do
|
81
|
+
Mechanize::Page.new nil, 'content-type' => 'text/plain;charset=utf-8'
|
82
|
+
end
|
83
|
+
|
84
|
+
assert_equal 'text/plain;charset=utf-8', e.content_type
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_override_content_type
|
88
|
+
page = Mechanize::Page.new nil, {'content-type' => 'text/html'}, WINDOWS_1255
|
89
|
+
assert page
|
90
|
+
assert_equal 'text/html; charset=windows-1255', page.content_type
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_canonical_uri
|
94
|
+
page = @agent.get("http://localhost/canonical_uri.html")
|
95
|
+
assert_equal(URI("http://localhost/canonical_uri"), page.canonical_uri)
|
96
|
+
|
97
|
+
page = @agent.get("http://localhost/file_upload.html")
|
98
|
+
assert_equal(nil, page.canonical_uri)
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_canonical_uri_unescaped
|
102
|
+
page = util_page <<-BODY
|
103
|
+
<head>
|
104
|
+
<link rel="canonical" href="http://example/white space"/>
|
105
|
+
</head>
|
106
|
+
BODY
|
107
|
+
|
108
|
+
assert_equal @uri + '/white%20space', page.canonical_uri
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_charset_from_content_type
|
112
|
+
charset = Mechanize::Page.__send__ :charset_from_content_type, 'text/html;charset=UTF-8'
|
113
|
+
|
114
|
+
assert_equal 'UTF-8', charset
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_charset_from_bad_content_type
|
118
|
+
charset = Mechanize::Page.__send__ :charset_from_content_type, 'text/html'
|
119
|
+
|
120
|
+
assert_nil charset
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_encoding
|
124
|
+
page = util_page WINDOWS_1255
|
125
|
+
|
126
|
+
assert_equal 'windows-1255', page.encoding
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_encoding_charset_after_title
|
130
|
+
page = util_page SJIS_AFTER_TITLE
|
131
|
+
|
132
|
+
assert_equal false, page.encoding_error?
|
133
|
+
|
134
|
+
assert_equal 'Shift_JIS', page.encoding
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_encoding_charset_after_title_bad
|
138
|
+
page = util_page UTF8
|
139
|
+
|
140
|
+
assert_equal false, page.encoding_error?
|
141
|
+
|
142
|
+
assert_equal 'UTF-8', page.encoding
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_encoding_charset_after_title_double_bad
|
146
|
+
page = util_page SJIS_BAD_AFTER_TITLE
|
147
|
+
|
148
|
+
assert_equal false, page.encoding_error?
|
149
|
+
|
150
|
+
assert_equal 'SHIFT_JIS', page.encoding
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_encoding_charset_bad
|
154
|
+
page = util_page "<title>#{UTF8_TITLE}</title>"
|
155
|
+
page.encodings.replace %w[
|
156
|
+
UTF-8
|
157
|
+
Shift_JIS
|
158
|
+
]
|
159
|
+
|
160
|
+
assert_equal false, page.encoding_error?
|
161
|
+
|
162
|
+
assert_equal 'UTF-8', page.encoding
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_encoding_meta_charset
|
166
|
+
page = util_page "<meta charset='UTF-8'>"
|
167
|
+
|
168
|
+
assert_equal 'UTF-8', page.encoding
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_encoding_equals
|
172
|
+
page = util_page
|
173
|
+
page.meta_refresh
|
174
|
+
assert page.instance_variable_get(:@meta_refresh)
|
175
|
+
|
176
|
+
page.encoding = 'UTF-8'
|
177
|
+
|
178
|
+
assert_nil page.instance_variable_get(:@meta_refresh)
|
179
|
+
|
180
|
+
assert_equal 'UTF-8', page.encoding
|
181
|
+
assert_equal 'UTF-8', page.parser.encoding
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_page_encoding_error?
|
185
|
+
page = util_page
|
186
|
+
page.parser.errors.clear
|
187
|
+
assert_equal false, page.encoding_error?
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_detect_libxml2error_indicate_encoding
|
191
|
+
page = util_page
|
192
|
+
page.parser.errors.clear
|
193
|
+
|
194
|
+
# error in libxml2-2.7.8/parser.c, HTMLparser.c or parserInternals.c
|
195
|
+
page.parser.errors = [ENCODING_ERROR_CLASS.new("Input is not proper UTF-8, indicate encoding !\n")]
|
196
|
+
assert_equal true, page.encoding_error?
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_detect_libxml2error_invalid_char
|
200
|
+
page = util_page
|
201
|
+
page.parser.errors.clear
|
202
|
+
|
203
|
+
# error in libxml2-2.7.8/HTMLparser.c
|
204
|
+
page.parser.errors = [ENCODING_ERROR_CLASS.new("Invalid char in CDATA 0x%X\n")]
|
205
|
+
assert_equal true, page.encoding_error?
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_detect_libxml2error_input_conversion_failed
|
209
|
+
page = util_page
|
210
|
+
page.parser.errors.clear
|
211
|
+
|
212
|
+
# error in libxml2-2.7.8/encoding.c
|
213
|
+
page.parser.errors = [ENCODING_ERROR_CLASS.new("input conversion failed due to input error\n")]
|
214
|
+
assert_equal true, page.encoding_error?
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_detect_libxml2error_which_unsupported_by_mechanize
|
218
|
+
page = util_page
|
219
|
+
page.parser.errors.clear
|
220
|
+
|
221
|
+
# error in libxml2-2.7.8/HTMLparser.c
|
222
|
+
page.parser.errors = [ENCODING_ERROR_CLASS.new("encoder error\n")]
|
223
|
+
assert_equal false, page.encoding_error?
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_encoding_equals_before_parser
|
227
|
+
# document has a bad encoding information - windows-1255
|
228
|
+
page = util_page BAD
|
229
|
+
|
230
|
+
# encoding is wrong, so user wants to force ISO-8859-2
|
231
|
+
page.encoding = 'ISO-8859-2'
|
232
|
+
|
233
|
+
assert_equal false, page.encoding_error?
|
234
|
+
assert_equal 'ISO-8859-2', page.encoding
|
235
|
+
assert_equal 'ISO-8859-2', page.parser.encoding
|
236
|
+
end
|
237
|
+
|
238
|
+
def test_encoding_equals_after_parser
|
239
|
+
# document has a bad encoding information - windows-1255
|
240
|
+
page = util_page BAD
|
241
|
+
page.parser
|
242
|
+
|
243
|
+
# autodetection sets encoding to windows-1255
|
244
|
+
assert_equal 'windows-1255', page.encoding
|
245
|
+
# believe in yourself, not machine
|
246
|
+
assert_equal false, page.encoding_error?
|
247
|
+
|
248
|
+
# encoding is wrong, so user wants to force ISO-8859-2
|
249
|
+
page.encoding = 'ISO-8859-2'
|
250
|
+
|
251
|
+
assert_equal false, page.encoding_error?
|
252
|
+
assert_equal 'ISO-8859-2', page.encoding
|
253
|
+
assert_equal 'ISO-8859-2', page.parser.encoding
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_frames_with
|
257
|
+
page = @agent.get("http://localhost/frame_test.html")
|
258
|
+
assert_equal(3, page.frames.size)
|
259
|
+
|
260
|
+
find_orig = page.frames.find_all { |f| f.name == 'frame1' }
|
261
|
+
|
262
|
+
find1 = page.frames_with(:name => 'frame1')
|
263
|
+
|
264
|
+
find_orig.zip(find1).each { |a,b|
|
265
|
+
assert_equal(a, b)
|
266
|
+
}
|
267
|
+
end
|
268
|
+
|
269
|
+
def test_links_with_dom_id
|
270
|
+
page = @agent.get("http://localhost/tc_links.html")
|
271
|
+
link = page.links_with(:dom_id => 'bold_aaron_link')
|
272
|
+
link_by_id = page.links_with(:id => 'bold_aaron_link')
|
273
|
+
assert_equal(1, link.length)
|
274
|
+
assert_equal('Aaron Patterson', link.first.text)
|
275
|
+
assert_equal(link, link_by_id)
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_link_with_encoded_space
|
279
|
+
page = @agent.get("http://localhost/tc_links.html")
|
280
|
+
link = page.link_with(:text => 'encoded space')
|
281
|
+
page = @agent.click link
|
282
|
+
end
|
283
|
+
|
284
|
+
def test_link_with_space
|
285
|
+
page = @agent.get("http://localhost/tc_links.html")
|
286
|
+
link = page.link_with(:text => 'not encoded space')
|
287
|
+
page = @agent.click link
|
288
|
+
end
|
289
|
+
|
290
|
+
def test_link_with_unusual_characters
|
291
|
+
page = @agent.get("http://localhost/tc_links.html")
|
292
|
+
link = page.link_with(:text => 'unusual characters')
|
293
|
+
|
294
|
+
@agent.click link
|
295
|
+
|
296
|
+
# HACK no assertion
|
297
|
+
end
|
298
|
+
|
299
|
+
def test_links
|
300
|
+
page = @agent.get("http://localhost/find_link.html")
|
301
|
+
assert_equal(18, page.links.length)
|
302
|
+
end
|
303
|
+
|
304
|
+
def test_links_with_bold
|
305
|
+
page = @agent.get("http://localhost/tc_links.html")
|
306
|
+
link = page.links_with(:text => /Bold Dude/)
|
307
|
+
assert_equal(1, link.length)
|
308
|
+
assert_equal('Bold Dude', link.first.text)
|
309
|
+
assert_equal [], link.first.rel
|
310
|
+
assert !link.first.rel?('me')
|
311
|
+
assert !link.first.rel?('nofollow')
|
312
|
+
|
313
|
+
link = page.links_with(:text => 'Aaron James Patterson')
|
314
|
+
assert_equal(1, link.length)
|
315
|
+
assert_equal('Aaron James Patterson', link.first.text)
|
316
|
+
assert_equal ['me'], link.first.rel
|
317
|
+
assert link.first.rel?('me')
|
318
|
+
assert !link.first.rel?('nofollow')
|
319
|
+
|
320
|
+
link = page.links_with(:text => 'Aaron Patterson')
|
321
|
+
assert_equal(1, link.length)
|
322
|
+
assert_equal('Aaron Patterson', link.first.text)
|
323
|
+
assert_equal ['me', 'nofollow'], link.first.rel
|
324
|
+
assert link.first.rel?('me')
|
325
|
+
assert link.first.rel?('nofollow')
|
326
|
+
|
327
|
+
link = page.links_with(:text => 'Ruby Rocks!')
|
328
|
+
assert_equal(1, link.length)
|
329
|
+
assert_equal('Ruby Rocks!', link.first.text)
|
330
|
+
end
|
331
|
+
|
332
|
+
def test_meta_refresh
|
333
|
+
page = @agent.get("http://localhost/find_link.html")
|
334
|
+
assert_equal(3, page.meta_refresh.length)
|
335
|
+
assert_equal(%w{
|
336
|
+
http://www.drphil.com/
|
337
|
+
http://www.upcase.com/
|
338
|
+
http://tenderlovemaking.com/ }.sort,
|
339
|
+
page.meta_refresh.map { |x| x.href.downcase }.sort)
|
340
|
+
end
|
341
|
+
|
342
|
+
def test_title
|
343
|
+
page = util_page
|
344
|
+
|
345
|
+
assert_equal('hi', page.title)
|
346
|
+
end
|
347
|
+
|
348
|
+
def test_title_none
|
349
|
+
page = util_page '' # invalid HTML
|
350
|
+
|
351
|
+
assert_equal(nil, page.title)
|
352
|
+
end
|
353
|
+
|
354
|
+
def test_page_decoded_with_charset
|
355
|
+
page = util_page @body, 'content-type' => 'text/html; charset=EUC-JP'
|
356
|
+
|
357
|
+
assert_equal 'EUC-JP', page.encoding
|
358
|
+
assert_equal 'EUC-JP', page.parser.encoding
|
359
|
+
end
|
360
|
+
|
361
|
+
def test_form
|
362
|
+
page = @agent.get("http://localhost/tc_form_action.html")
|
363
|
+
|
364
|
+
form = page.form(:name => 'post_form1')
|
365
|
+
assert form
|
366
|
+
yielded = false
|
367
|
+
|
368
|
+
form = page.form(:name => 'post_form1') { |f|
|
369
|
+
yielded = true
|
370
|
+
assert f
|
371
|
+
assert_equal(form, f)
|
372
|
+
}
|
373
|
+
|
374
|
+
assert yielded
|
375
|
+
|
376
|
+
form_by_action = page.form(:action => '/form_post?a=b&b=c')
|
377
|
+
assert form_by_action
|
378
|
+
assert_equal(form, form_by_action)
|
379
|
+
end
|
380
|
+
|
381
|
+
end
|
382
|
+
|