faraday 2.0.0.alpha.pre.2 → 2.0.1

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: 5490b5d7735630cd51e014b7c4c6d72b12ee3d95c21bccbea616f5363796d563
4
- data.tar.gz: 84bf392c4e1c7a8b37cbf2395686eccd8145c43a4ff02c61cef54971615de79a
3
+ metadata.gz: 06eb37848cd7af8f9bbca6515b5d39da8fd1b96242c02ef3e11f370102538030
4
+ data.tar.gz: 28e3ae036dddef796d0bb388cff43f7aea5bcedfdb92bd410099fd303255abd6
5
5
  SHA512:
6
- metadata.gz: d0baf3290c10dab7253a44fe495492c52b3d1098410c16a87bf7082d44952f4a007c133c1025dd868dddf43d74fdf3401f256d51aaf8024e220b5ddb670bc6ec
7
- data.tar.gz: 5bbcaf4565082cc50e67a1a0c6b80ac68c51d57901573799a6dcd18f3ff22045b12bd26dccd9d70b1b7298dd728d4d2f35ee8cfa65aea9c94c8e9e9d6fb72e71
6
+ metadata.gz: 3b492269396e55018147763939f526fd69b34da244c94e0b055b945dc4d4c32a5b4eee25accc0dcb7d60a74e31dca46787349909a0c806fd79428a6806a627fe
7
+ data.tar.gz: 7d61eaf95bcdd8206d20bb4e63575e774ecf49483623ee7f943d7e19dcfebc4c5820cac985382b4582e146540bf7438771a92a039462978c3081ef566221bab9
@@ -56,7 +56,7 @@ RSpec.describe Client do
56
56
 
57
57
  it 'handles exception' do
58
58
  stubs.get('/ebi') do
59
- raise Faraday::ConnectionFailed, nil
59
+ raise Faraday::ConnectionFailed
60
60
  end
61
61
 
62
62
  expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
@@ -60,7 +60,7 @@ class ClientTest < Test::Unit::TestCase
60
60
  def test_sushi_exception
61
61
  stubs = Faraday::Adapter::Test::Stubs.new
62
62
  stubs.get('/ebi') do
63
- raise Faraday::ConnectionFailed, nil
63
+ raise Faraday::ConnectionFailed
64
64
  end
65
65
 
66
66
  cli = client(stubs)
@@ -64,7 +64,7 @@ module Faraday
64
64
  options = ConnectionOptions.from(options)
65
65
 
66
66
  if url.is_a?(Hash) || url.is_a?(ConnectionOptions)
67
- options = options.merge(url)
67
+ options = Utils.deep_merge(options, url)
68
68
  url = options.url
69
69
  end
70
70
 
@@ -117,7 +117,7 @@ module Faraday
117
117
 
118
118
  extend Forwardable
119
119
 
120
- def_delegators :builder, :build, :use, :request, :response, :adapter, :app
120
+ def_delegators :builder, :use, :request, :response, :adapter, :app
121
121
 
122
122
  # Closes the underlying resources and/or connections. In the case of
123
123
  # persistent connections, this closes all currently open connections
data/lib/faraday/error.rb CHANGED
@@ -6,7 +6,7 @@ module Faraday
6
6
  class Error < StandardError
7
7
  attr_reader :response, :wrapped_exception
8
8
 
9
- def initialize(exc, response = nil)
9
+ def initialize(exc = nil, response = nil)
10
10
  @wrapped_exception = nil unless defined?(@wrapped_exception)
11
11
  @response = nil unless defined?(@response)
12
12
  super(exc_msg_and_response!(exc, response))
@@ -141,13 +141,7 @@ module Faraday
141
141
  class SSLError < Error
142
142
  end
143
143
 
144
- # Raised by FaradayMiddleware::ResponseMiddleware
144
+ # Raised by middlewares that parse the response, like the JSON response middleware.
145
145
  class ParsingError < Error
146
146
  end
147
-
148
- # Exception used to control the Retry middleware.
149
- #
150
- # @see Faraday::Request::Retry
151
- class RetriableResponse < Error
152
- end
153
147
  end
@@ -58,22 +58,21 @@ module Faraday
58
58
  end
59
59
  end
60
60
 
61
- def initialize(handlers = [], adapter = nil, &block)
62
- @adapter = adapter
63
- @handlers = handlers
64
- if block
65
- build(&block)
66
- elsif @handlers.empty?
67
- # default stack, if nothing else is configured
68
- request :url_encoded
69
- self.adapter Faraday.default_adapter
70
- end
61
+ def initialize(&block)
62
+ @adapter = nil
63
+ @handlers = []
64
+ build(&block)
65
+ end
66
+
67
+ def initialize_dup(original)
68
+ super
69
+ @adapter = original.adapter
70
+ @handlers = original.handlers.dup
71
71
  end
72
72
 
73
- def build(options = {})
73
+ def build
74
74
  raise_if_locked
75
- @handlers.clear unless options[:keep]
76
- yield(self) if block_given?
75
+ block_given? ? yield(self) : request(:url_encoded)
77
76
  adapter(Faraday.default_adapter) unless @adapter
78
77
  end
79
78
 
@@ -109,7 +108,7 @@ module Faraday
109
108
  end
110
109
 
111
110
  ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block)
112
- return @adapter if klass == NO_ARGUMENT
111
+ return @adapter if klass == NO_ARGUMENT || klass.nil?
113
112
 
114
113
  klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
115
114
  @adapter = self.class::Handler.new(klass, *args, &block)
@@ -164,6 +163,7 @@ module Faraday
164
163
  def app
165
164
  @app ||= begin
166
165
  lock!
166
+ ensure_adapter!
167
167
  to_app
168
168
  end
169
169
  end
@@ -182,10 +182,6 @@ module Faraday
182
182
  @adapter == other.adapter
183
183
  end
184
184
 
185
- def dup
186
- self.class.new(@handlers.dup, @adapter.dup)
187
- end
188
-
189
185
  # ENV Keys
190
186
  # :http_method - a symbolized request HTTP method (:get, :post)
191
187
  # :body - the request body that will eventually be converted to a string.
@@ -216,6 +212,9 @@ module Faraday
216
212
  private
217
213
 
218
214
  LOCK_ERR = "can't modify middleware stack after making a request"
215
+ MISSING_ADAPTER_ERROR = "An attempt to run a request with a Faraday::Connection without adapter has been made.\n" \
216
+ "Please set Faraday.default_adapter or provide one when initializing the connection.\n" \
217
+ 'For more info, check https://lostisland.github.io/faraday/usage/.'
219
218
 
220
219
  def raise_if_locked
221
220
  raise StackLocked, LOCK_ERR if locked?
@@ -227,6 +226,10 @@ module Faraday
227
226
  raise 'Adapter should be set using the `adapter` method, not `use`'
228
227
  end
229
228
 
229
+ def ensure_adapter!
230
+ raise MISSING_ADAPTER_ERROR unless @adapter
231
+ end
232
+
230
233
  def adapter_set?
231
234
  !@adapter.nil?
232
235
  end
@@ -8,10 +8,11 @@ module Faraday
8
8
 
9
9
  # @param app [#call]
10
10
  # @param type [String, Symbol] Type of Authorization
11
- # @param params [Array<String, Proc>] parameters to build the Authorization header.
11
+ # @param params [Array<String, Proc, #call>] parameters to build the Authorization header.
12
12
  # If the type is `:basic`, then these can be a login and password pair.
13
13
  # Otherwise, a single value is expected that will be appended after the type.
14
- # This value can be a proc, in which case it will be invoked on each request.
14
+ # This value can be a proc or an object responding to `.call`, in which case
15
+ # it will be invoked on each request.
15
16
  def initialize(app, type, *params)
16
17
  @type = type
17
18
  @params = params
@@ -37,7 +38,7 @@ module Faraday
37
38
  raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
38
39
  else
39
40
  value = params.first
40
- value = value.call if value.is_a?(Proc)
41
+ value = value.call if value.is_a?(Proc) || value.respond_to?(:call)
41
42
  "#{type} #{value}"
42
43
  end
43
44
  end
@@ -133,6 +133,4 @@ end
133
133
  require 'faraday/request/authorization'
134
134
  require 'faraday/request/instrumentation'
135
135
  require 'faraday/request/json'
136
- require 'faraday/request/multipart'
137
- require 'faraday/request/retry'
138
136
  require 'faraday/request/url_encoded'
data/lib/faraday/utils.rb CHANGED
@@ -101,7 +101,7 @@ module Faraday
101
101
  # Recursive hash update
102
102
  def deep_merge!(target, hash)
103
103
  hash.each do |key, value|
104
- target[key] = if value.is_a?(Hash) && target[key].is_a?(Hash)
104
+ target[key] = if value.is_a?(Hash) && (target[key].is_a?(Hash) || target[key].is_a?(Options))
105
105
  deep_merge(target[key], value)
106
106
  else
107
107
  value
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- VERSION = '2.0.0.alpha-2'
4
+ VERSION = '2.0.1'
5
5
  end
data/lib/faraday.rb CHANGED
@@ -17,9 +17,7 @@ require 'faraday/middleware'
17
17
  require 'faraday/adapter'
18
18
  require 'faraday/request'
19
19
  require 'faraday/response'
20
- require 'faraday/file_part'
21
- require 'faraday/param_part'
22
-
20
+ require 'faraday/net_http'
23
21
  # This is the main namespace for Faraday.
24
22
  #
25
23
  # It provides methods to create {Connection} objects, and HTTP-related
@@ -92,7 +90,7 @@ module Faraday
92
90
  # params: { page: 1 }
93
91
  # # => Faraday::Connection to http://faraday.com?page=1
94
92
  def new(url = nil, options = {}, &block)
95
- options = default_connection_options.merge(options)
93
+ options = Utils.deep_merge(default_connection_options, options)
96
94
  Faraday::Connection.new(url, options, &block)
97
95
  end
98
96
 
@@ -150,5 +148,5 @@ module Faraday
150
148
  self.ignore_env_proxy = false
151
149
  self.root_path = File.expand_path __dir__
152
150
  self.lib_path = File.expand_path 'faraday', __dir__
153
- self.default_adapter = :test
151
+ self.default_adapter = :net_http
154
152
  end
@@ -151,6 +151,9 @@ RSpec.describe Faraday::Connection do
151
151
  end
152
152
 
153
153
  describe '#close' do
154
+ before { Faraday.default_adapter = :test }
155
+ after { Faraday.default_adapter = nil }
156
+
154
157
  it 'can close underlying app' do
155
158
  expect(conn.app).to receive(:close)
156
159
  conn.close
@@ -639,6 +642,27 @@ RSpec.describe Faraday::Connection do
639
642
 
640
643
  it_behaves_like 'default connection options'
641
644
  end
645
+
646
+ context 'preserving a user_agent assigned via default_conncetion_options' do
647
+ around do |example|
648
+ old = Faraday.default_connection_options
649
+ Faraday.default_connection_options = { headers: { user_agent: 'My Agent 1.2' } }
650
+ example.run
651
+ Faraday.default_connection_options = old
652
+ end
653
+
654
+ context 'when url is a Hash' do
655
+ let(:conn) { Faraday.new(url: 'http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
656
+
657
+ it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
658
+ end
659
+
660
+ context 'when url is a String' do
661
+ let(:conn) { Faraday.new('http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
662
+
663
+ it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
664
+ end
665
+ end
642
666
  end
643
667
 
644
668
  describe 'request params' do
@@ -20,6 +20,8 @@ RSpec.describe Faraday::RackBuilder do
20
20
  end
21
21
 
22
22
  subject { conn.builder }
23
+ before { Faraday.default_adapter = :test }
24
+ after { Faraday.default_adapter = nil }
23
25
 
24
26
  context 'with default stack' do
25
27
  let(:conn) { Faraday::Connection.new }
@@ -86,13 +88,6 @@ RSpec.describe Faraday::RackBuilder do
86
88
 
87
89
  it { expect(subject.handlers).to eq([Apple]) }
88
90
 
89
- it 'allows rebuilding' do
90
- subject.build do |builder|
91
- builder.use(Orange)
92
- end
93
- expect(subject.handlers).to eq([Orange])
94
- end
95
-
96
91
  it 'allows use' do
97
92
  subject.use(Orange)
98
93
  expect(subject.handlers).to eq([Apple, Orange])
@@ -63,6 +63,15 @@ RSpec.describe Faraday::Request::Authorization do
63
63
  include_examples 'does not interfere with existing authentication'
64
64
  end
65
65
 
66
+ context 'when passed a callable' do
67
+ let(:callable) { double('Callable Authorizer', call: 'custom_from_callable') }
68
+ let(:auth_config) { [callable] }
69
+
70
+ it { expect(response.body).to eq('Bearer custom_from_callable') }
71
+
72
+ include_examples 'does not interfere with existing authentication'
73
+ end
74
+
66
75
  context 'when passed too many arguments' do
67
76
  let(:auth_config) { %w[baz foo] }
68
77
 
@@ -3,7 +3,6 @@
3
3
  RSpec.describe Faraday::Request::UrlEncoded do
4
4
  let(:conn) do
5
5
  Faraday.new do |b|
6
- b.request :multipart
7
6
  b.request :url_encoded
8
7
  b.adapter :test do |stub|
9
8
  stub.post('/echo') do |env|
@@ -53,4 +53,65 @@ RSpec.describe Faraday::Utils do
53
53
  expect(headers).not_to have_key('authorization')
54
54
  end
55
55
  end
56
+
57
+ describe '.deep_merge!' do
58
+ let(:connection_options) { Faraday::ConnectionOptions.new }
59
+ let(:url) do
60
+ {
61
+ url: 'http://example.com/abc',
62
+ headers: { 'Mime-Version' => '1.0' },
63
+ request: { oauth: { consumer_key: 'anonymous' } },
64
+ ssl: { version: '2' }
65
+ }
66
+ end
67
+
68
+ it 'recursively merges the headers' do
69
+ connection_options.headers = { user_agent: 'My Agent 1.0' }
70
+ deep_merge = Faraday::Utils.deep_merge!(connection_options, url)
71
+
72
+ expect(deep_merge.headers).to eq('Mime-Version' => '1.0', user_agent: 'My Agent 1.0')
73
+ end
74
+
75
+ context 'when a target hash has an Options Struct value' do
76
+ let(:request) do
77
+ {
78
+ params_encoder: nil,
79
+ proxy: nil,
80
+ bind: nil,
81
+ timeout: nil,
82
+ open_timeout: nil,
83
+ read_timeout: nil,
84
+ write_timeout: nil,
85
+ boundary: nil,
86
+ oauth: { consumer_key: 'anonymous' },
87
+ context: nil,
88
+ on_data: nil
89
+ }
90
+ end
91
+ let(:ssl) do
92
+ {
93
+ verify: nil,
94
+ ca_file: nil,
95
+ ca_path: nil,
96
+ verify_mode: nil,
97
+ cert_store: nil,
98
+ client_cert: nil,
99
+ client_key: nil,
100
+ certificate: nil,
101
+ private_key: nil,
102
+ verify_depth: nil,
103
+ version: '2',
104
+ min_version: nil,
105
+ max_version: nil
106
+ }
107
+ end
108
+
109
+ it 'does not overwrite an Options Struct value' do
110
+ deep_merge = Faraday::Utils.deep_merge!(connection_options, url)
111
+
112
+ expect(deep_merge.request.to_h).to eq(request)
113
+ expect(deep_merge.ssl.to_h).to eq(ssl)
114
+ end
115
+ end
116
+ end
56
117
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'multipart_parser/reader'
4
-
5
3
  module Faraday
6
4
  module HelperMethods
7
5
  def self.included(base)
@@ -86,41 +84,6 @@ module Faraday
86
84
  end
87
85
  end
88
86
 
89
- def multipart_file
90
- Faraday::FilePart.new(__FILE__, 'text/x-ruby')
91
- end
92
-
93
- # parse boundary out of a Content-Type header like:
94
- # Content-Type: multipart/form-data; boundary=gc0p4Jq0M2Yt08jU534c0p
95
- def parse_multipart_boundary(ctype)
96
- MultipartParser::Reader.extract_boundary_value(ctype)
97
- end
98
-
99
- # parse a multipart MIME message, returning a hash of any multipart errors
100
- def parse_multipart(boundary, body)
101
- reader = MultipartParser::Reader.new(boundary)
102
- result = { errors: [], parts: [] }
103
- def result.part(name)
104
- hash = self[:parts].detect { |h| h[:part].name == name }
105
- [hash[:part], hash[:body].join]
106
- end
107
-
108
- reader.on_part do |part|
109
- result[:parts] << thispart = {
110
- part: part,
111
- body: []
112
- }
113
- part.on_data do |chunk|
114
- thispart[:body] << chunk
115
- end
116
- end
117
- reader.on_error do |msg|
118
- result[:errors] << msg
119
- end
120
- reader.write(body)
121
- result
122
- end
123
-
124
87
  def method_with_body?(method)
125
88
  self.class.method_with_body?(method)
126
89
  end
@@ -40,7 +40,6 @@ shared_examples 'adapter examples' do |**options|
40
40
  conn_options[:ssl][:ca_file] ||= ENV['SSL_FILE']
41
41
 
42
42
  Faraday.new(remote, conn_options) do |conn|
43
- conn.request :multipart
44
43
  conn.request :url_encoded
45
44
  conn.response :raise_error
46
45
  conn.adapter described_class, *adapter_options
@@ -126,19 +126,6 @@ shared_examples 'a request method' do |http_method|
126
126
  expect { conn.public_send(http_method, '/') }.to raise_error(exc)
127
127
  end
128
128
 
129
- # Can't send files on get, head and delete methods
130
- if method_with_body?(http_method)
131
- it 'sends files' do
132
- payload = { uploaded_file: multipart_file }
133
- request_stub.with(headers: { 'Content-Type' => %r{\Amultipart/form-data} }) do |request|
134
- # WebMock does not support matching body for multipart/form-data requests yet :(
135
- # https://github.com/bblimke/webmock/issues/623
136
- request.body.include?('RubyMultipartPost')
137
- end
138
- conn.public_send(http_method, '/', payload)
139
- end
140
- end
141
-
142
129
  on_feature :reason_phrase_parse do
143
130
  it 'parses the reason phrase' do
144
131
  request_stub.to_return(status: [200, 'OK'])
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.0.0.alpha.pre.2
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "@technoweenie"
@@ -10,28 +10,22 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-10-28 00:00:00.000000000 Z
13
+ date: 2022-01-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: multipart-post
16
+ name: faraday-net_http
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - ">="
20
- - !ruby/object:Gem::Version
21
- version: '1.2'
22
- - - "<"
19
+ - - "~>"
23
20
  - !ruby/object:Gem::Version
24
- version: '3'
21
+ version: '2.0'
25
22
  type: :runtime
26
23
  prerelease: false
27
24
  version_requirements: !ruby/object:Gem::Requirement
28
25
  requirements:
29
- - - ">="
26
+ - - "~>"
30
27
  - !ruby/object:Gem::Version
31
- version: '1.2'
32
- - - "<"
33
- - !ruby/object:Gem::Version
34
- version: '3'
28
+ version: '2.0'
35
29
  - !ruby/object:Gem::Dependency
36
30
  name: ruby2_keywords
37
31
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +60,6 @@ files:
66
60
  - lib/faraday/encoders/flat_params_encoder.rb
67
61
  - lib/faraday/encoders/nested_params_encoder.rb
68
62
  - lib/faraday/error.rb
69
- - lib/faraday/file_part.rb
70
63
  - lib/faraday/logging/formatter.rb
71
64
  - lib/faraday/methods.rb
72
65
  - lib/faraday/middleware.rb
@@ -77,15 +70,12 @@ files:
77
70
  - lib/faraday/options/proxy_options.rb
78
71
  - lib/faraday/options/request_options.rb
79
72
  - lib/faraday/options/ssl_options.rb
80
- - lib/faraday/param_part.rb
81
73
  - lib/faraday/parameters.rb
82
74
  - lib/faraday/rack_builder.rb
83
75
  - lib/faraday/request.rb
84
76
  - lib/faraday/request/authorization.rb
85
77
  - lib/faraday/request/instrumentation.rb
86
78
  - lib/faraday/request/json.rb
87
- - lib/faraday/request/multipart.rb
88
- - lib/faraday/request/retry.rb
89
79
  - lib/faraday/request/url_encoded.rb
90
80
  - lib/faraday/response.rb
91
81
  - lib/faraday/response/json.rb
@@ -99,7 +89,6 @@ files:
99
89
  - spec/faraday/adapter/test_spec.rb
100
90
  - spec/faraday/adapter_registry_spec.rb
101
91
  - spec/faraday/adapter_spec.rb
102
- - spec/faraday/composite_read_io_spec.rb
103
92
  - spec/faraday/connection_spec.rb
104
93
  - spec/faraday/error_spec.rb
105
94
  - spec/faraday/middleware_spec.rb
@@ -113,8 +102,6 @@ files:
113
102
  - spec/faraday/request/authorization_spec.rb
114
103
  - spec/faraday/request/instrumentation_spec.rb
115
104
  - spec/faraday/request/json_spec.rb
116
- - spec/faraday/request/multipart_spec.rb
117
- - spec/faraday/request/retry_spec.rb
118
105
  - spec/faraday/request/url_encoded_spec.rb
119
106
  - spec/faraday/request_spec.rb
120
107
  - spec/faraday/response/json_spec.rb
@@ -137,7 +124,7 @@ licenses:
137
124
  - MIT
138
125
  metadata:
139
126
  homepage_uri: https://lostisland.github.io/faraday
140
- changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.0.0.alpha.pre.2
127
+ changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.0.1
141
128
  source_code_uri: https://github.com/lostisland/faraday
142
129
  bug_tracker_uri: https://github.com/lostisland/faraday/issues
143
130
  post_install_message:
@@ -152,9 +139,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
139
  version: '2.6'
153
140
  required_rubygems_version: !ruby/object:Gem::Requirement
154
141
  requirements:
155
- - - ">"
142
+ - - ">="
156
143
  - !ruby/object:Gem::Version
157
- version: 1.3.1
144
+ version: '0'
158
145
  requirements: []
159
146
  rubygems_version: 3.1.6
160
147
  signing_key: