faraday 2.5.2 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/faraday/connection.rb +26 -26
- data/lib/faraday/logging/formatter.rb +17 -3
- data/lib/faraday/middleware.rb +3 -0
- data/lib/faraday/request/authorization.rb +8 -3
- data/lib/faraday/request.rb +1 -1
- data/lib/faraday/response/logger.rb +4 -0
- data/lib/faraday/utils/headers.rb +6 -1
- data/lib/faraday/version.rb +1 -1
- data/spec/faraday/middleware_spec.rb +18 -0
- data/spec/faraday/request/authorization_spec.rb +35 -0
- data/spec/faraday/response/logger_spec.rb +28 -0
- data/spec/faraday/utils/headers_spec.rb +20 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aded1d146b09e5b537b81833177fcd70acbed297598160e1114807fe047c15c2
|
4
|
+
data.tar.gz: 3b4c82d7712d3667767da96b87050d0cdb76bc0d0d2ed3f67d7f7ccb595f474e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e37eda4374f7c99ea8e687a6c704fcc74935800c4209c5b94767737e2133dc343dcfaa49deda11ddd312fe6f8e8b661a765134b402d3aa787672ffc84026547
|
7
|
+
data.tar.gz: 8ce5708af5954af94fdb2c1aff6a4cefcf7ed512d2f91d05c7ac3cdfc72fcad2840e9674c5defb8aaed570e5c9e5f2324911fb22e4b05184b2597a87959f2853
|
data/lib/faraday/connection.rb
CHANGED
@@ -130,10 +130,10 @@ module Faraday
|
|
130
130
|
# Makes a GET HTTP request without a body.
|
131
131
|
# @!scope class
|
132
132
|
#
|
133
|
-
# @param url [String] The optional String base URL to use as a prefix for
|
133
|
+
# @param url [String, URI, nil] The optional String base URL to use as a prefix for
|
134
134
|
# all requests. Can also be the options Hash.
|
135
|
-
# @param params [Hash] Hash of URI query unencoded key/value pairs.
|
136
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
135
|
+
# @param params [Hash, nil] Hash of URI query unencoded key/value pairs.
|
136
|
+
# @param headers [Hash, nil] unencoded HTTP header key/value pairs.
|
137
137
|
#
|
138
138
|
# @example
|
139
139
|
# conn.get '/items', { page: 1 }, :accept => 'application/json'
|
@@ -152,10 +152,10 @@ module Faraday
|
|
152
152
|
# Makes a HEAD HTTP request without a body.
|
153
153
|
# @!scope class
|
154
154
|
#
|
155
|
-
# @param url [String] The optional String base URL to use as a prefix for
|
155
|
+
# @param url [String, URI, nil] The optional String base URL to use as a prefix for
|
156
156
|
# all requests. Can also be the options Hash.
|
157
|
-
# @param params [Hash] Hash of URI query unencoded key/value pairs.
|
158
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
157
|
+
# @param params [Hash, nil] Hash of URI query unencoded key/value pairs.
|
158
|
+
# @param headers [Hash, nil] unencoded HTTP header key/value pairs.
|
159
159
|
#
|
160
160
|
# @example
|
161
161
|
# conn.head '/items/1'
|
@@ -167,10 +167,10 @@ module Faraday
|
|
167
167
|
# Makes a DELETE HTTP request without a body.
|
168
168
|
# @!scope class
|
169
169
|
#
|
170
|
-
# @param url [String] The optional String base URL to use as a prefix for
|
170
|
+
# @param url [String, URI, nil] The optional String base URL to use as a prefix for
|
171
171
|
# all requests. Can also be the options Hash.
|
172
|
-
# @param params [Hash] Hash of URI query unencoded key/value pairs.
|
173
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
172
|
+
# @param params [Hash, nil] Hash of URI query unencoded key/value pairs.
|
173
|
+
# @param headers [Hash, nil] unencoded HTTP header key/value pairs.
|
174
174
|
#
|
175
175
|
# @example
|
176
176
|
# conn.delete '/items/1'
|
@@ -182,10 +182,10 @@ module Faraday
|
|
182
182
|
# Makes a TRACE HTTP request without a body.
|
183
183
|
# @!scope class
|
184
184
|
#
|
185
|
-
# @param url [String] The optional String base URL to use as a prefix for
|
185
|
+
# @param url [String, URI, nil] The optional String base URL to use as a prefix for
|
186
186
|
# all requests. Can also be the options Hash.
|
187
|
-
# @param params [Hash] Hash of URI query unencoded key/value pairs.
|
188
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
187
|
+
# @param params [Hash, nil] Hash of URI query unencoded key/value pairs.
|
188
|
+
# @param headers [Hash, nil] unencoded HTTP header key/value pairs.
|
189
189
|
#
|
190
190
|
# @example
|
191
191
|
# conn.connect '/items/1'
|
@@ -210,9 +210,9 @@ module Faraday
|
|
210
210
|
#
|
211
211
|
# @overload options(url, params = nil, headers = nil)
|
212
212
|
# Makes an OPTIONS HTTP request to the given URL.
|
213
|
-
# @param url [String] String base URL to sue as a prefix for all requests.
|
214
|
-
# @param params [Hash] Hash of URI query unencoded key/value pairs.
|
215
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
213
|
+
# @param url [String, URI, nil] String base URL to sue as a prefix for all requests.
|
214
|
+
# @param params [Hash, nil] Hash of URI query unencoded key/value pairs.
|
215
|
+
# @param headers [Hash, nil] unencoded HTTP header key/value pairs.
|
216
216
|
#
|
217
217
|
# @example
|
218
218
|
# conn.options '/items/1'
|
@@ -233,10 +233,10 @@ module Faraday
|
|
233
233
|
# Makes a POST HTTP request with a body.
|
234
234
|
# @!scope class
|
235
235
|
#
|
236
|
-
# @param url [String] The optional String base URL to use as a prefix for
|
236
|
+
# @param url [String, URI, nil] The optional String base URL to use as a prefix for
|
237
237
|
# all requests. Can also be the options Hash.
|
238
|
-
# @param body [String] body for the request.
|
239
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
238
|
+
# @param body [String, nil] body for the request.
|
239
|
+
# @param headers [Hash, nil] unencoded HTTP header key/value pairs.
|
240
240
|
#
|
241
241
|
# @example
|
242
242
|
# conn.post '/items', data, content_type: 'application/json'
|
@@ -255,10 +255,10 @@ module Faraday
|
|
255
255
|
# Makes a PUT HTTP request with a body.
|
256
256
|
# @!scope class
|
257
257
|
#
|
258
|
-
# @param url [String] The optional String base URL to use as a prefix for
|
258
|
+
# @param url [String, URI, nil] The optional String base URL to use as a prefix for
|
259
259
|
# all requests. Can also be the options Hash.
|
260
|
-
# @param body [String] body for the request.
|
261
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
260
|
+
# @param body [String, nil] body for the request.
|
261
|
+
# @param headers [Hash, nil] unencoded HTTP header key/value pairs.
|
262
262
|
#
|
263
263
|
# @example
|
264
264
|
# # TODO: Make it a PUT example
|
@@ -390,7 +390,7 @@ module Faraday
|
|
390
390
|
# Takes a relative url for a request and combines it with the defaults
|
391
391
|
# set on the connection instance.
|
392
392
|
#
|
393
|
-
# @param url [String]
|
393
|
+
# @param url [String, URI, nil]
|
394
394
|
# @param extra_params [Hash]
|
395
395
|
#
|
396
396
|
# @example
|
@@ -423,10 +423,10 @@ module Faraday
|
|
423
423
|
# Builds and runs the Faraday::Request.
|
424
424
|
#
|
425
425
|
# @param method [Symbol] HTTP method.
|
426
|
-
# @param url [String, URI] String or URI to access.
|
427
|
-
# @param body [
|
426
|
+
# @param url [String, URI, nil] String or URI to access.
|
427
|
+
# @param body [String, nil] The request body that will eventually be converted to
|
428
428
|
# a string.
|
429
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
429
|
+
# @param headers [Hash, nil] unencoded HTTP header key/value pairs.
|
430
430
|
#
|
431
431
|
# @return [Faraday::Response]
|
432
432
|
def run_request(method, url, body, headers)
|
@@ -462,7 +462,7 @@ module Faraday
|
|
462
462
|
|
463
463
|
# Build an absolute URL based on url_prefix.
|
464
464
|
#
|
465
|
-
# @param url [String, URI]
|
465
|
+
# @param url [String, URI, nil]
|
466
466
|
# @param params [Faraday::Utils::ParamsHash] A Faraday::Utils::ParamsHash to
|
467
467
|
# replace the query values
|
468
468
|
# of the resulting url (default: nil).
|
@@ -1,14 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'pp'
|
4
|
-
|
5
3
|
module Faraday
|
6
4
|
module Logging
|
7
5
|
# Serves as an integration point to customize logging
|
8
6
|
class Formatter
|
9
7
|
extend Forwardable
|
10
8
|
|
11
|
-
DEFAULT_OPTIONS = { headers: true, bodies: false,
|
9
|
+
DEFAULT_OPTIONS = { headers: true, bodies: false, errors: false,
|
12
10
|
log_level: :info }.freeze
|
13
11
|
|
14
12
|
def initialize(logger:, options:)
|
@@ -37,6 +35,18 @@ module Faraday
|
|
37
35
|
log_body('response', env[:body]) if env[:body] && log_body?(:response)
|
38
36
|
end
|
39
37
|
|
38
|
+
def error(error)
|
39
|
+
return unless log_errors?
|
40
|
+
|
41
|
+
error_log = proc { error.full_message }
|
42
|
+
public_send(log_level, 'error', &error_log)
|
43
|
+
|
44
|
+
log_headers('error', error.response_headers) if error.respond_to?(:response_headers) && log_headers?(:error)
|
45
|
+
return unless error.respond_to?(:response_body) && error.response_body && log_body?(:error)
|
46
|
+
|
47
|
+
log_body('error', error.response_body)
|
48
|
+
end
|
49
|
+
|
40
50
|
def filter(filter_word, filter_replacement)
|
41
51
|
@filter.push([filter_word, filter_replacement])
|
42
52
|
end
|
@@ -77,6 +87,10 @@ module Faraday
|
|
77
87
|
end
|
78
88
|
end
|
79
89
|
|
90
|
+
def log_errors?
|
91
|
+
@options[:errors]
|
92
|
+
end
|
93
|
+
|
80
94
|
def apply_filters(output)
|
81
95
|
@filter.each do |pattern, replacement|
|
82
96
|
output = output.to_s.gsub(pattern, replacement)
|
data/lib/faraday/middleware.rb
CHANGED
@@ -23,22 +23,27 @@ module Faraday
|
|
23
23
|
def on_request(env)
|
24
24
|
return if env.request_headers[KEY]
|
25
25
|
|
26
|
-
env.request_headers[KEY] = header_from(@type, *@params)
|
26
|
+
env.request_headers[KEY] = header_from(@type, env, *@params)
|
27
27
|
end
|
28
28
|
|
29
29
|
private
|
30
30
|
|
31
31
|
# @param type [String, Symbol]
|
32
|
+
# @param env [Faraday::Env]
|
32
33
|
# @param params [Array]
|
33
34
|
# @return [String] a header value
|
34
|
-
def header_from(type, *params)
|
35
|
+
def header_from(type, env, *params)
|
35
36
|
if type.to_s.casecmp('basic').zero? && params.size == 2
|
36
37
|
Utils.basic_header_from(*params)
|
37
38
|
elsif params.size != 1
|
38
39
|
raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
|
39
40
|
else
|
40
41
|
value = params.first
|
41
|
-
|
42
|
+
if (value.is_a?(Proc) && value.arity == 1) || (value.respond_to?(:call) && value.method(:call).arity == 1)
|
43
|
+
value = value.call(env)
|
44
|
+
elsif value.is_a?(Proc) || value.respond_to?(:call)
|
45
|
+
value = value.call
|
46
|
+
end
|
42
47
|
"#{type} #{value}"
|
43
48
|
end
|
44
49
|
end
|
data/lib/faraday/request.rb
CHANGED
@@ -132,7 +132,12 @@ module Faraday
|
|
132
132
|
|
133
133
|
# Join multiple values with a comma.
|
134
134
|
def add_parsed(key, value)
|
135
|
-
|
135
|
+
if key?(key)
|
136
|
+
self[key] = self[key].to_s
|
137
|
+
self[key] << ', ' << value
|
138
|
+
else
|
139
|
+
self[key] = value
|
140
|
+
end
|
136
141
|
end
|
137
142
|
end
|
138
143
|
end
|
data/lib/faraday/version.rb
CHANGED
@@ -33,6 +33,24 @@ RSpec.describe Faraday::Middleware do
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
describe '#on_error' do
|
37
|
+
subject do
|
38
|
+
Class.new(described_class) do
|
39
|
+
def on_error(error)
|
40
|
+
# do nothing
|
41
|
+
end
|
42
|
+
end.new(app)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'is called by #call' do
|
46
|
+
expect(app).to receive(:call).and_raise(Faraday::ConnectionFailed)
|
47
|
+
is_expected.to receive(:call).and_call_original
|
48
|
+
is_expected.to receive(:on_error)
|
49
|
+
|
50
|
+
expect { subject.call(double) }.to raise_error(Faraday::ConnectionFailed)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
36
54
|
describe '#close' do
|
37
55
|
context "with app that doesn't support \#close" do
|
38
56
|
it 'should issue warning' do
|
@@ -72,6 +72,41 @@ RSpec.describe Faraday::Request::Authorization do
|
|
72
72
|
include_examples 'does not interfere with existing authentication'
|
73
73
|
end
|
74
74
|
|
75
|
+
context 'with an argument' do
|
76
|
+
let(:response) { conn.get('/auth-echo', nil, 'middle' => 'crunchy surprise') }
|
77
|
+
|
78
|
+
context 'when passed a proc' do
|
79
|
+
let(:auth_config) { [proc { |env| "proc #{env.request_headers['middle']}" }] }
|
80
|
+
|
81
|
+
it { expect(response.body).to eq('Bearer proc crunchy surprise') }
|
82
|
+
|
83
|
+
include_examples 'does not interfere with existing authentication'
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'when passed a lambda' do
|
87
|
+
let(:auth_config) { [->(env) { "lambda #{env.request_headers['middle']}" }] }
|
88
|
+
|
89
|
+
it { expect(response.body).to eq('Bearer lambda crunchy surprise') }
|
90
|
+
|
91
|
+
include_examples 'does not interfere with existing authentication'
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'when passed a callable with an argument' do
|
95
|
+
let(:callable) do
|
96
|
+
Class.new do
|
97
|
+
def call(env)
|
98
|
+
"callable #{env.request_headers['middle']}"
|
99
|
+
end
|
100
|
+
end.new
|
101
|
+
end
|
102
|
+
let(:auth_config) { [callable] }
|
103
|
+
|
104
|
+
it { expect(response.body).to eq('Bearer callable crunchy surprise') }
|
105
|
+
|
106
|
+
include_examples 'does not interfere with existing authentication'
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
75
110
|
context 'when passed too many arguments' do
|
76
111
|
let(:auth_config) { %w[baz foo] }
|
77
112
|
|
@@ -64,6 +64,15 @@ RSpec.describe Faraday::Response::Logger do
|
|
64
64
|
expect(formatter).to receive(:response).with(an_instance_of(Faraday::Env))
|
65
65
|
conn.get '/hello'
|
66
66
|
end
|
67
|
+
|
68
|
+
context 'when no route' do
|
69
|
+
it 'delegates logging to the formatter' do
|
70
|
+
expect(formatter).to receive(:request).with(an_instance_of(Faraday::Env))
|
71
|
+
expect(formatter).to receive(:error).with(an_instance_of(Faraday::Adapter::Test::Stubs::NotFound))
|
72
|
+
|
73
|
+
expect { conn.get '/noroute' }.to raise_error(Faraday::Adapter::Test::Stubs::NotFound)
|
74
|
+
end
|
75
|
+
end
|
67
76
|
end
|
68
77
|
|
69
78
|
context 'with custom formatter' do
|
@@ -94,6 +103,16 @@ RSpec.describe Faraday::Response::Logger do
|
|
94
103
|
expect(string_io.string).to match('GET http:/hello')
|
95
104
|
end
|
96
105
|
|
106
|
+
it 'logs status' do
|
107
|
+
conn.get '/hello', nil, accept: 'text/html'
|
108
|
+
expect(string_io.string).to match('Status 200')
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'does not log error message by default' do
|
112
|
+
expect { conn.get '/noroute' }.to raise_error(Faraday::Adapter::Test::Stubs::NotFound)
|
113
|
+
expect(string_io.string).not_to match(%(no stubbed request for get http:/noroute))
|
114
|
+
end
|
115
|
+
|
97
116
|
it 'logs request headers by default' do
|
98
117
|
conn.get '/hello', nil, accept: 'text/html'
|
99
118
|
expect(string_io.string).to match(%(Accept: "text/html))
|
@@ -188,6 +207,15 @@ RSpec.describe Faraday::Response::Logger do
|
|
188
207
|
end
|
189
208
|
end
|
190
209
|
|
210
|
+
context 'when logging errors' do
|
211
|
+
let(:logger_options) { { errors: true } }
|
212
|
+
|
213
|
+
it 'logs error message' do
|
214
|
+
expect { conn.get '/noroute' }.to raise_error(Faraday::Adapter::Test::Stubs::NotFound)
|
215
|
+
expect(string_io.string).to match(%(no stubbed request for get http:/noroute))
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
191
219
|
context 'when using log_level' do
|
192
220
|
let(:logger_options) { { bodies: true, log_level: :debug } }
|
193
221
|
|
@@ -57,11 +57,11 @@ RSpec.describe Faraday::Utils::Headers do
|
|
57
57
|
end
|
58
58
|
|
59
59
|
describe '#parse' do
|
60
|
-
before { subject.parse(headers) }
|
61
|
-
|
62
60
|
context 'when response headers leave http status line out' do
|
63
61
|
let(:headers) { "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" }
|
64
62
|
|
63
|
+
before { subject.parse(headers) }
|
64
|
+
|
65
65
|
it { expect(subject.keys).to eq(%w[Content-Type]) }
|
66
66
|
it { expect(subject['Content-Type']).to eq('text/html') }
|
67
67
|
it { expect(subject['content-type']).to eq('text/html') }
|
@@ -70,13 +70,31 @@ RSpec.describe Faraday::Utils::Headers do
|
|
70
70
|
context 'when response headers values include a colon' do
|
71
71
|
let(:headers) { "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nLocation: http://httpbingo.org/\r\n\r\n" }
|
72
72
|
|
73
|
+
before { subject.parse(headers) }
|
74
|
+
|
73
75
|
it { expect(subject['location']).to eq('http://httpbingo.org/') }
|
74
76
|
end
|
75
77
|
|
76
78
|
context 'when response headers include a blank line' do
|
77
79
|
let(:headers) { "HTTP/1.1 200 OK\r\n\r\nContent-Type: text/html\r\n\r\n" }
|
78
80
|
|
81
|
+
before { subject.parse(headers) }
|
82
|
+
|
79
83
|
it { expect(subject['content-type']).to eq('text/html') }
|
80
84
|
end
|
85
|
+
|
86
|
+
context 'when response headers include already stored keys' do
|
87
|
+
let(:headers) { "HTTP/1.1 200 OK\r\nX-Numbers: 123\r\n\r\n" }
|
88
|
+
|
89
|
+
before do
|
90
|
+
h = subject
|
91
|
+
h[:x_numbers] = 8
|
92
|
+
h.parse(headers)
|
93
|
+
end
|
94
|
+
|
95
|
+
it do
|
96
|
+
expect(subject[:x_numbers]).to eq('8, 123')
|
97
|
+
end
|
98
|
+
end
|
81
99
|
end
|
82
100
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "@technoweenie"
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2022-
|
13
|
+
date: 2022-11-15 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: faraday-net_http
|
@@ -131,7 +131,7 @@ licenses:
|
|
131
131
|
- MIT
|
132
132
|
metadata:
|
133
133
|
homepage_uri: https://lostisland.github.io/faraday
|
134
|
-
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.
|
134
|
+
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.7.0
|
135
135
|
source_code_uri: https://github.com/lostisland/faraday
|
136
136
|
bug_tracker_uri: https://github.com/lostisland/faraday/issues
|
137
137
|
post_install_message:
|