faraday 1.0.1 → 1.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +104 -0
- data/LICENSE.md +1 -1
- data/README.md +3 -5
- data/examples/client_spec.rb +1 -1
- data/lib/faraday.rb +55 -41
- data/lib/faraday/adapter.rb +1 -8
- data/lib/faraday/autoload.rb +1 -6
- data/lib/faraday/connection.rb +11 -4
- 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 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +14 -4
- data/lib/faraday/options.rb +4 -8
- data/lib/faraday/options/proxy_options.rb +4 -0
- data/lib/faraday/rack_builder.rb +13 -12
- data/lib/faraday/request.rb +20 -10
- data/lib/faraday/request/multipart.rb +9 -2
- data/lib/faraday/request/retry.rb +2 -2
- data/lib/faraday/response.rb +0 -6
- data/lib/faraday/response/raise_error.rb +12 -1
- data/lib/faraday/utils.rb +2 -2
- data/lib/faraday/utils/headers.rb +2 -2
- data/lib/faraday/version.rb +5 -0
- data/spec/faraday/adapter/test_spec.rb +260 -0
- data/spec/faraday/connection_spec.rb +38 -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 +149 -0
- data/spec/faraday/request/authorization_spec.rb +2 -2
- data/spec/faraday/request/multipart_spec.rb +41 -13
- data/spec/faraday/request/retry_spec.rb +1 -1
- data/spec/faraday/request_spec.rb +16 -5
- data/spec/faraday/response/raise_error_spec.rb +63 -0
- data/spec/support/shared_examples/adapter.rb +2 -1
- data/spec/support/shared_examples/request_method.rb +39 -11
- metadata +92 -13
- data/lib/faraday/adapter/em_http.rb +0 -286
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
- data/lib/faraday/adapter/em_synchrony.rb +0 -150
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
- data/lib/faraday/adapter/excon.rb +0 -124
- data/lib/faraday/adapter/net_http.rb +0 -219
- data/lib/faraday/adapter/net_http_persistent.rb +0 -91
- data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
data/lib/faraday/middleware.rb
CHANGED
@@ -6,15 +6,25 @@ module Faraday
|
|
6
6
|
extend MiddlewareRegistry
|
7
7
|
extend DependencyLoader
|
8
8
|
|
9
|
-
|
9
|
+
attr_reader :app, :options
|
10
|
+
|
11
|
+
def initialize(app = nil, options = {})
|
10
12
|
@app = app
|
13
|
+
@options = options
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
on_request(env) if respond_to?(:on_request)
|
18
|
+
app.call(env).on_complete do |environment|
|
19
|
+
on_complete(environment) if respond_to?(:on_complete)
|
20
|
+
end
|
11
21
|
end
|
12
22
|
|
13
23
|
def close
|
14
|
-
if
|
15
|
-
|
24
|
+
if app.respond_to?(:close)
|
25
|
+
app.close
|
16
26
|
else
|
17
|
-
warn "#{
|
27
|
+
warn "#{app} does not implement \#close!"
|
18
28
|
end
|
19
29
|
end
|
20
30
|
end
|
data/lib/faraday/options.rb
CHANGED
@@ -103,12 +103,10 @@ module Faraday
|
|
103
103
|
end
|
104
104
|
|
105
105
|
# Public
|
106
|
-
def each_key
|
106
|
+
def each_key(&block)
|
107
107
|
return to_enum(:each_key) unless block_given?
|
108
108
|
|
109
|
-
keys.each
|
110
|
-
yield(key)
|
111
|
-
end
|
109
|
+
keys.each(&block)
|
112
110
|
end
|
113
111
|
|
114
112
|
# Public
|
@@ -119,12 +117,10 @@ module Faraday
|
|
119
117
|
alias has_key? key?
|
120
118
|
|
121
119
|
# Public
|
122
|
-
def each_value
|
120
|
+
def each_value(&block)
|
123
121
|
return to_enum(:each_value) unless block_given?
|
124
122
|
|
125
|
-
values.each
|
126
|
-
yield(value)
|
127
|
-
end
|
123
|
+
values.each(&block)
|
128
124
|
end
|
129
125
|
|
130
126
|
# Public
|
@@ -11,6 +11,9 @@ module Faraday
|
|
11
11
|
def self.from(value)
|
12
12
|
case value
|
13
13
|
when String
|
14
|
+
# URIs without a scheme should default to http (like 'example:123').
|
15
|
+
# This fixes #1282 and prevents a silent failure in some adapters.
|
16
|
+
value = "http://#{value}" unless value.include?('://')
|
14
17
|
value = { uri: Utils.URI(value) }
|
15
18
|
when URI
|
16
19
|
value = { uri: value }
|
@@ -19,6 +22,7 @@ module Faraday
|
|
19
22
|
value[:uri] = Utils.URI(uri)
|
20
23
|
end
|
21
24
|
end
|
25
|
+
|
22
26
|
super(value)
|
23
27
|
end
|
24
28
|
|
data/lib/faraday/rack_builder.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'ruby2_keywords'
|
3
4
|
require 'faraday/adapter_registry'
|
4
5
|
|
5
6
|
module Faraday
|
@@ -27,7 +28,7 @@ module Faraday
|
|
27
28
|
|
28
29
|
attr_reader :name
|
29
30
|
|
30
|
-
def initialize(klass, *args, &block)
|
31
|
+
ruby2_keywords def initialize(klass, *args, &block)
|
31
32
|
@name = klass.to_s
|
32
33
|
REGISTRY.set(klass) if klass.respond_to?(:name)
|
33
34
|
@args = args
|
@@ -89,7 +90,7 @@ module Faraday
|
|
89
90
|
@handlers.frozen?
|
90
91
|
end
|
91
92
|
|
92
|
-
def use(klass, *args, &block)
|
93
|
+
ruby2_keywords def use(klass, *args, &block)
|
93
94
|
if klass.is_a? Symbol
|
94
95
|
use_symbol(Faraday::Middleware, klass, *args, &block)
|
95
96
|
else
|
@@ -99,15 +100,15 @@ module Faraday
|
|
99
100
|
end
|
100
101
|
end
|
101
102
|
|
102
|
-
def request(key, *args, &block)
|
103
|
+
ruby2_keywords def request(key, *args, &block)
|
103
104
|
use_symbol(Faraday::Request, key, *args, &block)
|
104
105
|
end
|
105
106
|
|
106
|
-
def response(key, *args, &block)
|
107
|
+
ruby2_keywords def response(key, *args, &block)
|
107
108
|
use_symbol(Faraday::Response, key, *args, &block)
|
108
109
|
end
|
109
110
|
|
110
|
-
def adapter(klass = NO_ARGUMENT, *args, &block)
|
111
|
+
ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block)
|
111
112
|
return @adapter if klass == NO_ARGUMENT
|
112
113
|
|
113
114
|
klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
|
@@ -116,7 +117,7 @@ module Faraday
|
|
116
117
|
|
117
118
|
## methods to push onto the various positions in the stack:
|
118
119
|
|
119
|
-
def insert(index, *args, &block)
|
120
|
+
ruby2_keywords def insert(index, *args, &block)
|
120
121
|
raise_if_locked
|
121
122
|
index = assert_index(index)
|
122
123
|
handler = self.class::Handler.new(*args, &block)
|
@@ -125,12 +126,12 @@ module Faraday
|
|
125
126
|
|
126
127
|
alias insert_before insert
|
127
128
|
|
128
|
-
def insert_after(index, *args, &block)
|
129
|
+
ruby2_keywords def insert_after(index, *args, &block)
|
129
130
|
index = assert_index(index)
|
130
131
|
insert(index + 1, *args, &block)
|
131
132
|
end
|
132
133
|
|
133
|
-
def swap(index, *args, &block)
|
134
|
+
ruby2_keywords def swap(index, *args, &block)
|
134
135
|
raise_if_locked
|
135
136
|
index = assert_index(index)
|
136
137
|
@handlers.delete_at(index)
|
@@ -186,7 +187,7 @@ module Faraday
|
|
186
187
|
end
|
187
188
|
|
188
189
|
# ENV Keys
|
189
|
-
# :
|
190
|
+
# :http_method - a symbolized request HTTP method (:get, :post)
|
190
191
|
# :body - the request body that will eventually be converted to a string.
|
191
192
|
# :url - URI instance for the current request.
|
192
193
|
# :status - HTTP response status code
|
@@ -207,7 +208,7 @@ module Faraday
|
|
207
208
|
request.options.params_encoder
|
208
209
|
)
|
209
210
|
|
210
|
-
Env.new(request.
|
211
|
+
Env.new(request.http_method, request.body, exclusive_url,
|
211
212
|
request.options, request.headers, connection.ssl,
|
212
213
|
connection.parallel_manager)
|
213
214
|
end
|
@@ -231,10 +232,10 @@ module Faraday
|
|
231
232
|
end
|
232
233
|
|
233
234
|
def is_adapter?(klass) # rubocop:disable Naming/PredicateName
|
234
|
-
klass
|
235
|
+
klass <= Faraday::Adapter
|
235
236
|
end
|
236
237
|
|
237
|
-
def use_symbol(mod, key, *args, &block)
|
238
|
+
ruby2_keywords def use_symbol(mod, key, *args, &block)
|
238
239
|
use(mod.lookup_middleware(key), *args, &block)
|
239
240
|
end
|
240
241
|
|
data/lib/faraday/request.rb
CHANGED
@@ -12,7 +12,7 @@ module Faraday
|
|
12
12
|
# req.body = 'abc'
|
13
13
|
# end
|
14
14
|
#
|
15
|
-
# @!attribute
|
15
|
+
# @!attribute http_method
|
16
16
|
# @return [Symbol] the HTTP method of the Request
|
17
17
|
# @!attribute path
|
18
18
|
# @return [URI, String] the path
|
@@ -26,7 +26,9 @@ module Faraday
|
|
26
26
|
# @return [RequestOptions] options
|
27
27
|
#
|
28
28
|
# rubocop:disable Style/StructInheritance
|
29
|
-
class Request < Struct.new(
|
29
|
+
class Request < Struct.new(
|
30
|
+
:http_method, :path, :params, :headers, :body, :options
|
31
|
+
)
|
30
32
|
# rubocop:enable Style/StructInheritance
|
31
33
|
|
32
34
|
extend MiddlewareRegistry
|
@@ -56,6 +58,14 @@ module Faraday
|
|
56
58
|
end
|
57
59
|
end
|
58
60
|
|
61
|
+
def method
|
62
|
+
warn <<~TEXT
|
63
|
+
WARNING: `Faraday::Request##{__method__}` is deprecated; use `#http_method` instead. It will be removed in or after version 2.0.
|
64
|
+
`Faraday::Request##{__method__}` called from #{caller_locations(1..1).first}
|
65
|
+
TEXT
|
66
|
+
http_method
|
67
|
+
end
|
68
|
+
|
59
69
|
# Replace params, preserving the existing hash type.
|
60
70
|
#
|
61
71
|
# @param hash [Hash] new params
|
@@ -116,7 +126,7 @@ module Faraday
|
|
116
126
|
# @return [Hash] the hash ready to be serialized in Marshal.
|
117
127
|
def marshal_dump
|
118
128
|
{
|
119
|
-
|
129
|
+
http_method: http_method,
|
120
130
|
body: body,
|
121
131
|
headers: headers,
|
122
132
|
path: path,
|
@@ -129,17 +139,17 @@ module Faraday
|
|
129
139
|
# Restores the instance variables according to the +serialised+.
|
130
140
|
# @param serialised [Hash] the serialised object.
|
131
141
|
def marshal_load(serialised)
|
132
|
-
self.
|
133
|
-
self.body
|
134
|
-
self.headers
|
135
|
-
self.path
|
136
|
-
self.params
|
137
|
-
self.options
|
142
|
+
self.http_method = serialised[:http_method]
|
143
|
+
self.body = serialised[:body]
|
144
|
+
self.headers = serialised[:headers]
|
145
|
+
self.path = serialised[:path]
|
146
|
+
self.params = serialised[:params]
|
147
|
+
self.options = serialised[:options]
|
138
148
|
end
|
139
149
|
|
140
150
|
# @return [Env] the Env for this Request
|
141
151
|
def to_env(connection)
|
142
|
-
Env.new(
|
152
|
+
Env.new(http_method, body, connection.build_exclusive_url(path, params),
|
143
153
|
options, headers, connection.ssl, connection.parallel_manager)
|
144
154
|
end
|
145
155
|
end
|
@@ -12,6 +12,11 @@ module Faraday
|
|
12
12
|
DEFAULT_BOUNDARY_PREFIX = '-----------RubyMultipartPost'
|
13
13
|
end
|
14
14
|
|
15
|
+
def initialize(app = nil, options = {})
|
16
|
+
super(app)
|
17
|
+
@options = options
|
18
|
+
end
|
19
|
+
|
15
20
|
# Checks for files in the payload, otherwise leaves everything untouched.
|
16
21
|
#
|
17
22
|
# @param env [Faraday::Env]
|
@@ -30,7 +35,7 @@ module Faraday
|
|
30
35
|
type = request_type(env)
|
31
36
|
env.body.respond_to?(:each_key) && !env.body.empty? && (
|
32
37
|
(type.empty? && has_multipart?(env.body)) ||
|
33
|
-
|
38
|
+
(type == self.class.mime_type)
|
34
39
|
)
|
35
40
|
end
|
36
41
|
|
@@ -79,7 +84,9 @@ module Faraday
|
|
79
84
|
# @param pieces [Array]
|
80
85
|
def process_params(params, prefix = nil, pieces = nil, &block)
|
81
86
|
params.inject(pieces || []) do |all, (key, value)|
|
82
|
-
|
87
|
+
if prefix
|
88
|
+
key = @options[:flat_encode] ? prefix.to_s : "#{prefix}[#{key}]"
|
89
|
+
end
|
83
90
|
|
84
91
|
case value
|
85
92
|
when Array
|
@@ -21,7 +21,7 @@ module Faraday
|
|
21
21
|
# end
|
22
22
|
#
|
23
23
|
# This example will result in a first interval that is random between 0.05
|
24
|
-
# and 0.075 and a second interval that is random between 0.1 and 0.
|
24
|
+
# and 0.075 and a second interval that is random between 0.1 and 0.125.
|
25
25
|
class Retry < Faraday::Middleware
|
26
26
|
DEFAULT_EXCEPTIONS = [
|
27
27
|
Errno::ETIMEDOUT, 'Timeout::Error',
|
@@ -112,7 +112,7 @@ module Faraday
|
|
112
112
|
# not independent of the retry count. This would be useful
|
113
113
|
# if the exception produced is non-recoverable or if the
|
114
114
|
# the HTTP method called is not idempotent.
|
115
|
-
# @option options [Block] :retry_block block that is executed
|
115
|
+
# @option options [Block] :retry_block block that is executed before
|
116
116
|
# every retry. Request environment, middleware options, current number
|
117
117
|
# of retries and the exception is passed to the block as parameters.
|
118
118
|
# @option options [Array] :retry_statuses Array of Integer HTTP status
|
data/lib/faraday/response.rb
CHANGED
@@ -7,12 +7,6 @@ module Faraday
|
|
7
7
|
class Response
|
8
8
|
# Used for simple response middleware.
|
9
9
|
class Middleware < Faraday::Middleware
|
10
|
-
def call(env)
|
11
|
-
@app.call(env).on_complete do |environment|
|
12
|
-
on_complete(environment)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
10
|
# Override this to modify the environment after the response has finished.
|
17
11
|
# Calls the `parse` method if defined
|
18
12
|
# `parse` method can be defined as private, public and protected
|
@@ -38,7 +38,18 @@ module Faraday
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def response_values(env)
|
41
|
-
{
|
41
|
+
{
|
42
|
+
status: env.status,
|
43
|
+
headers: env.response_headers,
|
44
|
+
body: env.body,
|
45
|
+
request: {
|
46
|
+
method: env.method,
|
47
|
+
url_path: env.url.path,
|
48
|
+
params: env.params,
|
49
|
+
headers: env.request_headers,
|
50
|
+
body: env.request_body
|
51
|
+
}
|
52
|
+
}
|
42
53
|
end
|
43
54
|
end
|
44
55
|
end
|
data/lib/faraday/utils.rb
CHANGED
@@ -28,7 +28,7 @@ module Faraday
|
|
28
28
|
|
29
29
|
def escape(str)
|
30
30
|
str.to_s.gsub(ESCAPE_RE) do |match|
|
31
|
-
|
31
|
+
"%#{match.unpack('H2' * match.bytesize).join('%').upcase}"
|
32
32
|
end.gsub(' ', default_space_encoding)
|
33
33
|
end
|
34
34
|
|
@@ -89,7 +89,7 @@ module Faraday
|
|
89
89
|
# the path with the query string sorted.
|
90
90
|
def normalize_path(url)
|
91
91
|
url = URI(url)
|
92
|
-
(url.path.start_with?('/') ? url.path :
|
92
|
+
(url.path.start_with?('/') ? url.path : "/#{url.path}") +
|
93
93
|
(url.query ? "?#{sort_query_params(url.query)}" : '')
|
94
94
|
end
|
95
95
|
|
@@ -105,7 +105,7 @@ module Faraday
|
|
105
105
|
end
|
106
106
|
|
107
107
|
def to_hash
|
108
|
-
|
108
|
+
{}.update(self)
|
109
109
|
end
|
110
110
|
|
111
111
|
def parse(header_string)
|
@@ -114,7 +114,7 @@ module Faraday
|
|
114
114
|
headers = header_string.split(/\r\n/)
|
115
115
|
|
116
116
|
# Find the last set of response headers.
|
117
|
-
start_index = headers.rindex { |x| x.
|
117
|
+
start_index = headers.rindex { |x| x.start_with?('HTTP/') } || 0
|
118
118
|
last_response = headers.slice(start_index, headers.size)
|
119
119
|
|
120
120
|
last_response
|
@@ -0,0 +1,260 @@
|
|
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
|
+
end
|