faraday 2.7.10 → 2.14.1
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 +1 -1
- data/README.md +22 -11
- data/Rakefile +3 -0
- data/lib/faraday/adapter/test.rb +1 -1
- data/lib/faraday/adapter.rb +1 -1
- data/lib/faraday/connection.rb +17 -7
- data/lib/faraday/encoders/flat_params_encoder.rb +2 -2
- data/lib/faraday/encoders/nested_params_encoder.rb +1 -1
- data/lib/faraday/error.rb +50 -5
- data/lib/faraday/logging/formatter.rb +8 -8
- data/lib/faraday/middleware.rb +40 -1
- data/lib/faraday/options/env.rb +2 -2
- data/lib/faraday/options/proxy_options.rb +4 -2
- data/lib/faraday/options/ssl_options.rb +8 -2
- data/lib/faraday/options.rb +3 -3
- data/lib/faraday/rack_builder.rb +21 -25
- data/lib/faraday/request/json.rb +8 -2
- data/lib/faraday/response/json.rb +21 -1
- data/lib/faraday/response/logger.rb +5 -3
- data/lib/faraday/response/raise_error.rb +36 -19
- data/lib/faraday/response.rb +7 -3
- data/lib/faraday/utils/headers.rb +8 -2
- data/lib/faraday/utils.rb +3 -4
- data/lib/faraday/version.rb +1 -1
- data/lib/faraday.rb +2 -1
- data/spec/faraday/connection_spec.rb +35 -2
- data/spec/faraday/error_spec.rb +93 -3
- data/spec/faraday/middleware_spec.rb +143 -0
- data/spec/faraday/options/proxy_options_spec.rb +27 -0
- data/spec/faraday/params_encoders/nested_spec.rb +2 -1
- data/spec/faraday/request/json_spec.rb +64 -0
- data/spec/faraday/response/json_spec.rb +89 -0
- data/spec/faraday/response/logger_spec.rb +45 -4
- data/spec/faraday/response/raise_error_spec.rb +115 -13
- data/spec/faraday/response_spec.rb +7 -0
- data/spec/faraday/utils/headers_spec.rb +9 -0
- data/spec/faraday/utils_spec.rb +3 -1
- data/spec/faraday_spec.rb +10 -4
- data/spec/spec_helper.rb +6 -5
- data/spec/support/faraday_middleware_subclasses.rb +18 -0
- metadata +26 -14
|
@@ -114,4 +114,93 @@ RSpec.describe Faraday::Response::Json, type: :response do
|
|
|
114
114
|
expect(response.body).to eq(result)
|
|
115
115
|
end
|
|
116
116
|
end
|
|
117
|
+
|
|
118
|
+
context 'with decoder' do
|
|
119
|
+
let(:decoder) do
|
|
120
|
+
double('Decoder').tap do |e|
|
|
121
|
+
allow(e).to receive(:load) { |s, opts| JSON.parse(s, opts) }
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
let(:body) { '{"a": 1}' }
|
|
126
|
+
let(:result) { { a: 1 } }
|
|
127
|
+
|
|
128
|
+
context 'when decoder is passed as object' do
|
|
129
|
+
let(:options) do
|
|
130
|
+
{
|
|
131
|
+
parser_options: {
|
|
132
|
+
decoder: decoder,
|
|
133
|
+
option: :option_value,
|
|
134
|
+
symbolize_names: true
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
it 'passes relevant options to specified decoder\'s load method' do
|
|
140
|
+
expect(decoder).to receive(:load)
|
|
141
|
+
.with(body, { option: :option_value, symbolize_names: true })
|
|
142
|
+
.and_return(result)
|
|
143
|
+
|
|
144
|
+
response = process(body)
|
|
145
|
+
expect(response.body).to eq(result)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
context 'when decoder is passed as an object-method pair' do
|
|
150
|
+
let(:options) do
|
|
151
|
+
{
|
|
152
|
+
parser_options: {
|
|
153
|
+
decoder: [decoder, :load],
|
|
154
|
+
option: :option_value,
|
|
155
|
+
symbolize_names: true
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
it 'passes relevant options to specified decoder\'s method' do
|
|
161
|
+
expect(decoder).to receive(:load)
|
|
162
|
+
.with(body, { option: :option_value, symbolize_names: true })
|
|
163
|
+
.and_return(result)
|
|
164
|
+
|
|
165
|
+
response = process(body)
|
|
166
|
+
expect(response.body).to eq(result)
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
context 'when decoder is not passed' do
|
|
171
|
+
let(:options) do
|
|
172
|
+
{
|
|
173
|
+
parser_options: {
|
|
174
|
+
symbolize_names: true
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
it 'passes relevant options to JSON parse' do
|
|
180
|
+
expect(JSON).to receive(:parse)
|
|
181
|
+
.with(body, { symbolize_names: true })
|
|
182
|
+
.and_return(result)
|
|
183
|
+
|
|
184
|
+
response = process(body)
|
|
185
|
+
expect(response.body).to eq(result)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
it 'passes relevant options to JSON parse even when nil responds to :load' do
|
|
189
|
+
original_allow_message_expectations_on_nil = RSpec::Mocks.configuration.allow_message_expectations_on_nil
|
|
190
|
+
RSpec::Mocks.configuration.allow_message_expectations_on_nil = true
|
|
191
|
+
allow(nil).to receive(:respond_to?)
|
|
192
|
+
.with(:load)
|
|
193
|
+
.and_return(true)
|
|
194
|
+
|
|
195
|
+
expect(JSON).to receive(:parse)
|
|
196
|
+
.with(body, { symbolize_names: true })
|
|
197
|
+
.and_return(result)
|
|
198
|
+
|
|
199
|
+
response = process(body)
|
|
200
|
+
expect(response.body).to eq(result)
|
|
201
|
+
ensure
|
|
202
|
+
RSpec::Mocks.configuration.allow_message_expectations_on_nil = original_allow_message_expectations_on_nil
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
117
206
|
end
|
|
@@ -21,6 +21,7 @@ RSpec.describe Faraday::Response::Logger do
|
|
|
21
21
|
stubs.post('/ohai') { [200, { 'Content-Type' => 'text/html' }, 'fred'] }
|
|
22
22
|
stubs.post('/ohyes') { [200, { 'Content-Type' => 'text/html' }, 'pebbles'] }
|
|
23
23
|
stubs.get('/rubbles') { [200, { 'Content-Type' => 'application/json' }, rubbles] }
|
|
24
|
+
stubs.get('/8bit') { [200, { 'Content-Type' => 'text/html' }, (+'café!').force_encoding(Encoding::ASCII_8BIT)] }
|
|
24
25
|
stubs.get('/filtered_body') { [200, { 'Content-Type' => 'text/html' }, 'soylent green is people'] }
|
|
25
26
|
stubs.get('/filtered_headers') { [200, { 'Content-Type' => 'text/html' }, 'headers response'] }
|
|
26
27
|
stubs.get('/filtered_params') { [200, { 'Content-Type' => 'text/html' }, 'params response'] }
|
|
@@ -55,6 +56,26 @@ RSpec.describe Faraday::Response::Logger do
|
|
|
55
56
|
end
|
|
56
57
|
end
|
|
57
58
|
|
|
59
|
+
context 'when logger with program name' do
|
|
60
|
+
let(:logger) { Logger.new(string_io, progname: 'my_best_program') }
|
|
61
|
+
|
|
62
|
+
it 'logs with program name' do
|
|
63
|
+
conn.get '/hello'
|
|
64
|
+
|
|
65
|
+
expect(string_io.string).to match('-- my_best_program: request:')
|
|
66
|
+
expect(string_io.string).to match('-- my_best_program: response:')
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
context 'when logger without program name' do
|
|
71
|
+
it 'logs without program name' do
|
|
72
|
+
conn.get '/hello'
|
|
73
|
+
|
|
74
|
+
expect(string_io.string).to match('-- : request:')
|
|
75
|
+
expect(string_io.string).to match('-- : response:')
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
58
79
|
context 'with default formatter' do
|
|
59
80
|
let(:formatter) { instance_double(Faraday::Logging::Formatter, request: true, response: true, filter: []) }
|
|
60
81
|
|
|
@@ -169,7 +190,7 @@ RSpec.describe Faraday::Response::Logger do
|
|
|
169
190
|
context 'when logging request body' do
|
|
170
191
|
let(:logger_options) { { bodies: { request: true } } }
|
|
171
192
|
|
|
172
|
-
it '
|
|
193
|
+
it 'logs only request body' do
|
|
173
194
|
conn.post '/ohyes', 'name=Tamago', accept: 'text/html'
|
|
174
195
|
expect(string_io.string).to match(%(name=Tamago))
|
|
175
196
|
expect(string_io.string).not_to match(%(pebbles))
|
|
@@ -179,7 +200,7 @@ RSpec.describe Faraday::Response::Logger do
|
|
|
179
200
|
context 'when logging response body' do
|
|
180
201
|
let(:logger_options) { { bodies: { response: true } } }
|
|
181
202
|
|
|
182
|
-
it '
|
|
203
|
+
it 'logs only response body' do
|
|
183
204
|
conn.post '/ohyes', 'name=Hamachi', accept: 'text/html'
|
|
184
205
|
expect(string_io.string).to match(%(pebbles))
|
|
185
206
|
expect(string_io.string).not_to match(%(name=Hamachi))
|
|
@@ -189,13 +210,13 @@ RSpec.describe Faraday::Response::Logger do
|
|
|
189
210
|
context 'when logging request and response bodies' do
|
|
190
211
|
let(:logger_options) { { bodies: true } }
|
|
191
212
|
|
|
192
|
-
it '
|
|
213
|
+
it 'logs request and response body' do
|
|
193
214
|
conn.post '/ohyes', 'name=Ebi', accept: 'text/html'
|
|
194
215
|
expect(string_io.string).to match(%(name=Ebi))
|
|
195
216
|
expect(string_io.string).to match(%(pebbles))
|
|
196
217
|
end
|
|
197
218
|
|
|
198
|
-
it '
|
|
219
|
+
it 'logs response body object' do
|
|
199
220
|
conn.get '/rubbles', nil, accept: 'text/html'
|
|
200
221
|
expect(string_io.string).to match(%([\"Barney\", \"Betty\", \"Bam Bam\"]\n))
|
|
201
222
|
end
|
|
@@ -208,6 +229,26 @@ RSpec.describe Faraday::Response::Logger do
|
|
|
208
229
|
end
|
|
209
230
|
end
|
|
210
231
|
|
|
232
|
+
context 'when bodies are logged by default' do
|
|
233
|
+
before do
|
|
234
|
+
described_class.default_options = { bodies: true }
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
it 'logs response body' do
|
|
238
|
+
conn.post '/ohai'
|
|
239
|
+
expect(string_io.string).to match(%(fred))
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
it 'converts to UTF-8' do
|
|
243
|
+
conn.get '/8bit'
|
|
244
|
+
expect(string_io.string).to match(%(caf��!))
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
after do
|
|
248
|
+
described_class.default_options = { bodies: false }
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
|
|
211
252
|
context 'when logging errors' do
|
|
212
253
|
let(:logger_options) { { errors: true } }
|
|
213
254
|
|
|
@@ -13,7 +13,8 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
13
13
|
stub.get('proxy-error') { [407, { 'X-Reason' => 'because' }, 'keep looking'] }
|
|
14
14
|
stub.get('request-timeout') { [408, { 'X-Reason' => 'because' }, 'keep looking'] }
|
|
15
15
|
stub.get('conflict') { [409, { 'X-Reason' => 'because' }, 'keep looking'] }
|
|
16
|
-
stub.get('unprocessable-
|
|
16
|
+
stub.get('unprocessable-content') { [422, { 'X-Reason' => 'because' }, 'keep looking'] }
|
|
17
|
+
stub.get('too-many-requests') { [429, { 'X-Reason' => 'because' }, 'keep looking'] }
|
|
17
18
|
stub.get('4xx') { [499, { 'X-Reason' => 'because' }, 'keep looking'] }
|
|
18
19
|
stub.get('nil-status') { [nil, { 'X-Reason' => 'nil' }, 'fail'] }
|
|
19
20
|
stub.get('server-error') { [500, { 'X-Error' => 'bailout' }, 'fail'] }
|
|
@@ -27,7 +28,7 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
27
28
|
|
|
28
29
|
it 'raises Faraday::BadRequestError for 400 responses' do
|
|
29
30
|
expect { conn.get('bad-request') }.to raise_error(Faraday::BadRequestError) do |ex|
|
|
30
|
-
expect(ex.message).to eq('the server responded with status 400')
|
|
31
|
+
expect(ex.message).to eq('the server responded with status 400 for GET http:/bad-request')
|
|
31
32
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
|
32
33
|
expect(ex.response[:status]).to eq(400)
|
|
33
34
|
expect(ex.response_status).to eq(400)
|
|
@@ -38,7 +39,7 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
38
39
|
|
|
39
40
|
it 'raises Faraday::UnauthorizedError for 401 responses' do
|
|
40
41
|
expect { conn.get('unauthorized') }.to raise_error(Faraday::UnauthorizedError) do |ex|
|
|
41
|
-
expect(ex.message).to eq('the server responded with status 401')
|
|
42
|
+
expect(ex.message).to eq('the server responded with status 401 for GET http:/unauthorized')
|
|
42
43
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
|
43
44
|
expect(ex.response[:status]).to eq(401)
|
|
44
45
|
expect(ex.response_status).to eq(401)
|
|
@@ -49,7 +50,7 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
49
50
|
|
|
50
51
|
it 'raises Faraday::ForbiddenError for 403 responses' do
|
|
51
52
|
expect { conn.get('forbidden') }.to raise_error(Faraday::ForbiddenError) do |ex|
|
|
52
|
-
expect(ex.message).to eq('the server responded with status 403')
|
|
53
|
+
expect(ex.message).to eq('the server responded with status 403 for GET http:/forbidden')
|
|
53
54
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
|
54
55
|
expect(ex.response[:status]).to eq(403)
|
|
55
56
|
expect(ex.response_status).to eq(403)
|
|
@@ -60,7 +61,7 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
60
61
|
|
|
61
62
|
it 'raises Faraday::ResourceNotFound for 404 responses' do
|
|
62
63
|
expect { conn.get('not-found') }.to raise_error(Faraday::ResourceNotFound) do |ex|
|
|
63
|
-
expect(ex.message).to eq('the server responded with status 404')
|
|
64
|
+
expect(ex.message).to eq('the server responded with status 404 for GET http:/not-found')
|
|
64
65
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
|
65
66
|
expect(ex.response[:status]).to eq(404)
|
|
66
67
|
expect(ex.response_status).to eq(404)
|
|
@@ -82,7 +83,7 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
82
83
|
|
|
83
84
|
it 'raises Faraday::RequestTimeoutError for 408 responses' do
|
|
84
85
|
expect { conn.get('request-timeout') }.to raise_error(Faraday::RequestTimeoutError) do |ex|
|
|
85
|
-
expect(ex.message).to eq('the server responded with status 408')
|
|
86
|
+
expect(ex.message).to eq('the server responded with status 408 for GET http:/request-timeout')
|
|
86
87
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
|
87
88
|
expect(ex.response[:status]).to eq(408)
|
|
88
89
|
expect(ex.response_status).to eq(408)
|
|
@@ -93,7 +94,7 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
93
94
|
|
|
94
95
|
it 'raises Faraday::ConflictError for 409 responses' do
|
|
95
96
|
expect { conn.get('conflict') }.to raise_error(Faraday::ConflictError) do |ex|
|
|
96
|
-
expect(ex.message).to eq('the server responded with status 409')
|
|
97
|
+
expect(ex.message).to eq('the server responded with status 409 for GET http:/conflict')
|
|
97
98
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
|
98
99
|
expect(ex.response[:status]).to eq(409)
|
|
99
100
|
expect(ex.response_status).to eq(409)
|
|
@@ -102,9 +103,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
102
103
|
end
|
|
103
104
|
end
|
|
104
105
|
|
|
105
|
-
it 'raises Faraday::UnprocessableEntityError for 422 responses' do
|
|
106
|
-
expect { conn.get('unprocessable-
|
|
107
|
-
expect(ex.message).to eq('the server responded with status 422')
|
|
106
|
+
it 'raises legacy Faraday::UnprocessableEntityError for 422 responses' do
|
|
107
|
+
expect { conn.get('unprocessable-content') }.to raise_error(Faraday::UnprocessableEntityError) do |ex|
|
|
108
|
+
expect(ex.message).to eq('the server responded with status 422 for GET http:/unprocessable-content')
|
|
108
109
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
|
109
110
|
expect(ex.response[:status]).to eq(422)
|
|
110
111
|
expect(ex.response_status).to eq(422)
|
|
@@ -113,6 +114,28 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
113
114
|
end
|
|
114
115
|
end
|
|
115
116
|
|
|
117
|
+
it 'raises Faraday::UnprocessableContentError for 422 responses' do
|
|
118
|
+
expect { conn.get('unprocessable-content') }.to raise_error(Faraday::UnprocessableContentError) do |ex|
|
|
119
|
+
expect(ex.message).to eq('the server responded with status 422 for GET http:/unprocessable-content')
|
|
120
|
+
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
|
121
|
+
expect(ex.response[:status]).to eq(422)
|
|
122
|
+
expect(ex.response_status).to eq(422)
|
|
123
|
+
expect(ex.response_body).to eq('keep looking')
|
|
124
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
it 'raises Faraday::TooManyRequestsError for 429 responses' do
|
|
129
|
+
expect { conn.get('too-many-requests') }.to raise_error(Faraday::TooManyRequestsError) do |ex|
|
|
130
|
+
expect(ex.message).to eq('the server responded with status 429 for GET http:/too-many-requests')
|
|
131
|
+
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
|
132
|
+
expect(ex.response[:status]).to eq(429)
|
|
133
|
+
expect(ex.response_status).to eq(429)
|
|
134
|
+
expect(ex.response_body).to eq('keep looking')
|
|
135
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
116
139
|
it 'raises Faraday::NilStatusError for nil status in response' do
|
|
117
140
|
expect { conn.get('nil-status') }.to raise_error(Faraday::NilStatusError) do |ex|
|
|
118
141
|
expect(ex.message).to eq('http status could not be derived from the server response')
|
|
@@ -126,7 +149,7 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
126
149
|
|
|
127
150
|
it 'raises Faraday::ClientError for other 4xx responses' do
|
|
128
151
|
expect { conn.get('4xx') }.to raise_error(Faraday::ClientError) do |ex|
|
|
129
|
-
expect(ex.message).to eq('the server responded with status 499')
|
|
152
|
+
expect(ex.message).to eq('the server responded with status 499 for GET http:/4xx')
|
|
130
153
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
|
131
154
|
expect(ex.response[:status]).to eq(499)
|
|
132
155
|
expect(ex.response_status).to eq(499)
|
|
@@ -137,7 +160,7 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
137
160
|
|
|
138
161
|
it 'raises Faraday::ServerError for 500 responses' do
|
|
139
162
|
expect { conn.get('server-error') }.to raise_error(Faraday::ServerError) do |ex|
|
|
140
|
-
expect(ex.message).to eq('the server responded with status 500')
|
|
163
|
+
expect(ex.message).to eq('the server responded with status 500 for GET http:/server-error')
|
|
141
164
|
expect(ex.response[:headers]['X-Error']).to eq('bailout')
|
|
142
165
|
expect(ex.response[:status]).to eq(500)
|
|
143
166
|
expect(ex.response_status).to eq(500)
|
|
@@ -149,7 +172,7 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
149
172
|
describe 'request info' do
|
|
150
173
|
let(:conn) do
|
|
151
174
|
Faraday.new do |b|
|
|
152
|
-
b.response :raise_error
|
|
175
|
+
b.response :raise_error, **middleware_options
|
|
153
176
|
b.adapter :test do |stub|
|
|
154
177
|
stub.post(url, request_body, request_headers) do
|
|
155
178
|
[400, { 'X-Reason' => 'because' }, 'keep looking']
|
|
@@ -157,6 +180,7 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
157
180
|
end
|
|
158
181
|
end
|
|
159
182
|
end
|
|
183
|
+
let(:middleware_options) { {} }
|
|
160
184
|
let(:request_body) { JSON.generate({ 'item' => 'sth' }) }
|
|
161
185
|
let(:request_headers) { { 'Authorization' => 'Basic 123' } }
|
|
162
186
|
let(:url_path) { 'request' }
|
|
@@ -180,5 +204,83 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
|
180
204
|
expect(ex.response[:request][:body]).to eq(request_body)
|
|
181
205
|
end
|
|
182
206
|
end
|
|
207
|
+
|
|
208
|
+
describe 'DEFAULT_OPTION: include_request' do
|
|
209
|
+
before(:each) do
|
|
210
|
+
Faraday::Response::RaiseError.instance_variable_set(:@default_options, nil)
|
|
211
|
+
Faraday::Middleware.instance_variable_set(:@default_options, nil)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
after(:all) do
|
|
215
|
+
Faraday::Response::RaiseError.instance_variable_set(:@default_options, nil)
|
|
216
|
+
Faraday::Middleware.instance_variable_set(:@default_options, nil)
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
context 'when RaiseError DEFAULT_OPTION (include_request: true) is used' do
|
|
220
|
+
it 'includes request info in the exception' do
|
|
221
|
+
expect { perform_request }.to raise_error(Faraday::BadRequestError) do |ex|
|
|
222
|
+
expect(ex.response.keys).to contain_exactly(
|
|
223
|
+
:status,
|
|
224
|
+
:headers,
|
|
225
|
+
:body,
|
|
226
|
+
:request
|
|
227
|
+
)
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
context 'when application sets default_options `include_request: false`' do
|
|
233
|
+
before(:each) do
|
|
234
|
+
Faraday::Response::RaiseError.default_options = { include_request: false }
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
context 'and when include_request option is omitted' do
|
|
238
|
+
it 'does not include request info in the exception' do
|
|
239
|
+
expect { perform_request }.to raise_error(Faraday::BadRequestError) do |ex|
|
|
240
|
+
expect(ex.response.keys).to contain_exactly(
|
|
241
|
+
:status,
|
|
242
|
+
:headers,
|
|
243
|
+
:body
|
|
244
|
+
)
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
context 'and when include_request option is explicitly set for instance' do
|
|
250
|
+
let(:middleware_options) { { include_request: true } }
|
|
251
|
+
|
|
252
|
+
it 'includes request info in the exception' do
|
|
253
|
+
expect { perform_request }.to raise_error(Faraday::BadRequestError) do |ex|
|
|
254
|
+
expect(ex.response.keys).to contain_exactly(
|
|
255
|
+
:status,
|
|
256
|
+
:headers,
|
|
257
|
+
:body,
|
|
258
|
+
:request
|
|
259
|
+
)
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
describe 'allowing certain status codes' do
|
|
268
|
+
let(:conn) do
|
|
269
|
+
Faraday.new do |b|
|
|
270
|
+
b.response :raise_error, allowed_statuses: [404]
|
|
271
|
+
b.adapter :test do |stub|
|
|
272
|
+
stub.get('bad-request') { [400, { 'X-Reason' => 'because' }, 'keep looking'] }
|
|
273
|
+
stub.get('not-found') { [404, { 'X-Reason' => 'because' }, 'keep looking'] }
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
it 'raises an error for status codes that are not explicitly allowed' do
|
|
279
|
+
expect { conn.get('bad-request') }.to raise_error(Faraday::BadRequestError)
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
it 'does not raise an error for allowed status codes' do
|
|
283
|
+
expect { conn.get('not-found') }.not_to raise_error
|
|
284
|
+
end
|
|
183
285
|
end
|
|
184
286
|
end
|
|
@@ -13,6 +13,7 @@ RSpec.describe Faraday::Response do
|
|
|
13
13
|
it { expect(subject.success?).to be_falsey }
|
|
14
14
|
it { expect(subject.status).to eq(404) }
|
|
15
15
|
it { expect(subject.body).to eq('yikes') }
|
|
16
|
+
it { expect(subject.url).to eq(URI('https://lostisland.github.io/faraday')) }
|
|
16
17
|
it { expect(subject.headers['Content-Type']).to eq('text/plain') }
|
|
17
18
|
it { expect(subject['content-type']).to eq('text/plain') }
|
|
18
19
|
|
|
@@ -31,6 +32,12 @@ RSpec.describe Faraday::Response do
|
|
|
31
32
|
it { expect(hash[:response_headers]).to eq(subject.headers) }
|
|
32
33
|
it { expect(hash[:body]).to eq(subject.body) }
|
|
33
34
|
it { expect(hash[:url]).to eq(subject.env.url) }
|
|
35
|
+
|
|
36
|
+
context 'when response is not finished' do
|
|
37
|
+
subject { Faraday::Response.new.to_hash }
|
|
38
|
+
|
|
39
|
+
it { is_expected.to eq({ status: nil, body: nil, response_headers: {}, url: nil }) }
|
|
40
|
+
end
|
|
34
41
|
end
|
|
35
42
|
|
|
36
43
|
describe 'marshal serialization support' do
|
|
@@ -56,6 +56,15 @@ RSpec.describe Faraday::Utils::Headers do
|
|
|
56
56
|
it { expect(subject.delete('content-type')).to be_nil }
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
+
describe '#dig' do
|
|
60
|
+
before { subject['Content-Type'] = 'application/json' }
|
|
61
|
+
|
|
62
|
+
it { expect(subject&.dig('Content-Type')).to eq('application/json') }
|
|
63
|
+
it { expect(subject&.dig('CONTENT-TYPE')).to eq('application/json') }
|
|
64
|
+
it { expect(subject&.dig(:content_type)).to eq('application/json') }
|
|
65
|
+
it { expect(subject&.dig('invalid')).to be_nil }
|
|
66
|
+
end
|
|
67
|
+
|
|
59
68
|
describe '#parse' do
|
|
60
69
|
context 'when response headers leave http status line out' do
|
|
61
70
|
let(:headers) { "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" }
|
data/spec/faraday/utils_spec.rb
CHANGED
data/spec/faraday_spec.rb
CHANGED
|
@@ -18,10 +18,16 @@ RSpec.describe Faraday do
|
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
it 'uses method_missing on Faraday if there is no proxyable method' do
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
expected_message =
|
|
22
|
+
if RUBY_VERSION >= '3.4'
|
|
23
|
+
"undefined method 'this_method_does_not_exist' for module Faraday"
|
|
24
|
+
elsif RUBY_VERSION >= '3.3'
|
|
25
|
+
"undefined method `this_method_does_not_exist' for module Faraday"
|
|
26
|
+
else
|
|
27
|
+
"undefined method `this_method_does_not_exist' for Faraday:Module"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
expect { Faraday.this_method_does_not_exist }.to raise_error(NoMethodError, expected_message)
|
|
25
31
|
end
|
|
26
32
|
|
|
27
33
|
it 'proxied methods can be accessed' do
|
data/spec/spec_helper.rb
CHANGED
|
@@ -29,14 +29,15 @@ SimpleCov.start do
|
|
|
29
29
|
minimum_coverage_by_file 26
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
# Ensure all /lib files are loaded
|
|
33
|
-
# so they will be included in the test coverage report.
|
|
34
|
-
Dir['./lib/**/*.rb'].sort.each { |file| require file }
|
|
35
|
-
|
|
36
32
|
require 'faraday'
|
|
37
33
|
require 'pry'
|
|
38
34
|
|
|
39
|
-
|
|
35
|
+
# Ensure all /lib files are loaded
|
|
36
|
+
# so they will be included in the test coverage report.
|
|
37
|
+
Dir['./lib/**/*.rb'].each { |file| require file }
|
|
38
|
+
|
|
39
|
+
# Load all Rspec support files
|
|
40
|
+
Dir['./spec/support/**/*.rb'].each { |file| require file }
|
|
40
41
|
|
|
41
42
|
RSpec.configure do |config|
|
|
42
43
|
# rspec-expectations config goes here. You can use an alternate
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FaradayMiddlewareSubclasses
|
|
4
|
+
class SubclassNoOptions < Faraday::Middleware
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
class SubclassOneOption < Faraday::Middleware
|
|
8
|
+
DEFAULT_OPTIONS = { some_other_option: false }.freeze
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class SubclassTwoOptions < Faraday::Middleware
|
|
12
|
+
DEFAULT_OPTIONS = { some_option: true, some_other_option: false }.freeze
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
Faraday::Response.register_middleware(no_options: FaradayMiddlewareSubclasses::SubclassNoOptions)
|
|
17
|
+
Faraday::Response.register_middleware(one_option: FaradayMiddlewareSubclasses::SubclassOneOption)
|
|
18
|
+
Faraday::Response.register_middleware(two_options: FaradayMiddlewareSubclasses::SubclassTwoOptions)
|
metadata
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: faraday
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.14.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- "@technoweenie"
|
|
8
8
|
- "@iMacTia"
|
|
9
9
|
- "@olleolleolle"
|
|
10
|
-
autorequire:
|
|
11
10
|
bindir: bin
|
|
12
11
|
cert_chain: []
|
|
13
|
-
date:
|
|
12
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
14
13
|
dependencies:
|
|
15
14
|
- !ruby/object:Gem::Dependency
|
|
16
15
|
name: faraday-net_http
|
|
@@ -21,7 +20,7 @@ dependencies:
|
|
|
21
20
|
version: '2.0'
|
|
22
21
|
- - "<"
|
|
23
22
|
- !ruby/object:Gem::Version
|
|
24
|
-
version: '3.
|
|
23
|
+
version: '3.5'
|
|
25
24
|
type: :runtime
|
|
26
25
|
prerelease: false
|
|
27
26
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -31,22 +30,35 @@ dependencies:
|
|
|
31
30
|
version: '2.0'
|
|
32
31
|
- - "<"
|
|
33
32
|
- !ruby/object:Gem::Version
|
|
34
|
-
version: '3.
|
|
33
|
+
version: '3.5'
|
|
35
34
|
- !ruby/object:Gem::Dependency
|
|
36
|
-
name:
|
|
35
|
+
name: json
|
|
37
36
|
requirement: !ruby/object:Gem::Requirement
|
|
38
37
|
requirements:
|
|
39
38
|
- - ">="
|
|
40
39
|
- !ruby/object:Gem::Version
|
|
41
|
-
version: 0
|
|
40
|
+
version: '0'
|
|
42
41
|
type: :runtime
|
|
43
42
|
prerelease: false
|
|
44
43
|
version_requirements: !ruby/object:Gem::Requirement
|
|
45
44
|
requirements:
|
|
46
45
|
- - ">="
|
|
47
46
|
- !ruby/object:Gem::Version
|
|
48
|
-
version: 0
|
|
49
|
-
|
|
47
|
+
version: '0'
|
|
48
|
+
- !ruby/object:Gem::Dependency
|
|
49
|
+
name: logger
|
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
type: :runtime
|
|
56
|
+
prerelease: false
|
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
50
62
|
email: technoweenie@gmail.com
|
|
51
63
|
executables: []
|
|
52
64
|
extensions: []
|
|
@@ -121,6 +133,7 @@ files:
|
|
|
121
133
|
- spec/spec_helper.rb
|
|
122
134
|
- spec/support/disabling_stub.rb
|
|
123
135
|
- spec/support/fake_safe_buffer.rb
|
|
136
|
+
- spec/support/faraday_middleware_subclasses.rb
|
|
124
137
|
- spec/support/helper_methods.rb
|
|
125
138
|
- spec/support/shared_examples/adapter.rb
|
|
126
139
|
- spec/support/shared_examples/params_encoder.rb
|
|
@@ -131,10 +144,10 @@ licenses:
|
|
|
131
144
|
- MIT
|
|
132
145
|
metadata:
|
|
133
146
|
homepage_uri: https://lostisland.github.io/faraday
|
|
134
|
-
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.
|
|
147
|
+
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.14.1
|
|
135
148
|
source_code_uri: https://github.com/lostisland/faraday
|
|
136
149
|
bug_tracker_uri: https://github.com/lostisland/faraday/issues
|
|
137
|
-
|
|
150
|
+
rubygems_mfa_required: 'true'
|
|
138
151
|
rdoc_options: []
|
|
139
152
|
require_paths:
|
|
140
153
|
- lib
|
|
@@ -143,15 +156,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
143
156
|
requirements:
|
|
144
157
|
- - ">="
|
|
145
158
|
- !ruby/object:Gem::Version
|
|
146
|
-
version: '
|
|
159
|
+
version: '3.0'
|
|
147
160
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
161
|
requirements:
|
|
149
162
|
- - ">="
|
|
150
163
|
- !ruby/object:Gem::Version
|
|
151
164
|
version: '0'
|
|
152
165
|
requirements: []
|
|
153
|
-
rubygems_version: 3.
|
|
154
|
-
signing_key:
|
|
166
|
+
rubygems_version: 3.6.9
|
|
155
167
|
specification_version: 4
|
|
156
168
|
summary: HTTP/REST API client library.
|
|
157
169
|
test_files: []
|