kitamomonga-mechanize 0.9.3.20090724215219

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. data/CHANGELOG.rdoc +504 -0
  2. data/EXAMPLES.rdoc +171 -0
  3. data/FAQ.rdoc +11 -0
  4. data/GUIDE.rdoc +122 -0
  5. data/LICENSE.rdoc +340 -0
  6. data/Manifest.txt +176 -0
  7. data/README.rdoc +60 -0
  8. data/Rakefile +33 -0
  9. data/examples/flickr_upload.rb +23 -0
  10. data/examples/mech-dump.rb +7 -0
  11. data/examples/proxy_req.rb +9 -0
  12. data/examples/rubyforge.rb +21 -0
  13. data/examples/spider.rb +11 -0
  14. data/lib/mechanize.rb +666 -0
  15. data/lib/mechanize/chain.rb +34 -0
  16. data/lib/mechanize/chain/auth_headers.rb +78 -0
  17. data/lib/mechanize/chain/body_decoding_handler.rb +46 -0
  18. data/lib/mechanize/chain/connection_resolver.rb +76 -0
  19. data/lib/mechanize/chain/custom_headers.rb +21 -0
  20. data/lib/mechanize/chain/handler.rb +9 -0
  21. data/lib/mechanize/chain/header_resolver.rb +51 -0
  22. data/lib/mechanize/chain/parameter_resolver.rb +22 -0
  23. data/lib/mechanize/chain/post_connect_hook.rb +0 -0
  24. data/lib/mechanize/chain/post_page_hook.rb +18 -0
  25. data/lib/mechanize/chain/pre_connect_hook.rb +20 -0
  26. data/lib/mechanize/chain/request_resolver.rb +30 -0
  27. data/lib/mechanize/chain/response_body_parser.rb +38 -0
  28. data/lib/mechanize/chain/response_header_handler.rb +48 -0
  29. data/lib/mechanize/chain/response_reader.rb +39 -0
  30. data/lib/mechanize/chain/ssl_resolver.rb +40 -0
  31. data/lib/mechanize/chain/uri_resolver.rb +75 -0
  32. data/lib/mechanize/content_type_error.rb +14 -0
  33. data/lib/mechanize/cookie.rb +70 -0
  34. data/lib/mechanize/cookie_jar.rb +188 -0
  35. data/lib/mechanize/file.rb +71 -0
  36. data/lib/mechanize/file_response.rb +60 -0
  37. data/lib/mechanize/file_saver.rb +37 -0
  38. data/lib/mechanize/form.rb +364 -0
  39. data/lib/mechanize/form/button.rb +7 -0
  40. data/lib/mechanize/form/check_box.rb +11 -0
  41. data/lib/mechanize/form/field.rb +26 -0
  42. data/lib/mechanize/form/file_upload.rb +22 -0
  43. data/lib/mechanize/form/image_button.rb +21 -0
  44. data/lib/mechanize/form/multi_select_list.rb +67 -0
  45. data/lib/mechanize/form/option.rb +49 -0
  46. data/lib/mechanize/form/radio_button.rb +36 -0
  47. data/lib/mechanize/form/select_list.rb +43 -0
  48. data/lib/mechanize/headers.rb +11 -0
  49. data/lib/mechanize/history.rb +65 -0
  50. data/lib/mechanize/inspect.rb +88 -0
  51. data/lib/mechanize/monkey_patch.rb +35 -0
  52. data/lib/mechanize/page.rb +279 -0
  53. data/lib/mechanize/page/base.rb +8 -0
  54. data/lib/mechanize/page/encoding.rb +61 -0
  55. data/lib/mechanize/page/frame.rb +20 -0
  56. data/lib/mechanize/page/link.rb +53 -0
  57. data/lib/mechanize/page/meta.rb +50 -0
  58. data/lib/mechanize/pluggable_parsers.rb +101 -0
  59. data/lib/mechanize/redirect_limit_reached_error.rb +16 -0
  60. data/lib/mechanize/redirect_not_get_or_head_error.rb +18 -0
  61. data/lib/mechanize/response_code_error.rb +22 -0
  62. data/lib/mechanize/unsupported_scheme_error.rb +8 -0
  63. data/lib/mechanize/util.rb +73 -0
  64. data/test/chain/test_argument_validator.rb +14 -0
  65. data/test/chain/test_auth_headers.rb +25 -0
  66. data/test/chain/test_custom_headers.rb +18 -0
  67. data/test/chain/test_header_resolver.rb +28 -0
  68. data/test/chain/test_parameter_resolver.rb +35 -0
  69. data/test/chain/test_request_resolver.rb +29 -0
  70. data/test/chain/test_response_reader.rb +24 -0
  71. data/test/data/htpasswd +1 -0
  72. data/test/data/server.crt +16 -0
  73. data/test/data/server.csr +12 -0
  74. data/test/data/server.key +15 -0
  75. data/test/data/server.pem +15 -0
  76. data/test/helper.rb +129 -0
  77. data/test/htdocs/alt_text.html +10 -0
  78. data/test/htdocs/bad_form_test.html +9 -0
  79. data/test/htdocs/button.jpg +0 -0
  80. data/test/htdocs/empty_form.html +6 -0
  81. data/test/htdocs/file_upload.html +26 -0
  82. data/test/htdocs/find_link.html +41 -0
  83. data/test/htdocs/form_multi_select.html +16 -0
  84. data/test/htdocs/form_multival.html +37 -0
  85. data/test/htdocs/form_no_action.html +18 -0
  86. data/test/htdocs/form_no_input_name.html +16 -0
  87. data/test/htdocs/form_select.html +16 -0
  88. data/test/htdocs/form_select_all.html +16 -0
  89. data/test/htdocs/form_select_none.html +17 -0
  90. data/test/htdocs/form_select_noopts.html +10 -0
  91. data/test/htdocs/form_set_fields.html +14 -0
  92. data/test/htdocs/form_test.html +188 -0
  93. data/test/htdocs/frame_test.html +30 -0
  94. data/test/htdocs/google.html +13 -0
  95. data/test/htdocs/iframe_test.html +16 -0
  96. data/test/htdocs/index.html +6 -0
  97. data/test/htdocs/link with space.html +5 -0
  98. data/test/htdocs/meta_cookie.html +11 -0
  99. data/test/htdocs/no_title_test.html +6 -0
  100. data/test/htdocs/relative/tc_relative_links.html +21 -0
  101. data/test/htdocs/tc_bad_charset.html +9 -0
  102. data/test/htdocs/tc_bad_links.html +5 -0
  103. data/test/htdocs/tc_base_link.html +8 -0
  104. data/test/htdocs/tc_blank_form.html +11 -0
  105. data/test/htdocs/tc_charset.html +6 -0
  106. data/test/htdocs/tc_checkboxes.html +19 -0
  107. data/test/htdocs/tc_encoded_links.html +5 -0
  108. data/test/htdocs/tc_follow_meta.html +8 -0
  109. data/test/htdocs/tc_form_action.html +48 -0
  110. data/test/htdocs/tc_links.html +19 -0
  111. data/test/htdocs/tc_no_attributes.html +16 -0
  112. data/test/htdocs/tc_pretty_print.html +17 -0
  113. data/test/htdocs/tc_radiobuttons.html +17 -0
  114. data/test/htdocs/tc_referer.html +10 -0
  115. data/test/htdocs/tc_relative_links.html +19 -0
  116. data/test/htdocs/tc_textarea.html +23 -0
  117. data/test/htdocs/test_bad_encoding.html +52 -0
  118. data/test/htdocs/unusual______.html +5 -0
  119. data/test/servlets.rb +365 -0
  120. data/test/ssl_server.rb +48 -0
  121. data/test/test_authenticate.rb +71 -0
  122. data/test/test_bad_links.rb +25 -0
  123. data/test/test_blank_form.rb +16 -0
  124. data/test/test_checkboxes.rb +61 -0
  125. data/test/test_content_type.rb +13 -0
  126. data/test/test_cookie_class.rb +338 -0
  127. data/test/test_cookie_jar.rb +362 -0
  128. data/test/test_cookies.rb +123 -0
  129. data/test/test_encoded_links.rb +20 -0
  130. data/test/test_errors.rb +49 -0
  131. data/test/test_follow_meta.rb +108 -0
  132. data/test/test_form_action.rb +52 -0
  133. data/test/test_form_as_hash.rb +61 -0
  134. data/test/test_form_button.rb +38 -0
  135. data/test/test_form_no_inputname.rb +15 -0
  136. data/test/test_forms.rb +577 -0
  137. data/test/test_frames.rb +25 -0
  138. data/test/test_get_headers.rb +73 -0
  139. data/test/test_gzipping.rb +22 -0
  140. data/test/test_hash_api.rb +45 -0
  141. data/test/test_history.rb +142 -0
  142. data/test/test_history_added.rb +16 -0
  143. data/test/test_html_unscape_forms.rb +39 -0
  144. data/test/test_if_modified_since.rb +20 -0
  145. data/test/test_keep_alive.rb +31 -0
  146. data/test/test_links.rb +127 -0
  147. data/test/test_mech.rb +289 -0
  148. data/test/test_mechanize_file.rb +72 -0
  149. data/test/test_meta.rb +65 -0
  150. data/test/test_multi_select.rb +106 -0
  151. data/test/test_no_attributes.rb +13 -0
  152. data/test/test_option.rb +18 -0
  153. data/test/test_page.rb +127 -0
  154. data/test/test_page_encoding.rb +298 -0
  155. data/test/test_pluggable_parser.rb +145 -0
  156. data/test/test_post_form.rb +34 -0
  157. data/test/test_pretty_print.rb +22 -0
  158. data/test/test_radiobutton.rb +75 -0
  159. data/test/test_redirect_limit_reached.rb +39 -0
  160. data/test/test_redirect_verb_handling.rb +43 -0
  161. data/test/test_referer.rb +39 -0
  162. data/test/test_relative_links.rb +40 -0
  163. data/test/test_request.rb +13 -0
  164. data/test/test_response_code.rb +52 -0
  165. data/test/test_save_file.rb +103 -0
  166. data/test/test_scheme.rb +63 -0
  167. data/test/test_select.rb +106 -0
  168. data/test/test_select_all.rb +15 -0
  169. data/test/test_select_none.rb +15 -0
  170. data/test/test_select_noopts.rb +16 -0
  171. data/test/test_set_fields.rb +44 -0
  172. data/test/test_ssl_server.rb +20 -0
  173. data/test/test_subclass.rb +14 -0
  174. data/test/test_textarea.rb +45 -0
  175. data/test/test_upload.rb +109 -0
  176. data/test/test_verbs.rb +25 -0
  177. metadata +320 -0
data/test/test_mech.rb ADDED
@@ -0,0 +1,289 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class TestMechMethods < Test::Unit::TestCase
4
+ def setup
5
+ @agent = Mechanize.new
6
+ end
7
+
8
+ def test_initialize_with_param
9
+ init_params = {:user_agent_alias => 'Windows IE 7', :auth => ['user', 'pass']}
10
+ agent_with_param = Mechanize.new(init_params)
11
+ @agent.user_agent_alias = 'Windows IE 7'
12
+ @agent.auth('user', 'pass')
13
+ assert_equal(@agent.user_agent, agent_with_param.user_agent)
14
+ assert_equal(@agent.pass, agent_with_param.pass)
15
+ end
16
+
17
+ def test_initialize_with_invalid_param_raises_error
18
+ init_params = {:invalid_init_param => 'Bad Data'}
19
+ assert_raises(RuntimeError, 'Invalid parameter: invalid_init_param'){
20
+ Mechanize.new(init_params)
21
+ }
22
+ end
23
+
24
+ def test_get_with_tilde
25
+ page = @agent.get('http://localhost/?foo=~2')
26
+ assert_equal('http://localhost/?foo=~2', page.uri.to_s)
27
+ end
28
+
29
+ def test_parser_can_be_set
30
+ @agent.html_parser = {}
31
+ assert_raises(NoMethodError) {
32
+ @agent.get('http://localhost/?foo=~2').links
33
+ }
34
+ end
35
+
36
+ def test_submit_takes_arbirary_headers
37
+ page = @agent.get('http://localhost:2000/form_no_action.html')
38
+ assert form = page.forms.first
39
+ form.action = '/http_headers'
40
+ page = @agent.submit(form, nil, { 'foo' => 'bar' })
41
+ headers = Hash[*(
42
+ page.body.split("\n").map { |x| x.split('|') }.flatten
43
+ )]
44
+ assert_equal 'bar', headers['foo']
45
+ end
46
+
47
+ def test_get_with_params
48
+ page = @agent.get('http://localhost/', { :q => 'hello' })
49
+ assert_equal('http://localhost/?q=hello', page.uri.to_s)
50
+ end
51
+
52
+ def test_get_with_upper_http
53
+ page = @agent.get('HTTP://localhost/', { :q => 'hello' })
54
+ assert_equal('HTTP://localhost/?q=hello', page.uri.to_s)
55
+ end
56
+
57
+ def test_get_no_referer
58
+ requests = []
59
+ @agent.pre_connect_hooks << lambda { |params|
60
+ requests << params[:request]
61
+ }
62
+
63
+ @agent.get('http://localhost/')
64
+ @agent.get('http://localhost/')
65
+ assert_equal(2, requests.length)
66
+ requests.each do |request|
67
+ assert_nil request['referer']
68
+ end
69
+ end
70
+
71
+ def test_with_anchor
72
+ page = @agent.get('http://localhost/?foo=bar&#34;')
73
+ assert_equal('http://localhost/?foo=bar%22', page.uri.to_s)
74
+ end
75
+
76
+ def test_post_connect_hook_gets_called
77
+ response = nil
78
+ @agent.post_connect_hooks << lambda { |params|
79
+ response = params[:response]
80
+ }
81
+
82
+ @agent.get('http://localhost/')
83
+ assert(response)
84
+ end
85
+
86
+ def test_get_with_referer
87
+ request = nil
88
+ @agent.pre_connect_hooks << lambda { |params|
89
+ request = params[:request]
90
+ }
91
+
92
+ @agent.get('http://localhost/', URI.parse('http://google.com/'))
93
+ assert_equal 'http://google.com/', request['Referer']
94
+
95
+ @agent.get('http://localhost/', [], 'http://tenderlovemaking.com/')
96
+ assert_equal 'http://tenderlovemaking.com/', request['Referer']
97
+ end
98
+
99
+ def test_get_with_file_referer
100
+ assert_nothing_raised do
101
+ @agent.get('http://localhost', [], Mechanize::File.new(URI.parse('http://tenderlovemaking.com/crossdomain.xml')))
102
+ end
103
+ end
104
+
105
+ def test_weird_url
106
+ assert_nothing_raised {
107
+ @agent.get('http://localhost/?action=bing&bang=boom=1|a=|b=|c=')
108
+ }
109
+ assert_nothing_raised {
110
+ @agent.get('http://localhost/?a=b&#038;b=c&#038;c=d')
111
+ }
112
+ assert_nothing_raised {
113
+ @agent.get("http://localhost/?a=#{[0xd6].pack('U')}")
114
+ }
115
+ end
116
+
117
+ unless RUBY_VERSION >= '1.9.0'
118
+ def test_kcode_url
119
+ $KCODE = 'u'
120
+ page = @agent.get("http://localhost/?a=#{[0xd6].pack('U')}")
121
+ assert_not_nil(page)
122
+ assert_equal('http://localhost/?a=%D6', page.uri.to_s)
123
+ $KCODE = 'NONE'
124
+ end
125
+ end
126
+
127
+ def test_history
128
+ 0.upto(25) do |i|
129
+ assert_equal(i, @agent.history.size)
130
+ page = @agent.get("http://localhost/")
131
+ end
132
+ page = @agent.get("http://localhost/form_test.html")
133
+
134
+ assert_equal("http://localhost/form_test.html",
135
+ @agent.history.last.uri.to_s)
136
+ assert_equal("http://localhost/",
137
+ @agent.history[-2].uri.to_s)
138
+ assert_equal("http://localhost/",
139
+ @agent.history[-2].uri.to_s)
140
+
141
+ assert_equal("http://localhost/form_test.html",
142
+ @agent.visited_page(-1).uri.to_s)
143
+ assert_equal("http://localhost/",
144
+ @agent.visited_page(-2).uri.to_s)
145
+
146
+ assert_equal(true, @agent.visited?("http://localhost/"))
147
+ assert_equal(true, @agent.visited?("/form_test.html"))
148
+ assert_equal(false, @agent.visited?("http://google.com/"))
149
+ assert_equal(true, @agent.visited?(page.links.first))
150
+
151
+ end
152
+
153
+ def test_visited
154
+ @agent.get("http://localhost/content_type_test?ct=application/pdf")
155
+ assert_equal(true,
156
+ @agent.visited?("http://localhost/content_type_test?ct=application/pdf"))
157
+ assert_equal(false,
158
+ @agent.visited?("http://localhost/content_type_test"))
159
+ assert_equal(false,
160
+ @agent.visited?("http://localhost/content_type_test?ct=text/html"))
161
+ end
162
+
163
+ def test_visited_after_redirect
164
+ @agent.get("http://localhost/response_code?code=302")
165
+ assert_equal("http://localhost/index.html",
166
+ @agent.current_page.uri.to_s)
167
+ assert_equal(true,
168
+ @agent.visited?('http://localhost/response_code?code=302'))
169
+ end
170
+
171
+ def test_max_history
172
+ @agent.max_history = 10
173
+ 0.upto(10) do |i|
174
+ assert_equal(i, @agent.history.size)
175
+ page = @agent.get("http://localhost/")
176
+ end
177
+
178
+ 0.upto(10) do |i|
179
+ assert_equal(10, @agent.history.size)
180
+ page = @agent.get("http://localhost/")
181
+ end
182
+ end
183
+
184
+ def test_max_history_order
185
+ @agent.max_history = 2
186
+ assert_equal(0, @agent.history.length)
187
+
188
+ @agent.get('http://localhost/form_test.html')
189
+ assert_equal(1, @agent.history.length)
190
+
191
+ @agent.get('http://localhost/empty_form.html')
192
+ assert_equal(2, @agent.history.length)
193
+
194
+ @agent.get('http://localhost/tc_checkboxes.html')
195
+ assert_equal(2, @agent.history.length)
196
+ assert_equal('http://localhost/empty_form.html', @agent.history[0].uri.to_s)
197
+ assert_equal('http://localhost/tc_checkboxes.html',
198
+ @agent.history[1].uri.to_s)
199
+ end
200
+
201
+ def test_back_button
202
+ 0.upto(5) do |i|
203
+ assert_equal(i, @agent.history.size)
204
+ page = @agent.get("http://localhost/")
205
+ end
206
+ page = @agent.get("http://localhost/form_test.html")
207
+
208
+ assert_equal("http://localhost/form_test.html",
209
+ @agent.history.last.uri.to_s)
210
+ assert_equal("http://localhost/",
211
+ @agent.history[-2].uri.to_s)
212
+
213
+ assert_equal(7, @agent.history.size)
214
+ @agent.back
215
+ assert_equal(6, @agent.history.size)
216
+ assert_equal("http://localhost/",
217
+ @agent.history.last.uri.to_s)
218
+ end
219
+
220
+ def test_google
221
+ page = @agent.get("http://localhost/google.html")
222
+ search = page.forms.find { |f| f.name == "f" }
223
+ assert_not_nil(search)
224
+ assert_not_nil(search.field_with(:name => 'q'))
225
+ assert_not_nil(search.field_with(:name => 'hl'))
226
+ assert_not_nil(search.fields.find { |f| f.name == 'ie' })
227
+ end
228
+
229
+ def test_click
230
+ @agent.user_agent_alias = 'Mac Safari'
231
+ page = @agent.get("http://localhost/frame_test.html")
232
+ link = page.link_with(:text => "Form Test")
233
+ assert_not_nil(link)
234
+ page = @agent.click(link)
235
+ assert_equal("http://localhost/form_test.html",
236
+ @agent.history.last.uri.to_s)
237
+ end
238
+
239
+ def test_click_hpricot
240
+ page = @agent.get("http://localhost/frame_test.html")
241
+
242
+ link = (page/"//a[@class='bar']").first
243
+ assert_not_nil(link)
244
+ page = @agent.click(link)
245
+ assert_equal("http://localhost/form_test.html",
246
+ @agent.history.last.uri.to_s)
247
+ end
248
+
249
+ def test_click_hpricot_frame
250
+ page = @agent.get("http://localhost/frame_test.html")
251
+
252
+ link = (page/"//frame[@name='frame2']").first
253
+ assert_not_nil(link)
254
+ page = @agent.click(link)
255
+ assert_equal("http://localhost/form_test.html",
256
+ @agent.history.last.uri.to_s)
257
+ end
258
+
259
+ def test_new_find
260
+ page = @agent.get("http://localhost/frame_test.html")
261
+ assert_equal(3, page.frames.size)
262
+
263
+ find_orig = page.frames.find_all { |f| f.name == 'frame1' }
264
+ find1 = page.frames_with(:name => 'frame1')
265
+
266
+ find_orig.zip(find1).each { |a,b|
267
+ assert_equal(a, b)
268
+ }
269
+ end
270
+
271
+ def test_get_file
272
+ page = @agent.get("http://localhost/frame_test.html")
273
+ content_length = page.header['Content-Length']
274
+ page_as_string = @agent.get_file("http://localhost/frame_test.html")
275
+ assert_equal(content_length.to_i, page_as_string.length.to_i)
276
+ end
277
+
278
+ def test_transact
279
+ page = @agent.get("http://localhost/frame_test.html")
280
+ assert_equal(1, @agent.history.length)
281
+ @agent.transact { |a|
282
+ 5.times {
283
+ @agent.get("http://localhost/frame_test.html")
284
+ }
285
+ assert_equal(6, @agent.history.length)
286
+ }
287
+ assert_equal(1, @agent.history.length)
288
+ end
289
+ end
@@ -0,0 +1,72 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+
3
+ class MechanizeFileTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @agent = Mechanize.new
7
+ @agent.use_content_disposition = true
8
+ end
9
+
10
+ def test_content_disposition
11
+ file = Mechanize::File.new(
12
+ URI.parse('http://localhost/foo'),
13
+ { 'content-disposition' => 'attachment; filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"', },
14
+ '',
15
+ '200',
16
+ @agent
17
+ )
18
+ assert_equal('genome.jpeg', file.filename)
19
+
20
+ file = Mechanize::File.new(
21
+ URI.parse('http://localhost/foo'),
22
+ { 'content-disposition' => 'filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"', },
23
+ '',
24
+ '200',
25
+ @agent
26
+ )
27
+ assert_equal('genome.jpeg', file.filename)
28
+
29
+ file = Mechanize::File.new(
30
+ URI.parse('http://localhost/foo'),
31
+ { 'content-disposition' => 'filename=genome.jpeg', },
32
+ '',
33
+ '200',
34
+ @agent
35
+ )
36
+ assert_equal('genome.jpeg', file.filename)
37
+ end
38
+
39
+ def test_from_uri
40
+ file = Mechanize::File.new(
41
+ URI.parse('http://localhost/foo'),
42
+ {},
43
+ '',
44
+ '200',
45
+ @agent
46
+ )
47
+ assert_equal('foo.html', file.filename)
48
+
49
+ file = Mechanize::File.new(
50
+ URI.parse('http://localhost/foo.jpg'),
51
+ {},
52
+ '',
53
+ '200',
54
+ @agent
55
+ )
56
+ assert_equal('foo.jpg', file.filename)
57
+
58
+ file = Mechanize::File.new(
59
+ URI.parse('http://localhost/foo.jpg'),
60
+ nil,
61
+ '',
62
+ '200',
63
+ @agent
64
+ )
65
+ assert_equal('foo.jpg', file.filename)
66
+ end
67
+
68
+ def test_no_uri
69
+ file = Mechanize::File.new()
70
+ assert_equal('index.html', file.filename)
71
+ end
72
+ 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 = 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 = 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