faraday 0.9.1 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +360 -0
- data/LICENSE.md +1 -1
- data/README.md +28 -195
- data/Rakefile +4 -68
- data/examples/client_spec.rb +97 -0
- data/examples/client_test.rb +118 -0
- data/lib/faraday/adapter/test.rb +158 -58
- data/lib/faraday/adapter/typhoeus.rb +7 -115
- data/lib/faraday/adapter.rb +79 -20
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +39 -36
- data/lib/faraday/connection.rb +378 -168
- data/lib/faraday/dependency_loader.rb +37 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
- data/lib/faraday/error.rb +128 -29
- data/lib/faraday/file_part.rb +128 -0
- data/lib/faraday/logging/formatter.rb +105 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +19 -25
- data/lib/faraday/middleware_registry.rb +129 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +32 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +61 -193
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +4 -180
- data/lib/faraday/rack_builder.rb +84 -47
- data/lib/faraday/request/authorization.rb +51 -31
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +88 -45
- data/lib/faraday/request/retry.rb +212 -121
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +43 -23
- data/lib/faraday/request.rb +96 -32
- data/lib/faraday/response/logger.rb +22 -48
- data/lib/faraday/response/raise_error.rb +49 -14
- data/lib/faraday/response.rb +33 -25
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +38 -218
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +130 -213
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/em_http_spec.rb +49 -0
- data/spec/faraday/adapter/em_synchrony_spec.rb +18 -0
- data/spec/faraday/adapter/excon_spec.rb +49 -0
- data/spec/faraday/adapter/httpclient_spec.rb +73 -0
- data/spec/faraday/adapter/net_http_spec.rb +64 -0
- data/spec/faraday/adapter/patron_spec.rb +18 -0
- data/spec/faraday/adapter/rack_spec.rb +8 -0
- data/spec/faraday/adapter/test_spec.rb +377 -0
- data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/composite_read_io_spec.rb +80 -0
- data/spec/faraday/connection_spec.rb +736 -0
- data/spec/faraday/error_spec.rb +60 -0
- data/spec/faraday/middleware_spec.rb +52 -0
- data/spec/faraday/options/env_spec.rb +70 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +44 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/spec/faraday/params_encoders/nested_spec.rb +142 -0
- data/spec/faraday/rack_builder_spec.rb +345 -0
- data/spec/faraday/request/authorization_spec.rb +96 -0
- data/spec/faraday/request/instrumentation_spec.rb +76 -0
- data/spec/faraday/request/multipart_spec.rb +302 -0
- data/spec/faraday/request/retry_spec.rb +242 -0
- data/spec/faraday/request/url_encoded_spec.rb +83 -0
- data/spec/faraday/request_spec.rb +120 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/middleware_spec.rb +68 -0
- data/spec/faraday/response/raise_error_spec.rb +169 -0
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +56 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +132 -0
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +133 -0
- data/spec/support/shared_examples/adapter.rb +105 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +262 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- data/spec/support/webmock_rack_app.rb +68 -0
- metadata +199 -98
- data/.document +0 -6
- data/CONTRIBUTING.md +0 -36
- data/Gemfile +0 -25
- data/faraday.gemspec +0 -34
- data/lib/faraday/adapter/em_http.rb +0 -237
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
- data/lib/faraday/adapter/em_synchrony.rb +0 -92
- data/lib/faraday/adapter/excon.rb +0 -80
- data/lib/faraday/adapter/httpclient.rb +0 -106
- data/lib/faraday/adapter/net_http.rb +0 -130
- data/lib/faraday/adapter/net_http_persistent.rb +0 -48
- data/lib/faraday/adapter/patron.rb +0 -72
- data/lib/faraday/adapter/rack.rb +0 -58
- data/lib/faraday/upload_io.rb +0 -67
- data/script/cached-bundle +0 -46
- data/script/console +0 -7
- data/script/generate_certs +0 -42
- data/script/package +0 -7
- data/script/proxy-server +0 -42
- data/script/release +0 -17
- data/script/s3-put +0 -71
- data/script/server +0 -36
- data/script/test +0 -172
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -20
- data/test/adapters/em_synchrony_test.rb +0 -20
- data/test/adapters/excon_test.rb +0 -20
- data/test/adapters/httpclient_test.rb +0 -21
- data/test/adapters/integration.rb +0 -254
- data/test/adapters/logger_test.rb +0 -82
- data/test/adapters/net_http_persistent_test.rb +0 -20
- data/test/adapters/net_http_test.rb +0 -14
- data/test/adapters/patron_test.rb +0 -20
- data/test/adapters/rack_test.rb +0 -31
- data/test/adapters/test_middleware_test.rb +0 -114
- data/test/adapters/typhoeus_test.rb +0 -28
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -111
- data/test/connection_test.rb +0 -522
- data/test/env_test.rb +0 -218
- data/test/helper.rb +0 -81
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -177
- data/test/middleware_stack_test.rb +0 -173
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -252
- data/test/parameters_test.rb +0 -64
- data/test/request_middleware_test.rb +0 -142
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -58
data/lib/faraday.rb
CHANGED
@@ -1,268 +1,185 @@
|
|
1
|
-
|
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
|
-
|
7
|
-
|
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
|
+
unless defined?(JRUBY_VERSION)
|
31
|
+
require 'faraday/em_http'
|
32
|
+
require 'faraday/em_synchrony'
|
33
|
+
end
|
34
|
+
require 'faraday/excon'
|
35
|
+
require 'faraday/httpclient'
|
36
|
+
require 'faraday/net_http'
|
37
|
+
require 'faraday/net_http_persistent'
|
38
|
+
require 'faraday/patron'
|
39
|
+
require 'faraday/rack'
|
40
|
+
|
41
|
+
# This is the main namespace for Faraday.
|
8
42
|
#
|
9
|
-
#
|
43
|
+
# It provides methods to create {Connection} objects, and HTTP-related
|
44
|
+
# methods to use directly.
|
10
45
|
#
|
46
|
+
# @example Helpful class methods for easy usage
|
11
47
|
# Faraday.get "http://faraday.com"
|
12
48
|
#
|
49
|
+
# @example Helpful class method `.new` to create {Connection} objects.
|
13
50
|
# conn = Faraday.new "http://faraday.com"
|
14
51
|
# conn.get '/'
|
15
52
|
#
|
16
53
|
module Faraday
|
17
|
-
VERSION = "0.9.1"
|
18
|
-
|
19
54
|
class << self
|
20
|
-
#
|
21
|
-
#
|
55
|
+
# The root path that Faraday is being loaded from.
|
56
|
+
#
|
57
|
+
# This is the root from where the libraries are auto-loaded.
|
58
|
+
#
|
59
|
+
# @return [String]
|
22
60
|
attr_accessor :root_path
|
23
61
|
|
24
|
-
#
|
62
|
+
# Gets or sets the path that the Faraday libs are loaded from.
|
63
|
+
# @return [String]
|
25
64
|
attr_accessor :lib_path
|
26
65
|
|
27
|
-
#
|
28
|
-
#
|
66
|
+
# @overload default_adapter
|
67
|
+
# Gets the Symbol key identifying a default Adapter to use
|
68
|
+
# for the default {Faraday::Connection}. Defaults to `:net_http`.
|
69
|
+
# @return [Symbol] the default adapter
|
70
|
+
# @overload default_adapter=(adapter)
|
71
|
+
# Updates default adapter while resetting {.default_connection}.
|
72
|
+
# @return [Symbol] the new default_adapter.
|
29
73
|
attr_reader :default_adapter
|
30
74
|
|
31
|
-
#
|
32
|
-
# access the Faraday constant directly.
|
33
|
-
#
|
34
|
-
# Faraday.get "https://faraday.com"
|
75
|
+
# Documented below, see default_connection
|
35
76
|
attr_writer :default_connection
|
36
77
|
|
37
|
-
#
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
78
|
+
# Tells Faraday to ignore the environment proxy (http_proxy).
|
79
|
+
# Defaults to `false`.
|
80
|
+
# @return [Boolean]
|
81
|
+
attr_accessor :ignore_env_proxy
|
82
|
+
|
83
|
+
# Initializes a new {Connection}.
|
84
|
+
#
|
85
|
+
# @param url [String,Hash] The optional String base URL to use as a prefix
|
86
|
+
# for all requests. Can also be the options Hash. Any of these
|
87
|
+
# values will be set on every request made, unless overridden
|
88
|
+
# for a specific request.
|
89
|
+
# @param options [Hash]
|
90
|
+
# @option options [String] :url Base URL
|
91
|
+
# @option options [Hash] :params Hash of unencoded URI query params.
|
92
|
+
# @option options [Hash] :headers Hash of unencoded HTTP headers.
|
93
|
+
# @option options [Hash] :request Hash of request options.
|
94
|
+
# @option options [Hash] :ssl Hash of SSL options.
|
95
|
+
# @option options [Hash] :proxy Hash of Proxy options.
|
96
|
+
# @return [Faraday::Connection]
|
97
|
+
#
|
98
|
+
# @example With an URL argument
|
56
99
|
# Faraday.new 'http://faraday.com'
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
# #
|
62
|
-
#
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
def new(url = nil, options =
|
68
|
-
|
69
|
-
options = options ? default_connection_options.merge(options) : default_connection_options.dup
|
100
|
+
# # => Faraday::Connection to http://faraday.com
|
101
|
+
#
|
102
|
+
# @example With an URL argument and an options hash
|
103
|
+
# Faraday.new 'http://faraday.com', params: { page: 1 }
|
104
|
+
# # => Faraday::Connection to http://faraday.com?page=1
|
105
|
+
#
|
106
|
+
# @example With everything in an options hash
|
107
|
+
# Faraday.new url: 'http://faraday.com',
|
108
|
+
# params: { page: 1 }
|
109
|
+
# # => Faraday::Connection to http://faraday.com?page=1
|
110
|
+
def new(url = nil, options = {}, &block)
|
111
|
+
options = default_connection_options.merge(options)
|
70
112
|
Faraday::Connection.new(url, options, &block)
|
71
113
|
end
|
72
114
|
|
115
|
+
# @private
|
73
116
|
# Internal: Requires internal Faraday libraries.
|
74
117
|
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
# Returns nothing.
|
118
|
+
# @param libs [Array] one or more relative String names to Faraday classes.
|
119
|
+
# @return [void]
|
78
120
|
def require_libs(*libs)
|
79
121
|
libs.each do |lib|
|
80
122
|
require "#{lib_path}/#{lib}"
|
81
123
|
end
|
82
124
|
end
|
83
125
|
|
84
|
-
|
85
|
-
|
86
|
-
#
|
87
|
-
# Returns the new default_adapter.
|
126
|
+
alias require_lib require_libs
|
127
|
+
|
128
|
+
# Documented elsewhere, see default_adapter reader
|
88
129
|
def default_adapter=(adapter)
|
89
130
|
@default_connection = nil
|
90
131
|
@default_adapter = adapter
|
91
132
|
end
|
92
133
|
|
93
|
-
|
94
|
-
|
95
|
-
private
|
96
|
-
# Internal: Proxies method calls on the Faraday constant to
|
97
|
-
# #default_connection.
|
98
|
-
def method_missing(name, *args, &block)
|
99
|
-
default_connection.send(name, *args, &block)
|
134
|
+
def respond_to_missing?(symbol, include_private = false)
|
135
|
+
default_connection.respond_to?(symbol, include_private) || super
|
100
136
|
end
|
101
|
-
end
|
102
|
-
|
103
|
-
self.root_path = File.expand_path "..", __FILE__
|
104
|
-
self.lib_path = File.expand_path "../faraday", __FILE__
|
105
|
-
self.default_adapter = :net_http
|
106
137
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
@default_connection
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
@default_connection_options ||= ConnectionOptions.new
|
119
|
-
end
|
120
|
-
|
121
|
-
if (!defined?(RUBY_ENGINE) || "ruby" == RUBY_ENGINE) && RUBY_VERSION < '1.9'
|
122
|
-
begin
|
123
|
-
require 'system_timer'
|
124
|
-
Timer = SystemTimer
|
125
|
-
rescue LoadError
|
126
|
-
warn "Faraday: you may want to install system_timer for reliable timeouts"
|
138
|
+
# @overload default_connection
|
139
|
+
# Gets the default connection used for simple scripts.
|
140
|
+
# @return [Faraday::Connection] a connection configured with
|
141
|
+
# the default_adapter.
|
142
|
+
# @overload default_connection=(connection)
|
143
|
+
# @param connection [Faraday::Connection]
|
144
|
+
# Sets the default {Faraday::Connection} for simple scripts that
|
145
|
+
# access the Faraday constant directly, such as
|
146
|
+
# <code>Faraday.get "https://faraday.com"</code>.
|
147
|
+
def default_connection
|
148
|
+
@default_connection ||= Connection.new(default_connection_options)
|
127
149
|
end
|
128
|
-
end
|
129
|
-
|
130
|
-
unless const_defined? :Timer
|
131
|
-
require 'timeout'
|
132
|
-
Timer = Timeout
|
133
|
-
end
|
134
150
|
|
135
|
-
|
136
|
-
# middleware classes.
|
137
|
-
module MiddlewareRegistry
|
138
|
-
# Public: Register middleware class(es) on the current module.
|
139
|
-
#
|
140
|
-
# mapping - A Hash mapping Symbol keys to classes. Classes can be expressed
|
141
|
-
# as fully qualified constant, or a Proc that will be lazily
|
142
|
-
# called to return the former.
|
143
|
-
#
|
144
|
-
# Examples
|
145
|
-
#
|
146
|
-
# module Faraday
|
147
|
-
# class Whatever
|
148
|
-
# # Middleware looked up by :foo returns Faraday::Whatever::Foo.
|
149
|
-
# register_middleware :foo => Foo
|
150
|
-
#
|
151
|
-
# # Middleware looked up by :bar returns Faraday::Whatever.const_get(:Bar)
|
152
|
-
# register_middleware :bar => :Bar
|
151
|
+
# Gets the default connection options used when calling {Faraday#new}.
|
153
152
|
#
|
154
|
-
#
|
155
|
-
|
156
|
-
|
157
|
-
# end
|
158
|
-
#
|
159
|
-
# Returns nothing.
|
160
|
-
def register_middleware(autoload_path = nil, mapping = nil)
|
161
|
-
if mapping.nil?
|
162
|
-
mapping = autoload_path
|
163
|
-
autoload_path = nil
|
164
|
-
end
|
165
|
-
middleware_mutex do
|
166
|
-
@middleware_autoload_path = autoload_path if autoload_path
|
167
|
-
(@registered_middleware ||= {}).update(mapping)
|
168
|
-
end
|
153
|
+
# @return [Faraday::ConnectionOptions]
|
154
|
+
def default_connection_options
|
155
|
+
@default_connection_options ||= ConnectionOptions.new
|
169
156
|
end
|
170
157
|
|
171
|
-
#
|
172
|
-
#
|
173
|
-
# key - The Symbol key for the registered middleware.
|
174
|
-
#
|
175
|
-
# Examples
|
176
|
-
#
|
177
|
-
# module Faraday
|
178
|
-
# class Whatever
|
179
|
-
# register_middleware :foo => Foo
|
180
|
-
# end
|
181
|
-
# end
|
182
|
-
#
|
183
|
-
# Faraday::Whatever.lookup_middleware(:foo)
|
184
|
-
# # => Faraday::Whatever::Foo
|
158
|
+
# Sets the default options used when calling {Faraday#new}.
|
185
159
|
#
|
186
|
-
#
|
187
|
-
def
|
188
|
-
|
189
|
-
|
160
|
+
# @param options [Hash, Faraday::ConnectionOptions]
|
161
|
+
def default_connection_options=(options)
|
162
|
+
@default_connection = nil
|
163
|
+
@default_connection_options = ConnectionOptions.from(options)
|
190
164
|
end
|
191
165
|
|
192
|
-
|
193
|
-
@middleware_mutex ||= begin
|
194
|
-
require 'monitor'
|
195
|
-
Monitor.new
|
196
|
-
end
|
197
|
-
@middleware_mutex.synchronize(&block)
|
198
|
-
end
|
166
|
+
private
|
199
167
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
when Module
|
208
|
-
value
|
209
|
-
when Symbol, String
|
210
|
-
middleware_mutex do
|
211
|
-
@registered_middleware[key] = const_get(value)
|
212
|
-
end
|
213
|
-
when Proc
|
214
|
-
middleware_mutex do
|
215
|
-
@registered_middleware[key] = value.call
|
216
|
-
end
|
217
|
-
when Array
|
218
|
-
middleware_mutex do
|
219
|
-
const, path = value
|
220
|
-
if root = @middleware_autoload_path
|
221
|
-
path = "#{root}/#{path}"
|
222
|
-
end
|
223
|
-
require(path)
|
224
|
-
@registered_middleware[key] = const
|
225
|
-
end
|
226
|
-
load_middleware(key)
|
168
|
+
# Internal: Proxies method calls on the Faraday constant to
|
169
|
+
# .default_connection.
|
170
|
+
def method_missing(name, *args, &block)
|
171
|
+
if default_connection.respond_to?(name)
|
172
|
+
default_connection.send(name, *args, &block)
|
173
|
+
else
|
174
|
+
super
|
227
175
|
end
|
228
176
|
end
|
229
177
|
end
|
230
178
|
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
else
|
236
|
-
super
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
require_libs "utils", "options", "connection", "rack_builder", "parameters",
|
241
|
-
"middleware", "adapter", "request", "response", "upload_io", "error"
|
242
|
-
|
243
|
-
if !ENV["FARADAY_NO_AUTOLOAD"]
|
244
|
-
require_lib 'autoload'
|
245
|
-
end
|
246
|
-
end
|
179
|
+
self.ignore_env_proxy = false
|
180
|
+
self.root_path = File.expand_path __dir__
|
181
|
+
self.lib_path = File.expand_path 'faraday', __dir__
|
182
|
+
self.default_adapter = :net_http
|
247
183
|
|
248
|
-
|
249
|
-
class Object
|
250
|
-
# The primary purpose of this method is to "tap into" a method chain,
|
251
|
-
# in order to perform operations on intermediate results within the chain.
|
252
|
-
#
|
253
|
-
# Examples
|
254
|
-
#
|
255
|
-
# (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.
|
256
|
-
# tap { |x| puts "array: #{x.inspect}" }.
|
257
|
-
# select { |x| x%2 == 0 }.
|
258
|
-
# tap { |x| puts "evens: #{x.inspect}" }.
|
259
|
-
# map { |x| x*x }.
|
260
|
-
# tap { |x| puts "squares: #{x.inspect}" }
|
261
|
-
#
|
262
|
-
# Yields self.
|
263
|
-
# Returns self.
|
264
|
-
def tap
|
265
|
-
yield(self)
|
266
|
-
self
|
267
|
-
end unless Object.respond_to?(:tap)
|
184
|
+
require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
|
268
185
|
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,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
unless defined?(JRUBY_VERSION)
|
4
|
+
RSpec.describe Faraday::Adapter::EMHttp do
|
5
|
+
features :request_body_on_query_methods, :reason_phrase_parse, :trace_method,
|
6
|
+
:skip_response_body_on_head, :parallel, :local_socket_binding
|
7
|
+
|
8
|
+
it_behaves_like 'an adapter'
|
9
|
+
|
10
|
+
it 'allows to provide adapter specific configs' do
|
11
|
+
url = URI('https://example.com:1234')
|
12
|
+
adapter = described_class.new nil, inactivity_timeout: 20
|
13
|
+
req = adapter.create_request(url: url, request: {})
|
14
|
+
|
15
|
+
expect(req.connopts.inactivity_timeout).to eq(20)
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'Options' do
|
19
|
+
let(:request) { Faraday::RequestOptions.new }
|
20
|
+
let(:env) { { request: request } }
|
21
|
+
let(:options) { {} }
|
22
|
+
let(:adapter) { Faraday::Adapter::EMHttp.new }
|
23
|
+
|
24
|
+
it 'configures timeout' do
|
25
|
+
request.timeout = 5
|
26
|
+
adapter.configure_timeout(options, env)
|
27
|
+
expect(options[:inactivity_timeout]).to eq(5)
|
28
|
+
expect(options[:connect_timeout]).to eq(5)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'configures timeout and open_timeout' do
|
32
|
+
request.timeout = 5
|
33
|
+
request.open_timeout = 1
|
34
|
+
adapter.configure_timeout(options, env)
|
35
|
+
expect(options[:inactivity_timeout]).to eq(5)
|
36
|
+
expect(options[:connect_timeout]).to eq(1)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'configures all timeout settings' do
|
40
|
+
request.timeout = 5
|
41
|
+
request.read_timeout = 3
|
42
|
+
request.open_timeout = 1
|
43
|
+
adapter.configure_timeout(options, env)
|
44
|
+
expect(options[:inactivity_timeout]).to eq(3)
|
45
|
+
expect(options[:connect_timeout]).to eq(1)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
unless defined?(JRUBY_VERSION)
|
4
|
+
RSpec.describe Faraday::Adapter::EMSynchrony do
|
5
|
+
features :request_body_on_query_methods, :reason_phrase_parse,
|
6
|
+
:skip_response_body_on_head, :parallel, :local_socket_binding
|
7
|
+
|
8
|
+
it_behaves_like 'an adapter'
|
9
|
+
|
10
|
+
it 'allows to provide adapter specific configs' do
|
11
|
+
url = URI('https://example.com:1234')
|
12
|
+
adapter = described_class.new nil, inactivity_timeout: 20
|
13
|
+
req = adapter.create_request(url: url, request: {})
|
14
|
+
|
15
|
+
expect(req.connopts.inactivity_timeout).to eq(20)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
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
|