faraday 0.12.0 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +380 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +21 -308
  5. data/Rakefile +7 -0
  6. data/examples/client_spec.rb +65 -0
  7. data/examples/client_test.rb +79 -0
  8. data/lib/faraday/adapter/httpclient.rb +83 -59
  9. data/lib/faraday/adapter/patron.rb +92 -36
  10. data/lib/faraday/adapter/rack.rb +30 -13
  11. data/lib/faraday/adapter/test.rb +103 -62
  12. data/lib/faraday/adapter/typhoeus.rb +7 -115
  13. data/lib/faraday/adapter.rb +77 -22
  14. data/lib/faraday/adapter_registry.rb +30 -0
  15. data/lib/faraday/autoload.rb +42 -36
  16. data/lib/faraday/connection.rb +345 -179
  17. data/lib/faraday/dependency_loader.rb +37 -0
  18. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  19. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  20. data/lib/faraday/error.rb +127 -38
  21. data/lib/faraday/file_part.rb +128 -0
  22. data/lib/faraday/logging/formatter.rb +105 -0
  23. data/lib/faraday/methods.rb +6 -0
  24. data/lib/faraday/middleware.rb +19 -25
  25. data/lib/faraday/middleware_registry.rb +129 -0
  26. data/lib/faraday/options/connection_options.rb +22 -0
  27. data/lib/faraday/options/env.rb +181 -0
  28. data/lib/faraday/options/proxy_options.rb +32 -0
  29. data/lib/faraday/options/request_options.rb +22 -0
  30. data/lib/faraday/options/ssl_options.rb +59 -0
  31. data/lib/faraday/options.rb +39 -193
  32. data/lib/faraday/param_part.rb +53 -0
  33. data/lib/faraday/parameters.rb +4 -196
  34. data/lib/faraday/rack_builder.rb +84 -48
  35. data/lib/faraday/request/authorization.rb +44 -30
  36. data/lib/faraday/request/basic_authentication.rb +14 -7
  37. data/lib/faraday/request/instrumentation.rb +45 -27
  38. data/lib/faraday/request/multipart.rb +86 -48
  39. data/lib/faraday/request/retry.rb +209 -133
  40. data/lib/faraday/request/token_authentication.rb +15 -10
  41. data/lib/faraday/request/url_encoded.rb +43 -23
  42. data/lib/faraday/request.rb +94 -32
  43. data/lib/faraday/response/logger.rb +22 -69
  44. data/lib/faraday/response/raise_error.rb +49 -14
  45. data/lib/faraday/response.rb +27 -23
  46. data/lib/faraday/utils/headers.rb +139 -0
  47. data/lib/faraday/utils/params_hash.rb +61 -0
  48. data/lib/faraday/utils.rb +38 -238
  49. data/lib/faraday/version.rb +5 -0
  50. data/lib/faraday.rb +124 -187
  51. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  52. data/spec/faraday/adapter/em_http_spec.rb +47 -0
  53. data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
  54. data/spec/faraday/adapter/excon_spec.rb +49 -0
  55. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  56. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  57. data/spec/faraday/adapter/patron_spec.rb +18 -0
  58. data/spec/faraday/adapter/rack_spec.rb +8 -0
  59. data/spec/faraday/adapter/test_spec.rb +260 -0
  60. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  61. data/spec/faraday/adapter_registry_spec.rb +28 -0
  62. data/spec/faraday/adapter_spec.rb +55 -0
  63. data/spec/faraday/composite_read_io_spec.rb +80 -0
  64. data/spec/faraday/connection_spec.rb +736 -0
  65. data/spec/faraday/error_spec.rb +60 -0
  66. data/spec/faraday/middleware_spec.rb +52 -0
  67. data/spec/faraday/options/env_spec.rb +70 -0
  68. data/spec/faraday/options/options_spec.rb +297 -0
  69. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  70. data/spec/faraday/options/request_options_spec.rb +19 -0
  71. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  72. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  73. data/spec/faraday/rack_builder_spec.rb +345 -0
  74. data/spec/faraday/request/authorization_spec.rb +88 -0
  75. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  76. data/spec/faraday/request/multipart_spec.rb +302 -0
  77. data/spec/faraday/request/retry_spec.rb +242 -0
  78. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  79. data/spec/faraday/request_spec.rb +120 -0
  80. data/spec/faraday/response/logger_spec.rb +220 -0
  81. data/spec/faraday/response/middleware_spec.rb +68 -0
  82. data/spec/faraday/response/raise_error_spec.rb +169 -0
  83. data/spec/faraday/response_spec.rb +75 -0
  84. data/spec/faraday/utils/headers_spec.rb +82 -0
  85. data/spec/faraday/utils_spec.rb +56 -0
  86. data/spec/faraday_spec.rb +37 -0
  87. data/spec/spec_helper.rb +132 -0
  88. data/spec/support/disabling_stub.rb +14 -0
  89. data/spec/support/fake_safe_buffer.rb +15 -0
  90. data/spec/support/helper_methods.rb +133 -0
  91. data/spec/support/shared_examples/adapter.rb +105 -0
  92. data/spec/support/shared_examples/params_encoder.rb +18 -0
  93. data/spec/support/shared_examples/request_method.rb +262 -0
  94. data/spec/support/streaming_response_checker.rb +35 -0
  95. data/spec/support/webmock_rack_app.rb +68 -0
  96. metadata +164 -16
  97. data/lib/faraday/adapter/em_http.rb +0 -243
  98. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  99. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  100. data/lib/faraday/adapter/em_synchrony.rb +0 -106
  101. data/lib/faraday/adapter/excon.rb +0 -80
  102. data/lib/faraday/adapter/net_http.rb +0 -135
  103. data/lib/faraday/adapter/net_http_persistent.rb +0 -50
  104. data/lib/faraday/upload_io.rb +0 -67
data/lib/faraday.rb CHANGED
@@ -1,243 +1,180 @@
1
- require 'thread'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'cgi'
4
+ require 'date'
3
5
  require 'set'
4
6
  require 'forwardable'
7
+ require 'faraday/middleware_registry'
8
+ require 'faraday/dependency_loader'
9
+
10
+ unless defined?(::Faraday::Timer)
11
+ require 'timeout'
12
+ ::Faraday::Timer = Timeout
13
+ end
5
14
 
6
- # Public: This is the main namespace for Faraday. You can either use it to
7
- # create Faraday::Connection objects, or access it directly.
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
+
30
+ require 'faraday/em_http'
31
+ require 'faraday/em_synchrony'
32
+ require 'faraday/excon'
33
+ require 'faraday/net_http'
34
+ require 'faraday/net_http_persistent'
35
+
36
+ # This is the main namespace for Faraday.
8
37
  #
9
- # Examples
38
+ # It provides methods to create {Connection} objects, and HTTP-related
39
+ # methods to use directly.
10
40
  #
41
+ # @example Helpful class methods for easy usage
11
42
  # Faraday.get "http://faraday.com"
12
43
  #
44
+ # @example Helpful class method `.new` to create {Connection} objects.
13
45
  # conn = Faraday.new "http://faraday.com"
14
46
  # conn.get '/'
15
47
  #
16
48
  module Faraday
17
- VERSION = "0.12.0"
18
-
19
49
  class << self
20
- # Public: Gets or sets the root path that Faraday is being loaded from.
21
- # This is the root from where the libraries are auto-loaded from.
50
+ # The root path that Faraday is being loaded from.
51
+ #
52
+ # This is the root from where the libraries are auto-loaded.
53
+ #
54
+ # @return [String]
22
55
  attr_accessor :root_path
23
56
 
24
- # Public: Gets or sets the path that the Faraday libs are loaded from.
57
+ # Gets or sets the path that the Faraday libs are loaded from.
58
+ # @return [String]
25
59
  attr_accessor :lib_path
26
60
 
27
- # Public: Gets or sets the Symbol key identifying a default Adapter to use
28
- # for the default Faraday::Connection.
61
+ # @overload default_adapter
62
+ # Gets the Symbol key identifying a default Adapter to use
63
+ # for the default {Faraday::Connection}. Defaults to `:net_http`.
64
+ # @return [Symbol] the default adapter
65
+ # @overload default_adapter=(adapter)
66
+ # Updates default adapter while resetting {.default_connection}.
67
+ # @return [Symbol] the new default_adapter.
29
68
  attr_reader :default_adapter
30
69
 
31
- # Public: Sets the default Faraday::Connection for simple scripts that
32
- # access the Faraday constant directly.
33
- #
34
- # Faraday.get "https://faraday.com"
70
+ # Documented below, see default_connection
35
71
  attr_writer :default_connection
36
72
 
37
- # Public: Initializes a new Faraday::Connection.
38
- #
39
- # url - The optional String base URL to use as a prefix for all
40
- # requests. Can also be the options Hash.
41
- # options - The optional Hash used to configure this Faraday::Connection.
42
- # Any of these values will be set on every request made, unless
43
- # overridden for a specific request.
44
- # :url - String base URL.
45
- # :params - Hash of URI query unencoded key/value pairs.
46
- # :headers - Hash of unencoded HTTP header key/value pairs.
47
- # :request - Hash of request options.
48
- # :ssl - Hash of SSL options.
49
- # :proxy - Hash of Proxy options.
50
- #
51
- # Examples
52
- #
73
+ # Tells Faraday to ignore the environment proxy (http_proxy).
74
+ # Defaults to `false`.
75
+ # @return [Boolean]
76
+ attr_accessor :ignore_env_proxy
77
+
78
+ # Initializes a new {Connection}.
79
+ #
80
+ # @param url [String,Hash] The optional String base URL to use as a prefix
81
+ # for all requests. Can also be the options Hash. Any of these
82
+ # values will be set on every request made, unless overridden
83
+ # for a specific request.
84
+ # @param options [Hash]
85
+ # @option options [String] :url Base URL
86
+ # @option options [Hash] :params Hash of unencoded URI query params.
87
+ # @option options [Hash] :headers Hash of unencoded HTTP headers.
88
+ # @option options [Hash] :request Hash of request options.
89
+ # @option options [Hash] :ssl Hash of SSL options.
90
+ # @option options [Hash] :proxy Hash of Proxy options.
91
+ # @return [Faraday::Connection]
92
+ #
93
+ # @example With an URL argument
53
94
  # Faraday.new 'http://faraday.com'
54
- #
55
- # # http://faraday.com?page=1
56
- # Faraday.new 'http://faraday.com', :params => {:page => 1}
57
- #
58
- # # same
59
- #
60
- # Faraday.new :url => 'http://faraday.com',
61
- # :params => {:page => 1}
62
- #
63
- # Returns a Faraday::Connection.
64
- def new(url = nil, options = nil)
65
- block = block_given? ? Proc.new : nil
66
- options = options ? default_connection_options.merge(options) : default_connection_options
95
+ # # => Faraday::Connection to http://faraday.com
96
+ #
97
+ # @example With an URL argument and an options hash
98
+ # Faraday.new 'http://faraday.com', params: { page: 1 }
99
+ # # => Faraday::Connection to http://faraday.com?page=1
100
+ #
101
+ # @example With everything in an options hash
102
+ # Faraday.new url: 'http://faraday.com',
103
+ # params: { page: 1 }
104
+ # # => Faraday::Connection to http://faraday.com?page=1
105
+ def new(url = nil, options = {}, &block)
106
+ options = default_connection_options.merge(options)
67
107
  Faraday::Connection.new(url, options, &block)
68
108
  end
69
109
 
110
+ # @private
70
111
  # Internal: Requires internal Faraday libraries.
71
112
  #
72
- # *libs - One or more relative String names to Faraday classes.
73
- #
74
- # Returns nothing.
113
+ # @param libs [Array] one or more relative String names to Faraday classes.
114
+ # @return [void]
75
115
  def require_libs(*libs)
76
116
  libs.each do |lib|
77
117
  require "#{lib_path}/#{lib}"
78
118
  end
79
119
  end
80
120
 
81
- # Public: Updates default adapter while resetting
82
- # #default_connection.
83
- #
84
- # Returns the new default_adapter.
121
+ alias require_lib require_libs
122
+
123
+ # Documented elsewhere, see default_adapter reader
85
124
  def default_adapter=(adapter)
86
125
  @default_connection = nil
87
126
  @default_adapter = adapter
88
127
  end
89
128
 
90
- alias require_lib require_libs
91
-
92
- def respond_to?(symbol, include_private = false)
129
+ def respond_to_missing?(symbol, include_private = false)
93
130
  default_connection.respond_to?(symbol, include_private) || super
94
131
  end
95
132
 
96
- private
97
- # Internal: Proxies method calls on the Faraday constant to
98
- # #default_connection.
99
- def method_missing(name, *args, &block)
100
- default_connection.send(name, *args, &block)
133
+ # @overload default_connection
134
+ # Gets the default connection used for simple scripts.
135
+ # @return [Faraday::Connection] a connection configured with
136
+ # the default_adapter.
137
+ # @overload default_connection=(connection)
138
+ # @param connection [Faraday::Connection]
139
+ # Sets the default {Faraday::Connection} for simple scripts that
140
+ # access the Faraday constant directly, such as
141
+ # <code>Faraday.get "https://faraday.com"</code>.
142
+ def default_connection
143
+ @default_connection ||= Connection.new(default_connection_options)
101
144
  end
102
- end
103
-
104
- self.root_path = File.expand_path "..", __FILE__
105
- self.lib_path = File.expand_path "../faraday", __FILE__
106
- self.default_adapter = :net_http
107
-
108
- # Gets the default connection used for simple scripts.
109
- #
110
- # Returns a Faraday::Connection, configured with the #default_adapter.
111
- def self.default_connection
112
- @default_connection ||= Connection.new
113
- end
114
-
115
- # Gets the default connection options used when calling Faraday#new.
116
- #
117
- # Returns a Faraday::ConnectionOptions.
118
- def self.default_connection_options
119
- @default_connection_options ||= ConnectionOptions.new
120
- end
121
-
122
- # Public: Sets the default options used when calling Faraday#new.
123
- def self.default_connection_options=(options)
124
- @default_connection_options = ConnectionOptions.from(options)
125
- end
126
145
 
127
- unless const_defined? :Timer
128
- require 'timeout'
129
- Timer = Timeout
130
- end
131
-
132
- # Public: Adds the ability for other modules to register and lookup
133
- # middleware classes.
134
- module MiddlewareRegistry
135
- # Public: Register middleware class(es) on the current module.
136
- #
137
- # mapping - A Hash mapping Symbol keys to classes. Classes can be expressed
138
- # as fully qualified constant, or a Proc that will be lazily
139
- # called to return the former.
140
- #
141
- # Examples
146
+ # Gets the default connection options used when calling {Faraday#new}.
142
147
  #
143
- # module Faraday
144
- # class Whatever
145
- # # Middleware looked up by :foo returns Faraday::Whatever::Foo.
146
- # register_middleware :foo => Foo
147
- #
148
- # # Middleware looked up by :bar returns Faraday::Whatever.const_get(:Bar)
149
- # register_middleware :bar => :Bar
150
- #
151
- # # Middleware looked up by :baz requires 'baz' and returns Faraday::Whatever.const_get(:Baz)
152
- # register_middleware :baz => [:Baz, 'baz']
153
- # end
154
- # end
155
- #
156
- # Returns nothing.
157
- def register_middleware(autoload_path = nil, mapping = nil)
158
- if mapping.nil?
159
- mapping = autoload_path
160
- autoload_path = nil
161
- end
162
- middleware_mutex do
163
- @middleware_autoload_path = autoload_path if autoload_path
164
- (@registered_middleware ||= {}).update(mapping)
165
- end
148
+ # @return [Faraday::ConnectionOptions]
149
+ def default_connection_options
150
+ @default_connection_options ||= ConnectionOptions.new
166
151
  end
167
152
 
168
- # Public: Lookup middleware class with a registered Symbol shortcut.
169
- #
170
- # key - The Symbol key for the registered middleware.
171
- #
172
- # Examples
153
+ # Sets the default options used when calling {Faraday#new}.
173
154
  #
174
- # module Faraday
175
- # class Whatever
176
- # register_middleware :foo => Foo
177
- # end
178
- # end
179
- #
180
- # Faraday::Whatever.lookup_middleware(:foo)
181
- # # => Faraday::Whatever::Foo
182
- #
183
- # Returns a middleware Class.
184
- def lookup_middleware(key)
185
- load_middleware(key) ||
186
- raise(Faraday::Error.new("#{key.inspect} is not registered on #{self}"))
187
- end
188
-
189
- def middleware_mutex(&block)
190
- @middleware_mutex ||= begin
191
- require 'monitor'
192
- Monitor.new
193
- end
194
- @middleware_mutex.synchronize(&block)
155
+ # @param options [Hash, Faraday::ConnectionOptions]
156
+ def default_connection_options=(options)
157
+ @default_connection = nil
158
+ @default_connection_options = ConnectionOptions.from(options)
195
159
  end
196
160
 
197
- def fetch_middleware(key)
198
- defined?(@registered_middleware) && @registered_middleware[key]
199
- end
161
+ private
200
162
 
201
- def load_middleware(key)
202
- value = fetch_middleware(key)
203
- case value
204
- when Module
205
- value
206
- when Symbol, String
207
- middleware_mutex do
208
- @registered_middleware[key] = const_get(value)
209
- end
210
- when Proc
211
- middleware_mutex do
212
- @registered_middleware[key] = value.call
213
- end
214
- when Array
215
- middleware_mutex do
216
- const, path = value
217
- if root = @middleware_autoload_path
218
- path = "#{root}/#{path}"
219
- end
220
- require(path)
221
- @registered_middleware[key] = const
222
- end
223
- load_middleware(key)
163
+ # Internal: Proxies method calls on the Faraday constant to
164
+ # .default_connection.
165
+ def method_missing(name, *args, &block)
166
+ if default_connection.respond_to?(name)
167
+ default_connection.send(name, *args, &block)
168
+ else
169
+ super
224
170
  end
225
171
  end
226
172
  end
227
173
 
228
- def self.const_missing(name)
229
- if name.to_sym == :Builder
230
- warn "Faraday::Builder is now Faraday::RackBuilder."
231
- const_set name, RackBuilder
232
- else
233
- super
234
- end
235
- end
236
-
237
- require_libs "utils", "options", "connection", "rack_builder", "parameters",
238
- "middleware", "adapter", "request", "response", "upload_io", "error"
174
+ self.ignore_env_proxy = false
175
+ self.root_path = File.expand_path __dir__
176
+ self.lib_path = File.expand_path 'faraday', __dir__
177
+ self.default_adapter = :net_http
239
178
 
240
- if !ENV["FARADAY_NO_AUTOLOAD"]
241
- require_lib 'autoload'
242
- end
179
+ require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
243
180
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'webmock/rspec'
4
+ WebMock.disable_net_connect!(allow_localhost: true)
5
+
6
+ require_relative '../support/helper_methods'
7
+ require_relative '../support/disabling_stub'
8
+ require_relative '../support/streaming_response_checker'
9
+ require_relative '../support/shared_examples/adapter'
10
+ require_relative '../support/shared_examples/request_method'
11
+
12
+ RSpec.configure do |config|
13
+ config.include Faraday::HelperMethods
14
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Faraday::Adapter::EMHttp, unless: defined?(JRUBY_VERSION) do
4
+ features :request_body_on_query_methods, :reason_phrase_parse, :trace_method,
5
+ :skip_response_body_on_head, :parallel, :local_socket_binding
6
+
7
+ it_behaves_like 'an adapter'
8
+
9
+ it 'allows to provide adapter specific configs' do
10
+ url = URI('https://example.com:1234')
11
+ adapter = described_class.new nil, inactivity_timeout: 20
12
+ req = adapter.create_request(url: url, request: {})
13
+
14
+ expect(req.connopts.inactivity_timeout).to eq(20)
15
+ end
16
+
17
+ context 'Options' do
18
+ let(:request) { Faraday::RequestOptions.new }
19
+ let(:env) { { request: request } }
20
+ let(:options) { {} }
21
+ let(:adapter) { Faraday::Adapter::EMHttp.new }
22
+
23
+ it 'configures timeout' do
24
+ request.timeout = 5
25
+ adapter.configure_timeout(options, env)
26
+ expect(options[:inactivity_timeout]).to eq(5)
27
+ expect(options[:connect_timeout]).to eq(5)
28
+ end
29
+
30
+ it 'configures timeout and open_timeout' do
31
+ request.timeout = 5
32
+ request.open_timeout = 1
33
+ adapter.configure_timeout(options, env)
34
+ expect(options[:inactivity_timeout]).to eq(5)
35
+ expect(options[:connect_timeout]).to eq(1)
36
+ end
37
+
38
+ it 'configures all timeout settings' do
39
+ request.timeout = 5
40
+ request.read_timeout = 3
41
+ request.open_timeout = 1
42
+ adapter.configure_timeout(options, env)
43
+ expect(options[:inactivity_timeout]).to eq(3)
44
+ expect(options[:connect_timeout]).to eq(1)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Faraday::Adapter::EMSynchrony, unless: defined?(JRUBY_VERSION) do
4
+ features :request_body_on_query_methods, :reason_phrase_parse,
5
+ :skip_response_body_on_head, :parallel, :local_socket_binding
6
+
7
+ it_behaves_like 'an adapter'
8
+
9
+ it 'allows to provide adapter specific configs' do
10
+ url = URI('https://example.com:1234')
11
+ adapter = described_class.new nil, inactivity_timeout: 20
12
+ req = adapter.create_request(url: url, request: {})
13
+
14
+ expect(req.connopts.inactivity_timeout).to eq(20)
15
+ end
16
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Faraday::Adapter::Excon do
4
+ features :request_body_on_query_methods, :reason_phrase_parse, :trace_method
5
+
6
+ it_behaves_like 'an adapter'
7
+
8
+ it 'allows to provide adapter specific configs' do
9
+ url = URI('https://example.com:1234')
10
+
11
+ adapter = described_class.new(nil, debug_request: true)
12
+
13
+ conn = adapter.build_connection(url: url)
14
+
15
+ expect(conn.data[:debug_request]).to be_truthy
16
+ end
17
+
18
+ context 'config' do
19
+ let(:adapter) { Faraday::Adapter::Excon.new }
20
+ let(:request) { Faraday::RequestOptions.new }
21
+ let(:uri) { URI.parse('https://example.com') }
22
+ let(:env) { { request: request, url: uri } }
23
+
24
+ it 'sets timeout' do
25
+ request.timeout = 5
26
+ options = adapter.send(:opts_from_env, env)
27
+ expect(options[:read_timeout]).to eq(5)
28
+ expect(options[:write_timeout]).to eq(5)
29
+ expect(options[:connect_timeout]).to eq(5)
30
+ end
31
+
32
+ it 'sets timeout and open_timeout' do
33
+ request.timeout = 5
34
+ request.open_timeout = 3
35
+ options = adapter.send(:opts_from_env, env)
36
+ expect(options[:read_timeout]).to eq(5)
37
+ expect(options[:write_timeout]).to eq(5)
38
+ expect(options[:connect_timeout]).to eq(3)
39
+ end
40
+
41
+ it 'sets open_timeout' do
42
+ request.open_timeout = 3
43
+ options = adapter.send(:opts_from_env, env)
44
+ expect(options[:read_timeout]).to eq(nil)
45
+ expect(options[:write_timeout]).to eq(nil)
46
+ expect(options[:connect_timeout]).to eq(3)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Faraday::Adapter::HTTPClient do
4
+ # ruby gem defaults for testing purposes
5
+ HTTPCLIENT_OPEN = 60
6
+ HTTPCLIENT_READ = 60
7
+ HTTPCLIENT_WRITE = 120
8
+
9
+ features :request_body_on_query_methods, :reason_phrase_parse, :compression,
10
+ :trace_method, :local_socket_binding
11
+
12
+ it_behaves_like 'an adapter'
13
+
14
+ it 'allows to provide adapter specific configs' do
15
+ adapter = described_class.new do |client|
16
+ client.keep_alive_timeout = 20
17
+ client.ssl_config.timeout = 25
18
+ end
19
+
20
+ client = adapter.build_connection(url: URI.parse('https://example.com'))
21
+ expect(client.keep_alive_timeout).to eq(20)
22
+ expect(client.ssl_config.timeout).to eq(25)
23
+ end
24
+
25
+ context 'Options' do
26
+ let(:request) { Faraday::RequestOptions.new }
27
+ let(:env) { { request: request } }
28
+ let(:options) { {} }
29
+ let(:adapter) { Faraday::Adapter::HTTPClient.new }
30
+ let(:client) { adapter.connection(url: URI.parse('https://example.com')) }
31
+
32
+ it 'configures timeout' do
33
+ assert_default_timeouts!
34
+
35
+ request.timeout = 5
36
+ adapter.configure_timeouts(client, request)
37
+
38
+ expect(client.connect_timeout).to eq(5)
39
+ expect(client.send_timeout).to eq(5)
40
+ expect(client.receive_timeout).to eq(5)
41
+ end
42
+
43
+ it 'configures open timeout' do
44
+ assert_default_timeouts!
45
+
46
+ request.open_timeout = 1
47
+ adapter.configure_timeouts(client, request)
48
+
49
+ expect(client.connect_timeout).to eq(1)
50
+ expect(client.send_timeout).to eq(HTTPCLIENT_WRITE)
51
+ expect(client.receive_timeout).to eq(HTTPCLIENT_READ)
52
+ end
53
+
54
+ it 'configures multiple timeouts' do
55
+ assert_default_timeouts!
56
+
57
+ request.open_timeout = 1
58
+ request.write_timeout = 10
59
+ request.read_timeout = 5
60
+ adapter.configure_timeouts(client, request)
61
+
62
+ expect(client.connect_timeout).to eq(1)
63
+ expect(client.send_timeout).to eq(10)
64
+ expect(client.receive_timeout).to eq(5)
65
+ end
66
+
67
+ def assert_default_timeouts!
68
+ expect(client.connect_timeout).to eq(HTTPCLIENT_OPEN)
69
+ expect(client.send_timeout).to eq(HTTPCLIENT_WRITE)
70
+ expect(client.receive_timeout).to eq(HTTPCLIENT_READ)
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Faraday::Adapter::NetHttp do
4
+ features :request_body_on_query_methods, :reason_phrase_parse, :compression, :streaming, :trace_method
5
+
6
+ it_behaves_like 'an adapter'
7
+
8
+ context 'checking http' do
9
+ let(:url) { URI('http://example.com') }
10
+ let(:adapter) { described_class.new }
11
+ let(:http) { adapter.send(:connection, url: url, request: {}) }
12
+
13
+ it { expect(http.port).to eq(80) }
14
+
15
+ it 'sets max_retries to 0' do
16
+ adapter.send(:configure_request, http, {})
17
+
18
+ expect(http.max_retries).to eq(0) if http.respond_to?(:max_retries=)
19
+ end
20
+
21
+ it 'supports write_timeout' do
22
+ adapter.send(:configure_request, http, write_timeout: 10)
23
+
24
+ expect(http.write_timeout).to eq(10) if http.respond_to?(:write_timeout=)
25
+ end
26
+
27
+ it 'supports open_timeout' do
28
+ adapter.send(:configure_request, http, open_timeout: 10)
29
+
30
+ expect(http.open_timeout).to eq(10)
31
+ end
32
+
33
+ it 'supports read_timeout' do
34
+ adapter.send(:configure_request, http, read_timeout: 10)
35
+
36
+ expect(http.read_timeout).to eq(10)
37
+ end
38
+
39
+ context 'with https url' do
40
+ let(:url) { URI('https://example.com') }
41
+
42
+ it { expect(http.port).to eq(443) }
43
+ end
44
+
45
+ context 'with http url including port' do
46
+ let(:url) { URI('https://example.com:1234') }
47
+
48
+ it { expect(http.port).to eq(1234) }
49
+ end
50
+
51
+ context 'with custom adapter config' do
52
+ let(:adapter) do
53
+ described_class.new do |http|
54
+ http.continue_timeout = 123
55
+ end
56
+ end
57
+
58
+ it do
59
+ adapter.send(:configure_request, http, {})
60
+ expect(http.continue_timeout).to eq(123)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Faraday::Adapter::Patron, unless: defined?(JRUBY_VERSION) do
4
+ features :reason_phrase_parse
5
+
6
+ it_behaves_like 'an adapter'
7
+
8
+ it 'allows to provide adapter specific configs' do
9
+ conn = Faraday.new do |f|
10
+ f.adapter :patron do |session|
11
+ session.max_redirects = 10
12
+ raise 'Configuration block called'
13
+ end
14
+ end
15
+
16
+ expect { conn.get('/') }.to raise_error(RuntimeError, 'Configuration block called')
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Faraday::Adapter::Rack do
4
+ features :request_body_on_query_methods, :trace_method,
5
+ :skip_response_body_on_head
6
+
7
+ it_behaves_like 'an adapter', adapter_options: WebmockRackApp.new
8
+ end