|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
| 3 | +class CustomEncoder |
| 4 | + def encode(params) |
| 5 | + params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',') |
| 6 | + end |
| 7 | + |
| 8 | + def decode(params) |
| 9 | + params.split(',').map { |pair| pair.split('-') }.to_h |
| 10 | + end |
| 11 | +end |
| 12 | + |
3 | 13 | shared_examples 'initializer with url' do
|
4 | 14 | context 'with simple url' do
|
5 | 15 | let(:address) { 'http://sushi.com' }
|
|
130 | 140 | context 'with block' do
|
131 | 141 | let(:conn) do
|
132 | 142 | Faraday::Connection.new(params: { 'a' => '1' }) do |faraday|
|
133 |
| - faraday.adapter :net_http |
| 143 | + faraday.adapter :test |
134 | 144 | faraday.url_prefix = 'http://sushi.com/omnom'
|
135 | 145 | end
|
136 | 146 | end
|
|
540 | 550 | end
|
541 | 551 |
|
542 | 552 | context 'performing a request' do
|
543 |
| - before { stub_request(:get, 'http://example.com') } |
| 553 | + let(:url) { 'http://example.com' } |
| 554 | + let(:conn) do |
| 555 | + Faraday.new do |f| |
| 556 | + f.adapter :test do |stubs| |
| 557 | + stubs.get(url) do |
| 558 | + [200, {}, 'ok'] |
| 559 | + end |
| 560 | + end |
| 561 | + end |
| 562 | + end |
544 | 563 |
|
545 | 564 | it 'dynamically checks proxy' do
|
546 | 565 | with_env 'http_proxy' => 'http://proxy.com:80' do
|
547 |
| - conn = Faraday.new |
548 | 566 | expect(conn.proxy.uri.host).to eq('proxy.com')
|
549 | 567 |
|
550 |
| - conn.get('http://example.com') do |req| |
| 568 | + conn.get(url) do |req| |
551 | 569 | expect(req.options.proxy.uri.host).to eq('proxy.com')
|
552 | 570 | end
|
553 | 571 | end
|
554 | 572 |
|
555 |
| - conn.get('http://example.com') |
| 573 | + conn.get(url) |
556 | 574 | expect(conn.instance_variable_get('@temp_proxy')).to be_nil
|
557 | 575 | end
|
558 | 576 |
|
559 | 577 | it 'dynamically check no proxy' do
|
560 | 578 | with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
|
561 |
| - conn = Faraday.new |
562 |
| - |
563 | 579 | expect(conn.proxy.uri.host).to eq('proxy.com')
|
564 | 580 |
|
565 | 581 | conn.get('http://example.com') do |req|
|
|
628 | 644 | describe 'request params' do
|
629 | 645 | context 'with simple url' do
|
630 | 646 | let(:url) { 'http://example.com' }
|
631 |
| - let!(:stubbed) { stub_request(:get, 'http://example.com?a=a&p=3') } |
| 647 | + let(:stubs) { Faraday::Adapter::Test::Stubs.new } |
| 648 | + |
| 649 | + before do |
| 650 | + conn.adapter(:test, stubs) |
| 651 | + stubs.get('http://example.com?a=a&p=3') do |
| 652 | + [200, {}, 'ok'] |
| 653 | + end |
| 654 | + end |
632 | 655 |
|
633 |
| - after { expect(stubbed).to have_been_made.once } |
| 656 | + after { stubs.verify_stubbed_calls } |
634 | 657 |
|
635 | 658 | it 'test_overrides_request_params' do
|
636 | 659 | conn.get('?p=2&a=a', p: 3)
|
|
652 | 675 | context 'with url and extra params' do
|
653 | 676 | let(:url) { 'http://example.com?a=1&b=2' }
|
654 | 677 | let(:options) { { params: { c: 3 } } }
|
| 678 | + let(:stubs) { Faraday::Adapter::Test::Stubs.new } |
| 679 | + |
| 680 | + before do |
| 681 | + conn.adapter(:test, stubs) |
| 682 | + end |
655 | 683 |
|
656 | 684 | it 'merges connection and request params' do
|
657 |
| - stubbed = stub_request(:get, 'http://example.com?a=1&b=2&c=3&limit=5&page=1') |
| 685 | + expected = 'http://example.com?a=1&b=2&c=3&limit=5&page=1' |
| 686 | + stubs.get(expected) { [200, {}, 'ok'] } |
658 | 687 | conn.get('?page=1', limit: 5)
|
659 |
| - expect(stubbed).to have_been_made.once |
| 688 | + stubs.verify_stubbed_calls |
660 | 689 | end
|
661 | 690 |
|
662 | 691 | it 'allows to override all params' do
|
663 |
| - stubbed = stub_request(:get, 'http://example.com?b=b') |
| 692 | + expected = 'http://example.com?b=b' |
| 693 | + stubs.get(expected) { [200, {}, 'ok'] } |
664 | 694 | conn.get('?p=1&a=a', p: 2) do |req|
|
665 | 695 | expect(req.params[:a]).to eq('a')
|
666 | 696 | expect(req.params['c']).to eq(3)
|
667 | 697 | expect(req.params['p']).to eq(2)
|
668 | 698 | req.params = { b: 'b' }
|
669 | 699 | expect(req.params['b']).to eq('b')
|
670 | 700 | end
|
671 |
| - expect(stubbed).to have_been_made.once |
| 701 | + stubs.verify_stubbed_calls |
672 | 702 | end
|
673 | 703 |
|
674 | 704 | it 'allows to set params_encoder for single request' do
|
675 |
| - encoder = Object.new |
676 |
| - def encoder.encode(params) |
677 |
| - params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',') |
678 |
| - end |
679 |
| - stubbed = stub_request(:get, 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE') |
| 705 | + encoder = CustomEncoder.new |
| 706 | + expected = 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE' |
| 707 | + stubs.get(expected) { [200, {}, 'ok'] } |
680 | 708 |
|
681 |
| - conn.get('/', feeling: 'blue') do |req| |
| 709 | + conn.get('/', a: 1, b: 2, c: 3, feeling: 'blue') do |req| |
682 | 710 | req.options.params_encoder = encoder
|
683 | 711 | end
|
684 |
| - expect(stubbed).to have_been_made.once |
| 712 | + stubs.verify_stubbed_calls |
685 | 713 | end
|
686 | 714 | end
|
687 | 715 |
|
688 | 716 | context 'with default params encoder' do
|
689 |
| - let!(:stubbed) { stub_request(:get, 'http://example.com?color%5B%5D=red&color%5B%5D=blue') } |
690 |
| - after { expect(stubbed).to have_been_made.once } |
| 717 | + let(:stubs) { Faraday::Adapter::Test::Stubs.new } |
| 718 | + |
| 719 | + before do |
| 720 | + conn.adapter(:test, stubs) |
| 721 | + stubs.get('http://example.com?color%5B%5D=blue&color%5B%5D=red') do |
| 722 | + [200, {}, 'ok'] |
| 723 | + end |
| 724 | + end |
| 725 | + |
| 726 | + after { stubs.verify_stubbed_calls } |
691 | 727 |
|
692 | 728 | it 'supports array params in url' do
|
693 |
| - conn.get('http://example.com?color[]=red&color[]=blue') |
| 729 | + conn.get('http://example.com?color[]=blue&color[]=red') |
694 | 730 | end
|
695 | 731 |
|
696 | 732 | it 'supports array params in params' do
|
697 |
| - conn.get('http://example.com', color: %w[red blue]) |
| 733 | + conn.get('http://example.com', color: %w[blue red]) |
698 | 734 | end
|
699 | 735 | end
|
700 | 736 |
|
701 | 737 | context 'with flat params encoder' do
|
702 | 738 | let(:options) { { request: { params_encoder: Faraday::FlatParamsEncoder } } }
|
703 |
| - let!(:stubbed) { stub_request(:get, 'http://example.com?color=blue') } |
704 |
| - after { expect(stubbed).to have_been_made.once } |
| 739 | + let(:stubs) { Faraday::Adapter::Test::Stubs.new } |
| 740 | + |
| 741 | + before do |
| 742 | + conn.adapter(:test, stubs) |
| 743 | + stubs.get('http://example.com?color=blue&color=red') do |
| 744 | + [200, {}, 'ok'] |
| 745 | + end |
| 746 | + end |
| 747 | + |
| 748 | + after { stubs.verify_stubbed_calls } |
705 | 749 |
|
706 | 750 | it 'supports array params in params' do
|
707 |
| - conn.get('http://example.com', color: %w[red blue]) |
| 751 | + conn.get('http://example.com', color: %w[blue red]) |
708 | 752 | end
|
709 | 753 |
|
710 | 754 | context 'with array param in url' do
|
711 |
| - let(:url) { 'http://example.com?color[]=red&color[]=blue' } |
| 755 | + let(:url) { 'http://example.com?color[]=blue&color[]=red' } |
712 | 756 |
|
713 | 757 | it do
|
714 | 758 | conn.get('/')
|
|
0 commit comments