spec/faraday/connection_spec.rb in faraday-1.10.4 vs spec/faraday/connection_spec.rb in faraday-2.0.0.alpha.pre.1
- old
+ new
@@ -1,7 +1,17 @@
# frozen_string_literal: true
+class CustomEncoder
+ def encode(params)
+ params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',')
+ end
+
+ def decode(params)
+ params.split(',').map { |pair| pair.split('-') }.to_h
+ end
+end
+
shared_examples 'initializer with url' do
context 'with simple url' do
let(:address) { 'http://sushi.com' }
it { expect(subject.host).to eq('sushi.com') }
@@ -101,10 +111,16 @@
let(:url) { 'http://sushi.com/fish?a=1&b=2' }
let(:options) { { params: { a: 3 } } }
it { expect(subject.params).to eq('a' => 3, 'b' => '2') }
end
+ context 'with basic_auth in url' do
+ let(:url) { 'http://Aladdin:open%20sesame@sushi.com/fish' }
+
+ it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
+ end
+
context 'with custom headers' do
let(:options) { { headers: { user_agent: 'Faraday' } } }
it { expect(subject.headers['User-agent']).to eq('Faraday') }
end
@@ -122,11 +138,11 @@
end
context 'with block' do
let(:conn) do
Faraday::Connection.new(params: { 'a' => '1' }) do |faraday|
- faraday.adapter :net_http
+ faraday.adapter :test
faraday.url_prefix = 'http://sushi.com/omnom'
end
end
it { expect(conn.builder.handlers.size).to eq(0) }
@@ -139,32 +155,10 @@
expect(conn.app).to receive(:close)
conn.close
end
end
- describe 'basic_auth' do
- subject { conn }
-
- context 'calling the #basic_auth method' do
- before { subject.basic_auth 'Aladdin', 'open sesame' }
-
- it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
- end
-
- context 'adding basic auth info to url' do
- let(:url) { 'http://Aladdin:open%20sesame@sushi.com/fish' }
-
- it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
- end
- end
-
- describe '#token_auth' do
- before { subject.token_auth('abcdef', nonce: 'abc') }
-
- it { expect(subject.headers['Authorization']).to eq('Token nonce="abc", token="abcdef"') }
- end
-
describe '#build_exclusive_url' do
context 'with relative path' do
subject { conn.build_exclusive_url('sake.html') }
it 'uses connection host as default host' do
@@ -554,30 +548,36 @@
end
end
end
context 'performing a request' do
- before { stub_request(:get, 'http://example.com') }
+ let(:url) { 'http://example.com' }
+ let(:conn) do
+ Faraday.new do |f|
+ f.adapter :test do |stubs|
+ stubs.get(url) do
+ [200, {}, 'ok']
+ end
+ end
+ end
+ end
it 'dynamically checks proxy' do
with_env 'http_proxy' => 'http://proxy.com:80' do
- conn = Faraday.new
expect(conn.proxy.uri.host).to eq('proxy.com')
- conn.get('http://example.com') do |req|
+ conn.get(url) do |req|
expect(req.options.proxy.uri.host).to eq('proxy.com')
end
end
- conn.get('http://example.com')
+ conn.get(url)
expect(conn.instance_variable_get('@temp_proxy')).to be_nil
end
it 'dynamically check no proxy' do
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
- conn = Faraday.new
-
expect(conn.proxy.uri.host).to eq('proxy.com')
conn.get('http://example.com') do |req|
expect(req.options.proxy).to be_nil
end
@@ -603,11 +603,10 @@
it { expect(subject.headers['content-type']).to eq('text/plain') }
it { expect(subject.params['a']).to eq('1') }
context 'after manual changes' do
before do
- subject.basic_auth('', '')
subject.headers['content-length'] = 12
subject.params['b'] = '2'
subject.options[:open_timeout] = 10
end
@@ -643,14 +642,21 @@
end
describe 'request params' do
context 'with simple url' do
let(:url) { 'http://example.com' }
- let!(:stubbed) { stub_request(:get, 'http://example.com?a=a&p=3') }
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
- after { expect(stubbed).to have_been_made.once }
+ before do
+ conn.adapter(:test, stubs)
+ stubs.get('http://example.com?a=a&p=3') do
+ [200, {}, 'ok']
+ end
+ end
+ after { stubs.verify_stubbed_calls }
+
it 'test_overrides_request_params' do
conn.get('?p=2&a=a', p: 3)
end
it 'test_overrides_request_params_block' do
@@ -667,66 +673,87 @@
end
context 'with url and extra params' do
let(:url) { 'http://example.com?a=1&b=2' }
let(:options) { { params: { c: 3 } } }
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
+ before do
+ conn.adapter(:test, stubs)
+ end
+
it 'merges connection and request params' do
- stubbed = stub_request(:get, 'http://example.com?a=1&b=2&c=3&limit=5&page=1')
+ expected = 'http://example.com?a=1&b=2&c=3&limit=5&page=1'
+ stubs.get(expected) { [200, {}, 'ok'] }
conn.get('?page=1', limit: 5)
- expect(stubbed).to have_been_made.once
+ stubs.verify_stubbed_calls
end
it 'allows to override all params' do
- stubbed = stub_request(:get, 'http://example.com?b=b')
+ expected = 'http://example.com?b=b'
+ stubs.get(expected) { [200, {}, 'ok'] }
conn.get('?p=1&a=a', p: 2) do |req|
expect(req.params[:a]).to eq('a')
expect(req.params['c']).to eq(3)
expect(req.params['p']).to eq(2)
req.params = { b: 'b' }
expect(req.params['b']).to eq('b')
end
- expect(stubbed).to have_been_made.once
+ stubs.verify_stubbed_calls
end
it 'allows to set params_encoder for single request' do
- encoder = Object.new
- def encoder.encode(params)
- params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',')
- end
- stubbed = stub_request(:get, 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE')
+ encoder = CustomEncoder.new
+ expected = 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE'
+ stubs.get(expected) { [200, {}, 'ok'] }
- conn.get('/', feeling: 'blue') do |req|
+ conn.get('/', a: 1, b: 2, c: 3, feeling: 'blue') do |req|
req.options.params_encoder = encoder
end
- expect(stubbed).to have_been_made.once
+ stubs.verify_stubbed_calls
end
end
context 'with default params encoder' do
- let!(:stubbed) { stub_request(:get, 'http://example.com?color%5B%5D=red&color%5B%5D=blue') }
- after { expect(stubbed).to have_been_made.once }
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
+ before do
+ conn.adapter(:test, stubs)
+ stubs.get('http://example.com?color%5B%5D=blue&color%5B%5D=red') do
+ [200, {}, 'ok']
+ end
+ end
+
+ after { stubs.verify_stubbed_calls }
+
it 'supports array params in url' do
- conn.get('http://example.com?color[]=red&color[]=blue')
+ conn.get('http://example.com?color[]=blue&color[]=red')
end
it 'supports array params in params' do
- conn.get('http://example.com', color: %w[red blue])
+ conn.get('http://example.com', color: %w[blue red])
end
end
context 'with flat params encoder' do
let(:options) { { request: { params_encoder: Faraday::FlatParamsEncoder } } }
- let!(:stubbed) { stub_request(:get, 'http://example.com?color=blue') }
- after { expect(stubbed).to have_been_made.once }
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
+ before do
+ conn.adapter(:test, stubs)
+ stubs.get('http://example.com?color=blue&color=red') do
+ [200, {}, 'ok']
+ end
+ end
+
+ after { stubs.verify_stubbed_calls }
+
it 'supports array params in params' do
- conn.get('http://example.com', color: %w[red blue])
+ conn.get('http://example.com', color: %w[blue red])
end
context 'with array param in url' do
- let(:url) { 'http://example.com?color[]=red&color[]=blue' }
+ let(:url) { 'http://example.com?color[]=blue&color[]=red' }
it do
conn.get('/')
end
end