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
|
require 'spec_helper'
require 'puppet/http'
describe Puppet::HTTP::Client do
let(:uri) { URI.parse('https://www.example.com') }
let(:puppet_context) { Puppet::SSL::SSLContext.new }
let(:system_context) { Puppet::SSL::SSLContext.new }
let(:client) { described_class.new(ssl_context: puppet_context, system_ssl_context: system_context) }
let(:credentials) { ['user', 'pass'] }
it 'creates unique sessions' do
expect(client.create_session).to_not eq(client.create_session)
end
context "when connecting" do
it 'connects to HTTP URLs' do
uri = URI.parse('http://www.example.com')
client.connect(uri) do |http|
expect(http.address).to eq('www.example.com')
expect(http.port).to eq(80)
expect(http).to_not be_use_ssl
end
end
it 'connects to HTTPS URLs' do
client.connect(uri) do |http|
expect(http.address).to eq('www.example.com')
expect(http.port).to eq(443)
expect(http).to be_use_ssl
end
end
it 'raises ConnectionError if the connection is refused' do
allow_any_instance_of(Net::HTTP).to receive(:start).and_raise(Errno::ECONNREFUSED)
expect {
client.connect(uri)
}.to raise_error(Puppet::HTTP::ConnectionError, %r{^Request to https://www.example.com failed after .* seconds: (Connection refused|No connection could be made because the target machine actively refused it)})
end
it 'raises ConnectionError if the connect times out' do
allow_any_instance_of(Net::HTTP).to receive(:start).and_raise(Net::OpenTimeout)
expect {
client.connect(uri)
}.to raise_error(Puppet::HTTP::ConnectionError, %r{^Request to https://www.example.com timed out connect operation after .* seconds})
end
it 'connects using the default ssl context' do
expect(client.pool).to receive(:with_connection) do |_, verifier|
expect(verifier.ssl_context).to equal(puppet_context)
end
client.connect(uri)
end
it 'connects using a specified ssl context' do
other_context = Puppet::SSL::SSLContext.new
expect(client.pool).to receive(:with_connection) do |_, verifier|
expect(verifier.ssl_context).to equal(other_context)
end
client.connect(uri, options: {ssl_context: other_context})
end
it 'connects using the system store' do
expect(client.pool).to receive(:with_connection) do |_, verifier|
expect(verifier.ssl_context).to equal(system_context)
end
client.connect(uri, options: {include_system_store: true})
end
it 'does not create a verifier for HTTP connections' do
expect(client.pool).to receive(:with_connection) do |_, verifier|
expect(verifier).to be_nil
end
client.connect(URI.parse('http://www.example.com'))
end
it 'raises an HTTPError if both are specified' do
expect {
client.connect(uri, options: {ssl_context: puppet_context, include_system_store: true})
}.to raise_error(Puppet::HTTP::HTTPError, /The ssl_context and include_system_store parameters are mutually exclusive/)
end
end
context 'after connecting' do
def expect_http_error(cause, expected_message)
expect {
client.connect(uri) do |_|
raise cause, 'whoops'
end
}.to raise_error(Puppet::HTTP::HTTPError, expected_message)
end
it 're-raises HTTPError' do
expect_http_error(Puppet::HTTP::HTTPError, 'whoops')
end
it 'raises HTTPError if connection is interrupted while reading' do
expect_http_error(EOFError, %r{^Request to https://www.example.com interrupted after .* seconds})
end
it 'raises HTTPError if connection times out' do
expect_http_error(Net::ReadTimeout, %r{^Request to https://www.example.com timed out read operation after .* seconds})
end
it 'raises HTTPError if connection fails' do
expect_http_error(ArgumentError, %r{^Request to https://www.example.com failed after .* seconds})
end
end
context "when closing" do
it "closes all connections in the pool" do
expect(client.pool).to receive(:close)
client.close
end
it 'reloads the default ssl context' do
expect(client.pool).to receive(:with_connection) do |_, verifier|
expect(verifier.ssl_context).to_not equal(puppet_context)
end
client.close
client.connect(uri)
end
it 'reloads the default system ssl context' do
expect(client.pool).to receive(:with_connection) do |_, verifier|
expect(verifier.ssl_context).to_not equal(system_context)
end
client.close
client.connect(uri, options: {include_system_store: true})
end
end
context "for GET requests" do
it "includes default HTTP headers" do
stub_request(:get, uri).with do |request|
expect(request.headers).to include({'X-Puppet-Version' => /./, 'User-Agent' => /./})
expect(request.headers).to_not include('X-Puppet-Profiling')
end
client.get(uri)
end
it "stringifies keys and encodes values in the query" do
stub_request(:get, uri).with(query: "foo=bar%3Dbaz")
client.get(uri, params: {:foo => "bar=baz"})
end
it "fails if a user passes in an invalid param type" do
environment = Puppet::Node::Environment.create(:testing, [])
expect{client.get(uri, params: {environment: environment})}.to raise_error(Puppet::HTTP::SerializationError, /HTTP REST queries cannot handle values of type/)
end
it "merges custom headers with default ones" do
stub_request(:get, uri).with(headers: { 'X-Foo' => 'Bar', 'X-Puppet-Version' => /./, 'User-Agent' => /./ })
client.get(uri, headers: {'X-Foo' => 'Bar'})
end
it "returns the response" do
stub_request(:get, uri)
response = client.get(uri)
expect(response).to be_a(Puppet::HTTP::Response)
expect(response).to be_success
expect(response.code).to eq(200)
end
it "returns the entire response body" do
stub_request(:get, uri).to_return(body: "abc")
expect(client.get(uri).body).to eq("abc")
end
it "streams the response body when a block is given" do
stub_request(:get, uri).to_return(body: "abc")
io = StringIO.new
client.get(uri) do |response|
response.read_body do |data|
io.write(data)
end
end
expect(io.string).to eq("abc")
end
context 'when connecting' do
it 'uses a specified ssl context' do
stub_request(:get, uri).to_return(body: "abc")
other_context = Puppet::SSL::SSLContext.new
client.get(uri, options: {ssl_context: other_context})
end
it 'uses the system store' do
stub_request(:get, uri).to_return(body: "abc")
client.get(uri, options: {include_system_store: true})
end
it 'raises an HTTPError if both are specified' do
expect {
client.get(uri, options: {ssl_context: puppet_context, include_system_store: true})
}.to raise_error(Puppet::HTTP::HTTPError, /The ssl_context and include_system_store parameters are mutually exclusive/)
end
end
end
context "for HEAD requests" do
it "includes default HTTP headers" do
stub_request(:head, uri).with(headers: {'X-Puppet-Version' => /./, 'User-Agent' => /./})
client.head(uri)
end
it "stringifies keys and encodes values in the query" do
stub_request(:head, uri).with(query: "foo=bar%3Dbaz")
client.head(uri, params: {:foo => "bar=baz"})
end
it "merges custom headers with default ones" do
stub_request(:head, uri).with(headers: { 'X-Foo' => 'Bar', 'X-Puppet-Version' => /./, 'User-Agent' => /./ })
client.head(uri, headers: {'X-Foo' => 'Bar'})
end
it "returns the response" do
stub_request(:head, uri)
response = client.head(uri)
expect(response).to be_a(Puppet::HTTP::Response)
expect(response).to be_success
expect(response.code).to eq(200)
end
it "returns the entire response body" do
stub_request(:head, uri).to_return(body: "abc")
expect(client.head(uri).body).to eq("abc")
end
context 'when connecting' do
it 'uses a specified ssl context' do
stub_request(:head, uri)
other_context = Puppet::SSL::SSLContext.new
client.head(uri, options: {ssl_context: other_context})
end
it 'uses the system store' do
stub_request(:head, uri)
client.head(uri, options: {include_system_store: true})
end
it 'raises an HTTPError if both are specified' do
expect {
client.head(uri, options: {ssl_context: puppet_context, include_system_store: true})
}.to raise_error(Puppet::HTTP::HTTPError, /The ssl_context and include_system_store parameters are mutually exclusive/)
end
end
end
context "for PUT requests" do
it "includes default HTTP headers" do
stub_request(:put, uri).with do |request|
expect(request.headers).to include({'X-Puppet-Version' => /./, 'User-Agent' => /./})
expect(request.headers).to_not include('X-Puppet-Profiling')
end
client.put(uri, "", headers: {'Content-Type' => 'text/plain'})
end
it "stringifies keys and encodes values in the query" do
stub_request(:put, "https://www.example.com").with(query: "foo=bar%3Dbaz")
client.put(uri, "", params: {:foo => "bar=baz"}, headers: {'Content-Type' => 'text/plain'})
end
it "includes custom headers" do
stub_request(:put, "https://www.example.com").with(headers: { 'X-Foo' => 'Bar' })
client.put(uri, "", headers: {'X-Foo' => 'Bar', 'Content-Type' => 'text/plain'})
end
it "returns the response" do
stub_request(:put, uri)
response = client.put(uri, "", headers: {'Content-Type' => 'text/plain'})
expect(response).to be_a(Puppet::HTTP::Response)
expect(response).to be_success
expect(response.code).to eq(200)
end
it "sets content-length and content-type for the body" do
stub_request(:put, uri).with(headers: {"Content-Length" => "5", "Content-Type" => "text/plain"})
client.put(uri, "hello", headers: {'Content-Type' => 'text/plain'})
end
it 'raises an ArgumentError if `body` is missing' do
expect {
client.put(uri, nil, headers: {'Content-Type' => 'text/plain'})
}.to raise_error(ArgumentError, /'put' requires a string 'body' argument/)
end
it 'raises an ArgumentError if `content_type` is missing from the headers hash' do
expect {
client.put(uri, '')
}.to raise_error(ArgumentError, /'put' requires a 'content-type' header/)
end
context 'when connecting' do
it 'uses a specified ssl context' do
stub_request(:put, uri)
other_context = Puppet::SSL::SSLContext.new
client.put(uri, "", headers: {'Content-Type' => 'text/plain'}, options: {ssl_context: other_context})
end
it 'uses the system store' do
stub_request(:put, uri)
client.put(uri, "", headers: {'Content-Type' => 'text/plain'}, options: {include_system_store: true})
end
it 'raises an HTTPError if both are specified' do
expect {
client.put(uri, "", headers: {'Content-Type' => 'text/plain'}, options: {ssl_context: puppet_context, include_system_store: true})
}.to raise_error(Puppet::HTTP::HTTPError, /The ssl_context and include_system_store parameters are mutually exclusive/)
end
end
end
context "for POST requests" do
it "includes default HTTP headers" do
stub_request(:post, uri).with(headers: {'X-Puppet-Version' => /./, 'User-Agent' => /./})
client.post(uri, "", headers: {'Content-Type' => 'text/plain'})
end
it "stringifies keys and encodes values in the query" do
stub_request(:post, "https://www.example.com").with(query: "foo=bar%3Dbaz")
client.post(uri, "", params: {:foo => "bar=baz"}, headers: {'Content-Type' => 'text/plain'})
end
it "includes custom headers" do
stub_request(:post, "https://www.example.com").with(headers: { 'X-Foo' => 'Bar' })
client.post(uri, "", headers: {'X-Foo' => 'Bar', 'Content-Type' => 'text/plain'})
end
it "returns the response" do
stub_request(:post, uri)
response = client.post(uri, "", headers: {'Content-Type' => 'text/plain'})
expect(response).to be_a(Puppet::HTTP::Response)
expect(response).to be_success
expect(response.code).to eq(200)
end
it "sets content-length and content-type for the body" do
stub_request(:post, uri).with(headers: {"Content-Length" => "5", "Content-Type" => "text/plain"})
client.post(uri, "hello", headers: {'Content-Type' => 'text/plain'})
end
it "streams the response body when a block is given" do
stub_request(:post, uri).to_return(body: "abc")
io = StringIO.new
client.post(uri, "", headers: {'Content-Type' => 'text/plain'}) do |response|
response.read_body do |data|
io.write(data)
end
end
expect(io.string).to eq("abc")
end
it 'raises an ArgumentError if `body` is missing' do
expect {
client.post(uri, nil, headers: {'Content-Type' => 'text/plain'})
}.to raise_error(ArgumentError, /'post' requires a string 'body' argument/)
end
it 'raises an ArgumentError if `content_type` is missing from the headers hash' do
expect {
client.post(uri, "")
}.to raise_error(ArgumentError, /'post' requires a 'content-type' header/)
end
context 'when connecting' do
it 'uses a specified ssl context' do
stub_request(:post, uri)
other_context = Puppet::SSL::SSLContext.new
client.post(uri, "", headers: {'Content-Type' => 'text/plain'}, options: {body: "", ssl_context: other_context})
end
it 'uses the system store' do
stub_request(:post, uri)
client.post(uri, "", headers: {'Content-Type' => 'text/plain'}, options: {include_system_store: true})
end
it 'raises an HTTPError if both are specified' do
expect {
client.post(uri, "", headers: {'Content-Type' => 'text/plain'}, options: {ssl_context: puppet_context, include_system_store: true})
}.to raise_error(Puppet::HTTP::HTTPError, /The ssl_context and include_system_store parameters are mutually exclusive/)
end
end
end
context "for DELETE requests" do
it "includes default HTTP headers" do
stub_request(:delete, uri).with(headers: {'X-Puppet-Version' => /./, 'User-Agent' => /./})
client.delete(uri)
end
it "merges custom headers with default ones" do
stub_request(:delete, uri).with(headers: { 'X-Foo' => 'Bar', 'X-Puppet-Version' => /./, 'User-Agent' => /./ })
client.delete(uri, headers: {'X-Foo' => 'Bar'})
end
it "stringifies keys and encodes values in the query" do
stub_request(:delete, "https://www.example.com").with(query: "foo=bar%3Dbaz")
client.delete(uri, params: {:foo => "bar=baz"})
end
it "returns the response" do
stub_request(:delete, uri)
response = client.delete(uri)
expect(response).to be_a(Puppet::HTTP::Response)
expect(response).to be_success
expect(response.code).to eq(200)
end
it "returns the entire response body" do
stub_request(:delete, uri).to_return(body: "abc")
expect(client.delete(uri).body).to eq("abc")
end
context 'when connecting' do
it 'uses a specified ssl context' do
stub_request(:delete, uri)
other_context = Puppet::SSL::SSLContext.new
client.delete(uri, options: {ssl_context: other_context})
end
it 'uses the system store' do
stub_request(:delete, uri)
client.delete(uri, options: {include_system_store: true})
end
it 'raises an HTTPError if both are specified' do
expect {
client.delete(uri, options: {ssl_context: puppet_context, include_system_store: true})
}.to raise_error(Puppet::HTTP::HTTPError, /The ssl_context and include_system_store parameters are mutually exclusive/)
end
end
end
context "Basic Auth" do
it "submits credentials for GET requests" do
stub_request(:get, uri).with(basic_auth: credentials)
client.get(uri, options: {basic_auth: {user: 'user', password: 'pass'}})
end
it "submits credentials for PUT requests" do
stub_request(:put, uri).with(basic_auth: credentials)
client.put(uri, "hello", headers: {'Content-Type' => 'text/plain'}, options: {basic_auth: {user: 'user', password: 'pass'}})
end
it "returns response containing access denied" do
stub_request(:get, uri).with(basic_auth: credentials).to_return(status: [403, "Ye Shall Not Pass"])
response = client.get(uri, options: {basic_auth: {user: 'user', password: 'pass'}})
expect(response.code).to eq(403)
expect(response.reason).to eq("Ye Shall Not Pass")
expect(response).to_not be_success
end
it 'includes basic auth if user is nil' do
stub_request(:get, uri).with do |req|
expect(req.headers).to include('Authorization')
end
client.get(uri, options: {basic_auth: {user: nil, password: 'pass'}})
end
it 'includes basic auth if password is nil' do
stub_request(:get, uri).with do |req|
expect(req.headers).to include('Authorization')
end
client.get(uri, options: {basic_auth: {user: 'user', password: nil}})
end
it 'observes userinfo in the URL' do
stub_request(:get, uri).with(basic_auth: credentials)
client.get(URI("https://user:pass@www.example.com"))
end
it 'prefers explicit basic_auth credentials' do
uri = URI("https://ignored_user:ignored_pass@www.example.com")
stub_request(:get, "https://www.example.com").with(basic_auth: credentials)
client.get(uri, options: {basic_auth: {user: 'user', password: 'pass'}})
end
end
context "when redirecting" do
let(:start_url) { URI("https://www.example.com:8140/foo") }
let(:bar_url) { "https://www.example.com:8140/bar" }
let(:baz_url) { "https://www.example.com:8140/baz" }
let(:other_host) { "https://other.example.com:8140/qux" }
def redirect_to(status: 302, url:)
{ status: status, headers: { 'Location' => url }, body: "Redirected to #{url}" }
end
it "preserves GET method" do
stub_request(:get, start_url).to_return(redirect_to(url: bar_url))
stub_request(:get, bar_url).to_return(status: 200)
response = client.get(start_url)
expect(response).to be_success
end
it "preserves PUT method" do
stub_request(:put, start_url).to_return(redirect_to(url: bar_url))
stub_request(:put, bar_url).to_return(status: 200)
response = client.put(start_url, "", headers: {'Content-Type' => 'text/plain'})
expect(response).to be_success
end
it "updates the Host header from the Location host and port" do
stub_request(:get, start_url).with(headers: { 'Host' => 'www.example.com:8140' })
.to_return(redirect_to(url: other_host))
stub_request(:get, other_host).with(headers: { 'Host' => 'other.example.com:8140' })
.to_return(status: 200)
response = client.get(start_url)
expect(response).to be_success
end
it "omits the default HTTPS port from the Host header" do
stub_request(:get, start_url).with(headers: { 'Host' => 'www.example.com:8140' })
.to_return(redirect_to(url: "https://other.example.com/qux"))
stub_request(:get, "https://other.example.com/qux").with(headers: { 'Host' => 'other.example.com' })
.to_return(status: 200)
response = client.get(start_url)
expect(response).to be_success
end
it "omits the default HTTP port from the Host header" do
stub_request(:get, start_url).with(headers: { 'Host' => 'www.example.com:8140' })
.to_return(redirect_to(url: "http://other.example.com/qux"))
stub_request(:get, "http://other.example.com/qux").with(headers: { 'Host' => 'other.example.com' })
.to_return(status: 200)
response = client.get(start_url)
expect(response).to be_success
end
it "applies query parameters from the location header" do
query = { 'redirected' => false }
stub_request(:get, start_url).with(query: query).to_return(redirect_to(url: "#{bar_url}?redirected=true"))
stub_request(:get, bar_url).with(query: {'redirected' => 'true'}).to_return(status: 200)
response = client.get(start_url, params: query)
expect(response).to be_success
end
it "preserves custom and default headers when redirecting" do
headers = { 'X-Foo' => 'Bar', 'X-Puppet-Version' => Puppet.version }
stub_request(:get, start_url).with(headers: headers).to_return(redirect_to(url: bar_url))
stub_request(:get, bar_url).with(headers: headers).to_return(status: 200)
response = client.get(start_url, headers: headers)
expect(response).to be_success
end
it "does not preserve basic authorization when redirecting to different hosts" do
stub_request(:get, start_url).with(basic_auth: credentials).to_return(redirect_to(url: other_host))
stub_request(:get, other_host).to_return(status: 200)
client.get(start_url, options: {basic_auth: {user: 'user', password: 'pass'}})
expect(a_request(:get, other_host).
with{ |req| !req.headers.key?('Authorization')}).to have_been_made
end
it "does preserve basic authorization when redirecting to the same hosts" do
stub_request(:get, start_url).with(basic_auth: credentials).to_return(redirect_to(url: bar_url))
stub_request(:get, bar_url).with(basic_auth: credentials).to_return(status: 200)
client.get(start_url, options: {basic_auth: {user: 'user', password: 'pass'}})
expect(a_request(:get, bar_url).
with{ |req| req.headers.key?('Authorization')}).to have_been_made
end
it "does not preserve cookie header when redirecting to different hosts" do
headers = { 'Cookie' => 'TEST_COOKIE'}
stub_request(:get, start_url).with(headers: headers).to_return(redirect_to(url: other_host))
stub_request(:get, other_host).to_return(status: 200)
client.get(start_url, headers: headers)
expect(a_request(:get, other_host).
with{ |req| !req.headers.key?('Cookie')}).to have_been_made
end
it "does preserve cookie header when redirecting to the same hosts" do
headers = { 'Cookie' => 'TEST_COOKIE'}
stub_request(:get, start_url).with(headers: headers).to_return(redirect_to(url: bar_url))
stub_request(:get, bar_url).with(headers: headers).to_return(status: 200)
client.get(start_url, headers: headers)
expect(a_request(:get, bar_url).
with{ |req| req.headers.key?('Cookie')}).to have_been_made
end
it "does preserves cookie header and basic authentication when Puppet[:location_trusted] is true redirecting to different hosts" do
headers = { 'cookie' => 'TEST_COOKIE'}
Puppet[:location_trusted] = true
stub_request(:get, start_url).with(headers: headers, basic_auth: credentials).to_return(redirect_to(url: other_host))
stub_request(:get, other_host).with(headers: headers, basic_auth: credentials).to_return(status: 200)
client.get(start_url, headers: headers, options: {basic_auth: {user: 'user', password: 'pass'}})
expect(a_request(:get, other_host).
with{ |req| req.headers.key?('Authorization') && req.headers.key?('Cookie')}).to have_been_made
end
it "treats hosts as case-insensitive" do
start_url = URI("https://www.EXAmple.com:8140/Start")
bar_url = "https://www.example.com:8140/bar"
stub_request(:get, start_url).with(basic_auth: credentials).to_return(redirect_to(url: bar_url))
stub_request(:get, bar_url).with(basic_auth: credentials).to_return(status: 200)
client.get(start_url, options: {basic_auth: {user: 'user', password: 'pass'}})
expect(a_request(:get, bar_url).
with{ |req| req.headers.key?('Authorization')}).to have_been_made
end
it "redirects given a relative location" do
relative_url = "/people.html"
stub_request(:get, start_url).to_return(redirect_to(url: relative_url))
stub_request(:get, "https://www.example.com:8140/people.html").to_return(status: 200)
response = client.get(start_url)
expect(response).to be_success
end
it "applies query parameters from the location header" do
relative_url = "/people.html"
query = { 'redirected' => false }
stub_request(:get, start_url).with(query: query).to_return(redirect_to(url: "#{relative_url}?redirected=true"))
stub_request(:get, "https://www.example.com:8140/people.html").with(query: {'redirected' => 'true'}).to_return(status: 200)
response = client.get(start_url, params: query)
expect(response).to be_success
end
it "removes dot segments from a relative location" do
# from https://tools.ietf.org/html/rfc3986#section-5.4.2
base_url = URI("http://a/b/c/d;p?q")
relative_url = "../../../../g"
stub_request(:get, base_url).to_return(redirect_to(url: relative_url))
stub_request(:get, "http://a/g").to_return(status: 200)
response = client.get(base_url)
expect(response).to be_success
end
it "preserves request body for each request" do
data = 'some data'
stub_request(:put, start_url).with(body: data).to_return(redirect_to(url: bar_url))
stub_request(:put, bar_url).with(body: data).to_return(status: 200)
response = client.put(start_url, data, headers: {'Content-Type' => 'text/plain'})
expect(response).to be_success
end
it "returns the body from the final response" do
stub_request(:get, start_url).to_return(redirect_to(url: bar_url))
stub_request(:get, bar_url).to_return(status: 200, body: 'followed')
response = client.get(start_url)
expect(response.body).to eq('followed')
end
[301, 307].each do |code|
it "also redirects on #{code}" do
stub_request(:get, start_url).to_return(redirect_to(status: code, url: bar_url))
stub_request(:get, bar_url).to_return(status: 200)
response = client.get(start_url)
expect(response).to be_success
end
end
[303, 308].each do |code|
it "returns an error on #{code}" do
stub_request(:get, start_url).to_return(redirect_to(status: code, url: bar_url))
response = client.get(start_url)
expect(response.code).to eq(code)
expect(response).to_not be_success
end
end
it "raises an error if the Location header is missing" do
stub_request(:get, start_url).to_return(status: 302)
expect {
client.get(start_url)
}.to raise_error(Puppet::HTTP::ProtocolError, "Location response header is missing")
end
it "raises an error if the Location header is invalid" do
stub_request(:get, start_url).to_return(redirect_to(status: 302, url: 'http://foo"bar'))
expect {
client.get(start_url)
}.to raise_error(Puppet::HTTP::ProtocolError, /Location URI is invalid/)
end
it "raises an error if limit is 0 and we're asked to follow" do
stub_request(:get, start_url).to_return(redirect_to(url: bar_url))
client = described_class.new(redirect_limit: 0)
expect {
client.get(start_url)
}.to raise_error(Puppet::HTTP::TooManyRedirects, %r{Too many HTTP redirections for https://www.example.com:8140})
end
it "raises an error if asked to follow redirects more times than the limit" do
stub_request(:get, start_url).to_return(redirect_to(url: bar_url))
stub_request(:get, bar_url).to_return(redirect_to(url: baz_url))
client = described_class.new(redirect_limit: 1)
expect {
client.get(start_url)
}.to raise_error(Puppet::HTTP::TooManyRedirects, %r{Too many HTTP redirections for https://www.example.com:8140})
end
it "follows multiple redirects if equal to or less than the redirect limit" do
stub_request(:get, start_url).to_return(redirect_to(url: bar_url))
stub_request(:get, bar_url).to_return(redirect_to(url: baz_url))
stub_request(:get, baz_url).to_return(status: 200)
client = described_class.new(redirect_limit: 2)
response = client.get(start_url)
expect(response).to be_success
end
it "redirects to a different host" do
stub_request(:get, start_url).to_return(redirect_to(url: other_host))
stub_request(:get, other_host).to_return(status: 200)
response = client.get(start_url)
expect(response).to be_success
end
it "redirects from http to https" do
http = URI("http://example.com/foo")
https = URI("https://example.com/bar")
stub_request(:get, http).to_return(redirect_to(url: https))
stub_request(:get, https).to_return(status: 200)
response = client.get(http)
expect(response).to be_success
end
it "redirects from https to http" do
http = URI("http://example.com/foo")
https = URI("https://example.com/bar")
stub_request(:get, https).to_return(redirect_to(url: http))
stub_request(:get, http).to_return(status: 200)
response = client.get(https)
expect(response).to be_success
end
end
context "when response indicates an overloaded server" do
def retry_after(datetime)
stub_request(:get, uri)
.to_return(status: [503, 'Service Unavailable'], headers: {'Retry-After' => datetime}).then
.to_return(status: 200)
end
it "returns a 503 response if Retry-After is not set" do
stub_request(:get, uri).to_return(status: [503, 'Service Unavailable'])
expect(client.get(uri).code).to eq(503)
end
it "raises if Retry-After is not convertible to an Integer or RFC 2822 Date" do
stub_request(:get, uri).to_return(status: [503, 'Service Unavailable'], headers: {'Retry-After' => 'foo'})
expect {
client.get(uri)
}.to raise_error(Puppet::HTTP::ProtocolError, /Failed to parse Retry-After header 'foo' as an integer or RFC 2822 date/)
end
it "should close the connection before sleeping" do
retry_after('42')
site = Puppet::HTTP::Site.from_uri(uri)
http1 = Net::HTTP.new(site.host, site.port)
http1.use_ssl = true
allow(http1).to receive(:started?).and_return(true)
http2 = Net::HTTP.new(site.host, site.port)
http2.use_ssl = true
allow(http2).to receive(:started?).and_return(true)
pool = Puppet::HTTP::Pool.new(15)
client = Puppet::HTTP::Client.new(pool: pool)
# The "with_connection" method is required to yield started connections
allow(pool).to receive(:with_connection).and_yield(http1).and_yield(http2)
expect(http1).to receive(:finish).ordered
expect(::Kernel).to receive(:sleep).with(42).ordered
client.get(uri)
end
it "should sleep and retry if Retry-After is an Integer" do
retry_after('42')
expect(::Kernel).to receive(:sleep).with(42)
client.get(uri)
end
it "should sleep and retry if Retry-After is an RFC 2822 Date" do
retry_after('Wed, 13 Apr 2005 15:18:05 GMT')
now = DateTime.new(2005, 4, 13, 8, 17, 5, '-07:00')
allow(DateTime).to receive(:now).and_return(now)
expect(::Kernel).to receive(:sleep).with(60)
client.get(uri)
end
it "should sleep for no more than the Puppet runinterval" do
retry_after('60')
Puppet[:runinterval] = 30
expect(::Kernel).to receive(:sleep).with(30)
client.get(uri)
end
it "should sleep for 0 seconds if the RFC 2822 date has past" do
retry_after('Wed, 13 Apr 2005 15:18:05 GMT')
expect(::Kernel).to receive(:sleep).with(0)
client.get(uri)
end
end
context "persistent connections" do
before :each do
stub_request(:get, uri)
end
it 'defaults keepalive to http_keepalive_timeout' do
expect(client.pool.keepalive_timeout).to eq(Puppet[:http_keepalive_timeout])
end
it 'reuses a cached connection' do
allow(Puppet).to receive(:debug)
expect(Puppet).to receive(:debug).with(/^Creating new connection/)
expect(Puppet).to receive(:debug).with(/^Using cached connection/)
client.get(uri)
client.get(uri)
end
it 'can be disabled' do
Puppet[:http_keepalive_timeout] = 0
allow(Puppet).to receive(:debug)
expect(Puppet).to receive(:debug).with(/^Creating new connection/).twice
expect(Puppet).to receive(:debug).with(/^Using cached connection/).never
client.get(uri)
client.get(uri)
end
end
end
|