eric-mechanize 0.9.3.20090623142847
Sign up to get free protection for your applications and to get access to all the features.
- 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 +124 -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
data/test/test_mech.rb
ADDED
@@ -0,0 +1,268 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class TestMechMethods < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_get_with_tilde
|
9
|
+
page = @agent.get('http://localhost/?foo=~2')
|
10
|
+
assert_equal('http://localhost/?foo=~2', page.uri.to_s)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_parser_can_be_set
|
14
|
+
@agent.html_parser = {}
|
15
|
+
assert_raises(NoMethodError) {
|
16
|
+
@agent.get('http://localhost/?foo=~2').links
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_submit_takes_arbirary_headers
|
21
|
+
page = @agent.get('http://localhost:2000/form_no_action.html')
|
22
|
+
assert form = page.forms.first
|
23
|
+
form.action = '/http_headers'
|
24
|
+
page = @agent.submit(form, nil, { 'foo' => 'bar' })
|
25
|
+
headers = Hash[*(
|
26
|
+
page.body.split("\n").map { |x| x.split('|') }.flatten
|
27
|
+
)]
|
28
|
+
assert_equal 'bar', headers['foo']
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_get_with_params
|
32
|
+
page = @agent.get('http://localhost/', { :q => 'hello' })
|
33
|
+
assert_equal('http://localhost/?q=hello', page.uri.to_s)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_get_with_upper_http
|
37
|
+
page = @agent.get('HTTP://localhost/', { :q => 'hello' })
|
38
|
+
assert_equal('HTTP://localhost/?q=hello', page.uri.to_s)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_get_no_referer
|
42
|
+
requests = []
|
43
|
+
@agent.pre_connect_hooks << lambda { |params|
|
44
|
+
requests << params[:request]
|
45
|
+
}
|
46
|
+
|
47
|
+
@agent.get('http://localhost/')
|
48
|
+
@agent.get('http://localhost/')
|
49
|
+
assert_equal(2, requests.length)
|
50
|
+
requests.each do |request|
|
51
|
+
assert_nil request['referer']
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_with_anchor
|
56
|
+
page = @agent.get('http://localhost/?foo=bar"')
|
57
|
+
assert_equal('http://localhost/?foo=bar%22', page.uri.to_s)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_post_connect_hook_gets_called
|
61
|
+
response = nil
|
62
|
+
@agent.post_connect_hooks << lambda { |params|
|
63
|
+
response = params[:response]
|
64
|
+
}
|
65
|
+
|
66
|
+
@agent.get('http://localhost/')
|
67
|
+
assert(response)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_get_with_referer
|
71
|
+
request = nil
|
72
|
+
@agent.pre_connect_hooks << lambda { |params|
|
73
|
+
request = params[:request]
|
74
|
+
}
|
75
|
+
|
76
|
+
@agent.get('http://localhost/', URI.parse('http://google.com/'))
|
77
|
+
assert_equal 'http://google.com/', request['Referer']
|
78
|
+
|
79
|
+
@agent.get('http://localhost/', [], 'http://tenderlovemaking.com/')
|
80
|
+
assert_equal 'http://tenderlovemaking.com/', request['Referer']
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_get_with_file_referer
|
84
|
+
assert_nothing_raised do
|
85
|
+
@agent.get('http://localhost', [], WWW::Mechanize::File.new(URI.parse('http://tenderlovemaking.com/crossdomain.xml')))
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_weird_url
|
90
|
+
assert_nothing_raised {
|
91
|
+
@agent.get('http://localhost/?action=bing&bang=boom=1|a=|b=|c=')
|
92
|
+
}
|
93
|
+
assert_nothing_raised {
|
94
|
+
@agent.get('http://localhost/?a=b&b=c&c=d')
|
95
|
+
}
|
96
|
+
assert_nothing_raised {
|
97
|
+
@agent.get("http://localhost/?a=#{[0xd6].pack('U')}")
|
98
|
+
}
|
99
|
+
end
|
100
|
+
|
101
|
+
unless RUBY_VERSION >= '1.9.0'
|
102
|
+
def test_kcode_url
|
103
|
+
$KCODE = 'u'
|
104
|
+
page = @agent.get("http://localhost/?a=#{[0xd6].pack('U')}")
|
105
|
+
assert_not_nil(page)
|
106
|
+
assert_equal('http://localhost/?a=%D6', page.uri.to_s)
|
107
|
+
$KCODE = 'NONE'
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_history
|
112
|
+
0.upto(25) do |i|
|
113
|
+
assert_equal(i, @agent.history.size)
|
114
|
+
page = @agent.get("http://localhost/")
|
115
|
+
end
|
116
|
+
page = @agent.get("http://localhost/form_test.html")
|
117
|
+
|
118
|
+
assert_equal("http://localhost/form_test.html",
|
119
|
+
@agent.history.last.uri.to_s)
|
120
|
+
assert_equal("http://localhost/",
|
121
|
+
@agent.history[-2].uri.to_s)
|
122
|
+
assert_equal("http://localhost/",
|
123
|
+
@agent.history[-2].uri.to_s)
|
124
|
+
|
125
|
+
assert_equal(true, @agent.visited?("http://localhost/"))
|
126
|
+
assert_equal(true, @agent.visited?("/form_test.html"))
|
127
|
+
assert_equal(false, @agent.visited?("http://google.com/"))
|
128
|
+
assert_equal(true, @agent.visited?(page.links.first))
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_visited
|
133
|
+
@agent.get("http://localhost/content_type_test?ct=application/pdf")
|
134
|
+
assert_equal(true,
|
135
|
+
@agent.visited?("http://localhost/content_type_test?ct=application/pdf"))
|
136
|
+
assert_equal(false,
|
137
|
+
@agent.visited?("http://localhost/content_type_test"))
|
138
|
+
assert_equal(false,
|
139
|
+
@agent.visited?("http://localhost/content_type_test?ct=text/html"))
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_visited_after_redirect
|
143
|
+
@agent.get("http://localhost/response_code?code=302")
|
144
|
+
assert_equal("http://localhost/index.html",
|
145
|
+
@agent.current_page.uri.to_s)
|
146
|
+
assert_equal(true,
|
147
|
+
@agent.visited?('http://localhost/response_code?code=302'))
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_max_history
|
151
|
+
@agent.max_history = 10
|
152
|
+
0.upto(10) do |i|
|
153
|
+
assert_equal(i, @agent.history.size)
|
154
|
+
page = @agent.get("http://localhost/")
|
155
|
+
end
|
156
|
+
|
157
|
+
0.upto(10) do |i|
|
158
|
+
assert_equal(10, @agent.history.size)
|
159
|
+
page = @agent.get("http://localhost/")
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_max_history_order
|
164
|
+
@agent.max_history = 2
|
165
|
+
assert_equal(0, @agent.history.length)
|
166
|
+
|
167
|
+
@agent.get('http://localhost/form_test.html')
|
168
|
+
assert_equal(1, @agent.history.length)
|
169
|
+
|
170
|
+
@agent.get('http://localhost/empty_form.html')
|
171
|
+
assert_equal(2, @agent.history.length)
|
172
|
+
|
173
|
+
@agent.get('http://localhost/tc_checkboxes.html')
|
174
|
+
assert_equal(2, @agent.history.length)
|
175
|
+
assert_equal('http://localhost/empty_form.html', @agent.history[0].uri.to_s)
|
176
|
+
assert_equal('http://localhost/tc_checkboxes.html',
|
177
|
+
@agent.history[1].uri.to_s)
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_back_button
|
181
|
+
0.upto(5) do |i|
|
182
|
+
assert_equal(i, @agent.history.size)
|
183
|
+
page = @agent.get("http://localhost/")
|
184
|
+
end
|
185
|
+
page = @agent.get("http://localhost/form_test.html")
|
186
|
+
|
187
|
+
assert_equal("http://localhost/form_test.html",
|
188
|
+
@agent.history.last.uri.to_s)
|
189
|
+
assert_equal("http://localhost/",
|
190
|
+
@agent.history[-2].uri.to_s)
|
191
|
+
|
192
|
+
assert_equal(7, @agent.history.size)
|
193
|
+
@agent.back
|
194
|
+
assert_equal(6, @agent.history.size)
|
195
|
+
assert_equal("http://localhost/",
|
196
|
+
@agent.history.last.uri.to_s)
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_google
|
200
|
+
page = @agent.get("http://localhost/google.html")
|
201
|
+
search = page.forms.find { |f| f.name == "f" }
|
202
|
+
assert_not_nil(search)
|
203
|
+
assert_not_nil(search.field_with(:name => 'q'))
|
204
|
+
assert_not_nil(search.field_with(:name => 'hl'))
|
205
|
+
assert_not_nil(search.fields.find { |f| f.name == 'ie' })
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_click
|
209
|
+
@agent.user_agent_alias = 'Mac Safari'
|
210
|
+
page = @agent.get("http://localhost/frame_test.html")
|
211
|
+
link = page.link_with(:text => "Form Test")
|
212
|
+
assert_not_nil(link)
|
213
|
+
page = @agent.click(link)
|
214
|
+
assert_equal("http://localhost/form_test.html",
|
215
|
+
@agent.history.last.uri.to_s)
|
216
|
+
end
|
217
|
+
|
218
|
+
def test_click_hpricot
|
219
|
+
page = @agent.get("http://localhost/frame_test.html")
|
220
|
+
|
221
|
+
link = (page/"//a[@class='bar']").first
|
222
|
+
assert_not_nil(link)
|
223
|
+
page = @agent.click(link)
|
224
|
+
assert_equal("http://localhost/form_test.html",
|
225
|
+
@agent.history.last.uri.to_s)
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_click_hpricot_frame
|
229
|
+
page = @agent.get("http://localhost/frame_test.html")
|
230
|
+
|
231
|
+
link = (page/"//frame[@name='frame2']").first
|
232
|
+
assert_not_nil(link)
|
233
|
+
page = @agent.click(link)
|
234
|
+
assert_equal("http://localhost/form_test.html",
|
235
|
+
@agent.history.last.uri.to_s)
|
236
|
+
end
|
237
|
+
|
238
|
+
def test_new_find
|
239
|
+
page = @agent.get("http://localhost/frame_test.html")
|
240
|
+
assert_equal(3, page.frames.size)
|
241
|
+
|
242
|
+
find_orig = page.frames.find_all { |f| f.name == 'frame1' }
|
243
|
+
find1 = page.frames_with(:name => 'frame1')
|
244
|
+
|
245
|
+
find_orig.zip(find1).each { |a,b|
|
246
|
+
assert_equal(a, b)
|
247
|
+
}
|
248
|
+
end
|
249
|
+
|
250
|
+
def test_get_file
|
251
|
+
page = @agent.get("http://localhost/frame_test.html")
|
252
|
+
content_length = page.header['Content-Length']
|
253
|
+
page_as_string = @agent.get_file("http://localhost/frame_test.html")
|
254
|
+
assert_equal(content_length.to_i, page_as_string.length.to_i)
|
255
|
+
end
|
256
|
+
|
257
|
+
def test_transact
|
258
|
+
page = @agent.get("http://localhost/frame_test.html")
|
259
|
+
assert_equal(1, @agent.history.length)
|
260
|
+
@agent.transact { |a|
|
261
|
+
5.times {
|
262
|
+
@agent.get("http://localhost/frame_test.html")
|
263
|
+
}
|
264
|
+
assert_equal(6, @agent.history.length)
|
265
|
+
}
|
266
|
+
assert_equal(1, @agent.history.length)
|
267
|
+
end
|
268
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class MechanizeFileTest < Test::Unit::TestCase
|
4
|
+
def test_content_disposition
|
5
|
+
file = WWW::Mechanize::File.new(
|
6
|
+
URI.parse('http://localhost/foo'),
|
7
|
+
{ 'content-disposition' => 'attachment; filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"', }
|
8
|
+
)
|
9
|
+
assert_equal('genome.jpeg', file.filename)
|
10
|
+
|
11
|
+
file = WWW::Mechanize::File.new(
|
12
|
+
URI.parse('http://localhost/foo'),
|
13
|
+
{ 'content-disposition' => 'filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"', }
|
14
|
+
)
|
15
|
+
assert_equal('genome.jpeg', file.filename)
|
16
|
+
|
17
|
+
file = WWW::Mechanize::File.new(
|
18
|
+
URI.parse('http://localhost/foo'),
|
19
|
+
{ 'content-disposition' => 'filename=genome.jpeg', }
|
20
|
+
)
|
21
|
+
assert_equal('genome.jpeg', file.filename)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_from_uri
|
25
|
+
file = WWW::Mechanize::File.new(
|
26
|
+
URI.parse('http://localhost/foo'),
|
27
|
+
{}
|
28
|
+
)
|
29
|
+
assert_equal('foo.html', file.filename)
|
30
|
+
|
31
|
+
file = WWW::Mechanize::File.new(
|
32
|
+
URI.parse('http://localhost/foo.jpg'),
|
33
|
+
{}
|
34
|
+
)
|
35
|
+
assert_equal('foo.jpg', file.filename)
|
36
|
+
|
37
|
+
file = WWW::Mechanize::File.new(
|
38
|
+
URI.parse('http://localhost/foo.jpg')
|
39
|
+
)
|
40
|
+
assert_equal('foo.jpg', file.filename)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_no_uri
|
44
|
+
file = WWW::Mechanize::File.new()
|
45
|
+
assert_equal('index.html', file.filename)
|
46
|
+
end
|
47
|
+
end
|
data/test/test_meta.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class MetaTest < Test::Unit::TestCase
|
4
|
+
Meta = WWW::Mechanize::Page::Meta
|
5
|
+
|
6
|
+
#
|
7
|
+
# CONTENT_REGEXP test
|
8
|
+
#
|
9
|
+
|
10
|
+
def test_content_regexp
|
11
|
+
r = Meta::CONTENT_REGEXP
|
12
|
+
|
13
|
+
assert r =~ "0; url=http://localhost:8080/path"
|
14
|
+
assert_equal "0", $1
|
15
|
+
assert_equal "http://localhost:8080/path", $3
|
16
|
+
|
17
|
+
assert r =~ "100.001; url=http://localhost:8080/path"
|
18
|
+
assert_equal "100.001", $1
|
19
|
+
assert_equal "http://localhost:8080/path", $3
|
20
|
+
|
21
|
+
assert r =~ "0; url='http://localhost:8080/path'"
|
22
|
+
assert_equal "0", $1
|
23
|
+
assert_equal "http://localhost:8080/path", $3
|
24
|
+
|
25
|
+
assert r =~ "0; url=\"http://localhost:8080/path\""
|
26
|
+
assert_equal "0", $1
|
27
|
+
assert_equal "http://localhost:8080/path", $3
|
28
|
+
|
29
|
+
assert r =~ "0; url="
|
30
|
+
assert_equal "0", $1
|
31
|
+
assert_equal "", $3
|
32
|
+
|
33
|
+
assert r =~ "0"
|
34
|
+
assert_equal "0", $1
|
35
|
+
assert_equal nil, $3
|
36
|
+
|
37
|
+
assert r =~ " 0; "
|
38
|
+
assert_equal "0", $1
|
39
|
+
assert_equal nil, $3
|
40
|
+
|
41
|
+
assert r =~ "0; UrL=http://localhost:8080/path"
|
42
|
+
assert_equal "0", $1
|
43
|
+
assert_equal "http://localhost:8080/path", $3
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# parse test
|
48
|
+
#
|
49
|
+
|
50
|
+
def test_parse_documentation
|
51
|
+
uri = URI.parse('http://current.com/')
|
52
|
+
|
53
|
+
assert_equal ['5', 'http://example.com/'], Meta.parse("5;url=http://example.com/", uri)
|
54
|
+
assert_equal ['5', 'http://current.com/'], Meta.parse("5;url=", uri)
|
55
|
+
assert_equal ['5', 'http://current.com/'], Meta.parse("5", uri)
|
56
|
+
assert_equal nil, Meta.parse("invalid content", uri)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_parse_returns_nil_if_no_delay_and_url_can_be_parsed
|
60
|
+
uri = URI.parse('http://current.com/')
|
61
|
+
|
62
|
+
assert_equal nil, Meta.parse("invalid content", uri)
|
63
|
+
assert_equal nil, Meta.parse("invalid content", uri) {|delay, url| 'not nil' }
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
|
2
|
+
|
3
|
+
class MultiSelectTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@agent = WWW::Mechanize.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_select_none
|
9
|
+
page = @agent.get("http://localhost/form_multi_select.html")
|
10
|
+
form = page.forms.first
|
11
|
+
form.field_with(:name => 'list').select_none
|
12
|
+
page = @agent.submit(form)
|
13
|
+
assert_equal(0, page.links.length)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_select_all
|
17
|
+
page = @agent.get("http://localhost/form_multi_select.html")
|
18
|
+
form = page.forms.first
|
19
|
+
form.field_with(:name => 'list').select_all
|
20
|
+
page = @agent.submit(form)
|
21
|
+
assert_equal(6, page.links.length)
|
22
|
+
assert_equal(1, page.links_with(:text => 'list:1').length)
|
23
|
+
assert_equal(1, page.links_with(:text => 'list:2').length)
|
24
|
+
assert_equal(1, page.links_with(:text => 'list:3').length)
|
25
|
+
assert_equal(1, page.links_with(:text => 'list:4').length)
|
26
|
+
assert_equal(1, page.links_with(:text => 'list:5').length)
|
27
|
+
assert_equal(1, page.links_with(:text => 'list:6').length)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_click_all
|
31
|
+
page = @agent.get("http://localhost/form_multi_select.html")
|
32
|
+
form = page.forms.first
|
33
|
+
form.field_with(:name => 'list').options.each { |o| o.click }
|
34
|
+
page = @agent.submit(form)
|
35
|
+
assert_equal(5, page.links.length)
|
36
|
+
assert_equal(1, page.links_with(:text => 'list:1').length)
|
37
|
+
assert_equal(1, page.links_with(:text => 'list:3').length)
|
38
|
+
assert_equal(1, page.links_with(:text => 'list:4').length)
|
39
|
+
assert_equal(1, page.links_with(:text => 'list:5').length)
|
40
|
+
assert_equal(1, page.links_with(:text => 'list:6').length)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_select_default
|
44
|
+
page = @agent.get("http://localhost/form_multi_select.html")
|
45
|
+
form = page.forms.first
|
46
|
+
page = @agent.submit(form)
|
47
|
+
assert_equal(1, page.links.length)
|
48
|
+
assert_equal(1, page.links_with(:text => 'list:2').length)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_select_one
|
52
|
+
page = @agent.get("http://localhost/form_multi_select.html")
|
53
|
+
form = page.forms.first
|
54
|
+
form.list = 'Aaron'
|
55
|
+
assert_equal(['Aaron'], form.list)
|
56
|
+
page = @agent.submit(form)
|
57
|
+
assert_equal(1, page.links.length)
|
58
|
+
assert_equal('list:Aaron', page.links.first.text)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_select_two
|
62
|
+
page = @agent.get("http://localhost/form_multi_select.html")
|
63
|
+
form = page.forms.first
|
64
|
+
form.list = ['1', 'Aaron']
|
65
|
+
page = @agent.submit(form)
|
66
|
+
assert_equal(2, page.links.length)
|
67
|
+
assert_equal(1, page.links_with(:text => 'list:1').length)
|
68
|
+
assert_equal(1, page.links_with(:text => 'list:Aaron').length)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_select_three
|
72
|
+
page = @agent.get("http://localhost/form_multi_select.html")
|
73
|
+
form = page.forms.first
|
74
|
+
form.list = ['1', '2', '3']
|
75
|
+
page = @agent.submit(form)
|
76
|
+
assert_equal(3, page.links.length)
|
77
|
+
assert_equal(1, page.links_with(:text => 'list:1').length)
|
78
|
+
assert_equal(1, page.links_with(:text => 'list:2').length)
|
79
|
+
assert_equal(1, page.links_with(:text => 'list:3').length)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_select_three_twice
|
83
|
+
page = @agent.get("http://localhost/form_multi_select.html")
|
84
|
+
form = page.forms.first
|
85
|
+
form.list = ['1', '2', '3']
|
86
|
+
form.list = ['1', '2', '3']
|
87
|
+
page = @agent.submit(form)
|
88
|
+
assert_equal(3, page.links.length)
|
89
|
+
assert_equal(1, page.links_with(:text => 'list:1').length)
|
90
|
+
assert_equal(1, page.links_with(:text => 'list:2').length)
|
91
|
+
assert_equal(1, page.links_with(:text => 'list:3').length)
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_select_with_click
|
95
|
+
page = @agent.get("http://localhost/form_multi_select.html")
|
96
|
+
form = page.forms.first
|
97
|
+
form.list = ['1', 'Aaron']
|
98
|
+
form.field_with(:name => 'list').options[3].tick
|
99
|
+
assert_equal(['1', 'Aaron', '4'].sort, form.list.sort)
|
100
|
+
page = @agent.submit(form)
|
101
|
+
assert_equal(3, page.links.length)
|
102
|
+
assert_equal(1, page.links_with(:text => 'list:1').length)
|
103
|
+
assert_equal(1, page.links_with(:text => 'list:Aaron').length)
|
104
|
+
assert_equal(1, page.links_with(:text => 'list:4').length)
|
105
|
+
end
|
106
|
+
end
|