faraday 1.10.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +117 -1
  3. data/README.md +16 -9
  4. data/examples/client_spec.rb +1 -1
  5. data/examples/client_test.rb +2 -2
  6. data/lib/faraday/adapter/test.rb +2 -0
  7. data/lib/faraday/adapter.rb +2 -5
  8. data/lib/faraday/connection.rb +5 -86
  9. data/lib/faraday/encoders/nested_params_encoder.rb +2 -2
  10. data/lib/faraday/error.rb +3 -2
  11. data/lib/faraday/logging/formatter.rb +1 -0
  12. data/lib/faraday/middleware.rb +0 -1
  13. data/lib/faraday/middleware_registry.rb +15 -79
  14. data/lib/faraday/options.rb +3 -3
  15. data/lib/faraday/rack_builder.rb +21 -18
  16. data/lib/faraday/request/authorization.rb +28 -41
  17. data/lib/faraday/request/instrumentation.rb +2 -0
  18. data/lib/faraday/request/url_encoded.rb +2 -0
  19. data/lib/faraday/request.rb +6 -25
  20. data/lib/faraday/response/json.rb +4 -4
  21. data/lib/faraday/response/logger.rb +2 -0
  22. data/lib/faraday/response/raise_error.rb +9 -1
  23. data/lib/faraday/response.rb +7 -20
  24. data/lib/faraday/utils/headers.rb +1 -1
  25. data/lib/faraday/utils.rb +10 -5
  26. data/lib/faraday/version.rb +1 -1
  27. data/lib/faraday.rb +4 -46
  28. data/spec/faraday/connection_spec.rb +102 -51
  29. data/spec/faraday/options/env_spec.rb +2 -2
  30. data/spec/faraday/rack_builder_spec.rb +7 -50
  31. data/spec/faraday/request/authorization_spec.rb +19 -32
  32. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  33. data/spec/faraday/request/url_encoded_spec.rb +0 -1
  34. data/spec/faraday/request_spec.rb +0 -11
  35. data/spec/faraday/response/json_spec.rb +4 -6
  36. data/spec/faraday/response/raise_error_spec.rb +7 -4
  37. data/spec/faraday/utils_spec.rb +62 -1
  38. data/spec/support/fake_safe_buffer.rb +1 -1
  39. data/spec/support/helper_methods.rb +0 -37
  40. data/spec/support/shared_examples/adapter.rb +0 -1
  41. data/spec/support/shared_examples/request_method.rb +5 -18
  42. metadata +5 -161
  43. data/lib/faraday/adapter/typhoeus.rb +0 -15
  44. data/lib/faraday/autoload.rb +0 -87
  45. data/lib/faraday/dependency_loader.rb +0 -37
  46. data/lib/faraday/request/basic_authentication.rb +0 -20
  47. data/lib/faraday/request/token_authentication.rb +0 -20
  48. data/spec/faraday/adapter/em_http_spec.rb +0 -49
  49. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
  50. data/spec/faraday/adapter/excon_spec.rb +0 -49
  51. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  52. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  53. data/spec/faraday/adapter/patron_spec.rb +0 -18
  54. data/spec/faraday/adapter/rack_spec.rb +0 -8
  55. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  56. data/spec/faraday/composite_read_io_spec.rb +0 -80
  57. data/spec/faraday/response/middleware_spec.rb +0 -68
  58. data/spec/support/webmock_rack_app.rb +0 -68
@@ -1,5 +1,15 @@
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' }
@@ -103,6 +113,12 @@ RSpec.describe Faraday::Connection do
103
113
  it { expect(subject.params).to eq('a' => 3, 'b' => '2') }
104
114
  end
105
115
 
116
+ context 'with basic_auth in url' do
117
+ let(:url) { 'http://Aladdin:open%20sesame@sushi.com/fish' }
118
+
119
+ it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
120
+ end
121
+
106
122
  context 'with custom headers' do
107
123
  let(:options) { { headers: { user_agent: 'Faraday' } } }
108
124
 
@@ -124,7 +140,7 @@ RSpec.describe Faraday::Connection do
124
140
  context 'with block' do
125
141
  let(:conn) do
126
142
  Faraday::Connection.new(params: { 'a' => '1' }) do |faraday|
127
- faraday.adapter :net_http
143
+ faraday.adapter :test
128
144
  faraday.url_prefix = 'http://sushi.com/omnom'
129
145
  end
130
146
  end
@@ -135,34 +151,15 @@ RSpec.describe Faraday::Connection do
135
151
  end
136
152
 
137
153
  describe '#close' do
154
+ before { Faraday.default_adapter = :test }
155
+ after { Faraday.default_adapter = nil }
156
+
138
157
  it 'can close underlying app' do
139
158
  expect(conn.app).to receive(:close)
140
159
  conn.close
141
160
  end
142
161
  end
143
162
 
144
- describe 'basic_auth' do
145
- subject { conn }
146
-
147
- context 'calling the #basic_auth method' do
148
- before { subject.basic_auth 'Aladdin', 'open sesame' }
149
-
150
- it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
151
- end
152
-
153
- context 'adding basic auth info to url' do
154
- let(:url) { 'http://Aladdin:open%20sesame@sushi.com/fish' }
155
-
156
- it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
157
- end
158
- end
159
-
160
- describe '#token_auth' do
161
- before { subject.token_auth('abcdef', nonce: 'abc') }
162
-
163
- it { expect(subject.headers['Authorization']).to eq('Token nonce="abc", token="abcdef"') }
164
- end
165
-
166
163
  describe '#build_exclusive_url' do
167
164
  context 'with relative path' do
168
165
  subject { conn.build_exclusive_url('sake.html') }
@@ -556,26 +553,32 @@ RSpec.describe Faraday::Connection do
556
553
  end
557
554
 
558
555
  context 'performing a request' do
559
- before { stub_request(:get, 'http://example.com') }
556
+ let(:url) { 'http://example.com' }
557
+ let(:conn) do
558
+ Faraday.new do |f|
559
+ f.adapter :test do |stubs|
560
+ stubs.get(url) do
561
+ [200, {}, 'ok']
562
+ end
563
+ end
564
+ end
565
+ end
560
566
 
561
567
  it 'dynamically checks proxy' do
562
568
  with_env 'http_proxy' => 'http://proxy.com:80' do
563
- conn = Faraday.new
564
569
  expect(conn.proxy.uri.host).to eq('proxy.com')
565
570
 
566
- conn.get('http://example.com') do |req|
571
+ conn.get(url) do |req|
567
572
  expect(req.options.proxy.uri.host).to eq('proxy.com')
568
573
  end
569
574
  end
570
575
 
571
- conn.get('http://example.com')
576
+ conn.get(url)
572
577
  expect(conn.instance_variable_get('@temp_proxy')).to be_nil
573
578
  end
574
579
 
575
580
  it 'dynamically check no proxy' do
576
581
  with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
577
- conn = Faraday.new
578
-
579
582
  expect(conn.proxy.uri.host).to eq('proxy.com')
580
583
 
581
584
  conn.get('http://example.com') do |req|
@@ -605,7 +608,6 @@ RSpec.describe Faraday::Connection do
605
608
 
606
609
  context 'after manual changes' do
607
610
  before do
608
- subject.basic_auth('', '')
609
611
  subject.headers['content-length'] = 12
610
612
  subject.params['b'] = '2'
611
613
  subject.options[:open_timeout] = 10
@@ -640,14 +642,42 @@ RSpec.describe Faraday::Connection do
640
642
 
641
643
  it_behaves_like 'default connection options'
642
644
  end
645
+
646
+ context 'preserving a user_agent assigned via default_conncetion_options' do
647
+ around do |example|
648
+ old = Faraday.default_connection_options
649
+ Faraday.default_connection_options = { headers: { user_agent: 'My Agent 1.2' } }
650
+ example.run
651
+ Faraday.default_connection_options = old
652
+ end
653
+
654
+ context 'when url is a Hash' do
655
+ let(:conn) { Faraday.new(url: 'http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
656
+
657
+ it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
658
+ end
659
+
660
+ context 'when url is a String' do
661
+ let(:conn) { Faraday.new('http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
662
+
663
+ it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
664
+ end
665
+ end
643
666
  end
644
667
 
645
668
  describe 'request params' do
646
669
  context 'with simple url' do
647
670
  let(:url) { 'http://example.com' }
648
- let!(:stubbed) { stub_request(:get, 'http://example.com?a=a&p=3') }
671
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
672
+
673
+ before do
674
+ conn.adapter(:test, stubs)
675
+ stubs.get('http://example.com?a=a&p=3') do
676
+ [200, {}, 'ok']
677
+ end
678
+ end
649
679
 
650
- after { expect(stubbed).to have_been_made.once }
680
+ after { stubs.verify_stubbed_calls }
651
681
 
652
682
  it 'test_overrides_request_params' do
653
683
  conn.get('?p=2&a=a', p: 3)
@@ -669,15 +699,22 @@ RSpec.describe Faraday::Connection do
669
699
  context 'with url and extra params' do
670
700
  let(:url) { 'http://example.com?a=1&b=2' }
671
701
  let(:options) { { params: { c: 3 } } }
702
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
703
+
704
+ before do
705
+ conn.adapter(:test, stubs)
706
+ end
672
707
 
673
708
  it 'merges connection and request params' do
674
- stubbed = stub_request(:get, 'http://example.com?a=1&b=2&c=3&limit=5&page=1')
709
+ expected = 'http://example.com?a=1&b=2&c=3&limit=5&page=1'
710
+ stubs.get(expected) { [200, {}, 'ok'] }
675
711
  conn.get('?page=1', limit: 5)
676
- expect(stubbed).to have_been_made.once
712
+ stubs.verify_stubbed_calls
677
713
  end
678
714
 
679
715
  it 'allows to override all params' do
680
- stubbed = stub_request(:get, 'http://example.com?b=b')
716
+ expected = 'http://example.com?b=b'
717
+ stubs.get(expected) { [200, {}, 'ok'] }
681
718
  conn.get('?p=1&a=a', p: 2) do |req|
682
719
  expect(req.params[:a]).to eq('a')
683
720
  expect(req.params['c']).to eq(3)
@@ -685,47 +722,61 @@ RSpec.describe Faraday::Connection do
685
722
  req.params = { b: 'b' }
686
723
  expect(req.params['b']).to eq('b')
687
724
  end
688
- expect(stubbed).to have_been_made.once
725
+ stubs.verify_stubbed_calls
689
726
  end
690
727
 
691
728
  it 'allows to set params_encoder for single request' do
692
- encoder = Object.new
693
- def encoder.encode(params)
694
- params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',')
695
- end
696
- stubbed = stub_request(:get, 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE')
729
+ encoder = CustomEncoder.new
730
+ expected = 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE'
731
+ stubs.get(expected) { [200, {}, 'ok'] }
697
732
 
698
- conn.get('/', feeling: 'blue') do |req|
733
+ conn.get('/', a: 1, b: 2, c: 3, feeling: 'blue') do |req|
699
734
  req.options.params_encoder = encoder
700
735
  end
701
- expect(stubbed).to have_been_made.once
736
+ stubs.verify_stubbed_calls
702
737
  end
703
738
  end
704
739
 
705
740
  context 'with default params encoder' do
706
- let!(:stubbed) { stub_request(:get, 'http://example.com?color%5B%5D=red&color%5B%5D=blue') }
707
- after { expect(stubbed).to have_been_made.once }
741
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
742
+
743
+ before do
744
+ conn.adapter(:test, stubs)
745
+ stubs.get('http://example.com?color%5B%5D=blue&color%5B%5D=red') do
746
+ [200, {}, 'ok']
747
+ end
748
+ end
749
+
750
+ after { stubs.verify_stubbed_calls }
708
751
 
709
752
  it 'supports array params in url' do
710
- conn.get('http://example.com?color[]=red&color[]=blue')
753
+ conn.get('http://example.com?color[]=blue&color[]=red')
711
754
  end
712
755
 
713
756
  it 'supports array params in params' do
714
- conn.get('http://example.com', color: %w[red blue])
757
+ conn.get('http://example.com', color: %w[blue red])
715
758
  end
716
759
  end
717
760
 
718
761
  context 'with flat params encoder' do
719
762
  let(:options) { { request: { params_encoder: Faraday::FlatParamsEncoder } } }
720
- let!(:stubbed) { stub_request(:get, 'http://example.com?color=blue') }
721
- after { expect(stubbed).to have_been_made.once }
763
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
764
+
765
+ before do
766
+ conn.adapter(:test, stubs)
767
+ stubs.get('http://example.com?color=blue&color=red') do
768
+ [200, {}, 'ok']
769
+ end
770
+ end
771
+
772
+ after { stubs.verify_stubbed_calls }
722
773
 
723
774
  it 'supports array params in params' do
724
- conn.get('http://example.com', color: %w[red blue])
775
+ conn.get('http://example.com', color: %w[blue red])
725
776
  end
726
777
 
727
778
  context 'with array param in url' do
728
- let(:url) { 'http://example.com?color[]=red&color[]=blue' }
779
+ let(:url) { 'http://example.com?color[]=blue&color[]=red' }
729
780
 
730
781
  it do
731
782
  conn.get('/')
@@ -29,12 +29,12 @@ RSpec.describe Faraday::Env do
29
29
 
30
30
  it 'retains custom members' do
31
31
  env[:foo] = 'custom 1'
32
- env[:bar] = :custom_2
32
+ env[:bar] = :custom2
33
33
  env2 = Faraday::Env.from(env)
34
34
  env2[:baz] = 'custom 3'
35
35
 
36
36
  expect(env2[:foo]).to eq('custom 1')
37
- expect(env2[:bar]).to eq(:custom_2)
37
+ expect(env2[:bar]).to eq(:custom2)
38
38
  expect(env[:baz]).to be_nil
39
39
  end
40
40
 
@@ -12,16 +12,16 @@ RSpec.describe Faraday::RackBuilder do
12
12
 
13
13
  class Apple < Handler
14
14
  end
15
+
15
16
  class Orange < Handler
16
17
  end
17
- class Banana < Handler
18
- end
19
18
 
20
- class Broken < Faraday::Middleware
21
- dependency 'zomg/i_dont/exist'
19
+ class Banana < Handler
22
20
  end
23
21
 
24
22
  subject { conn.builder }
23
+ before { Faraday.default_adapter = :test }
24
+ after { Faraday.default_adapter = nil }
25
25
 
26
26
  context 'with default stack' do
27
27
  let(:conn) { Faraday::Connection.new }
@@ -88,13 +88,6 @@ RSpec.describe Faraday::RackBuilder do
88
88
 
89
89
  it { expect(subject.handlers).to eq([Apple]) }
90
90
 
91
- it 'allows rebuilding' do
92
- subject.build do |builder|
93
- builder.use(Orange)
94
- end
95
- expect(subject.handlers).to eq([Orange])
96
- end
97
-
98
91
  it 'allows use' do
99
92
  subject.use(Orange)
100
93
  expect(subject.handlers).to eq([Apple, Orange])
@@ -127,24 +120,6 @@ RSpec.describe Faraday::RackBuilder do
127
120
  subject.use(:apple)
128
121
  expect(subject.handlers).to eq([Apple])
129
122
  end
130
-
131
- it 'allows to register with symbol' do
132
- Faraday::Middleware.register_middleware(apple: :Apple)
133
- subject.use(:apple)
134
- expect(subject.handlers).to eq([Apple])
135
- end
136
-
137
- it 'allows to register with string' do
138
- Faraday::Middleware.register_middleware(apple: 'Apple')
139
- subject.use(:apple)
140
- expect(subject.handlers).to eq([Apple])
141
- end
142
-
143
- it 'allows to register with Proc' do
144
- Faraday::Middleware.register_middleware(apple: -> { Apple })
145
- subject.use(:apple)
146
- expect(subject.handlers).to eq([Apple])
147
- end
148
123
  end
149
124
 
150
125
  context 'when having two handlers' do
@@ -176,24 +151,6 @@ RSpec.describe Faraday::RackBuilder do
176
151
  end
177
152
  end
178
153
 
179
- context 'when having a handler with broken dependency' do
180
- let(:conn) do
181
- Faraday::Connection.new do |builder|
182
- builder.adapter :test do |stub|
183
- stub.get('/') { |_| [200, {}, ''] }
184
- end
185
- end
186
- end
187
-
188
- before { subject.use(Broken) }
189
-
190
- it 'raises an error while making a request' do
191
- expect { conn.get('/') }.to raise_error(RuntimeError) do |err|
192
- expect(err.message).to match(%r{missing dependency for Broken: .+ -- zomg/i_dont/exist})
193
- end
194
- end
195
- end
196
-
197
154
  context 'when middleware is added with named arguments' do
198
155
  let(:conn) { Faraday::Connection.new {} }
199
156
 
@@ -220,7 +177,7 @@ RSpec.describe Faraday::RackBuilder do
220
177
  end
221
178
  end
222
179
 
223
- context 'when a request adapter is added with named arguments' do
180
+ context 'when a middleware is added with named arguments' do
224
181
  let(:conn) { Faraday::Connection.new {} }
225
182
 
226
183
  let(:cat_request) do
@@ -247,11 +204,11 @@ RSpec.describe Faraday::RackBuilder do
247
204
  end
248
205
  end
249
206
 
250
- context 'when a response adapter is added with named arguments' do
207
+ context 'when a middleware is added with named arguments' do
251
208
  let(:conn) { Faraday::Connection.new {} }
252
209
 
253
210
  let(:fish_response) do
254
- Class.new(Faraday::Response::Middleware) do
211
+ Class.new(Faraday::Middleware) do
255
212
  attr_accessor :name
256
213
 
257
214
  def initialize(app, name:)
@@ -3,7 +3,7 @@
3
3
  RSpec.describe Faraday::Request::Authorization do
4
4
  let(:conn) do
5
5
  Faraday.new do |b|
6
- b.request auth_type, *auth_config
6
+ b.request :authorization, auth_type, *auth_config
7
7
  b.adapter :test do |stub|
8
8
  stub.get('/auth-echo') do |env|
9
9
  [200, {}, env[:request_headers]['Authorization']]
@@ -14,10 +14,10 @@ RSpec.describe Faraday::Request::Authorization do
14
14
 
15
15
  shared_examples 'does not interfere with existing authentication' do
16
16
  context 'and request already has an authentication header' do
17
- let(:response) { conn.get('/auth-echo', nil, authorization: 'Token token="bar"') }
17
+ let(:response) { conn.get('/auth-echo', nil, authorization: 'OAuth oauth_token') }
18
18
 
19
19
  it 'does not interfere with existing authorization' do
20
- expect(response.body).to eq('Token token="bar"')
20
+ expect(response.body).to eq('OAuth oauth_token')
21
21
  end
22
22
  end
23
23
  end
@@ -25,7 +25,7 @@ RSpec.describe Faraday::Request::Authorization do
25
25
  let(:response) { conn.get('/auth-echo') }
26
26
 
27
27
  describe 'basic_auth' do
28
- let(:auth_type) { :basic_auth }
28
+ let(:auth_type) { :basic }
29
29
 
30
30
  context 'when passed correct params' do
31
31
  let(:auth_config) { %w[aladdin opensesame] }
@@ -44,51 +44,38 @@ RSpec.describe Faraday::Request::Authorization do
44
44
  end
45
45
  end
46
46
 
47
- describe 'token_auth' do
48
- let(:auth_type) { :token_auth }
49
-
50
- context 'when passed correct params' do
51
- let(:auth_config) { 'quux' }
52
-
53
- it { expect(response.body).to eq('Token token="quux"') }
54
-
55
- include_examples 'does not interfere with existing authentication'
56
- end
47
+ describe 'authorization' do
48
+ let(:auth_type) { :Bearer }
57
49
 
58
- context 'when other values are provided' do
59
- let(:auth_config) { ['baz', { foo: 42 }] }
50
+ context 'when passed a string' do
51
+ let(:auth_config) { ['custom'] }
60
52
 
61
- it { expect(response.body).to match(/^Token /) }
62
- it { expect(response.body).to match(/token="baz"/) }
63
- it { expect(response.body).to match(/foo="42"/) }
53
+ it { expect(response.body).to eq('Bearer custom') }
64
54
 
65
55
  include_examples 'does not interfere with existing authentication'
66
56
  end
67
- end
68
-
69
- describe 'authorization' do
70
- let(:auth_type) { :authorization }
71
57
 
72
- context 'when passed two strings' do
73
- let(:auth_config) { ['custom', 'abc def'] }
58
+ context 'when passed a proc' do
59
+ let(:auth_config) { [-> { 'custom_from_proc' }] }
74
60
 
75
- it { expect(response.body).to eq('custom abc def') }
61
+ it { expect(response.body).to eq('Bearer custom_from_proc') }
76
62
 
77
63
  include_examples 'does not interfere with existing authentication'
78
64
  end
79
65
 
80
- context 'when passed a string and a hash' do
81
- let(:auth_config) { ['baz', { foo: 42 }] }
66
+ context 'when passed a callable' do
67
+ let(:callable) { double('Callable Authorizer', call: 'custom_from_callable') }
68
+ let(:auth_config) { [callable] }
82
69
 
83
- it { expect(response.body).to eq('baz foo="42"') }
70
+ it { expect(response.body).to eq('Bearer custom_from_callable') }
84
71
 
85
72
  include_examples 'does not interfere with existing authentication'
86
73
  end
87
74
 
88
- context 'when passed a string and a proc' do
89
- let(:auth_config) { ['Bearer', -> { 'custom_from_proc' }] }
75
+ context 'when passed too many arguments' do
76
+ let(:auth_config) { %w[baz foo] }
90
77
 
91
- it { expect(response.body).to eq('Bearer custom_from_proc') }
78
+ it { expect { response }.to raise_error(ArgumentError) }
92
79
 
93
80
  include_examples 'does not interfere with existing authentication'
94
81
  end
@@ -30,13 +30,11 @@ RSpec.describe Faraday::Request::Instrumentation do
30
30
 
31
31
  it { expect(options.name).to eq('request.faraday') }
32
32
  it 'defaults to ActiveSupport::Notifications' do
33
- begin
34
- res = options.instrumenter
35
- rescue NameError => e
36
- expect(e.to_s).to match('ActiveSupport')
37
- else
38
- expect(res).to eq(ActiveSupport::Notifications)
39
- end
33
+ res = options.instrumenter
34
+ rescue NameError => e
35
+ expect(e.to_s).to match('ActiveSupport')
36
+ else
37
+ expect(res).to eq(ActiveSupport::Notifications)
40
38
  end
41
39
 
42
40
  it 'instruments with default name' do
@@ -3,7 +3,6 @@
3
3
  RSpec.describe Faraday::Request::UrlEncoded do
4
4
  let(:conn) do
5
5
  Faraday.new do |b|
6
- b.request :multipart
7
6
  b.request :url_encoded
8
7
  b.adapter :test do |stub|
9
8
  stub.post('/echo') do |env|
@@ -22,17 +22,6 @@ RSpec.describe Faraday::Request do
22
22
  it { expect(subject.http_method).to eq(:post) }
23
23
  end
24
24
 
25
- describe 'deprecate method for HTTP method' do
26
- let(:http_method) { :post }
27
- let(:expected_warning) do
28
- %r{WARNING: `Faraday::Request#method` is deprecated; use `#http_method` instead. It will be removed in or after version 2.0.\n`Faraday::Request#method` called from .+/spec/faraday/request_spec.rb:\d+.}
29
- end
30
-
31
- it { expect(subject.method).to eq(:post) }
32
-
33
- it { expect { subject.method }.to output(expected_warning).to_stderr }
34
- end
35
-
36
25
  context 'when setting the url on setup with a URI' do
37
26
  let(:block) { proc { |req| req.url URI.parse('foo.json?a=1') } }
38
27
 
@@ -76,12 +76,10 @@ RSpec.describe Faraday::Response::Json, type: :response do
76
76
  end
77
77
 
78
78
  it 'includes the response on the ParsingError instance' do
79
- begin
80
- process('{') { |env| env[:response] = Faraday::Response.new }
81
- raise 'Parsing should have failed.'
82
- rescue Faraday::ParsingError => e
83
- expect(e.response).to be_a(Faraday::Response)
84
- end
79
+ process('{') { |env| env[:response] = Faraday::Response.new }
80
+ raise 'Parsing should have failed.'
81
+ rescue Faraday::ParsingError => e
82
+ expect(e.response).to be_a(Faraday::Response)
85
83
  end
86
84
 
87
85
  context 'HEAD responses' do
@@ -139,7 +139,7 @@ RSpec.describe Faraday::Response::RaiseError do
139
139
  Faraday.new do |b|
140
140
  b.response :raise_error
141
141
  b.adapter :test do |stub|
142
- stub.post('request?full=true', request_body, request_headers) do
142
+ stub.post(url, request_body, request_headers) do
143
143
  [400, { 'X-Reason' => 'because' }, 'keep looking']
144
144
  end
145
145
  end
@@ -147,11 +147,13 @@ RSpec.describe Faraday::Response::RaiseError do
147
147
  end
148
148
  let(:request_body) { JSON.generate({ 'item' => 'sth' }) }
149
149
  let(:request_headers) { { 'Authorization' => 'Basic 123' } }
150
+ let(:url_path) { 'request' }
151
+ let(:query_params) { 'full=true' }
152
+ let(:url) { "#{url_path}?#{query_params}" }
150
153
 
151
154
  subject(:perform_request) do
152
- conn.post 'request' do |req|
155
+ conn.post url do |req|
153
156
  req.headers['Authorization'] = 'Basic 123'
154
- req.params[:full] = true
155
157
  req.body = request_body
156
158
  end
157
159
  end
@@ -159,7 +161,8 @@ RSpec.describe Faraday::Response::RaiseError do
159
161
  it 'returns the request info in the exception' do
160
162
  expect { perform_request }.to raise_error(Faraday::BadRequestError) do |ex|
161
163
  expect(ex.response[:request][:method]).to eq(:post)
162
- expect(ex.response[:request][:url_path]).to eq('/request')
164
+ expect(ex.response[:request][:url]).to eq(URI("http:/#{url}"))
165
+ expect(ex.response[:request][:url_path]).to eq("/#{url_path}")
163
166
  expect(ex.response[:request][:params]).to eq({ 'full' => 'true' })
164
167
  expect(ex.response[:request][:headers]).to match(a_hash_including(request_headers))
165
168
  expect(ex.response[:request][:body]).to eq(request_body)
@@ -4,7 +4,7 @@ RSpec.describe Faraday::Utils do
4
4
  describe 'headers parsing' do
5
5
  let(:multi_response_headers) do
6
6
  "HTTP/1.x 500 OK\r\nContent-Type: text/html; charset=UTF-8\r\n" \
7
- "HTTP/1.x 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n"
7
+ "HTTP/1.x 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n"
8
8
  end
9
9
 
10
10
  it 'parse headers for aggregated responses' do
@@ -53,4 +53,65 @@ RSpec.describe Faraday::Utils do
53
53
  expect(headers).not_to have_key('authorization')
54
54
  end
55
55
  end
56
+
57
+ describe '.deep_merge!' do
58
+ let(:connection_options) { Faraday::ConnectionOptions.new }
59
+ let(:url) do
60
+ {
61
+ url: 'http://example.com/abc',
62
+ headers: { 'Mime-Version' => '1.0' },
63
+ request: { oauth: { consumer_key: 'anonymous' } },
64
+ ssl: { version: '2' }
65
+ }
66
+ end
67
+
68
+ it 'recursively merges the headers' do
69
+ connection_options.headers = { user_agent: 'My Agent 1.0' }
70
+ deep_merge = Faraday::Utils.deep_merge!(connection_options, url)
71
+
72
+ expect(deep_merge.headers).to eq('Mime-Version' => '1.0', user_agent: 'My Agent 1.0')
73
+ end
74
+
75
+ context 'when a target hash has an Options Struct value' do
76
+ let(:request) do
77
+ {
78
+ params_encoder: nil,
79
+ proxy: nil,
80
+ bind: nil,
81
+ timeout: nil,
82
+ open_timeout: nil,
83
+ read_timeout: nil,
84
+ write_timeout: nil,
85
+ boundary: nil,
86
+ oauth: { consumer_key: 'anonymous' },
87
+ context: nil,
88
+ on_data: nil
89
+ }
90
+ end
91
+ let(:ssl) do
92
+ {
93
+ verify: nil,
94
+ ca_file: nil,
95
+ ca_path: nil,
96
+ verify_mode: nil,
97
+ cert_store: nil,
98
+ client_cert: nil,
99
+ client_key: nil,
100
+ certificate: nil,
101
+ private_key: nil,
102
+ verify_depth: nil,
103
+ version: '2',
104
+ min_version: nil,
105
+ max_version: nil
106
+ }
107
+ end
108
+
109
+ it 'does not overwrite an Options Struct value' do
110
+ deep_merge = Faraday::Utils.deep_merge!(connection_options, url)
111
+
112
+ expect(deep_merge.request.to_h).to eq(request)
113
+ expect(deep_merge.ssl.to_h).to eq(ssl)
114
+ end
115
+ end
116
+ end
56
117
  end
@@ -8,7 +8,7 @@ FakeSafeBuffer = Struct.new(:string) do
8
8
 
9
9
  def gsub(regex)
10
10
  string.gsub(regex) do
11
- match, = $&, '' =~ /a/
11
+ match, = Regexp.last_match(0), '' =~ /a/ # rubocop:disable Performance/StringInclude
12
12
  yield(match)
13
13
  end
14
14
  end