faraday 1.0.0 → 2.0.0
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 +221 -1
- data/LICENSE.md +1 -1
- data/README.md +19 -14
- data/examples/client_spec.rb +36 -4
- data/examples/client_test.rb +43 -4
- data/lib/faraday/adapter/test.rb +61 -43
- data/lib/faraday/adapter.rb +3 -16
- data/lib/faraday/adapter_registry.rb +3 -1
- data/lib/faraday/connection.rb +25 -78
- data/lib/faraday/encoders/flat_params_encoder.rb +9 -2
- data/lib/faraday/encoders/nested_params_encoder.rb +9 -4
- data/lib/faraday/error.rb +23 -8
- data/lib/faraday/logging/formatter.rb +1 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +14 -5
- data/lib/faraday/middleware_registry.rb +15 -79
- data/lib/faraday/options/proxy_options.rb +4 -0
- data/lib/faraday/options.rb +7 -11
- data/lib/faraday/rack_builder.rb +34 -30
- data/lib/faraday/request/authorization.rb +32 -36
- data/lib/faraday/request/instrumentation.rb +2 -0
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +5 -1
- data/lib/faraday/request.rb +13 -23
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +4 -4
- data/lib/faraday/response/raise_error.rb +20 -1
- data/lib/faraday/response.rb +8 -22
- data/lib/faraday/utils/headers.rb +3 -3
- data/lib/faraday/utils.rb +21 -8
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +44 -59
- data/spec/faraday/adapter/test_spec.rb +377 -0
- data/spec/faraday/connection_spec.rb +147 -51
- data/spec/faraday/error_spec.rb +15 -0
- data/spec/faraday/middleware_spec.rb +32 -6
- data/spec/faraday/options/env_spec.rb +2 -2
- 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 +144 -38
- data/spec/faraday/request/authorization_spec.rb +19 -24
- data/spec/faraday/request/instrumentation_spec.rb +5 -7
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +13 -1
- data/spec/faraday/request_spec.rb +6 -6
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/raise_error_spec.rb +66 -0
- data/spec/faraday/utils_spec.rb +62 -1
- data/spec/support/fake_safe_buffer.rb +1 -1
- data/spec/support/helper_methods.rb +0 -37
- data/spec/support/shared_examples/adapter.rb +2 -2
- data/spec/support/shared_examples/request_method.rb +43 -28
- metadata +16 -48
- 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/adapter/typhoeus.rb +0 -15
- data/lib/faraday/autoload.rb +0 -95
- data/lib/faraday/dependency_loader.rb +0 -37
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/request/basic_authentication.rb +0 -20
- data/lib/faraday/request/multipart.rb +0 -99
- data/lib/faraday/request/retry.rb +0 -239
- data/lib/faraday/request/token_authentication.rb +0 -20
- data/spec/faraday/adapter/em_http_spec.rb +0 -47
- data/spec/faraday/adapter/em_synchrony_spec.rb +0 -16
- data/spec/faraday/adapter/excon_spec.rb +0 -49
- data/spec/faraday/adapter/httpclient_spec.rb +0 -73
- data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
- data/spec/faraday/adapter/net_http_spec.rb +0 -64
- data/spec/faraday/adapter/patron_spec.rb +0 -18
- data/spec/faraday/adapter/rack_spec.rb +0 -8
- data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
- data/spec/faraday/composite_read_io_spec.rb +0 -80
- data/spec/faraday/request/multipart_spec.rb +0 -274
- data/spec/faraday/request/retry_spec.rb +0 -242
- data/spec/faraday/response/middleware_spec.rb +0 -52
- data/spec/support/webmock_rack_app.rb +0 -68
data/lib/faraday.rb
CHANGED
@@ -1,10 +1,22 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'cgi'
|
4
|
+
require 'date'
|
4
5
|
require 'set'
|
5
6
|
require 'forwardable'
|
7
|
+
require 'faraday/version'
|
8
|
+
require 'faraday/methods'
|
9
|
+
require 'faraday/error'
|
6
10
|
require 'faraday/middleware_registry'
|
7
|
-
require 'faraday/
|
11
|
+
require 'faraday/utils'
|
12
|
+
require 'faraday/options'
|
13
|
+
require 'faraday/connection'
|
14
|
+
require 'faraday/rack_builder'
|
15
|
+
require 'faraday/parameters'
|
16
|
+
require 'faraday/middleware'
|
17
|
+
require 'faraday/adapter'
|
18
|
+
require 'faraday/request'
|
19
|
+
require 'faraday/response'
|
8
20
|
|
9
21
|
# This is the main namespace for Faraday.
|
10
22
|
#
|
@@ -19,9 +31,7 @@ require 'faraday/dependency_loader'
|
|
19
31
|
# conn.get '/'
|
20
32
|
#
|
21
33
|
module Faraday
|
22
|
-
|
23
|
-
METHODS_WITH_QUERY = %w[get head delete trace].freeze
|
24
|
-
METHODS_WITH_BODY = %w[post put patch].freeze
|
34
|
+
CONTENT_TYPE = 'Content-Type'
|
25
35
|
|
26
36
|
class << self
|
27
37
|
# The root path that Faraday is being loaded from.
|
@@ -37,7 +47,7 @@ module Faraday
|
|
37
47
|
|
38
48
|
# @overload default_adapter
|
39
49
|
# Gets the Symbol key identifying a default Adapter to use
|
40
|
-
# for the default {Faraday::Connection}. Defaults to `:
|
50
|
+
# for the default {Faraday::Connection}. Defaults to `:test`.
|
41
51
|
# @return [Symbol] the default adapter
|
42
52
|
# @overload default_adapter=(adapter)
|
43
53
|
# Updates default adapter while resetting {.default_connection}.
|
@@ -80,23 +90,10 @@ module Faraday
|
|
80
90
|
# params: { page: 1 }
|
81
91
|
# # => Faraday::Connection to http://faraday.com?page=1
|
82
92
|
def new(url = nil, options = {}, &block)
|
83
|
-
options =
|
93
|
+
options = Utils.deep_merge(default_connection_options, options)
|
84
94
|
Faraday::Connection.new(url, options, &block)
|
85
95
|
end
|
86
96
|
|
87
|
-
# @private
|
88
|
-
# Internal: Requires internal Faraday libraries.
|
89
|
-
#
|
90
|
-
# @param libs [Array] one or more relative String names to Faraday classes.
|
91
|
-
# @return [void]
|
92
|
-
def require_libs(*libs)
|
93
|
-
libs.each do |lib|
|
94
|
-
require "#{lib_path}/#{lib}"
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
alias require_lib require_libs
|
99
|
-
|
100
97
|
# Documented elsewhere, see default_adapter reader
|
101
98
|
def default_adapter=(adapter)
|
102
99
|
@default_connection = nil
|
@@ -107,6 +104,34 @@ module Faraday
|
|
107
104
|
default_connection.respond_to?(symbol, include_private) || super
|
108
105
|
end
|
109
106
|
|
107
|
+
# @overload default_connection
|
108
|
+
# Gets the default connection used for simple scripts.
|
109
|
+
# @return [Faraday::Connection] a connection configured with
|
110
|
+
# the default_adapter.
|
111
|
+
# @overload default_connection=(connection)
|
112
|
+
# @param connection [Faraday::Connection]
|
113
|
+
# Sets the default {Faraday::Connection} for simple scripts that
|
114
|
+
# access the Faraday constant directly, such as
|
115
|
+
# <code>Faraday.get "https://faraday.com"</code>.
|
116
|
+
def default_connection
|
117
|
+
@default_connection ||= Connection.new(default_connection_options)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Gets the default connection options used when calling {Faraday#new}.
|
121
|
+
#
|
122
|
+
# @return [Faraday::ConnectionOptions]
|
123
|
+
def default_connection_options
|
124
|
+
@default_connection_options ||= ConnectionOptions.new
|
125
|
+
end
|
126
|
+
|
127
|
+
# Sets the default options used when calling {Faraday#new}.
|
128
|
+
#
|
129
|
+
# @param options [Hash, Faraday::ConnectionOptions]
|
130
|
+
def default_connection_options=(options)
|
131
|
+
@default_connection = nil
|
132
|
+
@default_connection_options = ConnectionOptions.from(options)
|
133
|
+
end
|
134
|
+
|
110
135
|
private
|
111
136
|
|
112
137
|
# Internal: Proxies method calls on the Faraday constant to
|
@@ -123,44 +148,4 @@ module Faraday
|
|
123
148
|
self.ignore_env_proxy = false
|
124
149
|
self.root_path = File.expand_path __dir__
|
125
150
|
self.lib_path = File.expand_path 'faraday', __dir__
|
126
|
-
self.default_adapter = :net_http
|
127
|
-
|
128
|
-
# @overload default_connection
|
129
|
-
# Gets the default connection used for simple scripts.
|
130
|
-
# @return [Faraday::Connection] a connection configured with
|
131
|
-
# the default_adapter.
|
132
|
-
# @overload default_connection=(connection)
|
133
|
-
# @param connection [Faraday::Connection]
|
134
|
-
# Sets the default {Faraday::Connection} for simple scripts that
|
135
|
-
# access the Faraday constant directly, such as
|
136
|
-
# <code>Faraday.get "https://faraday.com"</code>.
|
137
|
-
def self.default_connection
|
138
|
-
@default_connection ||= Connection.new(default_connection_options)
|
139
|
-
end
|
140
|
-
|
141
|
-
# Gets the default connection options used when calling {Faraday#new}.
|
142
|
-
#
|
143
|
-
# @return [Faraday::ConnectionOptions]
|
144
|
-
def self.default_connection_options
|
145
|
-
@default_connection_options ||= ConnectionOptions.new
|
146
|
-
end
|
147
|
-
|
148
|
-
# Sets the default options used when calling {Faraday#new}.
|
149
|
-
#
|
150
|
-
# @param options [Hash, Faraday::ConnectionOptions]
|
151
|
-
def self.default_connection_options=(options)
|
152
|
-
@default_connection = nil
|
153
|
-
@default_connection_options = ConnectionOptions.from(options)
|
154
|
-
end
|
155
|
-
|
156
|
-
unless const_defined? :Timer
|
157
|
-
require 'timeout'
|
158
|
-
Timer = Timeout
|
159
|
-
end
|
160
|
-
|
161
|
-
require_libs 'utils', 'options', 'connection', 'rack_builder', 'parameters',
|
162
|
-
'middleware', 'adapter', 'request', 'response', 'error',
|
163
|
-
'file_part', 'param_part'
|
164
|
-
|
165
|
-
require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
|
166
151
|
end
|
@@ -0,0 +1,377 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::Adapter::Test do
|
4
|
+
let(:stubs) do
|
5
|
+
described_class::Stubs.new do |stub|
|
6
|
+
stub.get('http://domain.test/hello') do
|
7
|
+
[200, { 'Content-Type' => 'text/html' }, 'domain: hello']
|
8
|
+
end
|
9
|
+
|
10
|
+
stub.get('http://wrong.test/hello') do
|
11
|
+
[200, { 'Content-Type' => 'text/html' }, 'wrong: hello']
|
12
|
+
end
|
13
|
+
|
14
|
+
stub.get('http://wrong.test/bait') do
|
15
|
+
[404, { 'Content-Type' => 'text/html' }]
|
16
|
+
end
|
17
|
+
|
18
|
+
stub.get('/hello') do
|
19
|
+
[200, { 'Content-Type' => 'text/html' }, 'hello']
|
20
|
+
end
|
21
|
+
|
22
|
+
stub.get('/method-echo') do |env|
|
23
|
+
[200, { 'Content-Type' => 'text/html' }, env[:method].to_s]
|
24
|
+
end
|
25
|
+
|
26
|
+
stub.get(%r{\A/resources/\d+(?:\?|\z)}) do
|
27
|
+
[200, { 'Content-Type' => 'text/html' }, 'show']
|
28
|
+
end
|
29
|
+
|
30
|
+
stub.get(%r{\A/resources/(specified)\z}) do |_env, meta|
|
31
|
+
[200, { 'Content-Type' => 'text/html' }, "show #{meta[:match_data][1]}"]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
let(:connection) do
|
37
|
+
Faraday.new do |builder|
|
38
|
+
builder.adapter :test, stubs
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
let(:response) { connection.get('/hello') }
|
43
|
+
|
44
|
+
context 'with simple path sets status' do
|
45
|
+
subject { response.status }
|
46
|
+
|
47
|
+
it { is_expected.to eq 200 }
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'with simple path sets headers' do
|
51
|
+
subject { response.headers['Content-Type'] }
|
52
|
+
|
53
|
+
it { is_expected.to eq 'text/html' }
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'with simple path sets body' do
|
57
|
+
subject { response.body }
|
58
|
+
|
59
|
+
it { is_expected.to eq 'hello' }
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'with host points to the right stub' do
|
63
|
+
subject { connection.get('http://domain.test/hello').body }
|
64
|
+
|
65
|
+
it { is_expected.to eq 'domain: hello' }
|
66
|
+
end
|
67
|
+
|
68
|
+
describe 'can be called several times' do
|
69
|
+
subject { connection.get('/hello').body }
|
70
|
+
|
71
|
+
it { is_expected.to eq 'hello' }
|
72
|
+
end
|
73
|
+
|
74
|
+
describe 'can handle regular expression path' do
|
75
|
+
subject { connection.get('/resources/1').body }
|
76
|
+
|
77
|
+
it { is_expected.to eq 'show' }
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'can handle single parameter block' do
|
81
|
+
subject { connection.get('/method-echo').body }
|
82
|
+
|
83
|
+
it { is_expected.to eq 'get' }
|
84
|
+
end
|
85
|
+
|
86
|
+
describe 'can handle regular expression path with captured result' do
|
87
|
+
subject { connection.get('/resources/specified').body }
|
88
|
+
|
89
|
+
it { is_expected.to eq 'show specified' }
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'with get params' do
|
93
|
+
subject { connection.get('/param?a=1').body }
|
94
|
+
|
95
|
+
before do
|
96
|
+
stubs.get('/param?a=1') { [200, {}, 'a'] }
|
97
|
+
end
|
98
|
+
|
99
|
+
it { is_expected.to eq 'a' }
|
100
|
+
end
|
101
|
+
|
102
|
+
describe 'ignoring unspecified get params' do
|
103
|
+
before do
|
104
|
+
stubs.get('/optional?a=1') { [200, {}, 'a'] }
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'with multiple params' do
|
108
|
+
subject { connection.get('/optional?a=1&b=1').body }
|
109
|
+
|
110
|
+
it { is_expected.to eq 'a' }
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'with single param' do
|
114
|
+
subject { connection.get('/optional?a=1').body }
|
115
|
+
|
116
|
+
it { is_expected.to eq 'a' }
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'without params' do
|
120
|
+
subject(:request) { connection.get('/optional') }
|
121
|
+
|
122
|
+
it do
|
123
|
+
expect { request }.to raise_error(
|
124
|
+
Faraday::Adapter::Test::Stubs::NotFound
|
125
|
+
)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context 'with http headers' do
|
131
|
+
before do
|
132
|
+
stubs.get('/yo', 'X-HELLO' => 'hello') { [200, {}, 'a'] }
|
133
|
+
stubs.get('/yo') { [200, {}, 'b'] }
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'with header' do
|
137
|
+
subject do
|
138
|
+
connection.get('/yo') { |env| env.headers['X-HELLO'] = 'hello' }.body
|
139
|
+
end
|
140
|
+
|
141
|
+
it { is_expected.to eq 'a' }
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'without header' do
|
145
|
+
subject do
|
146
|
+
connection.get('/yo').body
|
147
|
+
end
|
148
|
+
|
149
|
+
it { is_expected.to eq 'b' }
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
describe 'different outcomes for the same request' do
|
154
|
+
def make_request
|
155
|
+
connection.get('/foo')
|
156
|
+
end
|
157
|
+
|
158
|
+
subject(:request) { make_request.body }
|
159
|
+
|
160
|
+
before do
|
161
|
+
stubs.get('/foo') { [200, { 'Content-Type' => 'text/html' }, 'hello'] }
|
162
|
+
stubs.get('/foo') { [200, { 'Content-Type' => 'text/html' }, 'world'] }
|
163
|
+
end
|
164
|
+
|
165
|
+
context 'the first request' do
|
166
|
+
it { is_expected.to eq 'hello' }
|
167
|
+
end
|
168
|
+
|
169
|
+
context 'the second request' do
|
170
|
+
before do
|
171
|
+
make_request
|
172
|
+
end
|
173
|
+
|
174
|
+
it { is_expected.to eq 'world' }
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
describe 'yielding env to stubs' do
|
179
|
+
subject { connection.get('http://foo.com/foo?a=1').body }
|
180
|
+
|
181
|
+
before do
|
182
|
+
stubs.get '/foo' do |env|
|
183
|
+
expect(env[:url].path).to eq '/foo'
|
184
|
+
expect(env[:url].host).to eq 'foo.com'
|
185
|
+
expect(env[:params]['a']).to eq '1'
|
186
|
+
expect(env[:request_headers]['Accept']).to eq 'text/plain'
|
187
|
+
[200, {}, 'a']
|
188
|
+
end
|
189
|
+
|
190
|
+
connection.headers['Accept'] = 'text/plain'
|
191
|
+
end
|
192
|
+
|
193
|
+
it { is_expected.to eq 'a' }
|
194
|
+
end
|
195
|
+
|
196
|
+
describe 'params parsing' do
|
197
|
+
subject { connection.get('http://foo.com/foo?a[b]=1').body }
|
198
|
+
|
199
|
+
context 'with default encoder' do
|
200
|
+
before do
|
201
|
+
stubs.get '/foo' do |env|
|
202
|
+
expect(env[:params]['a']['b']).to eq '1'
|
203
|
+
[200, {}, 'a']
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
it { is_expected.to eq 'a' }
|
208
|
+
end
|
209
|
+
|
210
|
+
context 'with nested encoder' do
|
211
|
+
before do
|
212
|
+
stubs.get '/foo' do |env|
|
213
|
+
expect(env[:params]['a']['b']).to eq '1'
|
214
|
+
[200, {}, 'a']
|
215
|
+
end
|
216
|
+
|
217
|
+
connection.options.params_encoder = Faraday::NestedParamsEncoder
|
218
|
+
end
|
219
|
+
|
220
|
+
it { is_expected.to eq 'a' }
|
221
|
+
end
|
222
|
+
|
223
|
+
context 'with flat encoder' do
|
224
|
+
before do
|
225
|
+
stubs.get '/foo' do |env|
|
226
|
+
expect(env[:params]['a[b]']).to eq '1'
|
227
|
+
[200, {}, 'a']
|
228
|
+
end
|
229
|
+
|
230
|
+
connection.options.params_encoder = Faraday::FlatParamsEncoder
|
231
|
+
end
|
232
|
+
|
233
|
+
it { is_expected.to eq 'a' }
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
describe 'raising an error if no stub was found' do
|
238
|
+
describe 'for request' do
|
239
|
+
subject(:request) { connection.get('/invalid') { [200, {}, []] } }
|
240
|
+
|
241
|
+
it { expect { request }.to raise_error described_class::Stubs::NotFound }
|
242
|
+
end
|
243
|
+
|
244
|
+
describe 'for specified host' do
|
245
|
+
subject(:request) { connection.get('http://domain.test/bait') }
|
246
|
+
|
247
|
+
it { expect { request }.to raise_error described_class::Stubs::NotFound }
|
248
|
+
end
|
249
|
+
|
250
|
+
describe 'for request without specified header' do
|
251
|
+
subject(:request) { connection.get('/yo') }
|
252
|
+
|
253
|
+
before do
|
254
|
+
stubs.get('/yo', 'X-HELLO' => 'hello') { [200, {}, 'a'] }
|
255
|
+
end
|
256
|
+
|
257
|
+
it { expect { request }.to raise_error described_class::Stubs::NotFound }
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
describe 'for request with non default params encoder' do
|
262
|
+
let(:connection) do
|
263
|
+
Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
|
264
|
+
builder.adapter :test, stubs
|
265
|
+
end
|
266
|
+
end
|
267
|
+
let(:stubs) do
|
268
|
+
described_class::Stubs.new do |stubs|
|
269
|
+
stubs.get('/path?a=x&a=y&a=z') { [200, {}, 'a'] }
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
context 'when all flat param values are correctly set' do
|
274
|
+
subject(:request) { connection.get('/path?a=x&a=y&a=z') }
|
275
|
+
|
276
|
+
it { expect(request.status).to eq 200 }
|
277
|
+
end
|
278
|
+
|
279
|
+
shared_examples 'raise NotFound when params do not satisfy the flat param values' do |params|
|
280
|
+
subject(:request) { connection.get('/path', params) }
|
281
|
+
|
282
|
+
context "with #{params.inspect}" do
|
283
|
+
it { expect { request }.to raise_error described_class::Stubs::NotFound }
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x] }
|
288
|
+
it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x y] }
|
289
|
+
it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x z y] } # NOTE: The order of the value is also compared.
|
290
|
+
it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { b: %w[x y z] }
|
291
|
+
end
|
292
|
+
|
293
|
+
describe 'strict_mode' do
|
294
|
+
let(:stubs) do
|
295
|
+
described_class::Stubs.new(strict_mode: true) do |stubs|
|
296
|
+
stubs.get('/strict?a=12&b=xy', 'Authorization' => 'Bearer m_ck', 'X-C' => 'hello') { [200, {}, 'a'] }
|
297
|
+
stubs.get('/with_user_agent?a=12&b=xy', authorization: 'Bearer m_ck', 'User-Agent' => 'My Agent') { [200, {}, 'a'] }
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
context 'when params and headers are exactly set' do
|
302
|
+
subject(:request) { connection.get('/strict', { a: '12', b: 'xy' }, { authorization: 'Bearer m_ck', x_c: 'hello' }) }
|
303
|
+
|
304
|
+
it { expect(request.status).to eq 200 }
|
305
|
+
end
|
306
|
+
|
307
|
+
context 'when params and headers are exactly set with a custom user agent' do
|
308
|
+
subject(:request) { connection.get('/with_user_agent', { a: '12', b: 'xy' }, { authorization: 'Bearer m_ck', 'User-Agent' => 'My Agent' }) }
|
309
|
+
|
310
|
+
it { expect(request.status).to eq 200 }
|
311
|
+
end
|
312
|
+
|
313
|
+
shared_examples 'raise NotFound when params do not satisfy the strict check' do |params|
|
314
|
+
subject(:request) { connection.get('/strict', params, { 'Authorization' => 'Bearer m_ck', 'X-C' => 'hello' }) }
|
315
|
+
|
316
|
+
context "with #{params.inspect}" do
|
317
|
+
it { expect { request }.to raise_error described_class::Stubs::NotFound }
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '12' }
|
322
|
+
it_behaves_like 'raise NotFound when params do not satisfy the strict check', { b: 'xy' }
|
323
|
+
it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '123', b: 'xy' }
|
324
|
+
it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '12', b: 'xyz' }
|
325
|
+
it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '12', b: 'xy', c: 'hello' }
|
326
|
+
it_behaves_like 'raise NotFound when params do not satisfy the strict check', { additional: 'special', a: '12', b: 'xy', c: 'hello' }
|
327
|
+
|
328
|
+
shared_examples 'raise NotFound when headers do not satisfy the strict check' do |path, headers|
|
329
|
+
subject(:request) { connection.get(path, { a: 12, b: 'xy' }, headers) }
|
330
|
+
|
331
|
+
context "with #{headers.inspect}" do
|
332
|
+
it { expect { request }.to raise_error described_class::Stubs::NotFound }
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck' }
|
337
|
+
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { 'X-C' => 'hello' }
|
338
|
+
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'Hi' }
|
339
|
+
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Basic m_ck', 'x-c': 'hello' }
|
340
|
+
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello', x_special: 'special' }
|
341
|
+
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck' }
|
342
|
+
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'Unknown' }
|
343
|
+
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'My Agent', x_special: 'special' }
|
344
|
+
|
345
|
+
context 'when strict_mode is disabled' do
|
346
|
+
before do
|
347
|
+
stubs.strict_mode = false
|
348
|
+
end
|
349
|
+
|
350
|
+
shared_examples 'does not raise NotFound even when params do not satisfy the strict check' do |params|
|
351
|
+
subject(:request) { connection.get('/strict', params, { 'Authorization' => 'Bearer m_ck', 'X-C' => 'hello' }) }
|
352
|
+
|
353
|
+
context "with #{params.inspect}" do
|
354
|
+
it { expect(request.status).to eq 200 }
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
it_behaves_like 'does not raise NotFound even when params do not satisfy the strict check', { a: '12', b: 'xy' }
|
359
|
+
it_behaves_like 'does not raise NotFound even when params do not satisfy the strict check', { a: '12', b: 'xy', c: 'hello' }
|
360
|
+
it_behaves_like 'does not raise NotFound even when params do not satisfy the strict check', { additional: 'special', a: '12', b: 'xy', c: 'hello' }
|
361
|
+
|
362
|
+
shared_examples 'does not raise NotFound even when headers do not satisfy the strict check' do |path, headers|
|
363
|
+
subject(:request) { connection.get(path, { a: 12, b: 'xy' }, headers) }
|
364
|
+
|
365
|
+
context "with #{headers.inspect}" do
|
366
|
+
it { expect(request.status).to eq 200 }
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello' }
|
371
|
+
it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello', x_special: 'special' }
|
372
|
+
it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello', user_agent: 'Special Agent' }
|
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
|
+
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
|
+
end
|
376
|
+
end
|
377
|
+
end
|