/
test_mechanize.rb
1372 lines (1020 loc) · 35.6 KB
/
test_mechanize.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
require 'mechanize/test_case'
class TestMechanize < Mechanize::TestCase
def setup
super
@uri = URI 'http://example/'
@req = Net::HTTP::Get.new '/'
@res = Net::HTTPOK.allocate
@res.instance_variable_set :@code, 200
@res.instance_variable_set :@header, {}
end
def test_back
0.upto(5) do |i|
assert_equal(i, @mech.history.size)
@mech.get("http://localhost/")
end
@mech.get("http://localhost/form_test.html")
assert_equal("http://localhost/form_test.html",
@mech.history.last.uri.to_s)
assert_equal("http://localhost/",
@mech.history[-2].uri.to_s)
assert_equal(7, @mech.history.size)
@mech.back
assert_equal(6, @mech.history.size)
assert_equal("http://localhost/",
@mech.history.last.uri.to_s)
end
def test_basic_auth
_, err = capture_io do
@mech.basic_auth 'user', 'pass' # warns
end
line = __LINE__ - 3
file = File.basename __FILE__
assert_match "#{file} line #{line}", err
page = @mech.get @uri + '/basic_auth'
assert_equal 'You are authenticated', page.body
end
def test_cert_key_file
in_tmpdir do
open 'key.pem', 'w' do |io| io.write ssl_private_key.to_pem end
open 'cert.pem', 'w' do |io| io.write ssl_certificate.to_pem end
mech = Mechanize.new do |a|
a.cert = 'cert.pem'
a.key = 'key.pem'
end
# Certificate#== seems broken
assert_equal ssl_certificate.to_pem, mech.certificate.to_pem
end
end
def test_cert_key_object
mech = Mechanize.new do |a|
a.cert = ssl_certificate
a.key = ssl_private_key
end
assert_equal ssl_certificate, mech.certificate
assert_equal ssl_certificate, mech.cert
assert_equal ssl_private_key, mech.key
end
def test_cert_store
assert_nil @mech.cert_store
store = OpenSSL::X509::Store.new
@mech.cert_store = store
assert_equal store, @mech.cert_store
end
def test_click
@mech.user_agent_alias = 'Mac Safari'
page = @mech.get("http://localhost/frame_test.html")
link = page.link_with(:text => "Form Test")
page = @mech.click(link)
assert_equal("http://localhost/form_test.html",
@mech.history.last.uri.to_s)
end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse body of NOFRAMES
def test_click_bogus_link_with_cookies
@mech.cookie_jar = cookie_jar("a=b")
page = html_page <<-BODY
<a href="http:///index.html">yes really</a>
BODY
page.links[0].click
assert_equal '/index.html', requests.first.path
end
def test_click_image_button
page = @mech.get("http://localhost/form_test.html")
get_form = page.forms.find { |f| f.name == "get_form1" }
image_button = get_form.buttons.first
new_page = @mech.click(image_button)
assert_equal "http://localhost/form_post?first_name=&button.x=0&button.y=0", new_page.uri.to_s
end
def test_click_submit_button
page = @mech.get("http://localhost/form_test.html")
get_form = page.forms.find { |f| f.name == "get_form1" }
submit_button = get_form.submits.first
new_page = @mech.click(submit_button)
assert_equal "http://localhost/form_post?first_name=", new_page.uri.to_s
end
def test_click_frame
frame = node 'frame', 'src' => '/index.html'
frame = Mechanize::Page::Frame.new frame, @mech, fake_page
@mech.click frame
assert_equal '/index.html', requests.first.path
end
def test_click_frame_hpricot_style
page = @mech.get("http://localhost/frame_test.html")
link = (page/"//frame[@name='frame2']").first
page = @mech.click(link)
assert_equal("http://localhost/form_test.html",
@mech.history.last.uri.to_s)
end
def test_click_hpricot_style # HACK move to test_divide in Page
page = @mech.get("http://localhost/frame_test.html")
link = (page/"//a[@class='bar']").first
page = @mech.click(link)
assert_equal("http://localhost/form_test.html",
@mech.history.last.uri.to_s)
end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse body of NOFRAMES
def test_click_link
link = node 'a', 'href' => '/index.html'
link = Mechanize::Page::Link.new link, @mech, fake_page
@mech.click link
assert_equal '/index.html', requests.first.path
assert_equal 'http://fake.example/', requests.first['Referer']
end
def test_click_link_nofollow
page = html_page <<-BODY
<meta name="ROBOTS" content="nofollow">
<p>Do not follow <a href="/index.html">this</a> or <a href="/">this</a>!
BODY
page.links[0].click
page.links[1].click
@mech.robots = true
assert_raises Mechanize::RobotsDisallowedError do
page.links[0].click
end
assert_raises Mechanize::RobotsDisallowedError do
page.links[1].click
end
end
def test_click_link_noreferrer
link = node 'a', 'href' => '/index.html', 'rel' => 'noreferrer'
link = Mechanize::Page::Link.new link, @mech, fake_page
@mech.click link
assert_nil requests.first['referer']
end
def test_click_link_rel_nofollow
page = html_page <<-BODY
<p>You can follow <a href="/index.html">this link</a>
but not <a href="/" rel="me nofollow">this</a>!
BODY
page.links[0].click
page.links[1].click
@mech.robots = true
page.links[0].click
assert_raises Mechanize::RobotsDisallowedError do
page.links[1].click
end
end
def test_click_link_parent
page = page URI 'http://example/a/index.html'
link = node 'a', 'href' => '../index.html'
link = Mechanize::Page::Link.new link, @mech, page
@mech.click link
assert_equal '/index.html', requests.first.path
end
def test_click_link_parent_extra
page = page URI 'http://example/a/index.html'
link = node 'a', 'href' => '../../index.html'
link = Mechanize::Page::Link.new link, @mech, page
@mech.click link
assert_equal '/index.html', requests.first.path
end
def test_click_link_hpricot_style # HACK move to test_search in Page
page = @mech.get("http://localhost/tc_encoded_links.html")
page = @mech.click(page.search('a').first)
assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
end
def test_click_link_query
page = @mech.get("http://localhost/tc_encoded_links.html")
link = page.links.first
assert_equal('/form_post?a=b&b=c', link.href)
page = @mech.click(link)
assert_equal("http://localhost/form_post?a=b&b=c", page.uri.to_s)
end
def test_click_link_space
page = @mech.get("http://localhost/tc_bad_links.html")
@mech.click page.links.first
assert_match(/alt_text.html$/, @mech.history.last.uri.to_s)
assert_equal(2, @mech.history.length)
end
def test_click_more
@mech.get 'http://localhost/test_click.html'
@mech.click 'A Button'
assert_equal 'http://localhost/frame_test.html?words=nil',
@mech.page.uri.to_s
@mech.back
@mech.click 'A Link'
assert_equal 'http://localhost/index.html',
@mech.page.uri.to_s
@mech.back
@mech.click @mech.page.link_with(:text => 'A Link')
assert_equal 'http://localhost/index.html',
@mech.page.uri.to_s
end
def test_cookies
uri = URI 'http://example'
jar = HTTP::CookieJar.new
jar.parse 'a=b', uri
@mech.cookie_jar = jar
refute_empty @mech.cookies
end
def test_cookie_jar
assert_kind_of Mechanize::CookieJar, @mech.cookie_jar
jar = HTTP::CookieJar.new
@mech.cookie_jar = jar
assert_equal jar, @mech.cookie_jar
end
def test_delete
page = @mech.delete('http://localhost/verb', { 'q' => 'foo' })
assert_equal 1, @mech.history.length
assert_equal 'DELETE', page.header['X-Request-Method']
end
def test_delete_redirect
page = @mech.delete('http://localhost/redirect')
assert_equal(page.uri.to_s, 'http://localhost/verb')
assert_equal 'GET', page.header['X-Request-Method']
end
def test_download
page = nil
in_tmpdir do
open 'download', 'w' do |io|
page = @mech.download 'http://example', io
refute io.closed?
end
assert_operator 1, :<=, File.stat('download').size
end
assert_empty @mech.history
assert_kind_of Mechanize::Page, page
end
def test_download_filename
page = nil
in_tmpdir do
page = @mech.download 'http://example', 'download'
assert_operator 1, :<=, File.stat('download').size
end
assert_empty @mech.history
assert_kind_of Mechanize::Page, page
end
def test_download_filename_error
in_tmpdir do
assert_raises Mechanize::UnauthorizedError do
@mech.download 'http://example/digest_auth', 'download'
end
refute File.exist? 'download'
end
end
def test_download_does_not_allow_command_injection
in_tmpdir do
@mech.download('http://example', '| ruby -rfileutils -e \'FileUtils.touch("vul.txt")\'')
refute_operator(File, :exist?, "vul.txt")
end
end
def test_get
uri = URI 'http://localhost'
page = @mech.get uri, { :q => 'h' }, 'http://example', { 'X-H' => 'v' }
assert_equal URI('http://localhost/?q=h'), page.uri
assert_equal URI('http://localhost'), uri
end
def test_get_HTTP
page = @mech.get('HTTP://localhost/', { :q => 'hello' })
assert_kind_of URI::HTTP, page.uri
assert_equal 'localhost', page.uri.host
assert_equal 80, page.uri.port
assert_equal '/?q=hello', page.uri.request_uri
end
def test_get_anchor
page = @mech.get('http://localhost/?foo=bar"')
assert_equal('http://localhost/?foo=bar%22', page.uri.to_s)
end
def test_get_bad_url
assert_raises ArgumentError do
@mech.get '/foo.html'
end
end
def test_get_auth
@mech.add_auth @uri, 'user', 'pass'
page = @mech.get @uri + '/basic_auth'
assert_equal 'You are authenticated', page.body
end
def test_get_auth_bad
@mech.add_auth(@uri, 'aaron', 'aaron')
e = assert_raises Mechanize::UnauthorizedError do
@mech.get(@uri + '/basic_auth')
end
assert_equal("401", e.response_code)
end
def test_get_auth_none
e = assert_raises Mechanize::UnauthorizedError do
@mech.get(@uri + '/basic_auth')
end
assert_equal("401", e.response_code)
end
def test_get_auth_realm
@mech.add_auth @uri, 'user', 'pass', 'Blah'
page = @mech.get @uri + '/basic_auth'
assert_equal 'You are authenticated', page.body
end
def test_get_conditional
assert_empty @mech.history
page = @mech.get 'http://localhost/if_modified_since'
assert_match(/You did not send/, page.body)
assert_equal 1, @mech.history.length
page2 = @mech.get 'http://localhost/if_modified_since'
assert_equal 2, @mech.history.length
assert_equal page.object_id, page2.object_id
end
def test_get_digest_auth
@mech.add_auth @uri, 'user', 'pass'
page = @mech.get @uri + '/digest_auth'
assert_equal 'You are authenticated', page.body
end
def test_get_follow_meta_refresh
@mech.follow_meta_refresh = true
page = @mech.get('http://localhost/tc_follow_meta.html')
assert_equal(2, @mech.history.length)
assert_equal('http://localhost/tc_follow_meta.html',
@mech.history.first.uri.to_s)
assert_equal('http://localhost/index.html', page.uri.to_s)
assert_equal('http://localhost/index.html', @mech.history.last.uri.to_s)
[5, 6].each { |limit|
@mech.redirection_limit = limit
begin
@mech.get('http://localhost/tc_follow_meta_loop_1.html')
rescue => e
assert_instance_of Mechanize::RedirectLimitReachedError, e
assert_equal limit, e.redirects
if limit % 2 == 0
assert_equal '/tc_follow_meta_loop_1.html', e.page.uri.path
else
assert_equal '/tc_follow_meta_loop_2.html', e.page.uri.path
end
end
}
end
def test_get_follow_meta_refresh_anywhere
@mech.follow_meta_refresh = :anywhere
@mech.get('http://localhost/tc_meta_in_body.html')
assert_equal 2, requests.length
end
def test_get_follow_meta_refresh_disabled
page = @mech.get('http://localhost/tc_follow_meta.html')
assert_equal('http://localhost/tc_follow_meta.html', page.uri.to_s)
assert_equal(1, page.meta_refresh.length)
end
def test_get_follow_meta_refresh_empty_url
@mech.follow_meta_refresh = true
@mech.follow_meta_refresh_self = true
page = @mech.get('http://example/refresh_with_empty_url')
assert_equal(3, @mech.history.length)
assert_equal('http://example/refresh_with_empty_url',
@mech.history[0].uri.to_s)
assert_equal('http://example/refresh_with_empty_url',
@mech.history[1].uri.to_s)
assert_equal('http://example/', page.uri.to_s)
assert_equal('http://example/', @mech.history.last.uri.to_s)
end
def test_get_follow_meta_refresh_in_body
@mech.follow_meta_refresh = true
@mech.get('http://localhost/tc_meta_in_body.html')
assert_equal 1, requests.length
end
def test_get_follow_meta_refresh_no_url
@mech.follow_meta_refresh = true
@mech.follow_meta_refresh_self = true
page = @mech.get('http://example/refresh_without_url')
assert_equal(3, @mech.history.length)
assert_equal('http://example/refresh_without_url',
@mech.history[0].uri.to_s)
assert_equal('http://example/refresh_without_url',
@mech.history[1].uri.to_s)
assert_equal('http://example/', page.uri.to_s)
assert_equal('http://example/', @mech.history.last.uri.to_s)
end
def test_get_follow_meta_refresh_referer_not_sent
@mech.follow_meta_refresh = true
@mech.get('http://localhost/tc_follow_meta.html')
assert_equal 2, @mech.history.length
assert_nil requests.last['referer']
end
def test_get_referer_download
download = Mechanize::Download.new URI 'http://example/prev'
uri = URI 'http://example'
page = @mech.get uri, { :q => 'h' }, download, { 'X-H' => 'v' }
assert_equal URI('http://example/?q=h'), page.uri
assert_equal URI('http://example'), uri
assert_equal 'http://example/prev', requests.first['referer']
end
def test_get_robots
@mech.robots = true
assert_equal "Page Title", @mech.get("http://localhost/index.html").title
assert_raises Mechanize::RobotsDisallowedError do
@mech.get "http://localhost/norobots.html"
end
end
def test_follow_meta_refresh_self
refute @mech.agent.follow_meta_refresh_self
@mech.follow_meta_refresh_self = true
assert @mech.agent.follow_meta_refresh_self
end
def test_get_gzip
page = @mech.get("http://localhost/gzip?file=index.html")
assert_kind_of(Mechanize::Page, page)
assert_match('Hello World', page.body)
end
def test_content_encoding_hooks_header
h = {'X-ResponseContentEncoding' => 'agzip'}
# test of X-ResponseContentEncoding feature
assert_raises(Mechanize::Error, 'Unsupported Content-Encoding: agzip') do
@mech.get("http://localhost/gzip?file=index.html", nil, nil, h)
end
@mech.content_encoding_hooks << lambda{|agent, uri, response, response_body_io|
response['content-encoding'] = 'gzip' if response['content-encoding'] == 'agzip'}
page = @mech.get("http://localhost/gzip?file=index.html", nil, nil, h)
assert_match('Hello World', page.body)
end
def external_cmd(io); Zlib::GzipReader.new(io).read; end
def test_content_encoding_hooks_body_io
h = {'X-ResponseContentEncoding' => 'unsupported_content_encoding'}
@mech.content_encoding_hooks << lambda{|agent, uri, response, response_body_io|
if response['content-encoding'] == 'unsupported_content_encoding'
response['content-encoding'] = 'none'
response_body_io.string = external_cmd(response_body_io)
end}
page = @mech.get("http://localhost/gzip?file=index.html", nil, nil, h)
assert_match('Hello World', page.body)
end
def test_get_http_refresh
@mech.follow_meta_refresh = true
page = @mech.get('http://example/http_refresh?refresh_time=0')
assert_equal('http://example/', page.uri.to_s)
assert_equal(2, @mech.history.length)
assert_nil requests.last['referer']
end
def test_get_http_refresh_delay
@mech.follow_meta_refresh = true
class << @mech.agent
attr_accessor :slept
def sleep *args
@slept = args
end
end
@mech.get('http://localhost/http_refresh?refresh_time=1')
assert_equal [1], @mech.agent.slept
end
def test_get_http_refresh_disabled
page = @mech.get('http://localhost/http_refresh?refresh_time=0')
assert_equal('http://localhost/http_refresh?refresh_time=0', page.uri.to_s)
end
def test_get_query
page = @mech.get('http://localhost/', { :q => 'hello' })
assert_equal('http://localhost/?q=hello', page.uri.to_s)
page = @mech.get('http://localhost/', { :q => %w[hello world]})
assert_equal('http://localhost/?q=hello&q=world', page.uri.to_s)
page = @mech.get('http://localhost/', { :paging => { start: 1, limit: 25 } })
assert_equal('http://localhost/?paging%5Bstart%5D=1&paging%5Blimit%5D=25', page.uri.to_s)
end
def test_get_redirect
page = @mech.get('http://localhost/redirect')
assert_equal(page.uri.to_s, 'http://localhost/verb')
assert_equal 'GET', page.header['X-Request-Method']
end
def test_get_redirect_found
page = @mech.get('http://localhost/response_code?code=302&ct=test/xml')
assert_equal('http://localhost/index.html', page.uri.to_s)
assert_equal(2, @mech.history.length)
end
def test_get_redirect_infinite
assert_raises(Mechanize::RedirectLimitReachedError) {
@mech.get('http://localhost/infinite_refresh')
}
end
def test_get_referer
request = nil
@mech.pre_connect_hooks << lambda { |_, req|
request = req
}
@mech.get('http://localhost/', [], 'http://tenderlovemaking.com/')
assert_equal 'http://tenderlovemaking.com/', request['Referer']
end
def test_get_referer_file
uri = URI 'http://tenderlovemaking.com/crossdomain.xml'
file = Mechanize::File.new uri
@mech.get('http://localhost', [], file)
# HACK no assertion of behavior
end
def test_get_referer_none
@mech.get('http://localhost/')
@mech.get('http://localhost/')
assert_equal(2, requests.length)
requests.each do |request|
assert_nil request['referer']
end
end
def test_get_scheme_unsupported
assert_raises Mechanize::UnsupportedSchemeError do
begin
@mech.get('ftp://server.com/foo.html')
rescue Mechanize::UnsupportedSchemeError => error
assert_equal 'ftp', error.scheme
assert_equal 'ftp://server.com/foo.html', error.uri.to_s
raise
end
end
end
def test_get_space
@mech.get("http://localhost/tc_bad_links.html ")
assert_match(/tc_bad_links.html$/, @mech.history.last.uri.to_s)
assert_equal(1, @mech.history.length)
end
def test_get_tilde
page = @mech.get('http://localhost/?foo=~2')
assert_equal('http://localhost/?foo=~2', page.uri.to_s)
end
def test_get_weird
@mech.get('http://localhost/?action=bing&bang=boom=1|a=|b=|c=')
@mech.get('http://localhost/?a=b&b=c&c=d')
@mech.get("http://localhost/?a=#{[0xd6].pack('U')}")
# HACK no assertion of behavior
end
def test_get_yield
pages = nil
@mech.get("http://localhost/file_upload.html") { |page|
pages = page
}
assert pages
assert_equal('File Upload Form', pages.title)
end
def test_get_file
body = @mech.get_file 'http://localhost/frame_test.html'
assert_kind_of String, body
refute_empty body
end
def test_get_file_download
# non-Mechanize::File
body = @mech.get_file 'http://localhost/button.jpg'
assert_kind_of String, body
refute_empty body
end
def test_head
page = @mech.head('http://localhost/verb', { 'q' => 'foo' })
assert_equal 0, @mech.history.length
assert_equal 'HEAD', page.header['X-Request-Method']
end
def test_head_redirect
page = @mech.head('http://localhost/redirect')
assert_equal(page.uri.to_s, 'http://localhost/verb')
assert_equal 'HEAD', page.header['X-Request-Method']
end
def test_history
2.times do |i|
assert_equal(i, @mech.history.size)
@mech.get("http://localhost/")
end
page = @mech.get("http://localhost/form_test.html")
assert_equal("http://localhost/form_test.html",
@mech.history.last.uri.to_s)
assert_equal("http://localhost/",
@mech.history[-2].uri.to_s)
assert @mech.visited?("http://localhost/")
assert @mech.visited?("/form_test.html"), 'relative'
assert !@mech.visited?("http://google.com/")
assert @mech.visited?(page.links.first)
end
def test_history_added_gets_called
added_page = nil
@mech.history_added = lambda { |page|
added_page = page
}
assert_equal @mech.get('http://localhost/tc_blank_form.html'), added_page
end
def test_history_order
@mech.max_history = 2
assert_equal(0, @mech.history.length)
@mech.get('http://localhost/form_test.html')
assert_equal(1, @mech.history.length)
@mech.get('http://localhost/empty_form.html')
assert_equal(2, @mech.history.length)
@mech.get('http://localhost/tc_checkboxes.html')
assert_equal(2, @mech.history.length)
assert_equal('http://localhost/empty_form.html', @mech.history[0].uri.to_s)
assert_equal('http://localhost/tc_checkboxes.html',
@mech.history[1].uri.to_s)
end
def test_initialize
mech = Mechanize.new
assert_equal 50, mech.max_history
end
def test_html_parser_equals
@mech.html_parser = {}
assert_raises(NoMethodError) {
@mech.get('http://localhost/?foo=~2').links
}
end
def test_idle_timeout_default
assert_equal 5, Mechanize.new.idle_timeout
end
def test_idle_timeout_equals
@mech.idle_timeout = 15
assert_equal 15, @mech.idle_timeout
end
def test_keep_alive_equals
assert @mech.keep_alive
@mech.keep_alive = false
refute @mech.keep_alive
end
def test_keep_alive_time
assert_equal 0, @mech.keep_alive_time
@mech.keep_alive_time = 1
assert_equal 1, @mech.keep_alive_time
end
def test_log
assert_nil @mech.log
end
def test_log_equals
@mech.log = Logger.new $stderr
refute_nil @mech.log
assert_nil Mechanize.log
end
def test_max_file_buffer_equals
@mech.max_file_buffer = 1024
assert_equal 1024, @mech.agent.max_file_buffer
end
def test_max_history_equals
@mech.max_history = 10
0.upto(10) do |i|
assert_equal(i, @mech.history.size)
@mech.get("http://localhost/")
end
0.upto(10) do |i|
assert_equal(10, @mech.history.size)
@mech.get("http://localhost/")
end
end
def test_open_timeout_equals
@mech.open_timeout = 5
assert_equal 5, @mech.open_timeout
end
def test_parse_download
@mech.pluggable_parser['application/octet-stream'] = Mechanize::Download
response = Net::HTTPOK.allocate
response.instance_variable_set(:@header,
'content-type' =>
['application/octet-stream'])
download = @mech.parse @uri, response, StringIO.new('raw')
assert_kind_of Mechanize::Download, download
assert_kind_of StringIO, download.content
end
def test_parse_html
response = Net::HTTPOK.allocate
response.instance_variable_set :@header, 'content-type' => ['text/html']
page = @mech.parse URI('http://example/'), response, ''
assert_kind_of Mechanize::Page, page
end
def test_post
@mech.post "http://example", 'gender' => 'female'
assert_equal "gender=female", requests.first.body
end
def test_post_auth
requests = []
@mech.pre_connect_hooks << proc { |agent, request|
requests << request.class
}
@mech.add_auth(@uri, 'user', 'pass')
page = @mech.post(@uri + '/basic_auth')
assert_equal('You are authenticated', page.body)
assert_equal(2, requests.length)
r1 = requests[0]
r2 = requests[1]
assert_equal(r1, r2)
end
def test_post_entity
@mech.post "http://localhost/form_post", 'json' => '["""]'
assert_equal "json=%5B%22%22%22%5D", requests.first.body
end
def test_post_multiple_values
@mech.post "http://localhost/form_post",
[%w[gender female], %w[gender male]]
assert_equal "gender=female&gender=male", requests.first.body
end
def test_post_multipart
page = @mech.post('http://localhost/file_upload', {
:name => 'Some file',
:userfile1 => File.open(__FILE__)
})
name = File.basename __FILE__
assert_match(
"Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{name}\"",
page.body
)
assert_operator page.body.bytesize, :>, File.size(__FILE__)
end
def test_post_file_upload_nonascii
name = 'ユーザファイル1'
file_upload = Mechanize::Form::FileUpload.new({'name' => 'userfile1'}, name)
file_upload.file_data = File.read(__FILE__)
file_upload.mime_type = 'application/zip'
page = @mech.post('http://localhost/file_upload', {
:name => 'Some file',
:userfile1 => file_upload
})
assert_match(
"Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{name}\"".force_encoding(Encoding::ASCII_8BIT),
page.body
)
assert_match("Content-Type: application/zip", page.body)
assert_operator page.body.bytesize, :>, File.size(__FILE__)
end
def test_post_file_upload
name = File.basename(__FILE__)
file_upload = Mechanize::Form::FileUpload.new({'name' => 'userfile1'}, name)
file_upload.file_data = File.read(__FILE__)
file_upload.mime_type = 'application/zip'
page = @mech.post('http://localhost/file_upload', {
:name => 'Some file',
:userfile1 => file_upload
})
assert_match(
"Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{name}\"",
page.body
)
assert_match("Content-Type: application/zip", page.body)
assert_operator page.body.bytesize, :>, File.size(__FILE__)
end
def test_post_redirect
page = @mech.post('http://localhost/redirect')
assert_equal(page.uri.to_s, 'http://localhost/verb')
assert_equal 'GET', page.header['X-Request-Method']
end