knu-mechanize 0.9.3.20090623142847
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +504 -0
- data/EXAMPLES.rdoc +171 -0
- data/FAQ.rdoc +11 -0
- data/GUIDE.rdoc +122 -0
- data/LICENSE.rdoc +340 -0
- data/Manifest.txt +169 -0
- data/README.rdoc +60 -0
- data/Rakefile +43 -0
- data/examples/flickr_upload.rb +23 -0
- data/examples/mech-dump.rb +7 -0
- data/examples/proxy_req.rb +9 -0
- data/examples/rubyforge.rb +21 -0
- data/examples/spider.rb +11 -0
- data/lib/mechanize.rb +7 -0
- data/lib/www/mechanize.rb +619 -0
- data/lib/www/mechanize/chain.rb +34 -0
- data/lib/www/mechanize/chain/auth_headers.rb +80 -0
- data/lib/www/mechanize/chain/body_decoding_handler.rb +48 -0
- data/lib/www/mechanize/chain/connection_resolver.rb +78 -0
- data/lib/www/mechanize/chain/custom_headers.rb +23 -0
- data/lib/www/mechanize/chain/handler.rb +9 -0
- data/lib/www/mechanize/chain/header_resolver.rb +53 -0
- data/lib/www/mechanize/chain/parameter_resolver.rb +24 -0
- data/lib/www/mechanize/chain/post_connect_hook.rb +0 -0
- data/lib/www/mechanize/chain/pre_connect_hook.rb +22 -0
- data/lib/www/mechanize/chain/request_resolver.rb +32 -0
- data/lib/www/mechanize/chain/response_body_parser.rb +40 -0
- data/lib/www/mechanize/chain/response_header_handler.rb +50 -0
- data/lib/www/mechanize/chain/response_reader.rb +41 -0
- data/lib/www/mechanize/chain/ssl_resolver.rb +42 -0
- data/lib/www/mechanize/chain/uri_resolver.rb +77 -0
- data/lib/www/mechanize/content_type_error.rb +16 -0
- data/lib/www/mechanize/cookie.rb +72 -0
- data/lib/www/mechanize/cookie_jar.rb +191 -0
- data/lib/www/mechanize/file.rb +73 -0
- data/lib/www/mechanize/file_response.rb +62 -0
- data/lib/www/mechanize/file_saver.rb +39 -0
- data/lib/www/mechanize/form.rb +360 -0
- data/lib/www/mechanize/form/button.rb +8 -0
- data/lib/www/mechanize/form/check_box.rb +13 -0
- data/lib/www/mechanize/form/field.rb +28 -0
- data/lib/www/mechanize/form/file_upload.rb +24 -0
- data/lib/www/mechanize/form/image_button.rb +23 -0
- data/lib/www/mechanize/form/multi_select_list.rb +69 -0
- data/lib/www/mechanize/form/option.rb +51 -0
- data/lib/www/mechanize/form/radio_button.rb +38 -0
- data/lib/www/mechanize/form/select_list.rb +45 -0
- data/lib/www/mechanize/headers.rb +12 -0
- data/lib/www/mechanize/history.rb +67 -0
- data/lib/www/mechanize/inspect.rb +90 -0
- data/lib/www/mechanize/monkey_patch.rb +37 -0
- data/lib/www/mechanize/page.rb +181 -0
- data/lib/www/mechanize/page/base.rb +10 -0
- data/lib/www/mechanize/page/frame.rb +22 -0
- data/lib/www/mechanize/page/link.rb +50 -0
- data/lib/www/mechanize/page/meta.rb +51 -0
- data/lib/www/mechanize/pluggable_parsers.rb +103 -0
- data/lib/www/mechanize/redirect_limit_reached_error.rb +18 -0
- data/lib/www/mechanize/redirect_not_get_or_head_error.rb +20 -0
- data/lib/www/mechanize/response_code_error.rb +25 -0
- data/lib/www/mechanize/unsupported_scheme_error.rb +10 -0
- data/lib/www/mechanize/util.rb +76 -0
- data/mechanize.gemspec +41 -0
- data/test/chain/test_argument_validator.rb +14 -0
- data/test/chain/test_auth_headers.rb +25 -0
- data/test/chain/test_custom_headers.rb +18 -0
- data/test/chain/test_header_resolver.rb +28 -0
- data/test/chain/test_parameter_resolver.rb +35 -0
- data/test/chain/test_request_resolver.rb +29 -0
- data/test/chain/test_response_reader.rb +24 -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 +129 -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/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_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/relative/tc_relative_links.html +21 -0
- data/test/htdocs/tc_bad_links.html +5 -0
- data/test/htdocs/tc_base_link.html +8 -0
- data/test/htdocs/tc_blank_form.html +11 -0
- data/test/htdocs/tc_checkboxes.html +19 -0
- data/test/htdocs/tc_encoded_links.html +5 -0
- data/test/htdocs/tc_follow_meta.html +8 -0
- data/test/htdocs/tc_form_action.html +48 -0
- data/test/htdocs/tc_links.html +18 -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 +10 -0
- data/test/htdocs/tc_relative_links.html +19 -0
- data/test/htdocs/tc_textarea.html +23 -0
- data/test/htdocs/unusual______.html +5 -0
- data/test/servlets.rb +365 -0
- data/test/ssl_server.rb +48 -0
- data/test/test_authenticate.rb +71 -0
- data/test/test_bad_links.rb +25 -0
- data/test/test_blank_form.rb +16 -0
- data/test/test_checkboxes.rb +61 -0
- data/test/test_content_type.rb +13 -0
- data/test/test_cookie_class.rb +338 -0
- data/test/test_cookie_jar.rb +362 -0
- data/test/test_cookies.rb +123 -0
- data/test/test_encoded_links.rb +20 -0
- data/test/test_errors.rb +49 -0
- data/test/test_follow_meta.rb +108 -0
- data/test/test_form_action.rb +52 -0
- data/test/test_form_as_hash.rb +61 -0
- data/test/test_form_button.rb +38 -0
- data/test/test_form_no_inputname.rb +15 -0
- data/test/test_forms.rb +564 -0
- data/test/test_frames.rb +25 -0
- data/test/test_get_headers.rb +52 -0
- data/test/test_gzipping.rb +22 -0
- data/test/test_hash_api.rb +45 -0
- data/test/test_history.rb +142 -0
- data/test/test_history_added.rb +16 -0
- data/test/test_html_unscape_forms.rb +39 -0
- data/test/test_if_modified_since.rb +20 -0
- data/test/test_keep_alive.rb +31 -0
- data/test/test_links.rb +120 -0
- data/test/test_mech.rb +268 -0
- data/test/test_mechanize_file.rb +47 -0
- data/test/test_meta.rb +65 -0
- data/test/test_multi_select.rb +106 -0
- data/test/test_no_attributes.rb +13 -0
- data/test/test_option.rb +18 -0
- data/test/test_page.rb +119 -0
- data/test/test_pluggable_parser.rb +145 -0
- data/test/test_post_form.rb +34 -0
- data/test/test_pretty_print.rb +22 -0
- data/test/test_radiobutton.rb +75 -0
- data/test/test_redirect_limit_reached.rb +41 -0
- data/test/test_redirect_verb_handling.rb +45 -0
- data/test/test_referer.rb +39 -0
- data/test/test_relative_links.rb +40 -0
- data/test/test_request.rb +13 -0
- data/test/test_response_code.rb +52 -0
- data/test/test_save_file.rb +48 -0
- data/test/test_scheme.rb +48 -0
- data/test/test_select.rb +106 -0
- data/test/test_select_all.rb +15 -0
- data/test/test_select_none.rb +15 -0
- data/test/test_select_noopts.rb +16 -0
- data/test/test_set_fields.rb +44 -0
- data/test/test_ssl_server.rb +20 -0
- data/test/test_subclass.rb +14 -0
- data/test/test_textarea.rb +45 -0
- data/test/test_upload.rb +109 -0
- data/test/test_verbs.rb +25 -0
- metadata +314 -0
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestNoAttributes < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_parse_no_attributes
|
9
|
+
assert_nothing_raised do
|
10
|
+
page = @agent.get('http://localhost/tc_no_attributes.html')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/test/test_option.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class OptionTest < Test::Unit::TestCase
|
4
|
+
class FakeAttribute < Hash
|
5
|
+
attr_reader :inner_text
|
6
|
+
def initialize(inner_text)
|
7
|
+
@inner_text = inner_text
|
8
|
+
end
|
9
|
+
alias :has_attribute? :has_key?
|
10
|
+
alias :attributes :keys
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_option_missing_value
|
14
|
+
attribute = FakeAttribute.new('blah')
|
15
|
+
option = WWW::Mechanize::Form::Option.new(attribute, nil)
|
16
|
+
assert_equal('blah', option.value)
|
17
|
+
end
|
18
|
+
end
|
data/test/test_page.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
require 'cgi'
|
4
|
+
|
5
|
+
class TestPage < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@agent = WWW::Mechanize.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_page_gets_charset_from_page
|
11
|
+
page = @agent.get("http://localhost/tc_charset.html")
|
12
|
+
assert_equal 'windows-1255', page.encoding
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_double_semicolon
|
16
|
+
page = @agent.get("http://localhost/http_headers?content-disposition=#{CGI.escape('attachment;; filename=fooooo')}")
|
17
|
+
assert page.parser
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_broken_charset
|
21
|
+
page = @agent.get("http://localhost/http_headers?content-type=#{CGI.escape('text/html; charset=akldsjfhaldjfksh')}")
|
22
|
+
assert page.parser
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_mostly_broken_charset
|
26
|
+
page = @agent.get("http://localhost/http_headers?content-type=#{CGI.escape('text/html; charset=ISO_8859-1')}")
|
27
|
+
assert_equal 'ISO_8859-1', page.encoding
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_another_mostly_broken_charset
|
31
|
+
page = @agent.get("http://localhost/http_headers?content-type=#{CGI.escape('text/html; charset=UTF8')}")
|
32
|
+
assert_equal 'UTF8', page.parser.encoding
|
33
|
+
assert_equal 'UTF8', page.encoding
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_upper_case_content_type
|
37
|
+
page = @agent.get("http://localhost/http_headers?content-type=#{CGI.escape('text/HTML')}")
|
38
|
+
assert_instance_of WWW::Mechanize::Page, page
|
39
|
+
assert_equal 'text/HTML', page.content_type
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_encoding_override_before_parser_initialized
|
43
|
+
# document has a bad encoding information - windows-1255
|
44
|
+
page = @agent.get("http://localhost/tc_bad_charset.html")
|
45
|
+
# encoding is wrong, so user wants to force ISO-8859-2
|
46
|
+
page.encoding = 'ISO-8859-2'
|
47
|
+
assert_equal 'ISO-8859-2', page.encoding
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_encoding_override_after_parser_was_initialized
|
51
|
+
# document has a bad encoding information - windows-1255
|
52
|
+
page = @agent.get("http://localhost/tc_bad_charset.html")
|
53
|
+
page.parser
|
54
|
+
# autodetection sets encoding to windows-1255
|
55
|
+
assert_equal 'windows-1255', page.encoding
|
56
|
+
# encoding is wrong, so user wants to force ISO-8859-2
|
57
|
+
page.encoding = 'ISO-8859-2'
|
58
|
+
assert_equal 'ISO-8859-2', page.encoding
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_page_gets_charset_sent_by_server
|
62
|
+
page = @agent.get("http://localhost/http_headers?content-type=#{CGI.escape('text/html; charset=UTF-8')}")
|
63
|
+
assert_equal 'UTF-8', page.encoding
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_set_encoding
|
67
|
+
page = @agent.get("http://localhost/file_upload.html")
|
68
|
+
page.encoding = 'UTF-8'
|
69
|
+
assert_equal 'UTF-8', page.parser.encoding
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_page_gets_yielded
|
73
|
+
pages = nil
|
74
|
+
@agent.get("http://localhost/file_upload.html") { |page|
|
75
|
+
pages = page
|
76
|
+
}
|
77
|
+
assert pages
|
78
|
+
assert_equal('File Upload Form', pages.title)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_title
|
82
|
+
page = @agent.get("http://localhost/file_upload.html")
|
83
|
+
assert_equal('File Upload Form', page.title)
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_no_title
|
87
|
+
page = @agent.get("http://localhost/no_title_test.html")
|
88
|
+
assert_equal(nil, page.title)
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_page_decoded_with_charset
|
92
|
+
page = WWW::Mechanize::Page.new(
|
93
|
+
URI.parse('http://tenderlovemaking.com/'),
|
94
|
+
{ 'content-type' => 'text/html; charset=EUC-JP' },
|
95
|
+
'<html><body>hello</body></html>',
|
96
|
+
400,
|
97
|
+
@agent
|
98
|
+
)
|
99
|
+
assert_equal 'EUC-JP', page.parser.encoding
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_find_form_with_hash
|
103
|
+
page = @agent.get("http://localhost/tc_form_action.html")
|
104
|
+
form = page.form(:name => 'post_form1')
|
105
|
+
assert form
|
106
|
+
yielded = false
|
107
|
+
form = page.form(:name => 'post_form1') { |f|
|
108
|
+
yielded = true
|
109
|
+
assert f
|
110
|
+
assert_equal(form, f)
|
111
|
+
}
|
112
|
+
assert yielded
|
113
|
+
|
114
|
+
form_by_action = page.form(:action => '/form_post?a=b&b=c')
|
115
|
+
assert form_by_action
|
116
|
+
assert_equal(form, form_by_action)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
@@ -0,0 +1,145 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class PluggableParserTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_content_type_error
|
9
|
+
page = @agent.get("http://localhost/bad_content_type")
|
10
|
+
assert_raise(WWW::Mechanize::ContentTypeError) {
|
11
|
+
page = WWW::Mechanize::Page.new(
|
12
|
+
page.uri,
|
13
|
+
page.response,
|
14
|
+
page.body,
|
15
|
+
page.code
|
16
|
+
)
|
17
|
+
}
|
18
|
+
begin
|
19
|
+
page = WWW::Mechanize::Page.new(
|
20
|
+
page.uri,
|
21
|
+
page.response,
|
22
|
+
page.body,
|
23
|
+
page.code
|
24
|
+
)
|
25
|
+
rescue WWW::Mechanize::ContentTypeError => ex
|
26
|
+
assert_equal('text/xml', ex.content_type)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_content_type
|
31
|
+
page = @agent.get("http://localhost/content_type_test")
|
32
|
+
assert_kind_of(WWW::Mechanize::Page, page)
|
33
|
+
end
|
34
|
+
|
35
|
+
class Filter < WWW::Mechanize::Page
|
36
|
+
def initialize(uri=nil, response=nil, body=nil, code=nil)
|
37
|
+
super( uri,
|
38
|
+
response,
|
39
|
+
body.gsub(/<body>/, '<body><a href="http://daapclient.rubyforge.org">Net::DAAP::Client</a>'),
|
40
|
+
code
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class FileFilter < WWW::Mechanize::File
|
46
|
+
def initialize(uri=nil, response=nil, body=nil, code=nil)
|
47
|
+
super( uri,
|
48
|
+
response,
|
49
|
+
body.gsub(/<body>/, '<body><a href="http://daapclient.rubyforge.org">Net::DAAP::Client</a>'),
|
50
|
+
code
|
51
|
+
)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_filter
|
56
|
+
@agent.pluggable_parser.html = Filter
|
57
|
+
page = @agent.get("http://localhost/find_link.html")
|
58
|
+
assert_kind_of(Filter, page)
|
59
|
+
assert_equal(19, page.links.length)
|
60
|
+
assert_not_nil(page.link_with(:text => 'Net::DAAP::Client'))
|
61
|
+
assert_equal(1, page.links_with(:text => 'Net::DAAP::Client').length)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_filter_hash
|
65
|
+
@agent.pluggable_parser['text/html'] = Filter
|
66
|
+
page = @agent.get("http://localhost/find_link.html")
|
67
|
+
assert_kind_of(Class, @agent.pluggable_parser['text/html'])
|
68
|
+
assert_equal(Filter, @agent.pluggable_parser['text/html'])
|
69
|
+
assert_kind_of(Filter, page)
|
70
|
+
assert_equal(19, page.links.length)
|
71
|
+
assert_not_nil(page.link_with(:text => 'Net::DAAP::Client'))
|
72
|
+
assert_equal(1, page.links_with(:text => 'Net::DAAP::Client').length)
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_file_saver
|
76
|
+
@agent.pluggable_parser.html = WWW::Mechanize::FileSaver
|
77
|
+
page = @agent.get('http://localhost:2000/form_no_action.html')
|
78
|
+
length = page.response['Content-Length']
|
79
|
+
file_length = nil
|
80
|
+
File.open("localhost/form_no_action.html", "r") { |f|
|
81
|
+
file_length = f.read.length
|
82
|
+
}
|
83
|
+
FileUtils.rm_rf("localhost")
|
84
|
+
assert_equal(length.to_i, file_length)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_content_type_pdf
|
88
|
+
@agent.pluggable_parser.pdf = FileFilter
|
89
|
+
page = @agent.get("http://localhost/content_type_test?ct=application/pdf")
|
90
|
+
assert_kind_of(Class, @agent.pluggable_parser['application/pdf'])
|
91
|
+
assert_equal(FileFilter, @agent.pluggable_parser['application/pdf'])
|
92
|
+
assert_kind_of(FileFilter, page)
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_content_type_csv
|
96
|
+
@agent.pluggable_parser.csv = FileFilter
|
97
|
+
page = @agent.get("http://localhost/content_type_test?ct=text/csv")
|
98
|
+
assert_kind_of(Class, @agent.pluggable_parser['text/csv'])
|
99
|
+
assert_equal(FileFilter, @agent.pluggable_parser['text/csv'])
|
100
|
+
assert_kind_of(FileFilter, page)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_content_type_xml
|
104
|
+
@agent.pluggable_parser.xml = FileFilter
|
105
|
+
page = @agent.get("http://localhost/content_type_test?ct=text/xml")
|
106
|
+
assert_kind_of(Class, @agent.pluggable_parser['text/xml'])
|
107
|
+
assert_equal(FileFilter, @agent.pluggable_parser['text/xml'])
|
108
|
+
assert_kind_of(FileFilter, page)
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_file_saver_no_path
|
112
|
+
url = URI::HTTP.new('http', nil, 'example.com', nil, nil, '', nil, nil, nil)
|
113
|
+
fs = WWW::Mechanize::FileSaver.new(url, nil, 'hello world', 200)
|
114
|
+
assert_equal('example.com/index.html', fs.filename)
|
115
|
+
FileUtils.rm_rf('example.com')
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_file_saver_slash
|
119
|
+
url = URI::HTTP.new('http', nil, 'example.com', nil, nil, '/', nil, nil, nil)
|
120
|
+
fs = WWW::Mechanize::FileSaver.new(url, nil, 'hello world', 200)
|
121
|
+
assert_equal('example.com/index.html', fs.filename)
|
122
|
+
FileUtils.rm_rf('example.com')
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_file_saver_slash_file
|
126
|
+
url = URI::HTTP.new('http', nil, 'example.com', nil, nil, '/foo.html', nil, nil, nil)
|
127
|
+
fs = WWW::Mechanize::FileSaver.new(url, nil, 'hello world', 200)
|
128
|
+
assert_equal('example.com/foo.html', fs.filename)
|
129
|
+
FileUtils.rm_rf('example.com')
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_file_saver_long_path_no_file
|
133
|
+
url = URI::HTTP.new('http', nil, 'example.com', nil, nil, '/one/two/', nil, nil, nil)
|
134
|
+
fs = WWW::Mechanize::FileSaver.new(url, nil, 'hello world', 200)
|
135
|
+
assert_equal('example.com/one/two/index.html', fs.filename)
|
136
|
+
FileUtils.rm_rf('example.com')
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_file_saver_long_path
|
140
|
+
url = URI::HTTP.new('http', nil, 'example.com', nil, nil, '/one/two/foo.html', nil, nil, nil)
|
141
|
+
fs = WWW::Mechanize::FileSaver.new(url, nil, 'hello world', 200)
|
142
|
+
assert_equal('example.com/one/two/foo.html', fs.filename)
|
143
|
+
FileUtils.rm_rf('example.com')
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class PostForm < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_post_form
|
9
|
+
page = @agent.post("http://localhost/form_post",
|
10
|
+
'gender' => 'female'
|
11
|
+
)
|
12
|
+
assert_not_nil(
|
13
|
+
page.links.find { |l| l.text == "gender:female" },
|
14
|
+
"gender field missing"
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_post_form_multival
|
19
|
+
page = @agent.post("http://localhost/form_post",
|
20
|
+
[ ['gender', 'female'],
|
21
|
+
['gender', 'male']
|
22
|
+
]
|
23
|
+
)
|
24
|
+
assert_not_nil(
|
25
|
+
page.links.find { |l| l.text == "gender:female" },
|
26
|
+
"gender field missing"
|
27
|
+
)
|
28
|
+
assert_not_nil(
|
29
|
+
page.links.find { |l| l.text == "gender:male" },
|
30
|
+
"gender field missing"
|
31
|
+
)
|
32
|
+
assert_equal(2, page.links.length)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestPrettyPrint < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_pretty_print
|
9
|
+
@agent.get("http://localhost/tc_pretty_print.html")
|
10
|
+
pretty_string = @agent.pretty_print_inspect
|
11
|
+
assert_match("{title \"tc_pretty_print.html\"}", pretty_string)
|
12
|
+
assert_match(/\{frames[^"]*"http:\/\/meme/, pretty_string)
|
13
|
+
assert_match(/\{iframes[^"]*"http:\/\/meme/, pretty_string)
|
14
|
+
assert_match(
|
15
|
+
"{links #<WWW::Mechanize::Page::Link \"Google\" \"http://google.com/\">}",
|
16
|
+
pretty_string
|
17
|
+
)
|
18
|
+
assert_match("form1", pretty_string)
|
19
|
+
assert_match("POST", pretty_string)
|
20
|
+
assert_match("{file_uploads}", pretty_string)
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestRadioButtons < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
@page = @agent.get("http://localhost/tc_radiobuttons.html")
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_select_all
|
10
|
+
form = @page.forms.first
|
11
|
+
form.radiobuttons_with(:name => 'color').each do |b|
|
12
|
+
b.check
|
13
|
+
end
|
14
|
+
form.radiobutton_with(:name => 'color', :value => 'green').check
|
15
|
+
|
16
|
+
assert_equal(true, form.radiobutton_with( :name => 'color',
|
17
|
+
:value => 'green').checked)
|
18
|
+
|
19
|
+
%w{ red blue yellow brown }.each do |button_value|
|
20
|
+
assert_equal(false, form.radiobutton_with( :name => 'color',
|
21
|
+
:value => button_value
|
22
|
+
).checked)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_unselect_all
|
27
|
+
form = @page.forms.first
|
28
|
+
form.radiobuttons_with(:name => 'color').each do |b|
|
29
|
+
b.uncheck
|
30
|
+
end
|
31
|
+
%w{ green red blue yellow brown }.each do |button_value|
|
32
|
+
assert_equal(false, form.radiobutton_with( :name => 'color',
|
33
|
+
:value => button_value
|
34
|
+
).checked)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_click_one
|
39
|
+
form = @page.forms.first
|
40
|
+
form.radiobutton_with(:name => 'color', :value => 'green').click
|
41
|
+
|
42
|
+
assert form.radiobutton_with(:name => 'color', :value => 'green').checked
|
43
|
+
|
44
|
+
%w{ red blue yellow brown }.each do |button_value|
|
45
|
+
assert_equal(false, form.radiobutton_with( :name => 'color',
|
46
|
+
:value => button_value
|
47
|
+
).checked)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_click_twice
|
52
|
+
form = @page.forms.first
|
53
|
+
form.radiobutton_with(:name => 'color', :value => 'green').click
|
54
|
+
assert form.radiobutton_with(:name => 'color', :value => 'green').checked
|
55
|
+
|
56
|
+
form.radiobutton_with(:name => 'color', :value => 'green').click
|
57
|
+
%w{ green red blue yellow brown }.each do |button_value|
|
58
|
+
assert_equal(false, form.radiobutton_with( :name => 'color',
|
59
|
+
:value => button_value
|
60
|
+
).checked)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_click_all
|
65
|
+
form = @page.forms.first
|
66
|
+
form.radiobuttons_with(:name => 'color').each do |button|
|
67
|
+
button.click
|
68
|
+
end
|
69
|
+
c = form.radiobuttons_with(:name => 'color').inject(0) do |m,button|
|
70
|
+
m += 1 if button.checked
|
71
|
+
m
|
72
|
+
end
|
73
|
+
assert_equal 1, c, 'Only one radio button should be checked'
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestRedirectLimitReached < Test::Unit::TestCase
|
4
|
+
include WWW
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@agent = WWW::Mechanize.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_to_s
|
11
|
+
page = MechTestHelper.fake_page(@agent)
|
12
|
+
error = Mechanize::RedirectLimitReachedError.new(page, 10)
|
13
|
+
assert_match(/10/, error.to_s)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_get_default_redirects
|
17
|
+
assert_raises(Mechanize::RedirectLimitReachedError) {
|
18
|
+
begin
|
19
|
+
@agent.get('http://localhost/infinite_redirect')
|
20
|
+
rescue Mechanize::RedirectLimitReachedError => ex
|
21
|
+
assert_equal(@agent.redirection_limit, ex.redirects)
|
22
|
+
assert_equal("q=#{@agent.redirection_limit}", ex.page.uri.query)
|
23
|
+
raise ex
|
24
|
+
end
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_get_2_redirects
|
29
|
+
@agent.redirection_limit = 2
|
30
|
+
assert_raises(Mechanize::RedirectLimitReachedError) {
|
31
|
+
begin
|
32
|
+
@agent.get('http://localhost/infinite_redirect')
|
33
|
+
rescue Mechanize::RedirectLimitReachedError => ex
|
34
|
+
assert_equal(2, ex.redirects)
|
35
|
+
assert_equal(@agent.redirection_limit, ex.redirects)
|
36
|
+
assert_equal("q=#{@agent.redirection_limit}", ex.page.uri.query)
|
37
|
+
raise ex
|
38
|
+
end
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|