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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
|
# 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
page = nil
page = @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
def test_put
page = @mech.put('http://localhost/verb', 'foo')
assert_equal 1, @mech.history.length
assert_equal 'PUT', page.header['X-Request-Method']
end
def test_put_redirect
page = @mech.put('http://localhost/redirect', 'foo')
assert_equal(page.uri.to_s, 'http://localhost/verb')
assert_equal 'GET', page.header['X-Request-Method']
end
def test_read_timeout_equals
@mech.read_timeout = 5
assert_equal 5, @mech.read_timeout
end
def test_timeouts_for_file_connection
uri = URI.parse "file://#{File.expand_path __FILE__}"
@mech.read_timeout = 5
@mech.open_timeout = 5
assert @mech.get(uri)
end
def test_referer
host_path = "localhost/tc_referer.html?t=1"
['http', 'https'].each { |proto|
referer = "#{proto}://#{host_path}"
[
"",
"@",
"user1@",
":@",
"user1:@",
":password1@",
"user1:password1@",
].each { |userinfo|
url = "#{proto}://#{userinfo}#{host_path}"
[url, url + "#foo"].each { |furl|
[
['relative', true],
['insecure', proto == 'http'],
['secure', true],
['relative noreferrer', false],
['insecure noreferrer', false],
['secure noreferrer', false],
].each_with_index { |(type, bool), i|
rpage = @mech.get(furl)
page = rpage.links[i].click
assert_equal bool ? referer : '', page.body, "%s link from %s" % [type, furl]
}
rpage = @mech.get(furl)
page = rpage.forms.first.submit
assert_equal referer, page.body, "post from %s" % furl
}
}
}
end
def test_retry_change_requests_equals
refute @mech.retry_change_requests
@mech.retry_change_requests = true
assert @mech.retry_change_requests
end
def test_set_proxy
http = @mech.agent.http
@mech.set_proxy 'localhost', 8080, 'user', 'pass'
assert_equal 'localhost', @mech.proxy_addr
assert_equal 8080, @mech.proxy_port
assert_equal 'user', @mech.proxy_user
assert_equal 'pass', @mech.proxy_pass
assert_equal URI('http://user:pass@localhost:8080'), http.proxy_uri
end
def test_shutdown
uri = URI 'http://localhost'
jar = HTTP::CookieJar.new
jar.parse 'a=b', uri
@mech.cookie_jar = jar
@mech.get("http://localhost/")
assert_match(/Hello World/, @mech.current_page.body)
refute_empty @mech.cookies
@mech.shutdown
assert_empty @mech.history
assert_empty @mech.cookies
end
def test_start
body = nil
Mechanize.start do |m|
body = m.get("http://localhost/").body
end
assert_match(/Hello World/, body)
end
def test_submit_bad_form_method
page = @mech.get("http://localhost/bad_form_test.html")
assert_raises ArgumentError do
@mech.submit(page.forms.first)
end
end
def test_submit_check_one
page = @mech.get('http://localhost/tc_checkboxes.html')
form = page.forms.first
form.checkboxes_with(:name => 'green')[1].check
page = @mech.submit(form)
assert_equal(1, page.links.length)
assert_equal('green:on', page.links.first.text)
end
def test_submit_check_two
page = @mech.get('http://localhost/tc_checkboxes.html')
form = page.forms.first
form.checkboxes_with(:name => 'green')[0].check
form.checkboxes_with(:name => 'green')[1].check
page = @mech.submit(form)
assert_equal(2, page.links.length)
assert_equal('green:on', page.links[0].text)
assert_equal('green:on', page.links[1].text)
end
def test_submit_enctype
page = @mech.get("http://localhost/file_upload.html")
assert_equal('multipart/form-data', page.forms[0].enctype)
form = page.forms.first
form.file_uploads.first.file_name = __FILE__
form.file_uploads.first.mime_type = "text/plain"
form.file_uploads.first.file_data = "Hello World\n\n"
page = @mech.submit(form)
basename = File.basename __FILE__
assert_match(
"Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{basename}\"",
page.body
)
assert_match(
"Content-Disposition: form-data; name=\"name\"",
page.body
)
assert_match('Content-Type: text/plain', page.body)
assert_match('Hello World', page.body)
assert_match('foo[aaron]', page.body)
end
def test_submit_file_data
page = @mech.get("http://localhost/file_upload.html")
assert_equal('multipart/form-data', page.forms[1].enctype)
form = page.forms[1]
form.file_uploads.first.file_name = __FILE__
form.file_uploads.first.file_data = File.read __FILE__
page = @mech.submit(form)
contents = File.binread __FILE__
basename = File.basename __FILE__
assert_match(
"Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"#{basename}\"",
page.body
)
assert_match(contents, page.body)
end
def test_submit_file_name
page = @mech.get("http://localhost/file_upload.html")
assert_equal('multipart/form-data', page.forms[1].enctype)
form = page.forms[1]
form.file_uploads.first.file_name = __FILE__
page = @mech.submit(form)
contents = File.binread __FILE__
basename = File.basename __FILE__
assert_match(
"Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"#{basename}\"",
page.body
)
assert_match(contents, page.body)
end
def test_submit_get
form = node 'form', 'method' => 'GET', 'action' => '/?a=b'
form = Mechanize::Form.new form, @mech, fake_page
@mech.submit form
assert_equal '/', requests.first.path
end
def test_submit_headers
page = @mech.get 'http://localhost:2000/form_no_action.html'
assert form = page.forms.first
form.action = '/http_headers'
page = @mech.submit form, nil, 'foo' => 'bar'
headers = page.body.split("\n").map { |x| x.split('|', 2) }.flatten
headers = Hash[*headers]
assert_equal 'bar', headers['foo']
end
def test_submit_multipart
page = @mech.get("http://localhost/file_upload.html")
assert_equal('multipart/form-data', page.forms[1].enctype)
form = page.forms[1]
form.file_uploads.first.file_name = __FILE__
form.file_uploads.first.mime_type = "text/plain"
form.file_uploads.first.file_data = "Hello World\n\n"
page = @mech.submit(form)
basename = File.basename __FILE__
assert_match(
"Content-Disposition: form-data; name=\"green[eggs]\"; filename=\"#{basename}\"",
page.body
)
end
def test_submit_no_file
page = @mech.get("http://localhost/file_upload.html")
form = page.forms.first
form.field_with(:name => 'name').value = 'Aaron'
@page = @mech.submit(form)
assert_match('Aaron', @page.body)
assert_match(
"Content-Disposition: form-data; name=\"userfile1\"; filename=\"\"",
@page.body
)
end
def test_submit_post
form = node 'form', 'method' => 'POST', 'action' => '/?a=b'
form = Mechanize::Form.new form, @mech, fake_page
@mech.submit form
assert_equal '/?a=b', requests.first.path
end
def test_submit_post_pound
form = node 'form', 'method' => 'POST', 'action' => '/#1'
form = Mechanize::Form.new form, @mech, fake_page
@mech.submit form
assert_equal '/', requests.first.path
end
def test_submit_too_many_radiobuttons
page = @mech.get("http://localhost/form_test.html")
form = page.form_with(:name => 'post_form1')
form.radiobuttons.each { |r| r.checked = true }
assert_raises Mechanize::Error do
@mech.submit(form)
end
end
def test_transact
@mech.get("http://localhost/frame_test.html")
assert_equal(1, @mech.history.length)
@mech.transact { |a|
5.times {
@mech.get("http://localhost/frame_test.html")
}
assert_equal(6, @mech.history.length)
}
assert_equal(1, @mech.history.length)
end
def test_user_agent_alias_equals_unknown
assert_raises ArgumentError do
@mech.user_agent_alias = "Aaron's Browser"
end
end
def test_verify_mode
assert_equal OpenSSL::SSL::VERIFY_PEER, @mech.verify_mode
@mech.verify_mode = OpenSSL::SSL::VERIFY_NONE
assert_equal OpenSSL::SSL::VERIFY_NONE, @mech.verify_mode
end
def test_visited_eh
@mech.get("http://localhost/content_type_test?ct=application/pdf")
assert \
@mech.visited?("http://localhost/content_type_test?ct=application/pdf")
assert \
!@mech.visited?("http://localhost/content_type_test")
assert \
!@mech.visited?("http://localhost/content_type_test?ct=text/html")
end
def test_visited_eh_link
@mech.get("http://example/index.html")
page = page URI 'http://example'
link = node 'a', 'href' => '/index.html'
link = Mechanize::Page::Link.new link, page, @mech
assert @mech.visited? link
end
def test_visited_eh_redirect
@mech.get("http://localhost/response_code?code=302")
assert_equal("http://localhost/index.html", @mech.current_page.uri.to_s)
assert @mech.visited?('http://localhost/response_code?code=302')
end
def test_no_frames_exists
page = @mech.get("http://localhost/empty_form.html");
assert_nil page.frame_with(:name => 'noframe')
assert_raises Mechanize::ElementNotFoundError do
page.frame_with!(:name => 'noframe')
end
end
def assert_header(page, header)
headers = {}
page.body.split(/[\r\n]+/).each do |page_header|
headers.[]=(*page_header.chomp.split(/\|/))
end
header.each do |key, value|
assert(headers.has_key?(key))
assert_equal(value, headers[key])
end
end
end
|