mechanize 2.14.1 → 2.14.2

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.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -1
  3. data/lib/mechanize/directory_saver.rb +2 -1
  4. data/lib/mechanize/file_saver.rb +2 -0
  5. data/lib/mechanize/parser.rb +33 -3
  6. data/lib/mechanize/test_case.rb +2 -9
  7. data/lib/mechanize/version.rb +1 -1
  8. metadata +3 -172
  9. data/.autotest +0 -6
  10. data/.github/dependabot.yml +0 -11
  11. data/.github/workflows/ci.yml +0 -43
  12. data/.github/workflows/upstream.yml +0 -51
  13. data/.gitignore +0 -15
  14. data/.yardopts +0 -8
  15. data/Gemfile +0 -12
  16. data/Rakefile +0 -56
  17. data/examples/flickr_upload.rb +0 -22
  18. data/examples/latest_user_agents.rb +0 -100
  19. data/examples/mech-dump.rb +0 -5
  20. data/examples/proxy_req.rb +0 -7
  21. data/examples/rubygems.rb +0 -23
  22. data/examples/spider.rb +0 -22
  23. data/examples/wikipedia_links_to_philosophy.rb +0 -158
  24. data/mechanize.gemspec +0 -74
  25. data/test/data/htpasswd +0 -1
  26. data/test/data/server.crt +0 -16
  27. data/test/data/server.csr +0 -12
  28. data/test/data/server.key +0 -15
  29. data/test/data/server.pem +0 -15
  30. data/test/test_mechanize.rb +0 -1382
  31. data/test/test_mechanize_cookie.rb +0 -559
  32. data/test/test_mechanize_cookie_jar.rb +0 -615
  33. data/test/test_mechanize_directory_saver.rb +0 -60
  34. data/test/test_mechanize_download.rb +0 -102
  35. data/test/test_mechanize_element_not_found_error.rb +0 -16
  36. data/test/test_mechanize_file.rb +0 -118
  37. data/test/test_mechanize_file_connection.rb +0 -40
  38. data/test/test_mechanize_file_request.rb +0 -26
  39. data/test/test_mechanize_file_response.rb +0 -48
  40. data/test/test_mechanize_file_saver.rb +0 -22
  41. data/test/test_mechanize_form.rb +0 -1025
  42. data/test/test_mechanize_form_check_box.rb +0 -49
  43. data/test/test_mechanize_form_encoding.rb +0 -110
  44. data/test/test_mechanize_form_field.rb +0 -78
  45. data/test/test_mechanize_form_file_upload.rb +0 -21
  46. data/test/test_mechanize_form_image_button.rb +0 -13
  47. data/test/test_mechanize_form_keygen.rb +0 -34
  48. data/test/test_mechanize_form_multi_select_list.rb +0 -98
  49. data/test/test_mechanize_form_option.rb +0 -60
  50. data/test/test_mechanize_form_radio_button.rb +0 -96
  51. data/test/test_mechanize_form_select_list.rb +0 -81
  52. data/test/test_mechanize_form_textarea.rb +0 -53
  53. data/test/test_mechanize_headers.rb +0 -36
  54. data/test/test_mechanize_history.rb +0 -104
  55. data/test/test_mechanize_http_agent.rb +0 -2145
  56. data/test/test_mechanize_http_auth_challenge.rb +0 -64
  57. data/test/test_mechanize_http_auth_realm.rb +0 -56
  58. data/test/test_mechanize_http_auth_store.rb +0 -233
  59. data/test/test_mechanize_http_content_disposition_parser.rb +0 -167
  60. data/test/test_mechanize_http_www_authenticate_parser.rb +0 -204
  61. data/test/test_mechanize_image.rb +0 -9
  62. data/test/test_mechanize_link.rb +0 -171
  63. data/test/test_mechanize_page.rb +0 -295
  64. data/test/test_mechanize_page_encoding.rb +0 -215
  65. data/test/test_mechanize_page_frame.rb +0 -17
  66. data/test/test_mechanize_page_image.rb +0 -191
  67. data/test/test_mechanize_page_link.rb +0 -377
  68. data/test/test_mechanize_page_meta_refresh.rb +0 -151
  69. data/test/test_mechanize_parser.rb +0 -316
  70. data/test/test_mechanize_pluggable_parser.rb +0 -68
  71. data/test/test_mechanize_redirect_limit_reached_error.rb +0 -25
  72. data/test/test_mechanize_redirect_not_get_or_head_error.rb +0 -15
  73. data/test/test_mechanize_response_read_error.rb +0 -29
  74. data/test/test_mechanize_subclass.rb +0 -23
  75. data/test/test_mechanize_util.rb +0 -144
  76. data/test/test_mechanize_xml_file.rb +0 -30
  77. data/test/test_multi_select.rb +0 -120
@@ -1,1382 +0,0 @@
1
- # coding: utf-8
2
- # frozen_string_literal: true
3
-
4
- require 'mechanize/test_case'
5
-
6
- class TestMechanize < Mechanize::TestCase
7
-
8
- def setup
9
- super
10
-
11
- @uri = URI 'http://example/'
12
- @req = Net::HTTP::Get.new '/'
13
-
14
- @res = Net::HTTPOK.allocate
15
- @res.instance_variable_set :@code, 200
16
- @res.instance_variable_set :@header, {}
17
- end
18
-
19
- def test_back
20
- 0.upto(5) do |i|
21
- assert_equal(i, @mech.history.size)
22
- @mech.get("http://localhost/")
23
- end
24
- @mech.get("http://localhost/form_test.html")
25
-
26
- assert_equal("http://localhost/form_test.html",
27
- @mech.history.last.uri.to_s)
28
- assert_equal("http://localhost/",
29
- @mech.history[-2].uri.to_s)
30
-
31
- assert_equal(7, @mech.history.size)
32
- @mech.back
33
- assert_equal(6, @mech.history.size)
34
- assert_equal("http://localhost/",
35
- @mech.history.last.uri.to_s)
36
- end
37
-
38
- def test_basic_auth
39
- _, err = capture_io do
40
- @mech.basic_auth 'user', 'pass' # warns
41
- end
42
-
43
- line = __LINE__ - 3
44
- file = File.basename __FILE__
45
-
46
- assert_match "#{file} line #{line}", err
47
-
48
- page = @mech.get @uri + '/basic_auth'
49
- assert_equal 'You are authenticated', page.body
50
- end
51
-
52
- def test_cert_key_file
53
- in_tmpdir do
54
- open 'key.pem', 'w' do |io| io.write ssl_private_key.to_pem end
55
- open 'cert.pem', 'w' do |io| io.write ssl_certificate.to_pem end
56
-
57
- mech = Mechanize.new do |a|
58
- a.cert = 'cert.pem'
59
- a.key = 'key.pem'
60
- end
61
-
62
- # Certificate#== seems broken
63
- assert_equal ssl_certificate.to_pem, mech.certificate.to_pem
64
- end
65
- end
66
-
67
- def test_cert_key_object
68
- mech = Mechanize.new do |a|
69
- a.cert = ssl_certificate
70
- a.key = ssl_private_key
71
- end
72
-
73
- assert_equal ssl_certificate, mech.certificate
74
- assert_equal ssl_certificate, mech.cert
75
- assert_equal ssl_private_key, mech.key
76
- end
77
-
78
- def test_cert_store
79
- assert_nil @mech.cert_store
80
-
81
- store = OpenSSL::X509::Store.new
82
- @mech.cert_store = store
83
-
84
- assert_equal store, @mech.cert_store
85
- end
86
-
87
- def test_click
88
- @mech.user_agent_alias = 'Mac Safari'
89
- page = @mech.get("http://localhost/frame_test.html")
90
- link = page.link_with(:text => "Form Test")
91
-
92
- page = @mech.click(link)
93
-
94
- assert_equal("http://localhost/form_test.html",
95
- @mech.history.last.uri.to_s)
96
- end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse body of NOFRAMES
97
-
98
- def test_click_bogus_link_with_cookies
99
- @mech.cookie_jar = cookie_jar("a=b")
100
-
101
- page = html_page <<-BODY
102
- <a href="http:///index.html">yes really</a>
103
- BODY
104
-
105
- page.links[0].click
106
-
107
- assert_equal '/index.html', requests.first.path
108
- end
109
-
110
- def test_click_image_button
111
- page = @mech.get("http://localhost/form_test.html")
112
- get_form = page.forms.find { |f| f.name == "get_form1" }
113
- image_button = get_form.buttons.first
114
- new_page = @mech.click(image_button)
115
- assert_equal "http://localhost/form_post?first_name=&button.x=0&button.y=0", new_page.uri.to_s
116
- end
117
-
118
- def test_click_submit_button
119
- page = @mech.get("http://localhost/form_test.html")
120
- get_form = page.forms.find { |f| f.name == "get_form1" }
121
- submit_button = get_form.submits.first
122
- new_page = @mech.click(submit_button)
123
- assert_equal "http://localhost/form_post?first_name=", new_page.uri.to_s
124
- end
125
-
126
- def test_click_frame
127
- frame = node 'frame', 'src' => '/index.html'
128
- frame = Mechanize::Page::Frame.new frame, @mech, fake_page
129
-
130
- @mech.click frame
131
-
132
- assert_equal '/index.html', requests.first.path
133
- end
134
-
135
- def test_click_frame_hpricot_style
136
- page = @mech.get("http://localhost/frame_test.html")
137
- link = (page/"//frame[@name='frame2']").first
138
-
139
- page = @mech.click(link)
140
-
141
- assert_equal("http://localhost/form_test.html",
142
- @mech.history.last.uri.to_s)
143
- end
144
-
145
- def test_click_hpricot_style # HACK move to test_divide in Page
146
- page = @mech.get("http://localhost/frame_test.html")
147
- link = (page/"//a[@class='bar']").first
148
-
149
- page = @mech.click(link)
150
-
151
- assert_equal("http://localhost/form_test.html",
152
- @mech.history.last.uri.to_s)
153
- end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse body of NOFRAMES
154
-
155
- def test_click_link
156
- link = node 'a', 'href' => '/index.html'
157
- link = Mechanize::Page::Link.new link, @mech, fake_page
158
-
159
- @mech.click link
160
-
161
- assert_equal '/index.html', requests.first.path
162
- assert_equal 'http://fake.example/', requests.first['Referer']
163
- end
164
-
165
- def test_click_link_nofollow
166
- page = html_page <<-BODY
167
- <meta name="ROBOTS" content="nofollow">
168
-
169
- <p>Do not follow <a href="/index.html">this</a> or <a href="/">this</a>!
170
- BODY
171
-
172
- page.links[0].click
173
- page.links[1].click
174
-
175
- @mech.robots = true
176
-
177
- assert_raises Mechanize::RobotsDisallowedError do
178
- page.links[0].click
179
- end
180
-
181
- assert_raises Mechanize::RobotsDisallowedError do
182
- page.links[1].click
183
- end
184
- end
185
-
186
- def test_click_link_noreferrer
187
- link = node 'a', 'href' => '/index.html', 'rel' => 'noreferrer'
188
- link = Mechanize::Page::Link.new link, @mech, fake_page
189
-
190
- @mech.click link
191
-
192
- assert_nil requests.first['referer']
193
- end
194
-
195
- def test_click_link_rel_nofollow
196
- page = html_page <<-BODY
197
- <p>You can follow <a href="/index.html">this link</a>
198
- but not <a href="/" rel="me nofollow">this</a>!
199
- BODY
200
-
201
- page.links[0].click
202
- page.links[1].click
203
-
204
- @mech.robots = true
205
-
206
- page.links[0].click
207
-
208
- assert_raises Mechanize::RobotsDisallowedError do
209
- page.links[1].click
210
- end
211
- end
212
-
213
- def test_click_link_parent
214
- page = page URI 'http://example/a/index.html'
215
- link = node 'a', 'href' => '../index.html'
216
- link = Mechanize::Page::Link.new link, @mech, page
217
-
218
- @mech.click link
219
-
220
- assert_equal '/index.html', requests.first.path
221
- end
222
-
223
- def test_click_link_parent_extra
224
- page = page URI 'http://example/a/index.html'
225
- link = node 'a', 'href' => '../../index.html'
226
- link = Mechanize::Page::Link.new link, @mech, page
227
-
228
- @mech.click link
229
-
230
- assert_equal '/index.html', requests.first.path
231
- end
232
-
233
- def test_click_link_hpricot_style # HACK move to test_search in Page
234
- page = @mech.get("http://localhost/tc_encoded_links.html")
235
-
236
- page = @mech.click(page.search('a').first)
237
-
238
- assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
239
- end
240
-
241
- def test_click_link_query
242
- page = @mech.get("http://localhost/tc_encoded_links.html")
243
- link = page.links.first
244
- assert_equal('/form_post?a=b&b=c', link.href)
245
-
246
- page = @mech.click(link)
247
-
248
- assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
249
- end
250
-
251
- def test_click_link_space
252
- page = @mech.get("http://localhost/tc_bad_links.html")
253
-
254
- @mech.click page.links.first
255
-
256
- assert_match(/alt_text.html$/, @mech.history.last.uri.to_s)
257
- assert_equal(2, @mech.history.length)
258
- end
259
-
260
- def test_click_more
261
- @mech.get 'http://localhost/test_click.html'
262
- @mech.click 'A Button'
263
- assert_equal 'http://localhost/frame_test.html?words=nil',
264
- @mech.page.uri.to_s
265
- @mech.back
266
- @mech.click 'A Link'
267
- assert_equal 'http://localhost/index.html',
268
- @mech.page.uri.to_s
269
- @mech.back
270
- @mech.click @mech.page.link_with(:text => 'A Link')
271
- assert_equal 'http://localhost/index.html',
272
- @mech.page.uri.to_s
273
- end
274
-
275
- def test_cookies
276
- uri = URI 'http://example'
277
- jar = HTTP::CookieJar.new
278
- jar.parse 'a=b', uri
279
-
280
- @mech.cookie_jar = jar
281
-
282
- refute_empty @mech.cookies
283
- end
284
-
285
- def test_cookie_jar
286
- assert_kind_of Mechanize::CookieJar, @mech.cookie_jar
287
-
288
- jar = HTTP::CookieJar.new
289
-
290
- @mech.cookie_jar = jar
291
-
292
- assert_equal jar, @mech.cookie_jar
293
- end
294
-
295
- def test_delete
296
- page = @mech.delete('http://localhost/verb', { 'q' => 'foo' })
297
- assert_equal 1, @mech.history.length
298
- assert_equal 'DELETE', page.header['X-Request-Method']
299
- end
300
-
301
- def test_delete_redirect
302
- page = @mech.delete('http://localhost/redirect')
303
-
304
- assert_equal(page.uri.to_s, 'http://localhost/verb')
305
-
306
- assert_equal 'GET', page.header['X-Request-Method']
307
- end
308
-
309
- def test_download
310
- page = nil
311
-
312
- in_tmpdir do
313
- open 'download', 'w' do |io|
314
- page = @mech.download 'http://example', io
315
-
316
- refute io.closed?
317
- end
318
-
319
- assert_operator 1, :<=, File.stat('download').size
320
- end
321
-
322
- assert_empty @mech.history
323
- assert_kind_of Mechanize::Page, page
324
- end
325
-
326
- def test_download_filename
327
- page = nil
328
-
329
- in_tmpdir do
330
- page = @mech.download 'http://example', 'download'
331
-
332
- assert_operator 1, :<=, File.stat('download').size
333
- end
334
-
335
- assert_empty @mech.history
336
- assert_kind_of Mechanize::Page, page
337
- end
338
-
339
- def test_download_filename_error
340
- in_tmpdir do
341
- assert_raises Mechanize::UnauthorizedError do
342
- @mech.download 'http://example/digest_auth', 'download'
343
- end
344
-
345
- refute File.exist? 'download'
346
- end
347
- end
348
-
349
- def test_download_does_not_allow_command_injection
350
- skip if windows?
351
- in_tmpdir do
352
- @mech.download('http://example', '| ruby -rfileutils -e \'FileUtils.touch("vul.txt")\'')
353
-
354
- refute_operator(File, :exist?, "vul.txt")
355
- end
356
- end
357
-
358
- def test_get
359
- uri = URI 'http://localhost'
360
-
361
- page = @mech.get uri, { :q => 'h' }, 'http://example', { 'X-H' => 'v' }
362
-
363
- assert_equal URI('http://localhost/?q=h'), page.uri
364
- assert_equal URI('http://localhost'), uri
365
- end
366
-
367
- def test_get_HTTP
368
- page = @mech.get('HTTP://localhost/', { :q => 'hello' })
369
-
370
- assert_kind_of URI::HTTP, page.uri
371
- assert_equal 'localhost', page.uri.host
372
- assert_equal 80, page.uri.port
373
- assert_equal '/?q=hello', page.uri.request_uri
374
- end
375
-
376
- def test_get_anchor
377
- page = @mech.get('http://localhost/?foo=bar&#34;')
378
- assert_equal('http://localhost/?foo=bar%22', page.uri.to_s)
379
- end
380
-
381
- def test_get_bad_url
382
- assert_raises ArgumentError do
383
- @mech.get '/foo.html'
384
- end
385
- end
386
-
387
- def test_get_auth
388
- @mech.add_auth @uri, 'user', 'pass'
389
-
390
- page = @mech.get @uri + '/basic_auth'
391
-
392
- assert_equal 'You are authenticated', page.body
393
- end
394
-
395
- def test_get_auth_bad
396
- @mech.add_auth(@uri, 'aaron', 'aaron')
397
-
398
- e = assert_raises Mechanize::UnauthorizedError do
399
- @mech.get(@uri + '/basic_auth')
400
- end
401
-
402
- assert_equal("401", e.response_code)
403
- end
404
-
405
- def test_get_auth_none
406
- e = assert_raises Mechanize::UnauthorizedError do
407
- @mech.get(@uri + '/basic_auth')
408
- end
409
-
410
- assert_equal("401", e.response_code)
411
- end
412
-
413
- def test_get_auth_realm
414
- @mech.add_auth @uri, 'user', 'pass', 'Blah'
415
-
416
- page = @mech.get @uri + '/basic_auth'
417
-
418
- assert_equal 'You are authenticated', page.body
419
- end
420
-
421
- def test_get_conditional
422
- assert_empty @mech.history
423
-
424
- page = @mech.get 'http://localhost/if_modified_since'
425
- assert_match(/You did not send/, page.body)
426
-
427
- assert_equal 1, @mech.history.length
428
- page2 = @mech.get 'http://localhost/if_modified_since'
429
-
430
- assert_equal 2, @mech.history.length
431
- assert_equal page.object_id, page2.object_id
432
- end
433
-
434
- def test_get_digest_auth
435
- @mech.add_auth @uri, 'user', 'pass'
436
-
437
- page = @mech.get @uri + '/digest_auth'
438
-
439
- assert_equal 'You are authenticated', page.body
440
- end
441
-
442
- def test_get_follow_meta_refresh
443
- @mech.follow_meta_refresh = true
444
-
445
- page = @mech.get('http://localhost/tc_follow_meta.html')
446
-
447
- assert_equal(2, @mech.history.length)
448
-
449
- assert_equal('http://localhost/tc_follow_meta.html',
450
- @mech.history.first.uri.to_s)
451
- assert_equal('http://localhost/index.html', page.uri.to_s)
452
- assert_equal('http://localhost/index.html', @mech.history.last.uri.to_s)
453
-
454
- [5, 6].each { |limit|
455
- @mech.redirection_limit = limit
456
- begin
457
- @mech.get('http://localhost/tc_follow_meta_loop_1.html')
458
- rescue => e
459
- assert_instance_of Mechanize::RedirectLimitReachedError, e
460
- assert_equal limit, e.redirects
461
- if limit % 2 == 0
462
- assert_equal '/tc_follow_meta_loop_1.html', e.page.uri.path
463
- else
464
- assert_equal '/tc_follow_meta_loop_2.html', e.page.uri.path
465
- end
466
- end
467
- }
468
- end
469
-
470
- def test_get_follow_meta_refresh_anywhere
471
- @mech.follow_meta_refresh = :anywhere
472
-
473
- @mech.get('http://localhost/tc_meta_in_body.html')
474
- assert_equal 2, requests.length
475
- end
476
-
477
- def test_get_follow_meta_refresh_disabled
478
- page = @mech.get('http://localhost/tc_follow_meta.html')
479
- assert_equal('http://localhost/tc_follow_meta.html', page.uri.to_s)
480
- assert_equal(1, page.meta_refresh.length)
481
- end
482
-
483
- def test_get_follow_meta_refresh_empty_url
484
- @mech.follow_meta_refresh = true
485
- @mech.follow_meta_refresh_self = true
486
-
487
- page = @mech.get('http://example/refresh_with_empty_url')
488
-
489
- assert_equal(3, @mech.history.length)
490
- assert_equal('http://example/refresh_with_empty_url',
491
- @mech.history[0].uri.to_s)
492
- assert_equal('http://example/refresh_with_empty_url',
493
- @mech.history[1].uri.to_s)
494
- assert_equal('http://example/', page.uri.to_s)
495
- assert_equal('http://example/', @mech.history.last.uri.to_s)
496
- end
497
-
498
- def test_get_follow_meta_refresh_in_body
499
- @mech.follow_meta_refresh = true
500
-
501
- @mech.get('http://localhost/tc_meta_in_body.html')
502
- assert_equal 1, requests.length
503
- end
504
-
505
- def test_get_follow_meta_refresh_no_url
506
- @mech.follow_meta_refresh = true
507
- @mech.follow_meta_refresh_self = true
508
-
509
- page = @mech.get('http://example/refresh_without_url')
510
-
511
- assert_equal(3, @mech.history.length)
512
- assert_equal('http://example/refresh_without_url',
513
- @mech.history[0].uri.to_s)
514
- assert_equal('http://example/refresh_without_url',
515
- @mech.history[1].uri.to_s)
516
- assert_equal('http://example/', page.uri.to_s)
517
- assert_equal('http://example/', @mech.history.last.uri.to_s)
518
- end
519
-
520
- def test_get_follow_meta_refresh_referer_not_sent
521
- @mech.follow_meta_refresh = true
522
-
523
- @mech.get('http://localhost/tc_follow_meta.html')
524
-
525
- assert_equal 2, @mech.history.length
526
- assert_nil requests.last['referer']
527
- end
528
-
529
- def test_get_referer_download
530
- download = Mechanize::Download.new URI 'http://example/prev'
531
-
532
- uri = URI 'http://example'
533
-
534
- page = @mech.get uri, { :q => 'h' }, download, { 'X-H' => 'v' }
535
-
536
- assert_equal URI('http://example/?q=h'), page.uri
537
- assert_equal URI('http://example'), uri
538
-
539
- assert_equal 'http://example/prev', requests.first['referer']
540
- end
541
-
542
- def test_get_robots
543
- @mech.robots = true
544
-
545
- assert_equal "Page Title", @mech.get("http://localhost/index.html").title
546
-
547
- assert_raises Mechanize::RobotsDisallowedError do
548
- @mech.get "http://localhost/norobots.html"
549
- end
550
- end
551
-
552
- def test_follow_meta_refresh_self
553
- refute @mech.agent.follow_meta_refresh_self
554
-
555
- @mech.follow_meta_refresh_self = true
556
-
557
- assert @mech.agent.follow_meta_refresh_self
558
- end
559
-
560
- def test_get_gzip
561
- page = @mech.get("http://localhost/gzip?file=index.html")
562
-
563
- assert_kind_of(Mechanize::Page, page)
564
-
565
- assert_match('Hello World', page.body)
566
- end
567
-
568
- def test_content_encoding_hooks_header
569
- h = {'X-ResponseContentEncoding' => 'agzip'}
570
-
571
- # test of X-ResponseContentEncoding feature
572
- assert_raises(Mechanize::Error, 'Unsupported Content-Encoding: agzip') do
573
- @mech.get("http://localhost/gzip?file=index.html", nil, nil, h)
574
- end
575
-
576
- @mech.content_encoding_hooks << lambda{|agent, uri, response, response_body_io|
577
- response['content-encoding'] = 'gzip' if response['content-encoding'] == 'agzip'}
578
-
579
- page = @mech.get("http://localhost/gzip?file=index.html", nil, nil, h)
580
-
581
- assert_match('Hello World', page.body)
582
- end
583
-
584
- def external_cmd(io); Zlib::GzipReader.new(io).read; end
585
-
586
- def test_content_encoding_hooks_body_io
587
- h = {'X-ResponseContentEncoding' => 'unsupported_content_encoding'}
588
-
589
- @mech.content_encoding_hooks << lambda{|agent, uri, response, response_body_io|
590
- if response['content-encoding'] == 'unsupported_content_encoding'
591
- response['content-encoding'] = 'none'
592
- response_body_io.string = external_cmd(response_body_io)
593
- end}
594
-
595
- page = @mech.get("http://localhost/gzip?file=index.html", nil, nil, h)
596
-
597
- assert_match('Hello World', page.body)
598
- end
599
-
600
- def test_get_http_refresh
601
- @mech.follow_meta_refresh = true
602
-
603
- page = @mech.get('http://example/http_refresh?refresh_time=0')
604
-
605
- assert_equal('http://example/', page.uri.to_s)
606
- assert_equal(2, @mech.history.length)
607
- assert_nil requests.last['referer']
608
- end
609
-
610
- def test_get_http_refresh_delay
611
- @mech.follow_meta_refresh = true
612
- class << @mech.agent
613
- attr_accessor :slept
614
- def sleep *args
615
- @slept = args
616
- end
617
- end
618
-
619
- @mech.get('http://localhost/http_refresh?refresh_time=1')
620
- assert_equal [1], @mech.agent.slept
621
- end
622
-
623
- def test_get_http_refresh_disabled
624
- page = @mech.get('http://localhost/http_refresh?refresh_time=0')
625
- assert_equal('http://localhost/http_refresh?refresh_time=0', page.uri.to_s)
626
- end
627
-
628
- def test_get_query
629
- page = @mech.get('http://localhost/', { :q => 'hello' })
630
- assert_equal('http://localhost/?q=hello', page.uri.to_s)
631
-
632
- page = @mech.get('http://localhost/', { :q => %w[hello world]})
633
- assert_equal('http://localhost/?q=hello&q=world', page.uri.to_s)
634
-
635
- page = @mech.get('http://localhost/', { :paging => { start: 1, limit: 25 } })
636
- assert_equal('http://localhost/?paging%5Bstart%5D=1&paging%5Blimit%5D=25', page.uri.to_s)
637
- end
638
-
639
- def test_get_redirect
640
- page = @mech.get('http://localhost/redirect')
641
-
642
- assert_equal(page.uri.to_s, 'http://localhost/verb')
643
-
644
- assert_equal 'GET', page.header['X-Request-Method']
645
- end
646
-
647
- def test_get_redirect_found
648
- page = @mech.get('http://localhost/response_code?code=302&ct=test/xml')
649
-
650
- assert_equal('http://localhost/index.html', page.uri.to_s)
651
-
652
- assert_equal(2, @mech.history.length)
653
- end
654
-
655
- def test_get_redirect_infinite
656
- assert_raises(Mechanize::RedirectLimitReachedError) {
657
- @mech.get('http://localhost/infinite_refresh')
658
- }
659
- end
660
-
661
- def test_get_referer
662
- request = nil
663
- @mech.pre_connect_hooks << lambda { |_, req|
664
- request = req
665
- }
666
-
667
- @mech.get('http://localhost/', [], 'http://tenderlovemaking.com/')
668
- assert_equal 'http://tenderlovemaking.com/', request['Referer']
669
- end
670
-
671
- def test_get_referer_file
672
- uri = URI 'http://tenderlovemaking.com/crossdomain.xml'
673
- file = Mechanize::File.new uri
674
-
675
- @mech.get('http://localhost', [], file)
676
-
677
- # HACK no assertion of behavior
678
- end
679
-
680
- def test_get_referer_none
681
- @mech.get('http://localhost/')
682
- @mech.get('http://localhost/')
683
- assert_equal(2, requests.length)
684
- requests.each do |request|
685
- assert_nil request['referer']
686
- end
687
- end
688
-
689
- def test_get_scheme_unsupported
690
- assert_raises Mechanize::UnsupportedSchemeError do
691
- begin
692
- @mech.get('ftp://server.com/foo.html')
693
- rescue Mechanize::UnsupportedSchemeError => error
694
- assert_equal 'ftp', error.scheme
695
- assert_equal 'ftp://server.com/foo.html', error.uri.to_s
696
- raise
697
- end
698
- end
699
- end
700
-
701
- def test_get_space
702
- @mech.get("http://localhost/tc_bad_links.html ")
703
-
704
- assert_match(/tc_bad_links.html$/, @mech.history.last.uri.to_s)
705
-
706
- assert_equal(1, @mech.history.length)
707
- end
708
-
709
- def test_get_tilde
710
- page = @mech.get('http://localhost/?foo=~2')
711
-
712
- assert_equal('http://localhost/?foo=~2', page.uri.to_s)
713
- end
714
-
715
- def test_get_weird
716
- @mech.get('http://localhost/?action=bing&bang=boom=1|a=|b=|c=')
717
- @mech.get('http://localhost/?a=b&#038;b=c&#038;c=d')
718
- @mech.get("http://localhost/?a=#{[0xd6].pack('U')}")
719
-
720
- # HACK no assertion of behavior
721
- end
722
-
723
- def test_get_yield
724
- pages = nil
725
-
726
- @mech.get("http://localhost/file_upload.html") { |page|
727
- pages = page
728
- }
729
-
730
- assert pages
731
- assert_equal('File Upload Form', pages.title)
732
- end
733
-
734
- def test_get_file
735
- body = @mech.get_file 'http://localhost/frame_test.html'
736
-
737
- assert_kind_of String, body
738
- refute_empty body
739
- end
740
-
741
- def test_get_file_download
742
- # non-Mechanize::File
743
- body = @mech.get_file 'http://localhost/button.jpg'
744
-
745
- assert_kind_of String, body
746
- refute_empty body
747
- end
748
-
749
- def test_head
750
- page = @mech.head('http://localhost/verb', { 'q' => 'foo' })
751
- assert_equal 0, @mech.history.length
752
- assert_equal 'HEAD', page.header['X-Request-Method']
753
- end
754
-
755
- def test_head_redirect
756
- page = @mech.head('http://localhost/redirect')
757
-
758
- assert_equal(page.uri.to_s, 'http://localhost/verb')
759
-
760
- assert_equal 'HEAD', page.header['X-Request-Method']
761
- end
762
-
763
- def test_history
764
- 2.times do |i|
765
- assert_equal(i, @mech.history.size)
766
-
767
- @mech.get("http://localhost/")
768
- end
769
-
770
- page = @mech.get("http://localhost/form_test.html")
771
-
772
- assert_equal("http://localhost/form_test.html",
773
- @mech.history.last.uri.to_s)
774
- assert_equal("http://localhost/",
775
- @mech.history[-2].uri.to_s)
776
-
777
- assert @mech.visited?("http://localhost/")
778
- assert @mech.visited?("/form_test.html"), 'relative'
779
- assert !@mech.visited?("http://google.com/")
780
- assert @mech.visited?(page.links.first)
781
- end
782
-
783
- def test_history_added_gets_called
784
- added_page = nil
785
-
786
- @mech.history_added = lambda { |page|
787
- added_page = page
788
- }
789
-
790
- assert_equal @mech.get('http://localhost/tc_blank_form.html'), added_page
791
- end
792
-
793
- def test_history_order
794
- @mech.max_history = 2
795
- assert_equal(0, @mech.history.length)
796
-
797
- @mech.get('http://localhost/form_test.html')
798
- assert_equal(1, @mech.history.length)
799
-
800
- @mech.get('http://localhost/empty_form.html')
801
- assert_equal(2, @mech.history.length)
802
-
803
- @mech.get('http://localhost/tc_checkboxes.html')
804
- assert_equal(2, @mech.history.length)
805
- assert_equal('http://localhost/empty_form.html', @mech.history[0].uri.to_s)
806
- assert_equal('http://localhost/tc_checkboxes.html',
807
- @mech.history[1].uri.to_s)
808
- end
809
-
810
- def test_initialize
811
- mech = Mechanize.new
812
-
813
- assert_equal 50, mech.max_history
814
- end
815
-
816
- def test_html_parser_equals
817
- @mech.html_parser = {}
818
- assert_raises(NoMethodError) {
819
- @mech.get('http://localhost/?foo=~2').links
820
- }
821
- end
822
-
823
- def test_idle_timeout_default
824
- assert_equal 5, Mechanize.new.idle_timeout
825
- end
826
-
827
- def test_idle_timeout_equals
828
- @mech.idle_timeout = 15
829
-
830
- assert_equal 15, @mech.idle_timeout
831
- end
832
-
833
- def test_keep_alive_equals
834
- assert @mech.keep_alive
835
-
836
- @mech.keep_alive = false
837
-
838
- refute @mech.keep_alive
839
- end
840
-
841
- def test_keep_alive_time
842
- assert_equal 0, @mech.keep_alive_time
843
-
844
- @mech.keep_alive_time = 1
845
-
846
- assert_equal 1, @mech.keep_alive_time
847
- end
848
-
849
- def test_log
850
- assert_nil @mech.log
851
- end
852
-
853
- def test_log_equals
854
- @mech.log = Logger.new $stderr
855
-
856
- refute_nil @mech.log
857
- assert_nil Mechanize.log
858
- end
859
-
860
- def test_max_file_buffer_equals
861
- @mech.max_file_buffer = 1024
862
-
863
- assert_equal 1024, @mech.agent.max_file_buffer
864
- end
865
-
866
- def test_max_history_equals
867
- @mech.max_history = 10
868
- 0.upto(10) do |i|
869
- assert_equal(i, @mech.history.size)
870
- @mech.get("http://localhost/")
871
- end
872
-
873
- 0.upto(10) do |i|
874
- assert_equal(10, @mech.history.size)
875
- @mech.get("http://localhost/")
876
- end
877
- end
878
-
879
- def test_open_timeout_equals
880
- @mech.open_timeout = 5
881
-
882
- assert_equal 5, @mech.open_timeout
883
- end
884
-
885
- def test_parse_download
886
- @mech.pluggable_parser['application/octet-stream'] = Mechanize::Download
887
-
888
- response = Net::HTTPOK.allocate
889
- response.instance_variable_set(:@header,
890
- 'content-type' =>
891
- ['application/octet-stream'])
892
-
893
- download = @mech.parse @uri, response, StringIO.new('raw')
894
-
895
- assert_kind_of Mechanize::Download, download
896
- assert_kind_of StringIO, download.content
897
- end
898
-
899
- def test_parse_html
900
- response = Net::HTTPOK.allocate
901
- response.instance_variable_set :@header, 'content-type' => ['text/html']
902
-
903
- page = @mech.parse URI('http://example/'), response, ''
904
-
905
- assert_kind_of Mechanize::Page, page
906
- end
907
-
908
- def test_post
909
- @mech.post "http://example", 'gender' => 'female'
910
-
911
- assert_equal "gender=female", requests.first.body
912
- end
913
-
914
- def test_post_auth
915
- requests = []
916
-
917
- @mech.pre_connect_hooks << proc { |agent, request|
918
- requests << request.class
919
- }
920
-
921
- @mech.add_auth(@uri, 'user', 'pass')
922
- page = @mech.post(@uri + '/basic_auth')
923
- assert_equal('You are authenticated', page.body)
924
- assert_equal(2, requests.length)
925
- r1 = requests[0]
926
- r2 = requests[1]
927
- assert_equal(r1, r2)
928
- end
929
-
930
- def test_post_entity
931
- @mech.post "http://localhost/form_post", 'json' => '["&quot;"]'
932
-
933
- assert_equal "json=%5B%22%22%22%5D", requests.first.body
934
- end
935
-
936
- def test_post_multiple_values
937
- @mech.post "http://localhost/form_post",
938
- [%w[gender female], %w[gender male]]
939
-
940
- assert_equal "gender=female&gender=male", requests.first.body
941
- end
942
-
943
- def test_post_multipart
944
- page = @mech.post('http://localhost/file_upload', {
945
- :name => 'Some file',
946
- :userfile1 => File.open(__FILE__)
947
- })
948
-
949
- name = File.basename __FILE__
950
- assert_match(
951
- "Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{name}\"",
952
- page.body
953
- )
954
- assert_operator page.body.bytesize, :>, file_contents_without_cr(__FILE__).length
955
- end
956
-
957
- def test_post_file_upload_nonascii
958
- name = 'ユーザファイル1'
959
- file_upload = Mechanize::Form::FileUpload.new({'name' => 'userfile1'}, name)
960
- file_upload.file_data = File.read(__FILE__)
961
- file_upload.mime_type = 'application/zip'
962
-
963
- page = @mech.post('http://localhost/file_upload', {
964
- :name => 'Some file',
965
- :userfile1 => file_upload
966
- })
967
-
968
- assert_match(
969
- "Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{name}\"".dup.force_encoding(Encoding::ASCII_8BIT),
970
- page.body
971
- )
972
- assert_match("Content-Type: application/zip", page.body)
973
- assert_operator page.body.bytesize, :>, file_contents_without_cr(__FILE__).length
974
- end
975
-
976
- def test_post_file_upload
977
- name = File.basename(__FILE__)
978
- file_upload = Mechanize::Form::FileUpload.new({'name' => 'userfile1'}, name)
979
- file_upload.file_data = File.read(__FILE__)
980
- file_upload.mime_type = 'application/zip'
981
-
982
- page = @mech.post('http://localhost/file_upload', {
983
- :name => 'Some file',
984
- :userfile1 => file_upload
985
- })
986
-
987
- assert_match(
988
- "Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{name}\"",
989
- page.body
990
- )
991
- assert_match("Content-Type: application/zip", page.body)
992
- assert_operator page.body.bytesize, :>, file_contents_without_cr(__FILE__).length
993
- end
994
-
995
- def test_post_redirect
996
- page = @mech.post('http://localhost/redirect')
997
-
998
- assert_equal(page.uri.to_s, 'http://localhost/verb')
999
-
1000
- assert_equal 'GET', page.header['X-Request-Method']
1001
- end
1002
-
1003
- def test_put
1004
- page = @mech.put('http://localhost/verb', 'foo')
1005
- assert_equal 1, @mech.history.length
1006
- assert_equal 'PUT', page.header['X-Request-Method']
1007
- end
1008
-
1009
- def test_put_redirect
1010
- page = @mech.put('http://localhost/redirect', 'foo')
1011
-
1012
- assert_equal(page.uri.to_s, 'http://localhost/verb')
1013
-
1014
- assert_equal 'GET', page.header['X-Request-Method']
1015
- end
1016
-
1017
- def test_read_timeout_equals
1018
- @mech.read_timeout = 5
1019
- assert_equal 5, @mech.read_timeout
1020
- assert @mech.get('http://localhost/response_code?code=200')
1021
- assert_equal 5, @mech.agent.http.read_timeout
1022
- end
1023
-
1024
- def test_write_timeout_equals
1025
- @mech.write_timeout = 7
1026
- assert_equal 7, @mech.write_timeout
1027
- assert @mech.get('http://localhost/response_code?code=200')
1028
- assert_equal 7, @mech.agent.http.write_timeout
1029
- end
1030
-
1031
- def test_timeouts_for_file_connection
1032
- uri = URI.parse "file://#{File.expand_path __FILE__}"
1033
- @mech.read_timeout = 5
1034
- @mech.open_timeout = 5
1035
- assert @mech.get(uri)
1036
- end
1037
-
1038
- def test_referer
1039
- host_path = "localhost/tc_referer.html?t=1"
1040
- ['http', 'https'].each { |proto|
1041
- referer = "#{proto}://#{host_path}"
1042
- [
1043
- "",
1044
- "@",
1045
- "user1@",
1046
- ":@",
1047
- "user1:@",
1048
- ":password1@",
1049
- "user1:password1@",
1050
- ].each { |userinfo|
1051
- url = "#{proto}://#{userinfo}#{host_path}"
1052
- [url, url + "#foo"].each { |furl|
1053
- [
1054
- ['relative', true],
1055
- ['insecure', proto == 'http'],
1056
- ['secure', true],
1057
- ['relative noreferrer', false],
1058
- ['insecure noreferrer', false],
1059
- ['secure noreferrer', false],
1060
- ].each_with_index { |(type, bool), i|
1061
- rpage = @mech.get(furl)
1062
- page = rpage.links[i].click
1063
- assert_equal bool ? referer : '', page.body, "%s link from %s" % [type, furl]
1064
- }
1065
-
1066
- rpage = @mech.get(furl)
1067
- page = rpage.forms.first.submit
1068
- assert_equal referer, page.body, "post from %s" % furl
1069
- }
1070
- }
1071
- }
1072
- end
1073
-
1074
- def test_retry_change_requests_equals
1075
- unless Gem::Requirement.new("< 4.0.0").satisfied_by?(Gem::Version.new(Net::HTTP::Persistent::VERSION))
1076
- # see https://github.com/drbrain/net-http-persistent/pull/100
1077
- skip("net-http-persistent 4.0.0 and later does not support retry_change_requests")
1078
- end
1079
-
1080
- refute @mech.retry_change_requests
1081
-
1082
- @mech.retry_change_requests = true
1083
-
1084
- assert @mech.retry_change_requests
1085
- end
1086
-
1087
- def test_set_proxy
1088
- http = @mech.agent.http
1089
-
1090
- @mech.set_proxy 'localhost', 8080, 'user', 'pass'
1091
-
1092
- assert_equal 'localhost', @mech.proxy_addr
1093
- assert_equal 8080, @mech.proxy_port
1094
- assert_equal 'user', @mech.proxy_user
1095
- assert_equal 'pass', @mech.proxy_pass
1096
-
1097
- assert_equal URI('http://user:pass@localhost:8080'), http.proxy_uri
1098
- end
1099
-
1100
- def test_shutdown
1101
- uri = URI 'http://localhost'
1102
- jar = HTTP::CookieJar.new
1103
- jar.parse 'a=b', uri
1104
-
1105
- @mech.cookie_jar = jar
1106
-
1107
- @mech.get("http://localhost/")
1108
-
1109
- assert_match(/Hello World/, @mech.current_page.body)
1110
- refute_empty @mech.cookies
1111
-
1112
- @mech.shutdown
1113
-
1114
- assert_empty @mech.history
1115
- assert_empty @mech.cookies
1116
- end
1117
-
1118
- def test_start
1119
- body = nil
1120
-
1121
- Mechanize.start do |m|
1122
- body = m.get("http://localhost/").body
1123
- end
1124
-
1125
- assert_match(/Hello World/, body)
1126
- end
1127
-
1128
- def test_submit_bad_form_method
1129
- page = @mech.get("http://localhost/bad_form_test.html")
1130
- assert_raises ArgumentError do
1131
- @mech.submit(page.forms.first)
1132
- end
1133
- end
1134
-
1135
- def test_submit_check_one
1136
- page = @mech.get('http://localhost/tc_checkboxes.html')
1137
- form = page.forms.first
1138
- form.checkboxes_with(:name => 'green')[1].check
1139
-
1140
- page = @mech.submit(form)
1141
-
1142
- assert_equal(1, page.links.length)
1143
- assert_equal('green:on', page.links.first.text)
1144
- end
1145
-
1146
- def test_submit_check_two
1147
- page = @mech.get('http://localhost/tc_checkboxes.html')
1148
- form = page.forms.first
1149
- form.checkboxes_with(:name => 'green')[0].check
1150
- form.checkboxes_with(:name => 'green')[1].check
1151
-
1152
- page = @mech.submit(form)
1153
-
1154
- assert_equal(2, page.links.length)
1155
- assert_equal('green:on', page.links[0].text)
1156
- assert_equal('green:on', page.links[1].text)
1157
- end
1158
-
1159
- def test_submit_enctype
1160
- page = @mech.get("http://localhost/file_upload.html")
1161
- assert_equal('multipart/form-data', page.forms[0].enctype)
1162
-
1163
- form = page.forms.first
1164
- form.file_uploads.first.file_name = __FILE__
1165
- form.file_uploads.first.mime_type = "text/plain"
1166
- form.file_uploads.first.file_data = "Hello World\n\n"
1167
-
1168
- page = @mech.submit(form)
1169
-
1170
- basename = File.basename __FILE__
1171
-
1172
- assert_match(
1173
- "Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{basename}\"",
1174
- page.body
1175
- )
1176
- assert_match(
1177
- "Content-Disposition: form-data; name=\"name\"",
1178
- page.body
1179
- )
1180
- assert_match('Content-Type: text/plain', page.body)
1181
- assert_match('Hello World', page.body)
1182
- assert_match('foo[aaron]', page.body)
1183
- end
1184
-
1185
- def test_submit_file_data
1186
- page = @mech.get("http://localhost/file_upload.html")
1187
- assert_equal('multipart/form-data', page.forms[1].enctype)
1188
-
1189
- form = page.forms[1]
1190
- form.file_uploads.first.file_name = __FILE__
1191
- form.file_uploads.first.file_data = File.read __FILE__
1192
-
1193
- page = @mech.submit(form)
1194
-
1195
- contents = File.binread(__FILE__).gsub(/\r\n/, "\n")
1196
- basename = File.basename __FILE__
1197
-
1198
- assert_match(
1199
- "Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"#{basename}\"",
1200
- page.body
1201
- )
1202
-
1203
- assert_match(contents, page.body)
1204
- end
1205
-
1206
- def test_submit_file_name
1207
- page = @mech.get("http://localhost/file_upload.html")
1208
- assert_equal('multipart/form-data', page.forms[1].enctype)
1209
-
1210
- form = page.forms[1]
1211
- form.file_uploads.first.file_name = __FILE__
1212
-
1213
- page = @mech.submit(form)
1214
-
1215
- contents = File.binread __FILE__
1216
- basename = File.basename __FILE__
1217
- assert_match(
1218
- "Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"#{basename}\"",
1219
- page.body
1220
- )
1221
- assert_match(contents, page.body)
1222
- end
1223
-
1224
- def test_submit_get
1225
- form = node 'form', 'method' => 'GET', 'action' => '/?a=b'
1226
- form = Mechanize::Form.new form, @mech, fake_page
1227
-
1228
- @mech.submit form
1229
-
1230
- assert_equal '/', requests.first.path
1231
- end
1232
-
1233
- def test_submit_headers
1234
- page = @mech.get 'http://localhost:2000/form_no_action.html'
1235
-
1236
- assert form = page.forms.first
1237
- form.action = '/http_headers'
1238
-
1239
- page = @mech.submit form, nil, 'foo' => 'bar'
1240
-
1241
- headers = page.body.split("\n").map { |x| x.split('|', 2) }.flatten
1242
- headers = Hash[*headers]
1243
-
1244
- assert_equal 'bar', headers['foo']
1245
- end
1246
-
1247
- def test_submit_multipart
1248
- page = @mech.get("http://localhost/file_upload.html")
1249
-
1250
- assert_equal('multipart/form-data', page.forms[1].enctype)
1251
-
1252
- form = page.forms[1]
1253
- form.file_uploads.first.file_name = __FILE__
1254
- form.file_uploads.first.mime_type = "text/plain"
1255
- form.file_uploads.first.file_data = "Hello World\n\n"
1256
-
1257
- page = @mech.submit(form)
1258
-
1259
- basename = File.basename __FILE__
1260
-
1261
- assert_match(
1262
- "Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"#{basename}\"",
1263
- page.body
1264
- )
1265
- end
1266
-
1267
- def test_submit_no_file
1268
- page = @mech.get("http://localhost/file_upload.html")
1269
- form = page.forms.first
1270
- form.field_with(:name => 'name').value = 'Aaron'
1271
- @page = @mech.submit(form)
1272
- assert_match('Aaron', @page.body)
1273
- assert_match(
1274
- "Content-Disposition: form-data; name=\"userfile1\"; filename=\"\"",
1275
- @page.body
1276
- )
1277
- end
1278
-
1279
- def test_submit_post
1280
- form = node 'form', 'method' => 'POST', 'action' => '/?a=b'
1281
- form = Mechanize::Form.new form, @mech, fake_page
1282
-
1283
- @mech.submit form
1284
-
1285
- assert_equal '/?a=b', requests.first.path
1286
- end
1287
-
1288
- def test_submit_post_pound
1289
- form = node 'form', 'method' => 'POST', 'action' => '/#1'
1290
- form = Mechanize::Form.new form, @mech, fake_page
1291
-
1292
- @mech.submit form
1293
-
1294
- assert_equal '/', requests.first.path
1295
- end
1296
-
1297
- def test_submit_too_many_radiobuttons
1298
- page = @mech.get("http://localhost/form_test.html")
1299
- form = page.form_with(:name => 'post_form1')
1300
- form.radiobuttons.each { |r| r.checked = true }
1301
-
1302
- assert_raises Mechanize::Error do
1303
- @mech.submit(form)
1304
- end
1305
- end
1306
-
1307
- def test_transact
1308
- @mech.get("http://localhost/frame_test.html")
1309
- assert_equal(1, @mech.history.length)
1310
- @mech.transact { |a|
1311
- 5.times {
1312
- @mech.get("http://localhost/frame_test.html")
1313
- }
1314
- assert_equal(6, @mech.history.length)
1315
- }
1316
- assert_equal(1, @mech.history.length)
1317
- end
1318
-
1319
- def test_user_agent_alias_equals_unknown
1320
- assert_raises ArgumentError do
1321
- @mech.user_agent_alias = "Aaron's Browser"
1322
- end
1323
- end
1324
-
1325
- def test_verify_mode
1326
- assert_equal OpenSSL::SSL::VERIFY_PEER, @mech.verify_mode
1327
-
1328
- @mech.verify_mode = OpenSSL::SSL::VERIFY_NONE
1329
-
1330
- assert_equal OpenSSL::SSL::VERIFY_NONE, @mech.verify_mode
1331
- end
1332
-
1333
- def test_visited_eh
1334
- @mech.get("http://localhost/content_type_test?ct=application/pdf")
1335
-
1336
- assert \
1337
- @mech.visited?("http://localhost/content_type_test?ct=application/pdf")
1338
- assert \
1339
- !@mech.visited?("http://localhost/content_type_test")
1340
- assert \
1341
- !@mech.visited?("http://localhost/content_type_test?ct=text/html")
1342
- end
1343
-
1344
- def test_visited_eh_link
1345
- @mech.get("http://example/index.html")
1346
-
1347
- page = page URI 'http://example'
1348
- link = node 'a', 'href' => '/index.html'
1349
- link = Mechanize::Page::Link.new link, page, @mech
1350
-
1351
- assert @mech.visited? link
1352
- end
1353
-
1354
- def test_visited_eh_redirect
1355
- @mech.get("http://localhost/response_code?code=302")
1356
-
1357
- assert_equal("http://localhost/index.html", @mech.current_page.uri.to_s)
1358
-
1359
- assert @mech.visited?('http://localhost/response_code?code=302')
1360
- end
1361
-
1362
- def test_no_frames_exists
1363
- page = @mech.get("http://localhost/empty_form.html");
1364
- assert_nil page.frame_with(:name => 'noframe')
1365
- assert_raises Mechanize::ElementNotFoundError do
1366
- page.frame_with!(:name => 'noframe')
1367
- end
1368
- end
1369
-
1370
- def assert_header(page, header)
1371
- headers = {}
1372
-
1373
- page.body.split(/[\r\n]+/).each do |page_header|
1374
- headers.[]=(*page_header.chomp.split(/\|/))
1375
- end
1376
-
1377
- header.each do |key, value|
1378
- assert(headers.has_key?(key))
1379
- assert_equal(value, headers[key])
1380
- end
1381
- end
1382
- end