faraday 1.1.0 → 1.2.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: 47236f354f9fda55254c8bd2b9e3552de12e8894fcecea0035ffbeb3ccc46b9c
4
- data.tar.gz: f6eb36d3da02314fe799368c1ff4c17c5841f0badd18c5a89772e00bae43065c
3
+ metadata.gz: a36c97856713ca2198c7ec0d6599136ecbf2033f4a192aab2f075135403e240c
4
+ data.tar.gz: '092c756c000f6650280080e92446aae63bea6db4603153512f78c5e748ba543a'
5
5
  SHA512:
6
- metadata.gz: 300153861873b2a5b3a3a9f552c47a2b70dd083ffd5478d8eca2c4c2005e5a74d09d962b51fb9a780ea90bb1ac87bdfa902639e86b2147d5b4d4374791789bd1
7
- data.tar.gz: 3628d85508cdd669111ae6175b6b5a191380d0d405f345612f6342e487aec0f3cc9d2e1726a2cdde7d82ee86fae82aa41f4a62ea330343598383ae3f644b5a85
6
+ metadata.gz: 00c58868dc1a050bfed2f55c20543477aff2841f67665cffe3a4f736414934024c1c6c162ef1022e4d8cb8691f7abf2a756062a792988438748317f5edd7bcae
7
+ data.tar.gz: 61090e76494671608acac5c406f7505bc3f01eef822dbc48739fecb7fc28b80dd43f54a3af4b5696e6bd29769440b4cae0d2c07a91c601e9c320e5061c4f32a5
@@ -1,11 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'cgi'
4
+ require 'date'
4
5
  require 'set'
5
6
  require 'forwardable'
6
7
  require 'faraday/middleware_registry'
7
8
  require 'faraday/dependency_loader'
8
9
 
10
+ unless defined?(::Faraday::Timer)
11
+ require 'timeout'
12
+ Timer = Timeout
13
+ end
14
+
15
+ require 'faraday/version'
16
+ require 'faraday/methods'
17
+ require 'faraday/utils'
18
+ require 'faraday/options'
19
+ require 'faraday/connection'
20
+ require 'faraday/rack_builder'
21
+ require 'faraday/parameters'
22
+ require 'faraday/middleware'
23
+ require 'faraday/adapter'
24
+ require 'faraday/request'
25
+ require 'faraday/response'
26
+ require 'faraday/error'
27
+ require 'faraday/file_part'
28
+ require 'faraday/param_part'
29
+
9
30
  # This is the main namespace for Faraday.
10
31
  #
11
32
  # It provides methods to create {Connection} objects, and HTTP-related
@@ -19,10 +40,6 @@ require 'faraday/dependency_loader'
19
40
  # conn.get '/'
20
41
  #
21
42
  module Faraday
22
- VERSION = '1.1.0'
23
- METHODS_WITH_QUERY = %w[get head delete trace].freeze
24
- METHODS_WITH_BODY = %w[post put patch].freeze
25
-
26
43
  class << self
27
44
  # The root path that Faraday is being loaded from.
28
45
  #
@@ -107,6 +124,34 @@ module Faraday
107
124
  default_connection.respond_to?(symbol, include_private) || super
108
125
  end
109
126
 
127
+ # @overload default_connection
128
+ # Gets the default connection used for simple scripts.
129
+ # @return [Faraday::Connection] a connection configured with
130
+ # the default_adapter.
131
+ # @overload default_connection=(connection)
132
+ # @param connection [Faraday::Connection]
133
+ # Sets the default {Faraday::Connection} for simple scripts that
134
+ # access the Faraday constant directly, such as
135
+ # <code>Faraday.get "https://faraday.com"</code>.
136
+ def default_connection
137
+ @default_connection ||= Connection.new(default_connection_options)
138
+ end
139
+
140
+ # Gets the default connection options used when calling {Faraday#new}.
141
+ #
142
+ # @return [Faraday::ConnectionOptions]
143
+ def default_connection_options
144
+ @default_connection_options ||= ConnectionOptions.new
145
+ end
146
+
147
+ # Sets the default options used when calling {Faraday#new}.
148
+ #
149
+ # @param options [Hash, Faraday::ConnectionOptions]
150
+ def default_connection_options=(options)
151
+ @default_connection = nil
152
+ @default_connection_options = ConnectionOptions.from(options)
153
+ end
154
+
110
155
  private
111
156
 
112
157
  # Internal: Proxies method calls on the Faraday constant to
@@ -125,42 +170,5 @@ module Faraday
125
170
  self.lib_path = File.expand_path 'faraday', __dir__
126
171
  self.default_adapter = :net_http
127
172
 
128
- # @overload default_connection
129
- # Gets the default connection used for simple scripts.
130
- # @return [Faraday::Connection] a connection configured with
131
- # the default_adapter.
132
- # @overload default_connection=(connection)
133
- # @param connection [Faraday::Connection]
134
- # Sets the default {Faraday::Connection} for simple scripts that
135
- # access the Faraday constant directly, such as
136
- # <code>Faraday.get "https://faraday.com"</code>.
137
- def self.default_connection
138
- @default_connection ||= Connection.new(default_connection_options)
139
- end
140
-
141
- # Gets the default connection options used when calling {Faraday#new}.
142
- #
143
- # @return [Faraday::ConnectionOptions]
144
- def self.default_connection_options
145
- @default_connection_options ||= ConnectionOptions.new
146
- end
147
-
148
- # Sets the default options used when calling {Faraday#new}.
149
- #
150
- # @param options [Hash, Faraday::ConnectionOptions]
151
- def self.default_connection_options=(options)
152
- @default_connection = nil
153
- @default_connection_options = ConnectionOptions.from(options)
154
- end
155
-
156
- unless defined?(::Faraday::Timer)
157
- require 'timeout'
158
- Timer = Timeout
159
- end
160
-
161
- require_libs 'utils', 'options', 'connection', 'rack_builder', 'parameters',
162
- 'middleware', 'adapter', 'request', 'response', 'error',
163
- 'file_part', 'param_part'
164
-
165
173
  require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
166
174
  end
@@ -90,7 +90,21 @@ module Faraday
90
90
 
91
91
  include Options
92
92
 
93
- dependency 'em-http'
93
+ dependency do
94
+ require 'em-http'
95
+
96
+ begin
97
+ require 'openssl'
98
+ rescue LoadError
99
+ warn 'Warning: no such file to load -- openssl. ' \
100
+ 'Make sure it is installed if you want HTTPS support'
101
+ else
102
+ require 'em-http/version'
103
+ if EventMachine::HttpRequest::VERSION < '1.1.6'
104
+ require 'faraday/adapter/em_http_ssl_patch'
105
+ end
106
+ end
107
+ end
94
108
 
95
109
  self.supports_parallel = true
96
110
 
@@ -273,14 +287,3 @@ module Faraday
273
287
  end
274
288
  end
275
289
  end
276
-
277
- if Faraday::Adapter::EMHttp.loaded?
278
- begin
279
- require 'openssl'
280
- rescue LoadError
281
- warn 'Warning: no such file to load -- openssl. ' \
282
- 'Make sure it is installed if you want HTTPS support'
283
- else
284
- require 'faraday/adapter/em_http_ssl_patch'
285
- end
286
- end
@@ -12,6 +12,22 @@ module Faraday
12
12
  require 'em-synchrony/em-http'
13
13
  require 'em-synchrony/em-multi'
14
14
  require 'fiber'
15
+
16
+ require 'faraday/adapter/em_synchrony/parallel_manager'
17
+
18
+ if Faraday::Adapter::EMSynchrony.loaded?
19
+ begin
20
+ require 'openssl'
21
+ rescue LoadError
22
+ warn 'Warning: no such file to load -- openssl. ' \
23
+ 'Make sure it is installed if you want HTTPS support'
24
+ else
25
+ require 'em-http/version'
26
+ if EventMachine::HttpRequest::VERSION < '1.1.6'
27
+ require 'faraday/adapter/em_http_ssl_patch'
28
+ end
29
+ end
30
+ end
15
31
  end
16
32
 
17
33
  self.supports_parallel = true
@@ -135,16 +151,3 @@ module Faraday
135
151
  end
136
152
  end
137
153
  end
138
-
139
- require 'faraday/adapter/em_synchrony/parallel_manager'
140
-
141
- if Faraday::Adapter::EMSynchrony.loaded?
142
- begin
143
- require 'openssl'
144
- rescue LoadError
145
- warn 'Warning: no such file to load -- openssl. ' \
146
- 'Make sure it is installed if you want HTTPS support'
147
- else
148
- require 'faraday/adapter/em_http_ssl_patch'
149
- end
150
- end
@@ -53,14 +53,15 @@ module Faraday
53
53
  end
54
54
 
55
55
  def net_http_connection(env)
56
- klass = if (proxy = env[:request][:proxy])
57
- Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port,
58
- proxy[:user], proxy[:password])
59
- else
60
- Net::HTTP
61
- end
56
+ proxy = env[:request][:proxy]
62
57
  port = env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)
63
- klass.new(env[:url].hostname, port)
58
+ if proxy
59
+ Net::HTTP.new(env[:url].hostname, port,
60
+ proxy[:uri].hostname, proxy[:uri].port,
61
+ proxy[:user], proxy[:password])
62
+ else
63
+ Net::HTTP.new(env[:url].hostname, port, nil)
64
+ end
64
65
  end
65
66
 
66
67
  def call(env)
@@ -182,7 +183,7 @@ module Faraday
182
183
  end
183
184
 
184
185
  if (sec = http.respond_to?(:write_timeout=) &&
185
- request_timeout(:write, req))
186
+ request_timeout(:write, req))
186
187
  http.write_timeout = sec
187
188
  end
188
189
 
@@ -200,19 +201,19 @@ module Faraday
200
201
  return ssl[:cert_store] if ssl[:cert_store]
201
202
 
202
203
  @ssl_cert_store ||= begin
203
- # Use the default cert store by default, i.e. system ca certs
204
- OpenSSL::X509::Store.new.tap(&:set_default_paths)
205
- end
204
+ # Use the default cert store by default, i.e. system ca certs
205
+ OpenSSL::X509::Store.new.tap(&:set_default_paths)
206
+ end
206
207
  end
207
208
 
208
209
  def ssl_verify_mode(ssl)
209
210
  ssl[:verify_mode] || begin
210
- if ssl.fetch(:verify, true)
211
- OpenSSL::SSL::VERIFY_PEER
212
- else
213
- OpenSSL::SSL::VERIFY_NONE
214
- end
215
- end
211
+ if ssl.fetch(:verify, true)
212
+ OpenSSL::SSL::VERIFY_PEER
213
+ else
214
+ OpenSSL::SSL::VERIFY_NONE
215
+ end
216
+ end
216
217
  end
217
218
  end
218
219
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ruby2_keywords'
4
-
5
3
  module Faraday
6
4
  # DependencyLoader helps Faraday adapters and middleware load dependencies.
7
5
  module DependencyLoader
@@ -15,7 +13,7 @@ module Faraday
15
13
  self.load_error = e
16
14
  end
17
15
 
18
- ruby2_keywords def new(*)
16
+ def new(*)
19
17
  unless loaded?
20
18
  raise "missing dependency for #{self}: #{load_error.message}"
21
19
  end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ METHODS_WITH_QUERY = %w[get head delete trace].freeze
5
+ METHODS_WITH_BODY = %w[post put patch].freeze
6
+ end
@@ -6,15 +6,25 @@ module Faraday
6
6
  extend MiddlewareRegistry
7
7
  extend DependencyLoader
8
8
 
9
- def initialize(app = nil)
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 @app.respond_to?(:close)
15
- @app.close
24
+ if app.respond_to?(:close)
25
+ app.close
16
26
  else
17
- warn "#{@app} does not implement \#close!"
27
+ warn "#{app} does not implement \#close!"
18
28
  end
19
29
  end
20
30
  end
@@ -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.15.
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',
@@ -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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ VERSION = '1.2.0'
5
+ end
@@ -2,23 +2,49 @@
2
2
 
3
3
  RSpec.describe Faraday::Middleware do
4
4
  subject { described_class.new(app) }
5
+ let(:app) { double }
6
+
7
+ describe 'options' do
8
+ context 'when options are passed to the middleware' do
9
+ subject { described_class.new(app, options) }
10
+ let(:options) { { field: 'value' } }
11
+
12
+ it 'accepts options when initialized' do
13
+ expect(subject.options[:field]).to eq('value')
14
+ end
15
+ end
16
+ end
17
+
18
+ describe '#on_request' do
19
+ subject do
20
+ Class.new(described_class) do
21
+ def on_request(env)
22
+ # do nothing
23
+ end
24
+ end.new(app)
25
+ end
26
+
27
+ it 'is called by #call' do
28
+ expect(app).to receive(:call).and_return(app)
29
+ expect(app).to receive(:on_complete)
30
+ is_expected.to receive(:call).and_call_original
31
+ is_expected.to receive(:on_request)
32
+ subject.call(double)
33
+ end
34
+ end
5
35
 
6
36
  describe '#close' do
7
37
  context "with app that doesn't support \#close" do
8
- let(:app) { double }
9
-
10
38
  it 'should issue warning' do
11
- expect(subject).to receive(:warn)
39
+ is_expected.to receive(:warn)
12
40
  subject.close
13
41
  end
14
42
  end
15
43
 
16
44
  context "with app that supports \#close" do
17
- let(:app) { double }
18
-
19
45
  it 'should issue warning' do
20
46
  expect(app).to receive(:close)
21
- expect(subject).to_not receive(:warn)
47
+ is_expected.to_not receive(:warn)
22
48
  subject.close
23
49
  end
24
50
  end
@@ -117,7 +117,7 @@ RSpec.describe Faraday::Request::Retry do
117
117
  let(:options) { { max: 2, interval: 0.1, interval_randomness: 0.05 } }
118
118
  let(:middleware) { Faraday::Request::Retry.new(nil, options) }
119
119
 
120
- it { expect(middleware.send(:calculate_retry_interval, 2)).to be_between(0.1, 0.15) }
120
+ it { expect(middleware.send(:calculate_retry_interval, 2)).to be_between(0.1, 0.105) }
121
121
  end
122
122
  end
123
123
 
@@ -33,6 +33,7 @@ shared_examples 'adapter examples' do |**options|
33
33
 
34
34
  let(:protocol) { ssl_mode? ? 'https' : 'http' }
35
35
  let(:remote) { "#{protocol}://example.com" }
36
+ let(:stub_remote) { remote }
36
37
 
37
38
  let(:conn) do
38
39
  conn_options[:ssl] ||= {}
@@ -46,7 +47,7 @@ shared_examples 'adapter examples' do |**options|
46
47
  end
47
48
  end
48
49
 
49
- let!(:request_stub) { stub_request(http_method, remote) }
50
+ let!(:request_stub) { stub_request(http_method, stub_remote) }
50
51
 
51
52
  after do
52
53
  expect(request_stub).to have_been_requested unless request_stub.disabled?
@@ -1,5 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ shared_examples 'proxy examples' do
4
+ it 'handles requests with proxy' do
5
+ res = conn.public_send(http_method, '/')
6
+
7
+ expect(res.status).to eq(200)
8
+ end
9
+
10
+ it 'handles proxy failures' do
11
+ request_stub.to_return(status: 407)
12
+
13
+ expect { conn.public_send(http_method, '/') }.to raise_error(Faraday::ProxyAuthError)
14
+ end
15
+ end
16
+
3
17
  shared_examples 'a request method' do |http_method|
4
18
  let(:query_or_body) { method_with_body?(http_method) ? :body : :query }
5
19
  let(:response) { conn.public_send(http_method, '/') }
@@ -218,17 +232,31 @@ shared_examples 'a request method' do |http_method|
218
232
  end
219
233
  end
220
234
 
221
- it 'handles requests with proxy' do
222
- conn_options[:proxy] = 'http://google.co.uk'
235
+ context 'when a proxy is provided as option' do
236
+ before do
237
+ conn_options[:proxy] = 'http://env-proxy.com:80'
238
+ end
223
239
 
224
- res = conn.public_send(http_method, '/')
225
- expect(res.status).to eq(200)
240
+ include_examples 'proxy examples'
226
241
  end
227
242
 
228
- it 'handles proxy failures' do
229
- conn_options[:proxy] = 'http://google.co.uk'
230
- request_stub.to_return(status: 407)
243
+ context 'when http_proxy env variable is set' do
244
+ let(:proxy_url) { 'http://env-proxy.com:80' }
231
245
 
232
- expect { conn.public_send(http_method, '/') }.to raise_error(Faraday::ProxyAuthError)
246
+ around do |example|
247
+ with_env 'http_proxy' => proxy_url do
248
+ example.run
249
+ end
250
+ end
251
+
252
+ include_examples 'proxy examples'
253
+
254
+ context 'when the env proxy is ignored' do
255
+ around do |example|
256
+ with_env_proxy_disabled(&example)
257
+ end
258
+
259
+ include_examples 'proxy examples'
260
+ end
233
261
  end
234
262
  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: 1.1.0
4
+ version: 1.2.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: 2020-10-17 00:00:00.000000000 Z
13
+ date: 2020-12-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multipart-post
@@ -81,6 +81,7 @@ files:
81
81
  - lib/faraday/error.rb
82
82
  - lib/faraday/file_part.rb
83
83
  - lib/faraday/logging/formatter.rb
84
+ - lib/faraday/methods.rb
84
85
  - lib/faraday/middleware.rb
85
86
  - lib/faraday/middleware_registry.rb
86
87
  - lib/faraday/options.rb
@@ -106,6 +107,7 @@ files:
106
107
  - lib/faraday/utils.rb
107
108
  - lib/faraday/utils/headers.rb
108
109
  - lib/faraday/utils/params_hash.rb
110
+ - lib/faraday/version.rb
109
111
  - spec/external_adapters/faraday_specs_setup.rb
110
112
  - spec/faraday/adapter/em_http_spec.rb
111
113
  - spec/faraday/adapter/em_synchrony_spec.rb
@@ -157,7 +159,7 @@ licenses:
157
159
  - MIT
158
160
  metadata:
159
161
  homepage_uri: https://lostisland.github.io/faraday
160
- changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.1.0
162
+ changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.2.0
161
163
  source_code_uri: https://github.com/lostisland/faraday
162
164
  bug_tracker_uri: https://github.com/lostisland/faraday/issues
163
165
  post_install_message: