faraday 1.0.0 → 1.10.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +104 -0
- data/LICENSE.md +1 -1
- data/README.md +4 -6
- data/examples/client_spec.rb +35 -3
- data/examples/client_test.rb +41 -2
- data/lib/faraday/adapter/test.rb +59 -43
- data/lib/faraday/adapter/typhoeus.rb +1 -1
- data/lib/faraday/adapter.rb +2 -12
- data/lib/faraday/adapter_registry.rb +3 -1
- data/lib/faraday/autoload.rb +2 -10
- data/lib/faraday/connection.rb +37 -9
- data/lib/faraday/dependency_loader.rb +3 -1
- data/lib/faraday/deprecate.rb +110 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +9 -2
- data/lib/faraday/encoders/nested_params_encoder.rb +7 -2
- data/lib/faraday/error.rb +20 -6
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +14 -4
- data/lib/faraday/options/proxy_options.rb +4 -0
- data/lib/faraday/options.rb +4 -8
- data/lib/faraday/rack_builder.rb +13 -12
- data/lib/faraday/request/authorization.rb +17 -8
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +3 -1
- data/lib/faraday/request.rb +19 -11
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +2 -4
- data/lib/faraday/response/raise_error.rb +12 -1
- data/lib/faraday/response.rb +7 -8
- data/lib/faraday/utils/headers.rb +2 -2
- data/lib/faraday/utils.rb +11 -3
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +67 -40
- data/spec/faraday/adapter/em_http_spec.rb +39 -37
- data/spec/faraday/adapter/em_synchrony_spec.rb +11 -9
- data/spec/faraday/adapter/patron_spec.rb +1 -1
- data/spec/faraday/adapter/test_spec.rb +377 -0
- data/spec/faraday/connection_spec.rb +45 -0
- data/spec/faraday/deprecate_spec.rb +147 -0
- data/spec/faraday/error_spec.rb +15 -0
- data/spec/faraday/middleware_spec.rb +32 -6
- data/spec/faraday/options/proxy_options_spec.rb +7 -0
- data/spec/faraday/params_encoders/flat_spec.rb +8 -0
- data/spec/faraday/params_encoders/nested_spec.rb +8 -0
- data/spec/faraday/rack_builder_spec.rb +150 -1
- data/spec/faraday/request/authorization_spec.rb +10 -2
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +13 -0
- data/spec/faraday/request_spec.rb +16 -5
- data/spec/faraday/response/json_spec.rb +119 -0
- data/spec/faraday/response/middleware_spec.rb +16 -0
- data/spec/faraday/response/raise_error_spec.rb +63 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/support/shared_examples/adapter.rb +2 -1
- data/spec/support/shared_examples/request_method.rb +39 -11
- metadata +157 -31
- data/UPGRADING.md +0 -55
- data/lib/faraday/adapter/em_http.rb +0 -285
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
- data/lib/faraday/adapter/em_synchrony.rb +0 -150
- data/lib/faraday/adapter/excon.rb +0 -124
- data/lib/faraday/adapter/httpclient.rb +0 -151
- data/lib/faraday/adapter/net_http.rb +0 -209
- data/lib/faraday/adapter/net_http_persistent.rb +0 -91
- data/lib/faraday/adapter/patron.rb +0 -132
- data/lib/faraday/adapter/rack.rb +0 -75
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/request/multipart.rb +0 -99
- data/lib/faraday/request/retry.rb +0 -239
- data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
- data/spec/faraday/request/multipart_spec.rb +0 -274
- data/spec/faraday/request/retry_spec.rb +0 -242
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a35b8ef069416d74f46cbe1f9b14b9d28e8b141bed4143d2c97ac137b766f18
|
4
|
+
data.tar.gz: cdbb3768e4aa6d316e2216ec7fdf2a93b5141abe83dd82b3ffa522002b01523c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 743727fca5013eb1bd1f005c334d171fc59a5cb56687395b3d14a48911ee4b7fef5da00e64ac6dbe15b75cc76edcb26d400b4d748e9676d9cdb12e8d2d8ee7b7
|
7
|
+
data.tar.gz: 203527c8adc2828334b74e97a33041ed9127548b91905d15331ff627c5f20d679c3d2ccd0b3102c0de35e011c5f15f2bac50b8aa4eccaeafaf1551611ca020e1
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,109 @@
|
|
1
1
|
# Faraday Changelog
|
2
2
|
|
3
|
+
## [v1.3.0](https://github.com/lostisland/faraday/releases/tag/v1.3.0) (2020-12-31)
|
4
|
+
|
5
|
+
### Highlights
|
6
|
+
Faraday v1.3.0 is the first release to officially support Ruby 3.0 in the CI pipeline 🎉 🍾!
|
7
|
+
|
8
|
+
This is also the first release with a previously "included" adapter (Net::HTTP) being isolated into a [separate gem](https://github.com/lostisland/faraday-net_http) 🎊!
|
9
|
+
The new adapter is added to Faraday as a dependency for now, so that means full backwards-compatibility, but just to be safe be careful when upgrading!
|
10
|
+
|
11
|
+
This is a huge step towards are Faraday v2.0 objective of pushing adapters and middleware into separate gems.
|
12
|
+
Many thanks to the Faraday Team, @JanDintel and everyone who attended the [ROSS Conf remote event](https://www.rossconf.io/event/remote/)
|
13
|
+
|
14
|
+
### Features
|
15
|
+
|
16
|
+
* Improves consistency with Faraday::Error and Faraday::RaiseError (#1229, @qsona, @iMacTia)
|
17
|
+
|
18
|
+
### Fixes
|
19
|
+
|
20
|
+
* Don't assign to global ::Timer (#1227, @bpo)
|
21
|
+
|
22
|
+
### Documentation
|
23
|
+
|
24
|
+
* CHANGELOG: add releases after 1.0 (#1225, @olleolleolle)
|
25
|
+
* Improves retry middleware documentation. (#1228, @iMacTia)
|
26
|
+
|
27
|
+
### Misc
|
28
|
+
|
29
|
+
* Move out Net::HTTP adapter (#1222, @JanDintel, @iMacTia)
|
30
|
+
* Adds Ruby 3.0 to CI Matrix (#1226, @iMacTia)
|
31
|
+
|
32
|
+
|
33
|
+
## [v1.2.0](https://github.com/lostisland/faraday/releases/tag/v1.2.0) (2020-12-23)
|
34
|
+
|
35
|
+
### Features
|
36
|
+
|
37
|
+
* Introduces `on_request` and `on_complete` methods in `Faraday::Middleware`. (#1194, @iMacTia)
|
38
|
+
|
39
|
+
### Fixes
|
40
|
+
|
41
|
+
* Require 'date' to avoid retry exception (#1206, @rustygeldmacher)
|
42
|
+
* Fix rdebug recursion issue (#1205, @native-api)
|
43
|
+
* Update call to `em_http_ssl_patch` (#1202, @kylekeesling)
|
44
|
+
* `EmHttp` adapter: drop superfluous loaded? check (#1213, @olleolleolle)
|
45
|
+
* Avoid 1 use of keyword hackery (#1211, @grosser)
|
46
|
+
* Fix #1219 `Net::HTTP` still uses env proxy (#1221, @iMacTia)
|
47
|
+
|
48
|
+
### Documentation
|
49
|
+
|
50
|
+
* Add comment in gemspec to explain exposure of `examples` and `spec` folders. (#1192, @iMacTia)
|
51
|
+
* Adapters, how to create them (#1193, @olleolleolle)
|
52
|
+
* Update documentation on using the logger (#1196, @tijmenb)
|
53
|
+
* Adjust the retry documentation and spec to align with implementation (#1198, @nbeyer)
|
54
|
+
|
55
|
+
### Misc
|
56
|
+
|
57
|
+
* Test against ruby head (#1208, @grosser)
|
58
|
+
|
59
|
+
## [v1.1.0](https://github.com/lostisland/faraday/releases/tag/v1.1.0) (2020-10-17)
|
60
|
+
|
61
|
+
### Features
|
62
|
+
|
63
|
+
* Makes parameters sorting configurable (#1162 @wishdev)
|
64
|
+
* Introduces `flat_encode` option for multipart adapter. (#1163 @iMacTia)
|
65
|
+
* Include request info in exceptions raised by RaiseError Middleware (#1181 @SandroDamilano)
|
66
|
+
|
67
|
+
### Fixes
|
68
|
+
|
69
|
+
* Avoid `last arg as keyword param` warning when building user middleware on Ruby 2.7 (#1153 @dgholz)
|
70
|
+
* Limits net-http-persistent version to < 4.0 (#1156 @iMacTia)
|
71
|
+
* Update `typhoeus` to new stable version (`1.4`) (#1159 @AlexWayfer)
|
72
|
+
* Properly fix test failure with Rack 2.1+. (#1171 @voxik)
|
73
|
+
|
74
|
+
### Documentation
|
75
|
+
|
76
|
+
* Improves documentation on how to contribute to the site by using Docker. (#1175 @iMacTia)
|
77
|
+
* Remove retry_change_requests from documentation (#1185 @stim371)
|
78
|
+
|
79
|
+
### Misc
|
80
|
+
|
81
|
+
* Link from GitHub Actions badge to CI workflow (#1141 @olleolleolle)
|
82
|
+
* Return tests of `Test` adapter (#1147 @AlexWayfer)
|
83
|
+
* Add 1.0 release to wording in CONTRIBUTING (#1155 @olleolleolle)
|
84
|
+
* Fix linting bumping Rubocop to 0.90.0 (#1182 @iMacTia)
|
85
|
+
* Drop `git ls-files` in gemspec (#1183 @utkarsh2102)
|
86
|
+
* Upgrade CI to ruby/setup-ruby (#1187 @gogainda)
|
87
|
+
|
88
|
+
## [v1.0.1](https://github.com/lostisland/faraday/releases/tag/v1.0.1) (2020-03-29)
|
89
|
+
|
90
|
+
### Fixes
|
91
|
+
|
92
|
+
* Use Net::HTTP#start(&block) to ensure closed TCP connections (#1117)
|
93
|
+
* Fully qualify constants to be checked (#1122)
|
94
|
+
* Allows `parse` method to be private/protected in response middleware (#1123)
|
95
|
+
* Encode Spaces in Query Strings as '%20' Instead of '+' (#1125)
|
96
|
+
* Limits rack to v2.0.x (#1127)
|
97
|
+
* Adapter Registry reads also use mutex (#1136)
|
98
|
+
|
99
|
+
### Documentation
|
100
|
+
|
101
|
+
* Retry middleware documentation fix (#1109)
|
102
|
+
* Docs(retry): precise usage of retry-after (#1111)
|
103
|
+
* README: Link the logo to the website (#1112)
|
104
|
+
* Website: add search bar (#1116)
|
105
|
+
* Fix request/response mix-up in docs text (#1132)
|
106
|
+
|
3
107
|
## v1.0
|
4
108
|
|
5
109
|
Features:
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
-
# 
|
1
|
+
# [][website]
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/faraday)
|
4
|
-

|
5
|
-
[](https://codeclimate.com/github/lostisland/faraday/test_coverage)
|
6
|
-
[](https://codeclimate.com/github/lostisland/faraday/maintainability)
|
4
|
+
[](https://github.com/lostisland/faraday/actions?query=workflow%3ACI)
|
7
5
|
[](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
8
6
|
|
9
7
|
|
@@ -43,12 +41,12 @@ Open the issues page and check for the `help wanted` label!
|
|
43
41
|
But before you start coding, please read our [Contributing Guide][contributing]
|
44
42
|
|
45
43
|
## Copyright
|
46
|
-
© 2009 -
|
44
|
+
© 2009 - 2020, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
|
47
45
|
|
48
46
|
[website]: https://lostisland.github.io/faraday
|
49
47
|
[faraday_team]: https://lostisland.github.io/faraday/team
|
50
48
|
[contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md
|
51
|
-
[apidoc]:
|
49
|
+
[apidoc]: https://www.rubydoc.info/github/lostisland/faraday
|
52
50
|
[actions]: https://github.com/lostisland/faraday/actions
|
53
51
|
[jruby]: http://jruby.org/
|
54
52
|
[rubinius]: http://rubini.us/
|
data/examples/client_spec.rb
CHANGED
@@ -12,14 +12,14 @@ class Client
|
|
12
12
|
@conn = conn
|
13
13
|
end
|
14
14
|
|
15
|
-
def sushi(jname)
|
16
|
-
res = @conn.get("/#{jname}")
|
15
|
+
def sushi(jname, params: {})
|
16
|
+
res = @conn.get("/#{jname}", params)
|
17
17
|
data = JSON.parse(res.body)
|
18
18
|
data['name']
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
|
22
|
+
RSpec.describe Client do
|
23
23
|
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
|
24
24
|
let(:conn) { Faraday.new { |b| b.adapter(:test, stubs) } }
|
25
25
|
let(:client) { Client.new(conn) }
|
@@ -62,4 +62,36 @@ Rspec.describe Client do
|
|
62
62
|
expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
|
63
63
|
stubs.verify_stubbed_calls
|
64
64
|
end
|
65
|
+
|
66
|
+
context 'When the test stub is run in strict_mode' do
|
67
|
+
let(:stubs) { Faraday::Adapter::Test::Stubs.new(strict_mode: true) }
|
68
|
+
|
69
|
+
it 'verifies the all parameter values are identical' do
|
70
|
+
stubs.get('/ebi?abc=123') do
|
71
|
+
[
|
72
|
+
200,
|
73
|
+
{ 'Content-Type': 'application/javascript' },
|
74
|
+
'{"name": "shrimp"}'
|
75
|
+
]
|
76
|
+
end
|
77
|
+
|
78
|
+
# uncomment to raise Stubs::NotFound
|
79
|
+
# expect(client.sushi('ebi', params: { abc: 123, foo: 'Kappa' })).to eq('shrimp')
|
80
|
+
expect(client.sushi('ebi', params: { abc: 123 })).to eq('shrimp')
|
81
|
+
stubs.verify_stubbed_calls
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'When the Faraday connection is configured with FlatParamsEncoder' do
|
86
|
+
let(:conn) { Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) { |b| b.adapter(:test, stubs) } }
|
87
|
+
|
88
|
+
it 'handles the same multiple URL parameters' do
|
89
|
+
stubs.get('/ebi?a=x&a=y&a=z') { [200, { 'Content-Type' => 'application/json' }, '{"name": "shrimp"}'] }
|
90
|
+
|
91
|
+
# uncomment to raise Stubs::NotFound
|
92
|
+
# expect(client.sushi('ebi', params: { a: %w[x y] })).to eq('shrimp')
|
93
|
+
expect(client.sushi('ebi', params: { a: %w[x y z] })).to eq('shrimp')
|
94
|
+
stubs.verify_stubbed_calls
|
95
|
+
end
|
96
|
+
end
|
65
97
|
end
|
data/examples/client_test.rb
CHANGED
@@ -13,8 +13,8 @@ class Client
|
|
13
13
|
@conn = conn
|
14
14
|
end
|
15
15
|
|
16
|
-
def sushi(jname)
|
17
|
-
res = @conn.get("/#{jname}")
|
16
|
+
def sushi(jname, params: {})
|
17
|
+
res = @conn.get("/#{jname}", params)
|
18
18
|
data = JSON.parse(res.body)
|
19
19
|
data['name']
|
20
20
|
end
|
@@ -70,6 +70,45 @@ class ClientTest < Test::Unit::TestCase
|
|
70
70
|
stubs.verify_stubbed_calls
|
71
71
|
end
|
72
72
|
|
73
|
+
def test_strict_mode
|
74
|
+
stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
|
75
|
+
stubs.get('/ebi?abc=123') do
|
76
|
+
[
|
77
|
+
200,
|
78
|
+
{ 'Content-Type': 'application/javascript' },
|
79
|
+
'{"name": "shrimp"}'
|
80
|
+
]
|
81
|
+
end
|
82
|
+
|
83
|
+
cli = client(stubs)
|
84
|
+
assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123 })
|
85
|
+
|
86
|
+
# uncomment to raise Stubs::NotFound
|
87
|
+
# assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123, foo: 'Kappa' })
|
88
|
+
stubs.verify_stubbed_calls
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_non_default_params_encoder
|
92
|
+
stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
|
93
|
+
stubs.get('/ebi?a=x&a=y&a=z') do
|
94
|
+
[
|
95
|
+
200,
|
96
|
+
{ 'Content-Type': 'application/javascript' },
|
97
|
+
'{"name": "shrimp"}'
|
98
|
+
]
|
99
|
+
end
|
100
|
+
conn = Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
|
101
|
+
builder.adapter :test, stubs
|
102
|
+
end
|
103
|
+
|
104
|
+
cli = Client.new(conn)
|
105
|
+
assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y z] })
|
106
|
+
|
107
|
+
# uncomment to raise Stubs::NotFound
|
108
|
+
# assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y] })
|
109
|
+
stubs.verify_stubbed_calls
|
110
|
+
end
|
111
|
+
|
73
112
|
def client(stubs)
|
74
113
|
conn = Faraday.new do |builder|
|
75
114
|
builder.adapter :test, stubs
|
data/lib/faraday/adapter/test.rb
CHANGED
@@ -25,6 +25,9 @@ module Faraday
|
|
25
25
|
# "showing item: #{meta[:match_data][1]}"
|
26
26
|
# ]
|
27
27
|
# end
|
28
|
+
#
|
29
|
+
# # You can set strict_mode to exactly match the stubbed requests.
|
30
|
+
# stub.strict_mode = true
|
28
31
|
# end
|
29
32
|
# end
|
30
33
|
#
|
@@ -47,10 +50,11 @@ module Faraday
|
|
47
50
|
class NotFound < StandardError
|
48
51
|
end
|
49
52
|
|
50
|
-
def initialize
|
53
|
+
def initialize(strict_mode: false)
|
51
54
|
# { get: [Stub, Stub] }
|
52
55
|
@stack = {}
|
53
56
|
@consumed = {}
|
57
|
+
@strict_mode = strict_mode
|
54
58
|
yield(self) if block_given?
|
55
59
|
end
|
56
60
|
|
@@ -58,18 +62,20 @@ module Faraday
|
|
58
62
|
@stack.empty?
|
59
63
|
end
|
60
64
|
|
61
|
-
|
65
|
+
# @param env [Faraday::Env]
|
66
|
+
def match(env)
|
67
|
+
request_method = env[:method]
|
62
68
|
return false unless @stack.key?(request_method)
|
63
69
|
|
64
70
|
stack = @stack[request_method]
|
65
71
|
consumed = (@consumed[request_method] ||= [])
|
66
72
|
|
67
|
-
stub, meta = matches?(stack,
|
73
|
+
stub, meta = matches?(stack, env)
|
68
74
|
if stub
|
69
75
|
consumed << stack.delete(stub)
|
70
76
|
return stub, meta
|
71
77
|
end
|
72
|
-
matches?(consumed,
|
78
|
+
matches?(consumed, env)
|
73
79
|
end
|
74
80
|
|
75
81
|
def get(path, headers = {}, &block)
|
@@ -115,6 +121,17 @@ module Faraday
|
|
115
121
|
raise failed_stubs.join(' ') unless failed_stubs.empty?
|
116
122
|
end
|
117
123
|
|
124
|
+
# Set strict_mode. If the value is true, this adapter tries to find matched requests strictly,
|
125
|
+
# which means that all of a path, parameters, and headers must be the same as an actual request.
|
126
|
+
def strict_mode=(value)
|
127
|
+
@strict_mode = value
|
128
|
+
@stack.each do |_method, stubs|
|
129
|
+
stubs.each do |stub|
|
130
|
+
stub.strict_mode = value
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
118
135
|
protected
|
119
136
|
|
120
137
|
def new_stub(request_method, path, headers = {}, body = nil, &block)
|
@@ -127,14 +144,18 @@ module Faraday
|
|
127
144
|
Faraday::Utils.URI(path).host
|
128
145
|
]
|
129
146
|
end
|
147
|
+
path, query = normalized_path.respond_to?(:split) ? normalized_path.split('?') : normalized_path
|
148
|
+
headers = Utils::Headers.new(headers)
|
130
149
|
|
131
|
-
stub = Stub.new(host,
|
150
|
+
stub = Stub.new(host, path, query, headers, body, @strict_mode, block)
|
132
151
|
(@stack[request_method] ||= []) << stub
|
133
152
|
end
|
134
153
|
|
135
|
-
|
154
|
+
# @param stack [Hash]
|
155
|
+
# @param env [Faraday::Env]
|
156
|
+
def matches?(stack, env)
|
136
157
|
stack.each do |stub|
|
137
|
-
match_result, meta = stub.matches?(
|
158
|
+
match_result, meta = stub.matches?(env)
|
138
159
|
return stub, meta if match_result
|
139
160
|
end
|
140
161
|
nil
|
@@ -142,35 +163,20 @@ module Faraday
|
|
142
163
|
end
|
143
164
|
|
144
165
|
# Stub request
|
145
|
-
# rubocop:disable Style/StructInheritance
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
Faraday::Utils.parse_nested_query(query)
|
153
|
-
else
|
154
|
-
{}
|
155
|
-
end
|
156
|
-
|
157
|
-
super(host, path, params, headers, body, block)
|
158
|
-
end
|
166
|
+
class Stub < Struct.new(:host, :path, :query, :headers, :body, :strict_mode, :block) # rubocop:disable Style/StructInheritance
|
167
|
+
# @param env [Faraday::Env]
|
168
|
+
def matches?(env)
|
169
|
+
request_host = env[:url].host
|
170
|
+
request_path = Faraday::Utils.normalize_path(env[:url].path)
|
171
|
+
request_headers = env.request_headers
|
172
|
+
request_body = env[:body]
|
159
173
|
|
160
|
-
def matches?(request_host, request_uri, request_headers, request_body)
|
161
|
-
request_path, request_query = request_uri.split('?')
|
162
|
-
request_params =
|
163
|
-
if request_query
|
164
|
-
Faraday::Utils.parse_nested_query(request_query)
|
165
|
-
else
|
166
|
-
{}
|
167
|
-
end
|
168
174
|
# meta is a hash used as carrier
|
169
175
|
# that will be yielded to consumer block
|
170
176
|
meta = {}
|
171
177
|
[(host.nil? || host == request_host) &&
|
172
178
|
path_match?(request_path, meta) &&
|
173
|
-
params_match?(
|
179
|
+
params_match?(env) &&
|
174
180
|
(body.to_s.size.zero? || request_body == body) &&
|
175
181
|
headers_match?(request_headers), meta]
|
176
182
|
end
|
@@ -183,13 +189,30 @@ module Faraday
|
|
183
189
|
end
|
184
190
|
end
|
185
191
|
|
186
|
-
|
192
|
+
# @param env [Faraday::Env]
|
193
|
+
def params_match?(env)
|
194
|
+
request_params = env[:params]
|
195
|
+
params = env.params_encoder.decode(query) || {}
|
196
|
+
|
197
|
+
if strict_mode
|
198
|
+
return Set.new(params) == Set.new(request_params)
|
199
|
+
end
|
200
|
+
|
187
201
|
params.keys.all? do |key|
|
188
202
|
request_params[key] == params[key]
|
189
203
|
end
|
190
204
|
end
|
191
205
|
|
192
206
|
def headers_match?(request_headers)
|
207
|
+
if strict_mode
|
208
|
+
headers_with_user_agent = headers.dup.tap do |hs|
|
209
|
+
# NOTE: Set User-Agent in case it's not set when creating Stubs.
|
210
|
+
# Users would not want to set Faraday's User-Agent explicitly.
|
211
|
+
hs[:user_agent] ||= Connection::USER_AGENT
|
212
|
+
end
|
213
|
+
return Set.new(headers_with_user_agent) == Set.new(request_headers)
|
214
|
+
end
|
215
|
+
|
193
216
|
headers.keys.all? do |key|
|
194
217
|
request_headers[key] == headers[key]
|
195
218
|
end
|
@@ -210,26 +233,19 @@ module Faraday
|
|
210
233
|
yield(stubs)
|
211
234
|
end
|
212
235
|
|
236
|
+
# @param env [Faraday::Env]
|
213
237
|
def call(env)
|
214
238
|
super
|
215
|
-
host = env[:url].host
|
216
|
-
normalized_path = Faraday::Utils.normalize_path(env[:url])
|
217
|
-
params_encoder = env.request.params_encoder ||
|
218
|
-
Faraday::Utils.default_params_encoder
|
219
239
|
|
220
|
-
|
221
|
-
|
240
|
+
env.request.params_encoder ||= Faraday::Utils.default_params_encoder
|
241
|
+
env[:params] = env.params_encoder.decode(env[:url].query) || {}
|
242
|
+
stub, meta = stubs.match(env)
|
222
243
|
|
223
244
|
unless stub
|
224
245
|
raise Stubs::NotFound, "no stubbed request for #{env[:method]} "\
|
225
|
-
"#{
|
246
|
+
"#{env[:url]} #{env[:body]}"
|
226
247
|
end
|
227
248
|
|
228
|
-
env[:params] = if (query = env[:url].query)
|
229
|
-
params_encoder.decode(query)
|
230
|
-
else
|
231
|
-
{}
|
232
|
-
end
|
233
249
|
block_arity = stub.block.arity
|
234
250
|
status, headers, body =
|
235
251
|
if block_arity >= 0
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module Faraday
|
4
4
|
class Adapter
|
5
5
|
# Typhoeus adapter. This class is just a stub, the real adapter is in
|
6
|
-
# https://github.com/
|
6
|
+
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
|
7
7
|
class Typhoeus < Faraday::Adapter
|
8
8
|
# Needs to define this method in order to support Typhoeus <= 1.3.0
|
9
9
|
def call; end
|
data/lib/faraday/adapter.rb
CHANGED
@@ -11,22 +11,12 @@ module Faraday
|
|
11
11
|
|
12
12
|
register_middleware File.expand_path('adapter', __dir__),
|
13
13
|
test: [:Test, 'test'],
|
14
|
-
|
15
|
-
net_http_persistent: [
|
16
|
-
:NetHttpPersistent,
|
17
|
-
'net_http_persistent'
|
18
|
-
],
|
19
|
-
typhoeus: [:Typhoeus, 'typhoeus'],
|
20
|
-
patron: [:Patron, 'patron'],
|
21
|
-
em_synchrony: [:EMSynchrony, 'em_synchrony'],
|
22
|
-
em_http: [:EMHttp, 'em_http'],
|
23
|
-
excon: [:Excon, 'excon'],
|
24
|
-
rack: [:Rack, 'rack'],
|
25
|
-
httpclient: [:HTTPClient, 'httpclient']
|
14
|
+
typhoeus: [:Typhoeus, 'typhoeus']
|
26
15
|
|
27
16
|
# This module marks an Adapter as supporting parallel requests.
|
28
17
|
module Parallelism
|
29
18
|
attr_writer :supports_parallel
|
19
|
+
|
30
20
|
def supports_parallel?
|
31
21
|
@supports_parallel
|
32
22
|
end
|
data/lib/faraday/autoload.rb
CHANGED
@@ -23,7 +23,7 @@ module Faraday
|
|
23
23
|
#
|
24
24
|
# @return [void]
|
25
25
|
def autoload_all(prefix, options)
|
26
|
-
if prefix
|
26
|
+
if prefix.match? %r{^faraday(/|$)}i
|
27
27
|
prefix = File.join(Faraday.root_path, prefix)
|
28
28
|
end
|
29
29
|
|
@@ -58,16 +58,8 @@ module Faraday
|
|
58
58
|
class Adapter
|
59
59
|
extend AutoloadHelper
|
60
60
|
autoload_all 'faraday/adapter',
|
61
|
-
NetHttp: 'net_http',
|
62
|
-
NetHttpPersistent: 'net_http_persistent',
|
63
|
-
EMSynchrony: 'em_synchrony',
|
64
|
-
EMHttp: 'em_http',
|
65
61
|
Typhoeus: 'typhoeus',
|
66
|
-
|
67
|
-
Excon: 'excon',
|
68
|
-
Test: 'test',
|
69
|
-
Rack: 'rack',
|
70
|
-
HTTPClient: 'httpclient'
|
62
|
+
Test: 'test'
|
71
63
|
end
|
72
64
|
|
73
65
|
# Request represents a single HTTP request for a Faraday adapter to make.
|
data/lib/faraday/connection.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'faraday/deprecate'
|
4
|
+
|
3
5
|
module Faraday
|
4
6
|
# Connection objects manage the default properties and the middleware
|
5
7
|
# stack for fulfilling an HTTP request.
|
@@ -15,6 +17,7 @@ module Faraday
|
|
15
17
|
class Connection
|
16
18
|
# A Set of allowed HTTP verbs.
|
17
19
|
METHODS = Set.new %i[get post put delete head patch options trace]
|
20
|
+
USER_AGENT = "Faraday v#{VERSION}"
|
18
21
|
|
19
22
|
# @return [Hash] URI query unencoded key/value pairs.
|
20
23
|
attr_reader :params
|
@@ -26,7 +29,7 @@ module Faraday
|
|
26
29
|
# Connection. This includes a default host name, scheme, port, and path.
|
27
30
|
attr_reader :url_prefix
|
28
31
|
|
29
|
-
# @return [Faraday::
|
32
|
+
# @return [Faraday::RackBuilder] Builder for this Connection.
|
30
33
|
attr_reader :builder
|
31
34
|
|
32
35
|
# @return [Hash] SSL options.
|
@@ -73,6 +76,7 @@ module Faraday
|
|
73
76
|
@options = options.request
|
74
77
|
@ssl = options.ssl
|
75
78
|
@default_parallel_manager = options.parallel_manager
|
79
|
+
@manual_proxy = nil
|
76
80
|
|
77
81
|
@builder = options.builder || begin
|
78
82
|
# pass an empty block to Builder so it doesn't assume default middleware
|
@@ -88,7 +92,7 @@ module Faraday
|
|
88
92
|
|
89
93
|
yield(self) if block_given?
|
90
94
|
|
91
|
-
@headers[:user_agent] ||=
|
95
|
+
@headers[:user_agent] ||= USER_AGENT
|
92
96
|
end
|
93
97
|
|
94
98
|
def initialize_proxy(url, options)
|
@@ -298,6 +302,9 @@ module Faraday
|
|
298
302
|
set_authorization_header(:basic_auth, login, pass)
|
299
303
|
end
|
300
304
|
|
305
|
+
extend Faraday::Deprecate
|
306
|
+
deprecate :basic_auth, '#request(:basic_auth, ...)', '2.0'
|
307
|
+
|
301
308
|
# Sets up the Authorization header with the given token.
|
302
309
|
#
|
303
310
|
# @param token [String]
|
@@ -315,6 +322,11 @@ module Faraday
|
|
315
322
|
set_authorization_header(:token_auth, token, options)
|
316
323
|
end
|
317
324
|
|
325
|
+
deprecate :token_auth,
|
326
|
+
'#request(:token_auth, ...)',
|
327
|
+
'2.0',
|
328
|
+
'See https://lostisland.github.io/faraday/middleware/authentication for more usage info.'
|
329
|
+
|
318
330
|
# Sets up a custom Authorization header.
|
319
331
|
#
|
320
332
|
# @param type [String] authorization type
|
@@ -337,6 +349,11 @@ module Faraday
|
|
337
349
|
set_authorization_header(:authorization, type, token)
|
338
350
|
end
|
339
351
|
|
352
|
+
deprecate :authorization,
|
353
|
+
'#request(:authorization, ...)',
|
354
|
+
'2.0',
|
355
|
+
'See https://lostisland.github.io/faraday/middleware/authentication for more usage info.'
|
356
|
+
|
340
357
|
# Check if the adapter is parallel-capable.
|
341
358
|
#
|
342
359
|
# @yield if the adapter isn't parallel-capable, or if no adapter is set yet.
|
@@ -416,9 +433,16 @@ module Faraday
|
|
416
433
|
uri.query = nil
|
417
434
|
|
418
435
|
with_uri_credentials(uri) do |user, password|
|
419
|
-
|
436
|
+
set_basic_auth(user, password)
|
420
437
|
uri.user = uri.password = nil
|
421
438
|
end
|
439
|
+
|
440
|
+
@proxy = proxy_from_env(url) unless @manual_proxy
|
441
|
+
end
|
442
|
+
|
443
|
+
def set_basic_auth(user, password)
|
444
|
+
header = Faraday::Request::BasicAuthentication.header(user, password)
|
445
|
+
headers[Faraday::Request::Authorization::KEY] = header
|
422
446
|
end
|
423
447
|
|
424
448
|
# Sets the path prefix and ensures that it always has a leading
|
@@ -429,7 +453,7 @@ module Faraday
|
|
429
453
|
# @return [String] the new path prefix
|
430
454
|
def path_prefix=(value)
|
431
455
|
url_prefix.path = if value
|
432
|
-
value =
|
456
|
+
value = "/#{value}" unless value[0, 1] == '/'
|
433
457
|
value
|
434
458
|
end
|
435
459
|
end
|
@@ -517,11 +541,11 @@ module Faraday
|
|
517
541
|
# @return [URI]
|
518
542
|
def build_exclusive_url(url = nil, params = nil, params_encoder = nil)
|
519
543
|
url = nil if url.respond_to?(:empty?) && url.empty?
|
520
|
-
base = url_prefix
|
544
|
+
base = url_prefix.dup
|
521
545
|
if url && base.path && base.path !~ %r{/$}
|
522
|
-
base = base.
|
523
|
-
base.path = base.path + '/' # ensure trailing slash
|
546
|
+
base.path = "#{base.path}/" # ensure trailing slash
|
524
547
|
end
|
548
|
+
url = url && URI.parse(url.to_s).opaque ? url.to_s.gsub(':', '%3A') : url
|
525
549
|
uri = url ? base + url : base
|
526
550
|
if params
|
527
551
|
uri.query = params.to_query(params_encoder || options.params_encoder)
|
@@ -576,7 +600,11 @@ module Faraday
|
|
576
600
|
case url
|
577
601
|
when String
|
578
602
|
uri = Utils.URI(url)
|
579
|
-
uri =
|
603
|
+
uri = if uri.host.nil?
|
604
|
+
find_default_proxy
|
605
|
+
else
|
606
|
+
URI.parse("#{uri.scheme}://#{uri.host}").find_proxy
|
607
|
+
end
|
580
608
|
when URI
|
581
609
|
uri = url.find_proxy
|
582
610
|
when nil
|
@@ -593,7 +621,7 @@ module Faraday
|
|
593
621
|
uri = ENV['http_proxy']
|
594
622
|
return unless uri && !uri.empty?
|
595
623
|
|
596
|
-
uri =
|
624
|
+
uri = "http://#{uri}" unless uri.match?(/^http/i)
|
597
625
|
uri
|
598
626
|
end
|
599
627
|
|