faraday 2.6.0 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9ba15c64f037b4f2c0194ae0d6340af38e79ded9d85b37a36f2ca3cde44d2f0
4
- data.tar.gz: b4ae3fe514db4cba5db21446e4b50d2d8b58ae3f3c077c7e288b4eb0a64637e1
3
+ metadata.gz: aded1d146b09e5b537b81833177fcd70acbed297598160e1114807fe047c15c2
4
+ data.tar.gz: 3b4c82d7712d3667767da96b87050d0cdb76bc0d0d2ed3f67d7f7ccb595f474e
5
5
  SHA512:
6
- metadata.gz: 20feaa462add0a309d25ceefbd6e5c4481f08a0e1f201d15a2f7459ea83611363002bfc40ba35b143b99d9ef29455edf10e40557455f2d7b057a8440d2f4e3ba
7
- data.tar.gz: f82680b983f1059eba7191b4ac7f17bc21f8b33736c6e1130c684c695d32fae3f3f278ea7e7b13adb92f89208befaf35fe7dc485dc5e5eb3f582943bde5a2b37
6
+ metadata.gz: 6e37eda4374f7c99ea8e687a6c704fcc74935800c4209c5b94767737e2133dc343dcfaa49deda11ddd312fe6f8e8b661a765134b402d3aa787672ffc84026547
7
+ data.tar.gz: 8ce5708af5954af94fdb2c1aff6a4cefcf7ed512d2f91d05c7ac3cdfc72fcad2840e9674c5defb8aaed570e5c9e5f2324911fb22e4b05184b2597a87959f2853
@@ -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)
@@ -17,6 +17,9 @@ module Faraday
17
17
  app.call(env).on_complete do |environment|
18
18
  on_complete(environment) if respond_to?(:on_complete)
19
19
  end
20
+ rescue StandardError => e
21
+ on_error(e) if respond_to?(:on_error)
22
+ raise
20
23
  end
21
24
 
22
25
  def close
@@ -26,6 +26,10 @@ module Faraday
26
26
  def on_complete(env)
27
27
  @formatter.response(env)
28
28
  end
29
+
30
+ def on_error(error)
31
+ @formatter.error(error) if @formatter.respond_to?(:error)
32
+ end
29
33
  end
30
34
  end
31
35
  end
@@ -132,7 +132,12 @@ module Faraday
132
132
 
133
133
  # Join multiple values with a comma.
134
134
  def add_parsed(key, value)
135
- self[key] ? self[key] << ', ' << value : self[key] = value
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- VERSION = '2.6.0'
4
+ VERSION = '2.7.0'
5
5
  end
@@ -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.6.0
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-10-03 00:00:00.000000000 Z
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.6.0
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: