faraday 2.3.0 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.md +1 -1
- data/README.md +1 -1
- data/examples/client_spec.rb +22 -0
- data/examples/client_test.rb +26 -0
- data/lib/faraday/adapter/test.rb +27 -1
- data/lib/faraday/options/ssl_options.rb +11 -1
- data/lib/faraday/version.rb +1 -1
- data/spec/faraday/adapter/test_spec.rb +36 -0
- data/spec/faraday/connection_spec.rb +6 -0
- data/spec/faraday/options/env_spec.rb +6 -0
- data/spec/faraday/request_spec.rb +1 -0
- data/spec/faraday/utils_spec.rb +2 -1
- data/spec/support/shared_examples/adapter.rb +1 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b9b61d836844815a4d9a5a1e9986c9a60416b8a9e9159d61f959e0c74ed52cc
|
4
|
+
data.tar.gz: 34fb0b4f2d4a8b22a72d56d0f3d4bfa884b2290c5012b1cdfe6de4b6886420a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f0db04b8a845215b109b3491985dc6ce6ac60ced07874bcdcdd5bab5d935a40e51bda4feab6f93375889597185c295785e5a6d1119576762d006ca696033794
|
7
|
+
data.tar.gz: 45debf037f0d1d02cccc70e33042c68d76146bb6b58bc8de839a7745404cd58a4171b0e209c75b1384475f6432531317a9145959ebf3ffe60837b65469f20e2b
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -42,7 +42,7 @@ Open the issues page and check for the `help wanted` label!
|
|
42
42
|
But before you start coding, please read our [Contributing Guide][contributing]
|
43
43
|
|
44
44
|
## Copyright
|
45
|
-
© 2009 -
|
45
|
+
© 2009 - 2022, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
|
46
46
|
|
47
47
|
[awesome]: https://github.com/lostisland/awesome-faraday/#adapters
|
48
48
|
[website]: https://lostisland.github.io/faraday
|
data/examples/client_spec.rb
CHANGED
@@ -17,6 +17,11 @@ class Client
|
|
17
17
|
data = JSON.parse(res.body)
|
18
18
|
data['origin']
|
19
19
|
end
|
20
|
+
|
21
|
+
def foo(params)
|
22
|
+
res = @conn.post('/foo', JSON.dump(params))
|
23
|
+
res.status
|
24
|
+
end
|
20
25
|
end
|
21
26
|
|
22
27
|
RSpec.describe Client do
|
@@ -94,4 +99,21 @@ RSpec.describe Client do
|
|
94
99
|
stubs.verify_stubbed_calls
|
95
100
|
end
|
96
101
|
end
|
102
|
+
|
103
|
+
context 'When you want to test the body, you can use a proc as well as string' do
|
104
|
+
it 'tests with a string' do
|
105
|
+
stubs.post('/foo', '{"name":"YK"}') { [200, {}, ''] }
|
106
|
+
|
107
|
+
expect(client.foo(name: 'YK')).to eq 200
|
108
|
+
stubs.verify_stubbed_calls
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'tests with a proc' do
|
112
|
+
check = ->(request_body) { JSON.parse(request_body).slice('name') == { 'name' => 'YK' } }
|
113
|
+
stubs.post('/foo', check) { [200, {}, ''] }
|
114
|
+
|
115
|
+
expect(client.foo(name: 'YK', created_at: Time.now)).to eq 200
|
116
|
+
stubs.verify_stubbed_calls
|
117
|
+
end
|
118
|
+
end
|
97
119
|
end
|
data/examples/client_test.rb
CHANGED
@@ -18,6 +18,11 @@ class Client
|
|
18
18
|
data = JSON.parse(res.body)
|
19
19
|
data['origin']
|
20
20
|
end
|
21
|
+
|
22
|
+
def foo(params)
|
23
|
+
res = @conn.post('/foo', JSON.dump(params))
|
24
|
+
res.status
|
25
|
+
end
|
21
26
|
end
|
22
27
|
|
23
28
|
# Example API client test
|
@@ -109,6 +114,27 @@ class ClientTest < Test::Unit::TestCase
|
|
109
114
|
stubs.verify_stubbed_calls
|
110
115
|
end
|
111
116
|
|
117
|
+
def test_with_string_body
|
118
|
+
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
119
|
+
stub.post('/foo', '{"name":"YK"}') { [200, {}, ''] }
|
120
|
+
end
|
121
|
+
cli = client(stubs)
|
122
|
+
assert_equal 200, cli.foo(name: 'YK')
|
123
|
+
|
124
|
+
stubs.verify_stubbed_calls
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_with_proc_body
|
128
|
+
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
129
|
+
check = ->(request_body) { JSON.parse(request_body).slice('name') == { 'name' => 'YK' } }
|
130
|
+
stub.post('/foo', check) { [200, {}, ''] }
|
131
|
+
end
|
132
|
+
cli = client(stubs)
|
133
|
+
assert_equal 200, cli.foo(name: 'YK', created_at: Time.now)
|
134
|
+
|
135
|
+
stubs.verify_stubbed_calls
|
136
|
+
end
|
137
|
+
|
112
138
|
def client(stubs)
|
113
139
|
conn = Faraday.new do |builder|
|
114
140
|
builder.adapter :test, stubs
|
data/lib/faraday/adapter/test.rb
CHANGED
@@ -26,6 +26,15 @@ module Faraday
|
|
26
26
|
# ]
|
27
27
|
# end
|
28
28
|
#
|
29
|
+
# # Test the request body is the same as the stubbed body
|
30
|
+
# stub.post('/bar', 'name=YK&word=call') { [200, {}, ''] }
|
31
|
+
#
|
32
|
+
# # You can pass a proc as a stubbed body and check the request body in your way.
|
33
|
+
# # In this case, the proc should return true or false.
|
34
|
+
# stub.post('/foo', ->(request_body) do
|
35
|
+
# JSON.parse(request_body).slice('name') == { 'name' => 'YK' } }) { [200, {}, '']
|
36
|
+
# end
|
37
|
+
#
|
29
38
|
# # You can set strict_mode to exactly match the stubbed requests.
|
30
39
|
# stub.strict_mode = true
|
31
40
|
# end
|
@@ -42,6 +51,12 @@ module Faraday
|
|
42
51
|
#
|
43
52
|
# resp = test.get '/items/2'
|
44
53
|
# resp.body # => 'showing item: 2'
|
54
|
+
#
|
55
|
+
# resp = test.post '/bar', 'name=YK&word=call'
|
56
|
+
# resp.status # => 200
|
57
|
+
#
|
58
|
+
# resp = test.post '/foo', JSON.dump(name: 'YK', created_at: Time.now)
|
59
|
+
# resp.status # => 200
|
45
60
|
class Test < Faraday::Adapter
|
46
61
|
attr_accessor :stubs
|
47
62
|
|
@@ -181,7 +196,7 @@ module Faraday
|
|
181
196
|
[(host.nil? || host == request_host) &&
|
182
197
|
path_match?(request_path, meta) &&
|
183
198
|
params_match?(env) &&
|
184
|
-
(
|
199
|
+
body_match?(request_body) &&
|
185
200
|
headers_match?(request_headers), meta]
|
186
201
|
end
|
187
202
|
|
@@ -222,6 +237,17 @@ module Faraday
|
|
222
237
|
end
|
223
238
|
end
|
224
239
|
|
240
|
+
def body_match?(request_body)
|
241
|
+
return true if body.to_s.size.zero?
|
242
|
+
|
243
|
+
case body
|
244
|
+
when Proc
|
245
|
+
body.call(request_body)
|
246
|
+
else
|
247
|
+
request_body == body
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
225
251
|
def to_s
|
226
252
|
"#{path} #{body}"
|
227
253
|
end
|
@@ -6,6 +6,10 @@ module Faraday
|
|
6
6
|
# @!attribute verify
|
7
7
|
# @return [Boolean] whether to verify SSL certificates or not
|
8
8
|
#
|
9
|
+
# @!attribute verify_hostname
|
10
|
+
# @return [Boolean] whether to enable hostname verification on server certificates
|
11
|
+
# during the handshake or not (see https://github.com/ruby/openssl/pull/60)
|
12
|
+
#
|
9
13
|
# @!attribute ca_file
|
10
14
|
# @return [String] CA file
|
11
15
|
#
|
@@ -41,7 +45,8 @@ module Faraday
|
|
41
45
|
#
|
42
46
|
# @!attribute max_version
|
43
47
|
# @return [String, Symbol] maximum SSL version (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL/SSLContext.html#method-i-max_version-3D)
|
44
|
-
class SSLOptions < Options.new(:verify, :
|
48
|
+
class SSLOptions < Options.new(:verify, :verify_hostname,
|
49
|
+
:ca_file, :ca_path, :verify_mode,
|
45
50
|
:cert_store, :client_cert, :client_key,
|
46
51
|
:certificate, :private_key, :verify_depth,
|
47
52
|
:version, :min_version, :max_version)
|
@@ -55,5 +60,10 @@ module Faraday
|
|
55
60
|
def disable?
|
56
61
|
!verify?
|
57
62
|
end
|
63
|
+
|
64
|
+
# @return [Boolean] true if should verify_hostname
|
65
|
+
def verify_hostname?
|
66
|
+
verify_hostname != false
|
67
|
+
end
|
58
68
|
end
|
59
69
|
end
|
data/lib/faraday/version.rb
CHANGED
@@ -373,5 +373,41 @@ RSpec.describe Faraday::Adapter::Test do
|
|
373
373
|
it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'My Agent' }
|
374
374
|
it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'My Agent', x_special: 'special' }
|
375
375
|
end
|
376
|
+
|
377
|
+
describe 'body_match?' do
|
378
|
+
let(:stubs) do
|
379
|
+
described_class::Stubs.new do |stubs|
|
380
|
+
stubs.post('/no_check') { [200, {}, 'ok'] }
|
381
|
+
stubs.post('/with_string', 'abc') { [200, {}, 'ok'] }
|
382
|
+
stubs.post(
|
383
|
+
'/with_proc',
|
384
|
+
->(request_body) { JSON.parse(request_body, symbolize_names: true) == { x: '!', a: [{ m: [{ a: true }], n: 123 }] } },
|
385
|
+
{ content_type: 'application/json' }
|
386
|
+
) do
|
387
|
+
[200, {}, 'ok']
|
388
|
+
end
|
389
|
+
end
|
390
|
+
end
|
391
|
+
|
392
|
+
context 'when trying without any args for body' do
|
393
|
+
subject(:without_body) { connection.post('/no_check') }
|
394
|
+
|
395
|
+
it { expect(without_body.status).to eq 200 }
|
396
|
+
end
|
397
|
+
|
398
|
+
context 'when trying with string body stubs' do
|
399
|
+
subject(:with_string) { connection.post('/with_string', 'abc') }
|
400
|
+
|
401
|
+
it { expect(with_string.status).to eq 200 }
|
402
|
+
end
|
403
|
+
|
404
|
+
context 'when trying with proc body stubs' do
|
405
|
+
subject(:with_proc) do
|
406
|
+
connection.post('/with_proc', JSON.dump(a: [{ n: 123, m: [{ a: true }] }], x: '!'), { 'Content-Type' => 'application/json' })
|
407
|
+
end
|
408
|
+
|
409
|
+
it { expect(with_proc.status).to eq 200 }
|
410
|
+
end
|
411
|
+
end
|
376
412
|
end
|
377
413
|
end
|
@@ -131,6 +131,12 @@ RSpec.describe Faraday::Connection do
|
|
131
131
|
it { expect(subject.ssl.verify?).to be_falsey }
|
132
132
|
end
|
133
133
|
|
134
|
+
context 'with verify_hostname false' do
|
135
|
+
let(:options) { { ssl: { verify_hostname: false } } }
|
136
|
+
|
137
|
+
it { expect(subject.ssl.verify_hostname?).to be_falsey }
|
138
|
+
end
|
139
|
+
|
134
140
|
context 'with empty block' do
|
135
141
|
let(:conn) { Faraday::Connection.new {} }
|
136
142
|
|
@@ -27,6 +27,12 @@ RSpec.describe Faraday::Env do
|
|
27
27
|
expect(ssl.fetch(:verify, true)).to be_falsey
|
28
28
|
end
|
29
29
|
|
30
|
+
it 'handle verify_hostname when fetching' do
|
31
|
+
ssl = Faraday::SSLOptions.new
|
32
|
+
ssl.verify_hostname = true
|
33
|
+
expect(ssl.fetch(:verify_hostname, false)).to be_truthy
|
34
|
+
end
|
35
|
+
|
30
36
|
it 'retains custom members' do
|
31
37
|
env[:foo] = 'custom 1'
|
32
38
|
env[:bar] = :custom2
|
@@ -14,6 +14,7 @@ RSpec.describe Faraday::Request do
|
|
14
14
|
context 'when nothing particular is configured' do
|
15
15
|
it { expect(subject.http_method).to eq(:get) }
|
16
16
|
it { expect(subject.to_env(conn).ssl.verify).to be_falsey }
|
17
|
+
it { expect(subject.to_env(conn).ssl.verify_hostname).to be_falsey }
|
17
18
|
end
|
18
19
|
|
19
20
|
context 'when HTTP method is post' do
|
data/spec/faraday/utils_spec.rb
CHANGED
@@ -38,6 +38,7 @@ shared_examples 'adapter examples' do |**options|
|
|
38
38
|
let(:conn) do
|
39
39
|
conn_options[:ssl] ||= {}
|
40
40
|
conn_options[:ssl][:ca_file] ||= ENV['SSL_FILE']
|
41
|
+
conn_options[:ssl][:verify_hostname] ||= ENV['SSL_VERIFY_HOSTNAME'] == 'yes'
|
41
42
|
|
42
43
|
Faraday.new(remote, conn_options) do |conn|
|
43
44
|
conn.request :url_encoded
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "@technoweenie"
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2022-
|
13
|
+
date: 2022-07-28 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: faraday-net_http
|
@@ -125,7 +125,7 @@ licenses:
|
|
125
125
|
- MIT
|
126
126
|
metadata:
|
127
127
|
homepage_uri: https://lostisland.github.io/faraday
|
128
|
-
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.
|
128
|
+
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.4.0
|
129
129
|
source_code_uri: https://github.com/lostisland/faraday
|
130
130
|
bug_tracker_uri: https://github.com/lostisland/faraday/issues
|
131
131
|
post_install_message:
|