How to use perform method of HTTP Package

Best Webmock_ruby code snippet using HTTP.perform

request_spec.rb

Source:request_spec.rb Github

copy

Full Screen

...312 it "should include any HTTP headers in the returned response" do313 @request.options[:format] = :html314 response = stub_response "Content"315 response.initialize_http_header("key" => "value")316 expect(@request.perform.headers).to eq({ "key" => ["value"] })317 end318 if "".respond_to?(:encoding)319 it "should process charset in content type properly" do320 response = stub_response "Content"321 response.initialize_http_header("Content-Type" => "text/plain;charset = utf-8")322 resp = @request.perform323 expect(resp.body.encoding).to eq(Encoding.find("UTF-8"))324 end325 it "should process charset in content type properly if it has a different case" do326 response = stub_response "Content"327 response.initialize_http_header("Content-Type" => "text/plain;CHARSET = utf-8")328 resp = @request.perform329 expect(resp.body.encoding).to eq(Encoding.find("UTF-8"))330 end331 it "should process quoted charset in content type properly" do332 response = stub_response "Content"333 response.initialize_http_header("Content-Type" => "text/plain;charset = \"utf-8\"")334 resp = @request.perform335 expect(resp.body.encoding).to eq(Encoding.find("UTF-8"))336 end337 it "should process utf-16 charset with little endian bom correctly" do338 @request.options[:assume_utf16_is_big_endian] = true339 response = stub_response "\xFF\xFEC\x00o\x00n\x00t\x00e\x00n\x00t\x00"340 response.initialize_http_header("Content-Type" => "text/plain;charset = utf-16")341 resp = @request.perform342 expect(resp.body.encoding).to eq(Encoding.find("UTF-16LE"))343 end344 it "should process utf-16 charset with big endian bom correctly" do345 @request.options[:assume_utf16_is_big_endian] = false346 response = stub_response "\xFE\xFF\x00C\x00o\x00n\x00t\x00e\x00n\x00t"347 response.initialize_http_header("Content-Type" => "text/plain;charset = utf-16")348 resp = @request.perform349 expect(resp.body.encoding).to eq(Encoding.find("UTF-16BE"))350 end351 it "should assume utf-16 little endian if options has been chosen" do352 @request.options[:assume_utf16_is_big_endian] = false353 response = stub_response "C\x00o\x00n\x00t\x00e\x00n\x00t\x00"354 response.initialize_http_header("Content-Type" => "text/plain;charset = utf-16")355 resp = @request.perform356 expect(resp.body.encoding).to eq(Encoding.find("UTF-16LE"))357 end358 it "should perform no encoding if the charset is not available" do359 response = stub_response "Content"360 response.initialize_http_header("Content-Type" => "text/plain;charset = utf-lols")361 resp = @request.perform362 expect(resp.body).to eq("Content")363 expect(resp.body.encoding).to eq("Content".encoding)364 end365 it "should perform no encoding if the content type is specified but no charset is specified" do366 response = stub_response "Content"367 response.initialize_http_header("Content-Type" => "text/plain")368 resp = @request.perform369 expect(resp.body).to eq("Content")370 expect(resp.body.encoding).to eq("Content".encoding)371 end372 end373 describe 'with non-200 responses' do374 context "3xx responses" do375 it 'returns a valid object for 304 not modified' do376 stub_response '', 304377 resp = @request.perform378 expect(resp.code).to eq(304)379 expect(resp.body).to eq('')380 expect(resp).to be_nil381 end382 it "redirects if a 300 contains a location header" do383 redirect = stub_response '', 300384 redirect['location'] = 'http://foo.com/foo'385 ok = stub_response('<hash><foo>bar</foo></hash>', 200)386 allow(@http).to receive(:request).and_return(redirect, ok)387 response = @request.perform388 expect(response.request.base_uri.to_s).to eq("http://foo.com")389 expect(response.request.path.to_s).to eq("http://foo.com/foo")390 expect(response.request.uri.request_uri).to eq("/foo")391 expect(response.request.uri.to_s).to eq("http://foo.com/foo")392 expect(response.parsed_response).to eq({"hash" => {"foo" => "bar"}})393 end394 it "calls block given to perform with each redirect" do395 @request = HTTParty::Request.new(Net::HTTP::Get, 'http://test.com/redirect', format: :xml)396 FakeWeb.register_uri(:get, "http://test.com/redirect", status: [300, "REDIRECT"], location: "http://api.foo.com/v2")397 FakeWeb.register_uri(:get, "http://api.foo.com/v2", body: "<hash><foo>bar</foo></hash>")398 body = ""399 response = @request.perform { |chunk| body += chunk }400 expect(body.length).to eq(27)401 end402 it "redirects if a 300 contains a relative location header" do403 redirect = stub_response '', 300404 redirect['location'] = '/foo/bar'405 ok = stub_response('<hash><foo>bar</foo></hash>', 200)406 allow(@http).to receive(:request).and_return(redirect, ok)407 response = @request.perform408 expect(response.request.base_uri.to_s).to eq("http://api.foo.com")409 expect(response.request.path.to_s).to eq("/foo/bar")410 expect(response.request.uri.request_uri).to eq("/foo/bar")411 expect(response.request.uri.to_s).to eq("http://api.foo.com/foo/bar")412 expect(response.parsed_response).to eq({"hash" => {"foo" => "bar"}})413 end414 it "handles multiple redirects and relative location headers on different hosts" do415 @request = HTTParty::Request.new(Net::HTTP::Get, 'http://test.com/redirect', format: :xml)416 FakeWeb.register_uri(:get, "http://test.com/redirect", status: [300, "REDIRECT"], location: "http://api.foo.com/v2")417 FakeWeb.register_uri(:get, "http://api.foo.com/v2", status: [300, "REDIRECT"], location: "/v3")418 FakeWeb.register_uri(:get, "http://api.foo.com/v3", body: "<hash><foo>bar</foo></hash>")419 response = @request.perform420 expect(response.request.base_uri.to_s).to eq("http://api.foo.com")421 expect(response.request.path.to_s).to eq("/v3")422 expect(response.request.uri.request_uri).to eq("/v3")423 expect(response.request.uri.to_s).to eq("http://api.foo.com/v3")424 expect(response.parsed_response).to eq({"hash" => {"foo" => "bar"}})425 end426 it "raises an error if redirect has duplicate location header" do427 @request = HTTParty::Request.new(Net::HTTP::Get, 'http://test.com/redirect', format: :xml)428 FakeWeb.register_uri(:get, "http://test.com/redirect", status: [300, "REDIRECT"], location: ["http://api.foo.com/v2","http://api.foo.com/v2"])429 expect {@request.perform}.to raise_error(HTTParty::DuplicateLocationHeader)430 end431 it "returns the HTTParty::Response when the 300 does not contain a location header" do432 stub_response '', 300433 expect(HTTParty::Response).to be === @request.perform434 end435 it "redirects including port" do436 FakeWeb.register_uri(:get, "http://withport.com:3000/v1", status: [301, "Moved Permanently"], location: "http://withport.com:3000/v2")437 FakeWeb.register_uri(:get, "http://withport.com:3000/v2", status: 200)438 request = HTTParty::Request.new(Net::HTTP::Get, 'http://withport.com:3000/v1')439 response = request.perform440 expect(response.request.base_uri.to_s).to eq("http://withport.com:3000")441 end442 end443 it 'should return a valid object for 4xx response' do444 stub_response '<foo><bar>yes</bar></foo>', 401445 resp = @request.perform446 expect(resp.code).to eq(401)447 expect(resp.body).to eq("<foo><bar>yes</bar></foo>")448 expect(resp['foo']['bar']).to eq("yes")449 end450 it 'should return a valid object for 5xx response' do451 stub_response '<foo><bar>error</bar></foo>', 500452 resp = @request.perform453 expect(resp.code).to eq(500)454 expect(resp.body).to eq("<foo><bar>error</bar></foo>")455 expect(resp['foo']['bar']).to eq("error")456 end457 it "parses response lazily so codes can be checked prior" do458 stub_response 'not xml', 500459 @request.options[:format] = :xml460 expect {461 response = @request.perform462 expect(response.code).to eq(500)463 expect(response.body).to eq('not xml')464 }.not_to raise_error465 end466 end467 end468 it "should not attempt to parse empty responses" do469 [204, 304].each do |code|470 stub_response "", code471 @request.options[:format] = :xml472 expect(@request.perform).to be_nil473 end474 end475 it "should not fail for missing mime type" do476 stub_response "Content for you"477 @request.options[:format] = :html478 expect(@request.perform.parsed_response).to eq('Content for you')479 end480 [300, 301, 302, 305].each do |code|481 describe "a request that #{code} redirects" do482 before(:each) do483 @redirect = stub_response("", code)484 @redirect['location'] = '/foo'485 @ok = stub_response('<hash><foo>bar</foo></hash>', 200)486 end487 describe "once" do488 before(:each) do489 allow(@http).to receive(:request).and_return(@redirect, @ok)490 end491 it "should be handled by GET transparently" do492 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})493 end494 it "should be handled by POST transparently" do495 @request.http_method = Net::HTTP::Post496 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})497 end498 it "should be handled by DELETE transparently" do499 @request.http_method = Net::HTTP::Delete500 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})501 end502 it "should be handled by MOVE transparently" do503 @request.http_method = Net::HTTP::Move504 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})505 end506 it "should be handled by COPY transparently" do507 @request.http_method = Net::HTTP::Copy508 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})509 end510 it "should be handled by PATCH transparently" do511 @request.http_method = Net::HTTP::Patch512 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})513 end514 it "should be handled by PUT transparently" do515 @request.http_method = Net::HTTP::Put516 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})517 end518 it "should be handled by HEAD transparently" do519 @request.http_method = Net::HTTP::Head520 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})521 end522 it "should be handled by OPTIONS transparently" do523 @request.http_method = Net::HTTP::Options524 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})525 end526 it "should be handled by MKCOL transparently" do527 @request.http_method = Net::HTTP::Mkcol528 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})529 end530 it "should keep track of cookies between redirects" do531 @redirect['Set-Cookie'] = 'foo=bar; name=value; HTTPOnly'532 @request.perform533 expect(@request.options[:headers]['Cookie']).to match(/foo=bar/)534 expect(@request.options[:headers]['Cookie']).to match(/name=value/)535 end536 it 'should update cookies with redirects' do537 @request.options[:headers] = {'Cookie' => 'foo=bar;'}538 @redirect['Set-Cookie'] = 'foo=tar;'539 @request.perform540 expect(@request.options[:headers]['Cookie']).to match(/foo=tar/)541 end542 it 'should keep cookies between redirects' do543 @request.options[:headers] = {'Cookie' => 'keep=me'}544 @redirect['Set-Cookie'] = 'foo=tar;'545 @request.perform546 expect(@request.options[:headers]['Cookie']).to match(/keep=me/)547 end548 it "should handle multiple Set-Cookie headers between redirects" do549 @redirect.add_field 'set-cookie', 'foo=bar; name=value; HTTPOnly'550 @redirect.add_field 'set-cookie', 'one=1; two=2; HTTPOnly'551 @request.perform552 expect(@request.options[:headers]['Cookie']).to match(/foo=bar/)553 expect(@request.options[:headers]['Cookie']).to match(/name=value/)554 expect(@request.options[:headers]['Cookie']).to match(/one=1/)555 expect(@request.options[:headers]['Cookie']).to match(/two=2/)556 end557 it 'should make resulting request a get request if it not already' do558 @request.http_method = Net::HTTP::Delete559 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})560 expect(@request.http_method).to eq(Net::HTTP::Get)561 end562 it 'should not make resulting request a get request if options[:maintain_method_across_redirects] is true' do563 @request.options[:maintain_method_across_redirects] = true564 @request.http_method = Net::HTTP::Delete565 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})566 expect(@request.http_method).to eq(Net::HTTP::Delete)567 end568 it 'should log the redirection' do569 logger_double = double570 expect(logger_double).to receive(:info).twice571 @request.options[:logger] = logger_double572 @request.perform573 end574 end575 describe "infinitely" do576 before(:each) do577 allow(@http).to receive(:request).and_return(@redirect)578 end579 it "should raise an exception" do580 expect { @request.perform }.to raise_error(HTTParty::RedirectionTooDeep)581 end582 end583 end584 end585 describe "a request that 303 redirects" do586 before(:each) do587 @redirect = stub_response("", 303)588 @redirect['location'] = '/foo'589 @ok = stub_response('<hash><foo>bar</foo></hash>', 200)590 end591 describe "once" do592 before(:each) do593 allow(@http).to receive(:request).and_return(@redirect, @ok)594 end595 it "should be handled by GET transparently" do596 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})597 end598 it "should be handled by POST transparently" do599 @request.http_method = Net::HTTP::Post600 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})601 end602 it "should be handled by DELETE transparently" do603 @request.http_method = Net::HTTP::Delete604 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})605 end606 it "should be handled by MOVE transparently" do607 @request.http_method = Net::HTTP::Move608 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})609 end610 it "should be handled by COPY transparently" do611 @request.http_method = Net::HTTP::Copy612 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})613 end614 it "should be handled by PATCH transparently" do615 @request.http_method = Net::HTTP::Patch616 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})617 end618 it "should be handled by PUT transparently" do619 @request.http_method = Net::HTTP::Put620 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})621 end622 it "should be handled by HEAD transparently" do623 @request.http_method = Net::HTTP::Head624 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})625 end626 it "should be handled by OPTIONS transparently" do627 @request.http_method = Net::HTTP::Options628 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})629 end630 it "should be handled by MKCOL transparently" do631 @request.http_method = Net::HTTP::Mkcol632 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})633 end634 it "should keep track of cookies between redirects" do635 @redirect['Set-Cookie'] = 'foo=bar; name=value; HTTPOnly'636 @request.perform637 expect(@request.options[:headers]['Cookie']).to match(/foo=bar/)638 expect(@request.options[:headers]['Cookie']).to match(/name=value/)639 end640 it 'should update cookies with redirects' do641 @request.options[:headers] = {'Cookie' => 'foo=bar;'}642 @redirect['Set-Cookie'] = 'foo=tar;'643 @request.perform644 expect(@request.options[:headers]['Cookie']).to match(/foo=tar/)645 end646 it 'should keep cookies between redirects' do647 @request.options[:headers] = {'Cookie' => 'keep=me'}648 @redirect['Set-Cookie'] = 'foo=tar;'649 @request.perform650 expect(@request.options[:headers]['Cookie']).to match(/keep=me/)651 end652 it "should handle multiple Set-Cookie headers between redirects" do653 @redirect.add_field 'set-cookie', 'foo=bar; name=value; HTTPOnly'654 @redirect.add_field 'set-cookie', 'one=1; two=2; HTTPOnly'655 @request.perform656 expect(@request.options[:headers]['Cookie']).to match(/foo=bar/)657 expect(@request.options[:headers]['Cookie']).to match(/name=value/)658 expect(@request.options[:headers]['Cookie']).to match(/one=1/)659 expect(@request.options[:headers]['Cookie']).to match(/two=2/)660 end661 it 'should make resulting request a get request if it not already' do662 @request.http_method = Net::HTTP::Delete663 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})664 expect(@request.http_method).to eq(Net::HTTP::Get)665 end666 it 'should make resulting request a get request if options[:maintain_method_across_redirects] is false' do667 @request.options[:maintain_method_across_redirects] = false668 @request.http_method = Net::HTTP::Delete669 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})670 expect(@request.http_method).to eq(Net::HTTP::Get)671 end672 it 'should make resulting request a get request if options[:maintain_method_across_redirects] is true but options[:resend_on_redirect] is false' do673 @request.options[:maintain_method_across_redirects] = true674 @request.options[:resend_on_redirect] = false675 @request.http_method = Net::HTTP::Delete676 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})677 expect(@request.http_method).to eq(Net::HTTP::Get)678 end679 it 'should not make resulting request a get request if options[:maintain_method_across_redirects] and options[:resend_on_redirect] is true' do680 @request.options[:maintain_method_across_redirects] = true681 @request.options[:resend_on_redirect] = true682 @request.http_method = Net::HTTP::Delete683 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})684 expect(@request.http_method).to eq(Net::HTTP::Delete)685 end686 it 'should log the redirection' do687 logger_double = double688 expect(logger_double).to receive(:info).twice689 @request.options[:logger] = logger_double690 @request.perform691 end692 end693 describe "infinitely" do694 before(:each) do695 allow(@http).to receive(:request).and_return(@redirect)696 end697 it "should raise an exception" do698 expect { @request.perform }.to raise_error(HTTParty::RedirectionTooDeep)699 end700 end701 end702 describe "a request that returns 304" do703 before(:each) do704 @redirect = stub_response("", 304)705 @redirect['location'] = '/foo'706 end707 before(:each) do708 allow(@http).to receive(:request).and_return(@redirect)709 end710 it "should report 304 with a GET request" do711 expect(@request.perform.code).to eq(304)712 end713 it "should report 304 with a POST request" do714 @request.http_method = Net::HTTP::Post715 expect(@request.perform.code).to eq(304)716 end717 it "should report 304 with a DELETE request" do718 @request.http_method = Net::HTTP::Delete719 expect(@request.perform.code).to eq(304)720 end721 it "should report 304 with a MOVE request" do722 @request.http_method = Net::HTTP::Move723 expect(@request.perform.code).to eq(304)724 end725 it "should report 304 with a COPY request" do726 @request.http_method = Net::HTTP::Copy727 expect(@request.perform.code).to eq(304)728 end729 it "should report 304 with a PATCH request" do730 @request.http_method = Net::HTTP::Patch731 expect(@request.perform.code).to eq(304)732 end733 it "should report 304 with a PUT request" do734 @request.http_method = Net::HTTP::Put735 expect(@request.perform.code).to eq(304)736 end737 it "should report 304 with a HEAD request" do738 @request.http_method = Net::HTTP::Head739 expect(@request.perform.code).to eq(304)740 end741 it "should report 304 with a OPTIONS request" do742 @request.http_method = Net::HTTP::Options743 expect(@request.perform.code).to eq(304)744 end745 it "should report 304 with a MKCOL request" do746 @request.http_method = Net::HTTP::Mkcol747 expect(@request.perform.code).to eq(304)748 end749 it 'should not log the redirection' do750 logger_double = double751 expect(logger_double).to receive(:info).once752 @request.options[:logger] = logger_double753 @request.perform754 end755 end756 [307, 308].each do |code|757 describe "a request that #{code} redirects" do758 before(:each) do759 @redirect = stub_response("", code)760 @redirect['location'] = '/foo'761 @ok = stub_response('<hash><foo>bar</foo></hash>', 200)762 end763 describe "once" do764 before(:each) do765 allow(@http).to receive(:request).and_return(@redirect, @ok)766 end767 it "should be handled by GET transparently" do768 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})769 end770 it "should be handled by POST transparently" do771 @request.http_method = Net::HTTP::Post772 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})773 end774 it "should be handled by DELETE transparently" do775 @request.http_method = Net::HTTP::Delete776 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})777 end778 it "should be handled by MOVE transparently" do779 @request.http_method = Net::HTTP::Move780 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})781 end782 it "should be handled by COPY transparently" do783 @request.http_method = Net::HTTP::Copy784 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})785 end786 it "should be handled by PATCH transparently" do787 @request.http_method = Net::HTTP::Patch788 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})789 end790 it "should be handled by PUT transparently" do791 @request.http_method = Net::HTTP::Put792 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})793 end794 it "should be handled by HEAD transparently" do795 @request.http_method = Net::HTTP::Head796 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})797 end798 it "should be handled by OPTIONS transparently" do799 @request.http_method = Net::HTTP::Options800 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})801 end802 it "should be handled by MKCOL transparently" do803 @request.http_method = Net::HTTP::Mkcol804 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})805 end806 it "should keep track of cookies between redirects" do807 @redirect['Set-Cookie'] = 'foo=bar; name=value; HTTPOnly'808 @request.perform809 expect(@request.options[:headers]['Cookie']).to match(/foo=bar/)810 expect(@request.options[:headers]['Cookie']).to match(/name=value/)811 end812 it 'should update cookies with redirects' do813 @request.options[:headers] = {'Cookie' => 'foo=bar;'}814 @redirect['Set-Cookie'] = 'foo=tar;'815 @request.perform816 expect(@request.options[:headers]['Cookie']).to match(/foo=tar/)817 end818 it 'should keep cookies between redirects' do819 @request.options[:headers] = {'Cookie' => 'keep=me'}820 @redirect['Set-Cookie'] = 'foo=tar;'821 @request.perform822 expect(@request.options[:headers]['Cookie']).to match(/keep=me/)823 end824 it "should handle multiple Set-Cookie headers between redirects" do825 @redirect.add_field 'set-cookie', 'foo=bar; name=value; HTTPOnly'826 @redirect.add_field 'set-cookie', 'one=1; two=2; HTTPOnly'827 @request.perform828 expect(@request.options[:headers]['Cookie']).to match(/foo=bar/)829 expect(@request.options[:headers]['Cookie']).to match(/name=value/)830 expect(@request.options[:headers]['Cookie']).to match(/one=1/)831 expect(@request.options[:headers]['Cookie']).to match(/two=2/)832 end833 it 'should maintain method in resulting request' do834 @request.http_method = Net::HTTP::Delete835 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})836 expect(@request.http_method).to eq(Net::HTTP::Delete)837 end838 it 'should maintain method in resulting request if options[:maintain_method_across_redirects] is false' do839 @request.options[:maintain_method_across_redirects] = false840 @request.http_method = Net::HTTP::Delete841 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})842 expect(@request.http_method).to eq(Net::HTTP::Delete)843 end844 it 'should maintain method in resulting request if options[:maintain_method_across_redirects] is true' do845 @request.options[:maintain_method_across_redirects] = true846 @request.http_method = Net::HTTP::Delete847 expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})848 expect(@request.http_method).to eq(Net::HTTP::Delete)849 end850 it 'should log the redirection' do851 logger_double = double852 expect(logger_double).to receive(:info).twice853 @request.options[:logger] = logger_double854 @request.perform855 end856 end857 describe "infinitely" do858 before(:each) do859 allow(@http).to receive(:request).and_return(@redirect)860 end861 it "should raise an exception" do862 expect { @request.perform }.to raise_error(HTTParty::RedirectionTooDeep)863 end864 end865 end866 end867 describe "#handle_deflation" do868 context "context-encoding" do869 before do870 @request.options[:format] = :html871 @last_response = double872 allow(@last_response).to receive(:body).and_return('')873 end874 it "should inflate the gzipped body with content-encoding: gzip" do875 allow(@last_response).to receive(:[]).with("content-encoding").and_return("gzip")876 allow(@request).to receive(:last_response).and_return(@last_response)877 expect(Zlib::GzipReader).to receive(:new).and_return(StringIO.new(''))878 expect(@request.last_response).to receive(:delete).with('content-encoding')879 @request.send(:handle_deflation)880 end881 it "should inflate the gzipped body with content-encoding: x-gzip" do882 allow(@last_response).to receive(:[]).with("content-encoding").and_return("x-gzip")883 allow(@request).to receive(:last_response).and_return(@last_response)884 expect(Zlib::GzipReader).to receive(:new).and_return(StringIO.new(''))885 expect(@request.last_response).to receive(:delete).with('content-encoding')886 @request.send(:handle_deflation)887 end888 it "should inflate the deflated body" do889 allow(@last_response).to receive(:[]).with("content-encoding").and_return("deflate")890 allow(@request).to receive(:last_response).and_return(@last_response)891 expect(Zlib::Inflate).to receive(:inflate).and_return('')892 expect(@request.last_response).to receive(:delete).with('content-encoding')893 @request.send(:handle_deflation)894 end895 it "should not inflate a redirected response with content-encoding: gzip" do896 allow(@last_response).to receive(:[]).with("content-encoding").and_return("gzip")897 allow(@request).to receive(:last_response).and_return(@last_response)898 allow(@request).to receive(:response_redirects?).and_return(true)899 @request.send(:handle_deflation)900 end901 it "should not inflate a redirected response with content-encoding: deflate" do902 allow(@last_response).to receive(:[]).with("content-encoding").and_return("deflate")903 allow(@request).to receive(:last_response).and_return(@last_response)904 allow(@request).to receive(:response_redirects?).and_return(true)905 @request.send(:handle_deflation)906 end907 end908 end909 describe "#send_authorization_header?" do910 context "basic_auth" do911 before do912 @credentials = { username: "username", password: "password" }913 @authorization = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="914 @request.options[:basic_auth] = @credentials915 @redirect = stub_response("", 302)916 @ok = stub_response('<hash><foo>bar</foo></hash>', 200)917 end918 before(:each) do919 allow(@http).to receive(:request).and_return(@redirect, @ok)920 end921 it "should not send Authorization header when redirecting to a different host" do922 @redirect['location'] = 'http://example.com/'923 @request.perform924 @request.send(:setup_raw_request)925 expect(@request.instance_variable_get(:@raw_request)['authorization']).to be_nil926 end927 it "should send Authorization header when redirecting to a relative path" do928 @redirect['location'] = '/v3'929 @request.perform930 @request.send(:setup_raw_request)931 expect(@request.instance_variable_get(:@raw_request)['authorization']).to eq(@authorization)932 end933 it "should send Authorization header when redirecting to the same host" do934 @redirect['location'] = 'http://api.foo.com/v2'935 @request.perform936 @request.send(:setup_raw_request)937 expect(@request.instance_variable_get(:@raw_request)['authorization']).to eq(@authorization)938 end939 it "should send Authorization header when redirecting to a different port on the same host" do940 @redirect['location'] = 'http://api.foo.com:3000/v3'941 @request.perform942 @request.send(:setup_raw_request)943 expect(@request.instance_variable_get(:@raw_request)['authorization']).to eq(@authorization)944 end945 end946 end947 context "with POST http method" do948 it "should raise argument error if query is not a hash" do949 expect {950 HTTParty::Request.new(Net::HTTP::Post, 'http://api.foo.com/v1', format: :xml, query: 'astring').perform951 }.to raise_error(ArgumentError)952 end953 end954 describe "argument validation" do955 it "should raise argument error if basic_auth and digest_auth are both present" do956 expect {957 HTTParty::Request.new(Net::HTTP::Post, 'http://api.foo.com/v1', basic_auth: {}, digest_auth: {}).perform958 }.to raise_error(ArgumentError, "only one authentication method, :basic_auth or :digest_auth may be used at a time")959 end960 it "should raise argument error if basic_auth is not a hash" do961 expect {962 HTTParty::Request.new(Net::HTTP::Post, 'http://api.foo.com/v1', basic_auth: %w(foo bar)).perform963 }.to raise_error(ArgumentError, ":basic_auth must be a hash")964 end965 it "should raise argument error if digest_auth is not a hash" do966 expect {967 HTTParty::Request.new(Net::HTTP::Post, 'http://api.foo.com/v1', digest_auth: %w(foo bar)).perform968 }.to raise_error(ArgumentError, ":digest_auth must be a hash")969 end970 it "should raise argument error if headers is not a hash" do971 expect {972 HTTParty::Request.new(Net::HTTP::Post, 'http://api.foo.com/v1', headers: %w(foo bar)).perform973 }.to raise_error(ArgumentError, ":headers must be a hash")974 end975 it "should raise argument error if options method is not http accepted method" do976 expect {977 HTTParty::Request.new('SuperPost', 'http://api.foo.com/v1').perform978 }.to raise_error(ArgumentError, "only get, post, patch, put, delete, head, and options methods are supported")979 end980 it "should raise argument error if http method is post and query is not hash" do981 expect {982 HTTParty::Request.new(Net::HTTP::Post, 'http://api.foo.com/v1', query: "message: hello").perform983 }.to raise_error(ArgumentError, ":query must be hash if using HTTP Post")984 end985 it "should raise RedirectionTooDeep error if limit is negative" do986 expect {987 HTTParty::Request.new(Net::HTTP::Post, 'http://api.foo.com/v1', limit: -1).perform988 }.to raise_error(HTTParty::RedirectionTooDeep, 'HTTP redirects too deep')989 end990 end991end...

Full Screen

Full Screen

redirector_spec.rb

Source:redirector_spec.rb Github

copy

Full Screen

...24 let(:redirector) { described_class.new }25 it { is_expected.to eq 5 }26 end27 end28 describe "#perform" do29 let(:options) { {} }30 let(:redirector) { described_class.new options }31 it "fails with TooManyRedirectsError if max hops reached" do32 req = HTTP::Request.new :verb => :head, :uri => "http://example.com"33 res = proc { |prev_req| redirect_response(301, "#{prev_req.uri}/1") }34 expect { redirector.perform(req, res.call(req), &res) }.35 to raise_error HTTP::Redirector::TooManyRedirectsError36 end37 it "fails with EndlessRedirectError if endless loop detected" do38 req = HTTP::Request.new :verb => :head, :uri => "http://example.com"39 res = redirect_response(301, req.uri)40 expect { redirector.perform(req, res) { res } }.41 to raise_error HTTP::Redirector::EndlessRedirectError42 end43 it "fails with StateError if there were no Location header" do44 req = HTTP::Request.new :verb => :head, :uri => "http://example.com"45 res = simple_response(301)46 expect { |b| redirector.perform(req, res, &b) }.47 to raise_error HTTP::StateError48 end49 it "returns first non-redirect response" do50 req = HTTP::Request.new :verb => :head, :uri => "http://example.com"51 hops = [52 redirect_response(301, "http://example.com/1"),53 redirect_response(301, "http://example.com/2"),54 redirect_response(301, "http://example.com/3"),55 simple_response(200, "foo"),56 redirect_response(301, "http://example.com/4"),57 simple_response(200, "bar")58 ]59 res = redirector.perform(req, hops.shift) { hops.shift }60 expect(res.to_s).to eq "foo"61 end62 context "following 300 redirect" do63 context "with strict mode" do64 let(:options) { {:strict => true} }65 it "it follows with original verb if it's safe" do66 req = HTTP::Request.new :verb => :head, :uri => "http://example.com"67 res = redirect_response 300, "http://example.com/1"68 redirector.perform(req, res) do |prev_req, _|69 expect(prev_req.verb).to be :head70 simple_response 20071 end72 end73 it "raises StateError if original request was PUT" do74 req = HTTP::Request.new :verb => :put, :uri => "http://example.com"75 res = redirect_response 300, "http://example.com/1"76 expect { redirector.perform(req, res) { simple_response 200 } }.77 to raise_error HTTP::StateError78 end79 it "raises StateError if original request was POST" do80 req = HTTP::Request.new :verb => :post, :uri => "http://example.com"81 res = redirect_response 300, "http://example.com/1"82 expect { redirector.perform(req, res) { simple_response 200 } }.83 to raise_error HTTP::StateError84 end85 it "raises StateError if original request was DELETE" do86 req = HTTP::Request.new :verb => :delete, :uri => "http://example.com"87 res = redirect_response 300, "http://example.com/1"88 expect { redirector.perform(req, res) { simple_response 200 } }.89 to raise_error HTTP::StateError90 end91 end92 context "with non-strict mode" do93 let(:options) { {:strict => false} }94 it "it follows with original verb if it's safe" do95 req = HTTP::Request.new :verb => :head, :uri => "http://example.com"96 res = redirect_response 300, "http://example.com/1"97 redirector.perform(req, res) do |prev_req, _|98 expect(prev_req.verb).to be :head99 simple_response 200100 end101 end102 it "it follows with GET if original request was PUT" do103 req = HTTP::Request.new :verb => :put, :uri => "http://example.com"104 res = redirect_response 300, "http://example.com/1"105 redirector.perform(req, res) do |prev_req, _|106 expect(prev_req.verb).to be :get107 simple_response 200108 end109 end110 it "it follows with GET if original request was POST" do111 req = HTTP::Request.new :verb => :post, :uri => "http://example.com"112 res = redirect_response 300, "http://example.com/1"113 redirector.perform(req, res) do |prev_req, _|114 expect(prev_req.verb).to be :get115 simple_response 200116 end117 end118 it "it follows with GET if original request was DELETE" do119 req = HTTP::Request.new :verb => :delete, :uri => "http://example.com"120 res = redirect_response 300, "http://example.com/1"121 redirector.perform(req, res) do |prev_req, _|122 expect(prev_req.verb).to be :get123 simple_response 200124 end125 end126 end127 end128 context "following 301 redirect" do129 context "with strict mode" do130 let(:options) { {:strict => true} }131 it "it follows with original verb if it's safe" do132 req = HTTP::Request.new :verb => :head, :uri => "http://example.com"133 res = redirect_response 301, "http://example.com/1"134 redirector.perform(req, res) do |prev_req, _|135 expect(prev_req.verb).to be :head136 simple_response 200137 end138 end139 it "raises StateError if original request was PUT" do140 req = HTTP::Request.new :verb => :put, :uri => "http://example.com"141 res = redirect_response 301, "http://example.com/1"142 expect { redirector.perform(req, res) { simple_response 200 } }.143 to raise_error HTTP::StateError144 end145 it "raises StateError if original request was POST" do146 req = HTTP::Request.new :verb => :post, :uri => "http://example.com"147 res = redirect_response 301, "http://example.com/1"148 expect { redirector.perform(req, res) { simple_response 200 } }.149 to raise_error HTTP::StateError150 end151 it "raises StateError if original request was DELETE" do152 req = HTTP::Request.new :verb => :delete, :uri => "http://example.com"153 res = redirect_response 301, "http://example.com/1"154 expect { redirector.perform(req, res) { simple_response 200 } }.155 to raise_error HTTP::StateError156 end157 end158 context "with non-strict mode" do159 let(:options) { {:strict => false} }160 it "it follows with original verb if it's safe" do161 req = HTTP::Request.new :verb => :head, :uri => "http://example.com"162 res = redirect_response 301, "http://example.com/1"163 redirector.perform(req, res) do |prev_req, _|164 expect(prev_req.verb).to be :head165 simple_response 200166 end167 end168 it "it follows with GET if original request was PUT" do169 req = HTTP::Request.new :verb => :put, :uri => "http://example.com"170 res = redirect_response 301, "http://example.com/1"171 redirector.perform(req, res) do |prev_req, _|172 expect(prev_req.verb).to be :get173 simple_response 200174 end175 end176 it "it follows with GET if original request was POST" do177 req = HTTP::Request.new :verb => :post, :uri => "http://example.com"178 res = redirect_response 301, "http://example.com/1"179 redirector.perform(req, res) do |prev_req, _|180 expect(prev_req.verb).to be :get181 simple_response 200182 end183 end184 it "it follows with GET if original request was DELETE" do185 req = HTTP::Request.new :verb => :delete, :uri => "http://example.com"186 res = redirect_response 301, "http://example.com/1"187 redirector.perform(req, res) do |prev_req, _|188 expect(prev_req.verb).to be :get189 simple_response 200190 end191 end192 end193 end194 context "following 302 redirect" do195 context "with strict mode" do196 let(:options) { {:strict => true} }197 it "it follows with original verb if it's safe" do198 req = HTTP::Request.new :verb => :head, :uri => "http://example.com"199 res = redirect_response 302, "http://example.com/1"200 redirector.perform(req, res) do |prev_req, _|201 expect(prev_req.verb).to be :head202 simple_response 200203 end204 end205 it "raises StateError if original request was PUT" do206 req = HTTP::Request.new :verb => :put, :uri => "http://example.com"207 res = redirect_response 302, "http://example.com/1"208 expect { redirector.perform(req, res) { simple_response 200 } }.209 to raise_error HTTP::StateError210 end211 it "raises StateError if original request was POST" do212 req = HTTP::Request.new :verb => :post, :uri => "http://example.com"213 res = redirect_response 302, "http://example.com/1"214 expect { redirector.perform(req, res) { simple_response 200 } }.215 to raise_error HTTP::StateError216 end217 it "raises StateError if original request was DELETE" do218 req = HTTP::Request.new :verb => :delete, :uri => "http://example.com"219 res = redirect_response 302, "http://example.com/1"220 expect { redirector.perform(req, res) { simple_response 200 } }.221 to raise_error HTTP::StateError222 end223 end224 context "with non-strict mode" do225 let(:options) { {:strict => false} }226 it "it follows with original verb if it's safe" do227 req = HTTP::Request.new :verb => :head, :uri => "http://example.com"228 res = redirect_response 302, "http://example.com/1"229 redirector.perform(req, res) do |prev_req, _|230 expect(prev_req.verb).to be :head231 simple_response 200232 end233 end234 it "it follows with GET if original request was PUT" do235 req = HTTP::Request.new :verb => :put, :uri => "http://example.com"236 res = redirect_response 302, "http://example.com/1"237 redirector.perform(req, res) do |prev_req, _|238 expect(prev_req.verb).to be :get239 simple_response 200240 end241 end242 it "it follows with GET if original request was POST" do243 req = HTTP::Request.new :verb => :post, :uri => "http://example.com"244 res = redirect_response 302, "http://example.com/1"245 redirector.perform(req, res) do |prev_req, _|246 expect(prev_req.verb).to be :get247 simple_response 200248 end249 end250 it "it follows with GET if original request was DELETE" do251 req = HTTP::Request.new :verb => :delete, :uri => "http://example.com"252 res = redirect_response 302, "http://example.com/1"253 redirector.perform(req, res) do |prev_req, _|254 expect(prev_req.verb).to be :get255 simple_response 200256 end257 end258 end259 end260 context "following 303 redirect" do261 it "follows with HEAD if original request was HEAD" do262 req = HTTP::Request.new :verb => :head, :uri => "http://example.com"263 res = redirect_response 303, "http://example.com/1"264 redirector.perform(req, res) do |prev_req, _|265 expect(prev_req.verb).to be :head266 simple_response 200267 end268 end269 it "follows with GET if original request was GET" do270 req = HTTP::Request.new :verb => :get, :uri => "http://example.com"271 res = redirect_response 303, "http://example.com/1"272 redirector.perform(req, res) do |prev_req, _|273 expect(prev_req.verb).to be :get274 simple_response 200275 end276 end277 it "follows with GET if original request was neither GET nor HEAD" do278 req = HTTP::Request.new :verb => :post, :uri => "http://example.com"279 res = redirect_response 303, "http://example.com/1"280 redirector.perform(req, res) do |prev_req, _|281 expect(prev_req.verb).to be :get282 simple_response 200283 end284 end285 end286 context "following 307 redirect" do287 it "follows with original request's verb" do288 req = HTTP::Request.new :verb => :post, :uri => "http://example.com"289 res = redirect_response 307, "http://example.com/1"290 redirector.perform(req, res) do |prev_req, _|291 expect(prev_req.verb).to be :post292 simple_response 200293 end294 end295 end296 context "following 308 redirect" do297 it "follows with original request's verb" do298 req = HTTP::Request.new :verb => :post, :uri => "http://example.com"299 res = redirect_response 308, "http://example.com/1"300 redirector.perform(req, res) do |prev_req, _|301 expect(prev_req.verb).to be :post302 simple_response 200303 end304 end305 end306 end307end...

Full Screen

Full Screen

perform

Using AI Code Generation

copy

Full Screen

1 def perform(url)2 uri = URI.parse(url)3 response = Net::HTTP.get_response(uri)4http.perform('http://www.google.com')

Full Screen

Full Screen

perform

Using AI Code Generation

copy

Full Screen

1response = HTTP.get("http://localhost:3000")2response = HTTP.get("http://localhost:3000")3response = HTTP.get("http://localhost:3000")4response = HTTP.get("http://localhost:3000")5response = HTTP.get("http://localhost:3000")6response = HTTP.get("http://localhost:3000")7response = HTTP.get("http://localhost:3000")8response = HTTP.get("http://localhost:3000")

Full Screen

Full Screen

perform

Using AI Code Generation

copy

Full Screen

1response = HTTP.get("https://api.github.com/users/andrew")2response = HTTP.get("https://api.github.com/users/andrew")3response = HTTP.get("https://api.github.com/users/andrew")4response = HTTP.get("https://api.github.com/users/andrew")5response = HTTP.get("https://api.github.com/users/andrew")6response = HTTP.get("https://api.github.com/users/andrew")7response = HTTP.get("https://api.github.com/users/andrew")8response = HTTP.get("https://api.github.com/users/andrew")9response = HTTP.get("https://api.github.com/users/andrew")10response = HTTP.get("https://api.github.com/users/andrew")11response = HTTP.get("https://api.github.com/users/andrew")12response = HTTP.get("https://api.github.com

Full Screen

Full Screen

perform

Using AI Code Generation

copy

Full Screen

1response = HTTP.get("http://localhost:3000")2response = HTTP.get("http://localhost:3000")3response = HTTP.get("http://localhost:3000")4response = HTTP.get("http://localhost:3000")5response = HTTP.get("http://localhost:3000")6response = HTTP.get("http://localhost:3000")7response = HTTP.get("http://localhost:3000")8response = HTTP.get("http://localhost:3000")

Full Screen

Full Screen

perform

Using AI Code Generation

copy

Full Screen

1response = HTTP.new.perform("GET", "http://localhost:3000")2 def perform(method, url)3 def initialize(body)4server = TCPServer.new(3000)5response = HTTP.new.perform("POST", "http://localhost:3000", { name: "John" })6 def perform(method, url, request_body = {})7 def initialize(body)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful