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
|
# coding: US-ASCII
# frozen_string_literal: false
require 'test/unit'
require 'net/http'
require 'stringio'
require_relative 'utils'
class TestNetHTTP < Test::Unit::TestCase
def test_class_Proxy
no_proxy_class = Net::HTTP.Proxy nil
assert_equal Net::HTTP, no_proxy_class
proxy_class = Net::HTTP.Proxy 'proxy.example', 8000, 'user', 'pass'
assert_not_equal Net::HTTP, proxy_class
assert_operator proxy_class, :<, Net::HTTP
assert_equal 'proxy.example', proxy_class.proxy_address
assert_equal 8000, proxy_class.proxy_port
assert_equal 'user', proxy_class.proxy_user
assert_equal 'pass', proxy_class.proxy_pass
http = proxy_class.new 'hostname.example'
assert_not_predicate http, :proxy_from_env?
proxy_class = Net::HTTP.Proxy 'proxy.example'
assert_equal 'proxy.example', proxy_class.proxy_address
assert_equal 80, proxy_class.proxy_port
end
def test_class_Proxy_from_ENV
TestNetHTTPUtils.clean_http_proxy_env do
ENV['http_proxy'] = 'http://proxy.example:8000'
# These are ignored on purpose. See Bug 4388 and Feature 6546
ENV['http_proxy_user'] = 'user'
ENV['http_proxy_pass'] = 'pass'
proxy_class = Net::HTTP.Proxy :ENV
assert_not_equal Net::HTTP, proxy_class
assert_operator proxy_class, :<, Net::HTTP
assert_nil proxy_class.proxy_address
assert_nil proxy_class.proxy_user
assert_nil proxy_class.proxy_pass
assert_not_equal 8000, proxy_class.proxy_port
http = proxy_class.new 'hostname.example'
assert http.proxy_from_env?
end
end
def test_addr_port
http = Net::HTTP.new 'hostname.example', nil, nil
addr_port = http.__send__ :addr_port
assert_equal 'hostname.example', addr_port
http.use_ssl = true
addr_port = http.__send__ :addr_port
assert_equal 'hostname.example:80', addr_port
http = Net::HTTP.new '203.0.113.1', nil, nil
addr_port = http.__send__ :addr_port
assert_equal '203.0.113.1', addr_port
http.use_ssl = true
addr_port = http.__send__ :addr_port
assert_equal '203.0.113.1:80', addr_port
http = Net::HTTP.new '2001:db8::1', nil, nil
addr_port = http.__send__ :addr_port
assert_equal '[2001:db8::1]', addr_port
http.use_ssl = true
addr_port = http.__send__ :addr_port
assert_equal '[2001:db8::1]:80', addr_port
end
def test_edit_path
http = Net::HTTP.new 'hostname.example', nil, nil
edited = http.send :edit_path, '/path'
assert_equal '/path', edited
http.use_ssl = true
edited = http.send :edit_path, '/path'
assert_equal '/path', edited
end
def test_edit_path_proxy
http = Net::HTTP.new 'hostname.example', nil, 'proxy.example'
edited = http.send :edit_path, '/path'
assert_equal 'http://hostname.example/path', edited
http.use_ssl = true
edited = http.send :edit_path, '/path'
assert_equal '/path', edited
end
def test_proxy_address
TestNetHTTPUtils.clean_http_proxy_env do
http = Net::HTTP.new 'hostname.example', nil, 'proxy.example'
assert_equal 'proxy.example', http.proxy_address
http = Net::HTTP.new 'hostname.example', nil
assert_equal nil, http.proxy_address
end
end
def test_proxy_address_no_proxy
TestNetHTTPUtils.clean_http_proxy_env do
http = Net::HTTP.new 'hostname.example', nil, 'proxy.example', nil, nil, nil, 'example'
assert_nil http.proxy_address
http = Net::HTTP.new '10.224.1.1', nil, 'proxy.example', nil, nil, nil, 'example,10.224.0.0/22'
assert_nil http.proxy_address
end
end
def test_proxy_from_env_ENV
TestNetHTTPUtils.clean_http_proxy_env do
ENV['http_proxy'] = 'http://proxy.example:8000'
assert_equal false, Net::HTTP.proxy_class?
http = Net::HTTP.new 'hostname.example'
assert_equal true, http.proxy_from_env?
end
end
def test_proxy_address_ENV
TestNetHTTPUtils.clean_http_proxy_env do
ENV['http_proxy'] = 'http://proxy.example:8000'
http = Net::HTTP.new 'hostname.example'
assert_equal 'proxy.example', http.proxy_address
end
end
def test_proxy_eh_no_proxy
TestNetHTTPUtils.clean_http_proxy_env do
assert_equal false, Net::HTTP.new('hostname.example', nil, nil).proxy?
end
end
def test_proxy_eh_ENV
TestNetHTTPUtils.clean_http_proxy_env do
ENV['http_proxy'] = 'http://proxy.example:8000'
http = Net::HTTP.new 'hostname.example'
assert_equal true, http.proxy?
end
end
def test_proxy_eh_ENV_with_user
TestNetHTTPUtils.clean_http_proxy_env do
ENV['http_proxy'] = 'http://foo:bar@proxy.example:8000'
http = Net::HTTP.new 'hostname.example'
assert_equal true, http.proxy?
if Net::HTTP::ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE
assert_equal 'foo', http.proxy_user
assert_equal 'bar', http.proxy_pass
else
assert_nil http.proxy_user
assert_nil http.proxy_pass
end
end
end
def test_proxy_eh_ENV_with_urlencoded_user
TestNetHTTPUtils.clean_http_proxy_env do
ENV['http_proxy'] = 'http://Y%5CX:R%25S%5D%20%3FX@proxy.example:8000'
http = Net::HTTP.new 'hostname.example'
assert_equal true, http.proxy?
if Net::HTTP::ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE
assert_equal "Y\\X", http.proxy_user
assert_equal "R%S] ?X", http.proxy_pass
else
assert_nil http.proxy_user
assert_nil http.proxy_pass
end
end
end
def test_proxy_eh_ENV_none_set
TestNetHTTPUtils.clean_http_proxy_env do
assert_equal false, Net::HTTP.new('hostname.example').proxy?
end
end
def test_proxy_eh_ENV_no_proxy
TestNetHTTPUtils.clean_http_proxy_env do
ENV['http_proxy'] = 'http://proxy.example:8000'
ENV['no_proxy'] = 'hostname.example'
assert_equal false, Net::HTTP.new('hostname.example').proxy?
end
end
def test_proxy_port
TestNetHTTPUtils.clean_http_proxy_env do
http = Net::HTTP.new 'example', nil, 'proxy.example'
assert_equal 'proxy.example', http.proxy_address
assert_equal 80, http.proxy_port
http = Net::HTTP.new 'example', nil, 'proxy.example', 8000
assert_equal 8000, http.proxy_port
http = Net::HTTP.new 'example', nil
assert_equal nil, http.proxy_port
end
end
def test_proxy_port_ENV
TestNetHTTPUtils.clean_http_proxy_env do
ENV['http_proxy'] = 'http://proxy.example:8000'
http = Net::HTTP.new 'hostname.example'
assert_equal 8000, http.proxy_port
end
end
def test_newobj
TestNetHTTPUtils.clean_http_proxy_env do
ENV['http_proxy'] = 'http://proxy.example:8000'
http = Net::HTTP.newobj 'hostname.example'
assert_equal false, http.proxy?
end
end
def test_failure_message_includes_failed_domain_and_port
# hostname to be included in the error message
host = Struct.new(:to_s).new("<example>")
port = 2119
# hack to let TCPSocket.open fail
def host.to_str; raise SocketError, "open failure"; end
uri = Struct.new(:scheme, :hostname, :port).new("http", host, port)
assert_raise_with_message(SocketError, /#{host}:#{port}/) do
TestNetHTTPUtils.clean_http_proxy_env{ Net::HTTP.get(uri) }
end
end
end
module TestNetHTTP_version_1_1_methods
def test_s_start
begin
h = Net::HTTP.start(config('host'), config('port'))
ensure
h&.finish
end
assert_equal config('host'), h.address
assert_equal config('port'), h.port
assert_equal true, h.instance_variable_get(:@proxy_from_env)
begin
h = Net::HTTP.start(config('host'), config('port'), :ENV)
ensure
h&.finish
end
assert_equal config('host'), h.address
assert_equal config('port'), h.port
assert_equal true, h.instance_variable_get(:@proxy_from_env)
begin
h = Net::HTTP.start(config('host'), config('port'), nil)
ensure
h&.finish
end
assert_equal config('host'), h.address
assert_equal config('port'), h.port
assert_equal false, h.instance_variable_get(:@proxy_from_env)
end
def test_s_get
assert_equal $test_net_http_data,
Net::HTTP.get(config('host'), '/', config('port'))
assert_equal $test_net_http_data, Net::HTTP.get(
URI.parse("http://#{config('host')}:#{config('port')}")
)
assert_equal $test_net_http_data, Net::HTTP.get(
URI.parse("http://#{config('host')}:#{config('port')}"), "Accept" => "text/plain"
)
end
def test_s_get_response
res = Net::HTTP.get_response(
URI.parse("http://#{config('host')}:#{config('port')}")
)
assert_equal "application/octet-stream", res["Content-Type"]
assert_equal $test_net_http_data, res.body
res = Net::HTTP.get_response(
URI.parse("http://#{config('host')}:#{config('port')}"), "Accept" => "text/plain"
)
assert_equal "text/plain", res["Content-Type"]
assert_equal $test_net_http_data, res.body
end
def test_head
start {|http|
res = http.head('/')
assert_kind_of Net::HTTPResponse, res
assert_equal $test_net_http_data_type, res['Content-Type']
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_equal $test_net_http_data.size, res['Content-Length'].to_i
end
}
end
def test_get
start {|http|
_test_get__get http
_test_get__iter http
_test_get__chunked http
}
end
def _test_get__get(http)
res = http.get('/')
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_equal $test_net_http_data_type, res['Content-Type']
assert_equal $test_net_http_data.size, res.body.size
assert_equal $test_net_http_data, res.body
assert_nothing_raised {
http.get('/', { 'User-Agent' => 'test' }.freeze)
}
assert res.decode_content, '[Bug #7924]' if Net::HTTP::HAVE_ZLIB
end
def _test_get__iter(http)
buf = ''
res = http.get('/') {|s| buf << s }
assert_kind_of Net::HTTPResponse, res
# assert_kind_of String, res.body
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_equal $test_net_http_data_type, res['Content-Type']
assert_equal $test_net_http_data.size, buf.size
assert_equal $test_net_http_data, buf
# assert_equal $test_net_http_data.size, res.body.size
# assert_equal $test_net_http_data, res.body
end
def _test_get__chunked(http)
buf = ''
res = http.get('/') {|s| buf << s }
assert_kind_of Net::HTTPResponse, res
# assert_kind_of String, res.body
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_equal $test_net_http_data_type, res['Content-Type']
assert_equal $test_net_http_data.size, buf.size
assert_equal $test_net_http_data, buf
# assert_equal $test_net_http_data.size, res.body.size
# assert_equal $test_net_http_data, res.body
end
def test_get__break
i = 0
start {|http|
http.get('/') do |str|
i += 1
break
end
}
assert_equal 1, i
@log_tester = nil # server may encount ECONNRESET
end
def test_get__implicit_start
res = new().get('/')
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
end
assert_equal $test_net_http_data_type, res['Content-Type']
assert_equal $test_net_http_data.size, res.body.size
assert_equal $test_net_http_data, res.body
end
def test_get__crlf
start {|http|
assert_raise(ArgumentError) do
http.get("\r")
end
assert_raise(ArgumentError) do
http.get("\n")
end
}
end
def test_get2
start {|http|
http.get2('/') {|res|
EnvUtil.suppress_warning do
assert_kind_of Net::HTTPResponse, res
assert_kind_of Net::HTTPResponse, res.header
end
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
end
assert_equal $test_net_http_data_type, res['Content-Type']
assert_kind_of String, res.body
assert_kind_of String, res.entity
assert_equal $test_net_http_data.size, res.body.size
assert_equal $test_net_http_data, res.body
assert_equal $test_net_http_data, res.entity
}
}
end
def test_post
start {|http|
_test_post__base http
_test_post__file http
_test_post__no_data http
}
end
def _test_post__base(http)
uheader = {}
uheader['Accept'] = 'application/octet-stream'
uheader['Content-Type'] = 'application/x-www-form-urlencoded'
data = 'post data'
res = http.post('/', data, uheader)
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
assert_equal data, res.body
assert_equal data, res.entity
end
def _test_post__file(http)
data = 'post data'
f = StringIO.new
http.post('/', data, {'content-type' => 'application/x-www-form-urlencoded'}, f)
assert_equal data, f.string
end
def _test_post__no_data(http)
unless self.is_a?(TestNetHTTP_v1_2_chunked)
EnvUtil.suppress_warning do
data = nil
res = http.post('/', data)
assert_not_equal '411', res.code
end
end
end
def test_s_post
url = "http://#{config('host')}:#{config('port')}/?q=a"
res = assert_warning(/Content-Type did not set/) do
Net::HTTP.post(
URI.parse(url),
"a=x")
end
assert_equal "application/x-www-form-urlencoded", res["Content-Type"]
assert_equal "a=x", res.body
assert_equal url, res["X-request-uri"]
res = Net::HTTP.post(
URI.parse(url),
"hello world",
"Content-Type" => "text/plain; charset=US-ASCII")
assert_equal "text/plain; charset=US-ASCII", res["Content-Type"]
assert_equal "hello world", res.body
end
def test_s_post_form
url = "http://#{config('host')}:#{config('port')}/"
res = Net::HTTP.post_form(
URI.parse(url),
"a" => "x")
assert_equal ["a=x"], res.body.split(/[;&]/).sort
res = Net::HTTP.post_form(
URI.parse(url),
"a" => "x",
"b" => "y")
assert_equal ["a=x", "b=y"], res.body.split(/[;&]/).sort
res = Net::HTTP.post_form(
URI.parse(url),
"a" => ["x1", "x2"],
"b" => "y")
assert_equal url, res['X-request-uri']
assert_equal ["a=x1", "a=x2", "b=y"], res.body.split(/[;&]/).sort
res = Net::HTTP.post_form(
URI.parse(url + '?a=x'),
"b" => "y")
assert_equal url + '?a=x', res['X-request-uri']
assert_equal ["b=y"], res.body.split(/[;&]/).sort
end
def test_patch
start {|http|
_test_patch__base http
}
end
def _test_patch__base(http)
uheader = {}
uheader['Accept'] = 'application/octet-stream'
uheader['Content-Type'] = 'application/x-www-form-urlencoded'
data = 'patch data'
res = http.patch('/', data, uheader)
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
assert_equal data, res.body
assert_equal data, res.entity
end
def test_timeout_during_HTTP_session_write
th = nil
# listen for connections... but deliberately do not read
TCPServer.open('localhost', 0) {|server|
port = server.addr[1]
conn = Net::HTTP.new('localhost', port)
conn.write_timeout = EnvUtil.apply_timeout_scale(0.01)
conn.read_timeout = EnvUtil.apply_timeout_scale(0.01) if windows?
conn.open_timeout = EnvUtil.apply_timeout_scale(0.1)
th = Thread.new do
err = !windows? ? Net::WriteTimeout : Net::ReadTimeout
assert_raise(err) do
assert_warning(/Content-Type did not set/) do
conn.post('/', "a"*50_000_000)
end
end
end
assert th.join(EnvUtil.apply_timeout_scale(10))
}
ensure
th&.kill
th&.join
end
def test_timeout_during_non_chunked_streamed_HTTP_session_write
th = nil
# listen for connections... but deliberately do not read
TCPServer.open('localhost', 0) {|server|
port = server.addr[1]
conn = Net::HTTP.new('localhost', port)
conn.write_timeout = 0.01
conn.read_timeout = 0.01 if windows?
conn.open_timeout = 0.1
req = Net::HTTP::Post.new('/')
data = "a"*50_000_000
req.content_length = data.size
req['Content-Type'] = 'application/x-www-form-urlencoded'
req.body_stream = StringIO.new(data)
th = Thread.new do
assert_raise(Net::WriteTimeout) { conn.request(req) }
end
assert th.join(10)
}
ensure
th&.kill
th&.join
end
def test_timeout_during_HTTP_session
bug4246 = "expected the HTTP session to have timed out but have not. c.f. [ruby-core:34203]"
th = nil
# listen for connections... but deliberately do not read
TCPServer.open('localhost', 0) {|server|
port = server.addr[1]
conn = Net::HTTP.new('localhost', port)
conn.read_timeout = EnvUtil.apply_timeout_scale(0.01)
conn.open_timeout = EnvUtil.apply_timeout_scale(1)
th = Thread.new do
assert_raise(Net::ReadTimeout) {
conn.get('/')
}
end
assert th.join(EnvUtil.apply_timeout_scale(10)), bug4246
}
ensure
th.kill
th.join
end
end
module TestNetHTTP_version_1_2_methods
def test_request
start {|http|
_test_request__GET http
_test_request__accept_encoding http
_test_request__file http
# _test_request__range http # WEBrick does not support Range: header.
_test_request__HEAD http
_test_request__POST http
_test_request__stream_body http
_test_request__uri http
_test_request__uri_host http
}
end
def _test_request__GET(http)
req = Net::HTTP::Get.new('/')
http.request(req) {|res|
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_equal $test_net_http_data.size, res.body.size
assert_equal $test_net_http_data, res.body
assert res.decode_content, 'Bug #7831' if Net::HTTP::HAVE_ZLIB
}
end
def _test_request__accept_encoding(http)
req = Net::HTTP::Get.new('/', 'accept-encoding' => 'deflate')
http.request(req) {|res|
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_equal $test_net_http_data.size, res.body.size
assert_equal $test_net_http_data, res.body
assert_not_predicate res, :decode_content, 'Bug #7831' if Net::HTTP::HAVE_ZLIB
}
end
def _test_request__file(http)
req = Net::HTTP::Get.new('/')
http.request(req) {|res|
assert_kind_of Net::HTTPResponse, res
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
f = StringIO.new("".force_encoding("ASCII-8BIT"))
res.read_body f
assert_equal $test_net_http_data.bytesize, f.string.bytesize
assert_equal $test_net_http_data.encoding, f.string.encoding
assert_equal $test_net_http_data, f.string
}
end
def _test_request__range(http)
req = Net::HTTP::Get.new('/')
req['range'] = 'bytes=0-5'
assert_equal $test_net_http_data[0,6], http.request(req).body
end
def _test_request__HEAD(http)
req = Net::HTTP::Head.new('/')
http.request(req) {|res|
assert_kind_of Net::HTTPResponse, res
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_nil res.body
}
end
def _test_request__POST(http)
data = 'post data'
req = Net::HTTP::Post.new('/')
req['Accept'] = $test_net_http_data_type
req['Content-Type'] = 'application/x-www-form-urlencoded'
http.request(req, data) {|res|
assert_kind_of Net::HTTPResponse, res
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_equal data.size, res['content-length'].to_i
end
assert_kind_of String, res.body
assert_equal data, res.body
}
end
def _test_request__stream_body(http)
req = Net::HTTP::Post.new('/')
data = $test_net_http_data
req.content_length = data.size
req['Content-Type'] = 'application/x-www-form-urlencoded'
req.body_stream = StringIO.new(data)
res = http.request(req)
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
assert_equal data.size, res.body.size
assert_equal data, res.body
end
def _test_request__path(http)
uri = URI 'https://hostname.example/'
req = Net::HTTP::Get.new('/')
res = http.request(req)
assert_kind_of URI::Generic, req.uri
assert_not_equal uri, req.uri
assert_equal uri, res.uri
assert_not_same uri, req.uri
assert_not_same req.uri, res.uri
end
def _test_request__uri(http)
uri = URI 'https://hostname.example/'
req = Net::HTTP::Get.new(uri)
res = http.request(req)
assert_kind_of URI::Generic, req.uri
assert_not_equal uri, req.uri
assert_equal req.uri, res.uri
assert_not_same uri, req.uri
assert_not_same req.uri, res.uri
end
def _test_request__uri_host(http)
uri = URI 'http://other.example/'
req = Net::HTTP::Get.new(uri)
req['host'] = 'hostname.example'
res = http.request(req)
assert_kind_of URI::Generic, req.uri
assert_equal URI("http://hostname.example:#{http.port}"), res.uri
end
def test_send_request
start {|http|
_test_send_request__GET http
_test_send_request__HEAD http
_test_send_request__POST http
}
end
def _test_send_request__GET(http)
res = http.send_request('GET', '/')
assert_kind_of Net::HTTPResponse, res
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_kind_of String, res.body
assert_equal $test_net_http_data, res.body
end
def _test_send_request__HEAD(http)
res = http.send_request('HEAD', '/')
assert_kind_of Net::HTTPResponse, res
unless self.is_a?(TestNetHTTP_v1_2_chunked)
assert_not_nil res['content-length']
assert_equal $test_net_http_data.size, res['content-length'].to_i
end
assert_nil res.body
end
def _test_send_request__POST(http)
data = 'aaabbb cc ddddddddddd lkjoiu4j3qlkuoa'
res = http.send_request('POST', '/', data, 'content-type' => 'application/x-www-form-urlencoded')
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
assert_equal data.size, res.body.size
assert_equal data, res.body
end
def test_set_form
require 'tempfile'
Tempfile.create('ruby-test') {|file|
file << "\u{30c7}\u{30fc}\u{30bf}"
data = [
['name', 'Gonbei Nanashi'],
['name', "\u{540d}\u{7121}\u{3057}\u{306e}\u{6a29}\u{5175}\u{885b}"],
['s"i\o', StringIO.new("\u{3042 3044 4e9c 925b}")],
["file", file, filename: "ruby-test"]
]
expected = <<"__EOM__".gsub(/\n/, "\r\n")
--<boundary>
Content-Disposition: form-data; name="name"
Gonbei Nanashi
--<boundary>
Content-Disposition: form-data; name="name"
\xE5\x90\x8D\xE7\x84\xA1\xE3\x81\x97\xE3\x81\xAE\xE6\xA8\xA9\xE5\x85\xB5\xE8\xA1\x9B
--<boundary>
Content-Disposition: form-data; name="s\\"i\\\\o"
\xE3\x81\x82\xE3\x81\x84\xE4\xBA\x9C\xE9\x89\x9B
--<boundary>
Content-Disposition: form-data; name="file"; filename="ruby-test"
Content-Type: application/octet-stream
\xE3\x83\x87\xE3\x83\xBC\xE3\x82\xBF
--<boundary>--
__EOM__
start {|http|
_test_set_form_urlencoded(http, data.reject{|k,v|!v.is_a?(String)})
_test_set_form_multipart(http, false, data, expected)
_test_set_form_multipart(http, true, data, expected)
}
}
end
def _test_set_form_urlencoded(http, data)
req = Net::HTTP::Post.new('/')
req.set_form(data)
res = http.request req
assert_equal "name=Gonbei+Nanashi&name=%E5%90%8D%E7%84%A1%E3%81%97%E3%81%AE%E6%A8%A9%E5%85%B5%E8%A1%9B", res.body
end
def _test_set_form_multipart(http, chunked_p, data, expected)
data.each{|k,v|v.rewind rescue nil}
req = Net::HTTP::Post.new('/')
req.set_form(data, 'multipart/form-data')
req['Transfer-Encoding'] = 'chunked' if chunked_p
res = http.request req
body = res.body
assert_match(/\A--(?<boundary>\S+)/, body)
/\A--(?<boundary>\S+)/ =~ body
expected = expected.gsub(/<boundary>/, boundary)
assert_equal(expected, body)
end
def test_set_form_with_file
require 'tempfile'
Tempfile.create('ruby-test') {|file|
file.binmode
file << $test_net_http_data
filename = File.basename(file.to_path)
data = [['file', file]]
expected = <<"__EOM__".gsub(/\n/, "\r\n")
--<boundary>
Content-Disposition: form-data; name="file"; filename="<filename>"
Content-Type: application/octet-stream
<data>
--<boundary>--
__EOM__
expected.sub!(/<filename>/, filename)
expected.sub!(/<data>/, $test_net_http_data)
start {|http|
data.each{|k,v|v.rewind rescue nil}
req = Net::HTTP::Post.new('/')
req.set_form(data, 'multipart/form-data')
res = http.request req
body = res.body
header, _ = body.split(/\r\n\r\n/, 2)
assert_match(/\A--(?<boundary>\S+)/, body)
/\A--(?<boundary>\S+)/ =~ body
expected = expected.gsub(/<boundary>/, boundary)
assert_match(/^--(?<boundary>\S+)\r\n/, header)
assert_match(
/^Content-Disposition: form-data; name="file"; filename="#{filename}"\r\n/,
header)
assert_equal(expected, body)
data.each{|k,v|v.rewind rescue nil}
req['Transfer-Encoding'] = 'chunked'
res = http.request req
#assert_equal(expected, res.body)
}
}
end
end
class TestNetHTTP_v1_2 < Test::Unit::TestCase
CONFIG = {
'host' => '127.0.0.1',
'proxy_host' => nil,
'proxy_port' => nil,
}
include TestNetHTTPUtils
include TestNetHTTP_version_1_1_methods
include TestNetHTTP_version_1_2_methods
def new
Net::HTTP.version_1_2
super
end
def test_send_large_POST_request
start {|http|
data = ' '*6000000
res = http.send_request('POST', '/', data, 'content-type' => 'application/x-www-form-urlencoded')
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
assert_equal data.size, res.body.size
assert_equal data, res.body
}
end
end
class TestNetHTTP_v1_2_chunked < Test::Unit::TestCase
CONFIG = {
'host' => '127.0.0.1',
'proxy_host' => nil,
'proxy_port' => nil,
'chunked' => true,
}
include TestNetHTTPUtils
include TestNetHTTP_version_1_1_methods
include TestNetHTTP_version_1_2_methods
def new
Net::HTTP.version_1_2
super
end
def test_chunked_break
assert_nothing_raised("[ruby-core:29229]") {
start {|http|
http.request_get('/') {|res|
res.read_body {|chunk|
break
}
}
}
}
end
end
class TestNetHTTPContinue < Test::Unit::TestCase
CONFIG = {
'host' => '127.0.0.1',
'proxy_host' => nil,
'proxy_port' => nil,
'chunked' => true,
}
include TestNetHTTPUtils
def logfile
@debug = StringIO.new('')
end
def mount_proc(&block)
@server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc))
end
def test_expect_continue
mount_proc {|req, res|
req.continue
res.body = req.query['body']
}
start {|http|
uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}
http.continue_timeout = 0.2
http.request_post('/continue', 'body=BODY', uheader) {|res|
assert_equal('BODY', res.read_body)
}
}
assert_match(/Expect: 100-continue/, @debug.string)
assert_match(/HTTP\/1.1 100 continue/, @debug.string)
end
def test_expect_continue_timeout
mount_proc {|req, res|
sleep 0.2
req.continue # just ignored because it's '100'
res.body = req.query['body']
}
start {|http|
uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}
http.continue_timeout = 0
http.request_post('/continue', 'body=BODY', uheader) {|res|
assert_equal('BODY', res.read_body)
}
}
assert_match(/Expect: 100-continue/, @debug.string)
assert_match(/HTTP\/1.1 100 continue/, @debug.string)
end
def test_expect_continue_error
mount_proc {|req, res|
res.status = 501
res.body = req.query['body']
}
start {|http|
uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}
http.continue_timeout = 0
http.request_post('/continue', 'body=ERROR', uheader) {|res|
assert_equal('ERROR', res.read_body)
}
}
assert_match(/Expect: 100-continue/, @debug.string)
assert_not_match(/HTTP\/1.1 100 continue/, @debug.string)
end
def test_expect_continue_error_before_body
@log_tester = nil
mount_proc {|req, res|
raise WEBrick::HTTPStatus::Forbidden
}
start {|http|
uheader = {'content-type' => 'application/x-www-form-urlencoded', 'content-length' => '5', 'expect' => '100-continue'}
http.continue_timeout = 1 # allow the server to respond before sending
http.request_post('/continue', 'data', uheader) {|res|
assert_equal(res.code, '403')
}
}
assert_match(/Expect: 100-continue/, @debug.string)
assert_not_match(/HTTP\/1.1 100 continue/, @debug.string)
end
def test_expect_continue_error_while_waiting
mount_proc {|req, res|
res.status = 501
res.body = req.query['body']
}
start {|http|
uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}
http.continue_timeout = 0.5
http.request_post('/continue', 'body=ERROR', uheader) {|res|
assert_equal('ERROR', res.read_body)
}
}
assert_match(/Expect: 100-continue/, @debug.string)
assert_not_match(/HTTP\/1.1 100 continue/, @debug.string)
end
end
class TestNetHTTPSwitchingProtocols < Test::Unit::TestCase
CONFIG = {
'host' => '127.0.0.1',
'proxy_host' => nil,
'proxy_port' => nil,
'chunked' => true,
}
include TestNetHTTPUtils
def logfile
@debug = StringIO.new('')
end
def mount_proc(&block)
@server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc))
end
def test_info
mount_proc {|req, res|
req.instance_variable_get(:@socket) << "HTTP/1.1 101 Switching Protocols\r\n\r\n"
res.body = req.query['body']
}
start {|http|
http.continue_timeout = 0.2
http.request_post('/continue', 'body=BODY',
'content-type' => 'application/x-www-form-urlencoded') {|res|
assert_equal('BODY', res.read_body)
}
}
assert_match(/HTTP\/1.1 101 Switching Protocols/, @debug.string)
end
end
class TestNetHTTPKeepAlive < Test::Unit::TestCase
CONFIG = {
'host' => '127.0.0.1',
'proxy_host' => nil,
'proxy_port' => nil,
'RequestTimeout' => 1,
}
include TestNetHTTPUtils
def test_keep_alive_get_auto_reconnect
start {|http|
res = http.get('/')
http.keep_alive_timeout = 1
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
sleep 1.5
assert_nothing_raised {
res = http.get('/')
}
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
}
end
def test_server_closed_connection_auto_reconnect
start {|http|
res = http.get('/')
http.keep_alive_timeout = 5
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
sleep 1.5
assert_nothing_raised {
# Net::HTTP should detect the closed connection before attempting the
# request, since post requests cannot be retried.
res = http.post('/', 'query=foo', 'content-type' => 'application/x-www-form-urlencoded')
}
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
}
end
def test_keep_alive_get_auto_retry
start {|http|
res = http.get('/')
http.keep_alive_timeout = 5
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
sleep 1.5
res = http.get('/')
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
}
end
def test_keep_alive_reset_on_new_connection
# Using WEBrick's debug log output on accepting connection:
#
# "[2021-04-29 20:36:46] DEBUG accept: 127.0.0.1:50674\n"
@log_tester = nil
@server.logger.level = WEBrick::BasicLog::DEBUG
start {|http|
res = http.get('/')
http.keep_alive_timeout = 1
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
http.finish
assert_equal 1, @log.grep(/accept/i).size
sleep 1.5
http.start
res = http.get('/')
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
assert_equal 2, @log.grep(/accept/i).size
}
end
class MockSocket
attr_reader :count
def initialize(success_after: nil)
@success_after = success_after
@count = 0
end
def close
end
def closed?
end
def write(_)
end
def readline
@count += 1
if @success_after && @success_after <= @count
"HTTP/1.1 200 OK"
else
raise Errno::ECONNRESET
end
end
def readuntil(*_)
""
end
def read_all(_)
end
end
def test_http_retry_success
start {|http|
socket = MockSocket.new(success_after: 10)
http.instance_variable_get(:@socket).close
http.instance_variable_set(:@socket, socket)
assert_equal 0, socket.count
http.max_retries = 10
res = http.get('/')
assert_equal 10, socket.count
assert_kind_of Net::HTTPResponse, res
assert_kind_of String, res.body
}
end
def test_http_retry_failed
start {|http|
socket = MockSocket.new
http.instance_variable_get(:@socket).close
http.instance_variable_set(:@socket, socket)
http.max_retries = 10
assert_raise(Errno::ECONNRESET){ http.get('/') }
assert_equal 11, socket.count
}
end
def test_keep_alive_server_close
def @server.run(sock)
sock.close
end
start {|http|
assert_raise(EOFError, Errno::ECONNRESET, IOError) {
http.get('/')
}
}
end
end
class TestNetHTTPLocalBind < Test::Unit::TestCase
CONFIG = {
'host' => 'localhost',
'proxy_host' => nil,
'proxy_port' => nil,
}
include TestNetHTTPUtils
def test_bind_to_local_host
@server.mount_proc('/show_ip') { |req, res| res.body = req.remote_ip }
http = Net::HTTP.new(config('host'), config('port'))
http.local_host = Addrinfo.tcp(config('host'), config('port')).ip_address
assert_not_nil(http.local_host)
assert_nil(http.local_port)
res = http.get('/show_ip')
assert_equal(http.local_host, res.body)
end
def test_bind_to_local_port
@server.mount_proc('/show_port') { |req, res| res.body = req.peeraddr[1].to_s }
http = Net::HTTP.new(config('host'), config('port'))
http.local_host = Addrinfo.tcp(config('host'), config('port')).ip_address
http.local_port = Addrinfo.tcp(config('host'), 0).bind {|s|
s.local_address.ip_port.to_s
}
assert_not_nil(http.local_host)
assert_not_nil(http.local_port)
res = http.get('/show_port')
assert_equal(http.local_port, res.body)
end
end
|