faraday 2.6.0 → 2.7.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/lib/faraday/logging/formatter.rb +18 -2
- data/lib/faraday/middleware.rb +3 -0
- 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/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: 655acb55a8ad9a7088984d61b2e520fdf11e5aa3d9221212ef6e783349d72614
|
4
|
+
data.tar.gz: 620ec5a7c8c76109f6b82462b7a40b6440d79a96f6beaff33164be8743f1db7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc0321f9fed0b30548454555345bc4fa26af757142fd4dd767868c281bdc50b5833b73aa005c48dbc638c3c2f0dff00dddcde6d313a2e2cf25eae15dca928d3a
|
7
|
+
data.tar.gz: 3ebe07829ccb50bb0a0d1d0a5e77d2d91fb8ff6c57dd7d107792f8a6ef92656c4c8e67e650f2ac88862686f1051d36cabc1b9fd5ec933b59e70977d0777a5845
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'pp'
|
3
|
+
require 'pp' # rubocop:disable Lint/RedundantRequireStatement
|
4
4
|
|
5
5
|
module Faraday
|
6
6
|
module Logging
|
@@ -8,7 +8,7 @@ module Faraday
|
|
8
8
|
class Formatter
|
9
9
|
extend Forwardable
|
10
10
|
|
11
|
-
DEFAULT_OPTIONS = { headers: true, bodies: false,
|
11
|
+
DEFAULT_OPTIONS = { headers: true, bodies: false, errors: false,
|
12
12
|
log_level: :info }.freeze
|
13
13
|
|
14
14
|
def initialize(logger:, options:)
|
@@ -37,6 +37,18 @@ module Faraday
|
|
37
37
|
log_body('response', env[:body]) if env[:body] && log_body?(:response)
|
38
38
|
end
|
39
39
|
|
40
|
+
def error(error)
|
41
|
+
return unless log_errors?
|
42
|
+
|
43
|
+
error_log = proc { error.full_message }
|
44
|
+
public_send(log_level, 'error', &error_log)
|
45
|
+
|
46
|
+
log_headers('error', error.response_headers) if error.respond_to?(:response_headers) && log_headers?(:error)
|
47
|
+
return unless error.respond_to?(:response_body) && error.response_body && log_body?(:error)
|
48
|
+
|
49
|
+
log_body('error', error.response_body)
|
50
|
+
end
|
51
|
+
|
40
52
|
def filter(filter_word, filter_replacement)
|
41
53
|
@filter.push([filter_word, filter_replacement])
|
42
54
|
end
|
@@ -77,6 +89,10 @@ module Faraday
|
|
77
89
|
end
|
78
90
|
end
|
79
91
|
|
92
|
+
def log_errors?
|
93
|
+
@options[:errors]
|
94
|
+
end
|
95
|
+
|
80
96
|
def apply_filters(output)
|
81
97
|
@filter.each do |pattern, replacement|
|
82
98
|
output = output.to_s.gsub(pattern, replacement)
|
data/lib/faraday/middleware.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
|
@@ -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.1
|
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-18 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.1
|
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:
|