faraday 0.16.0 → 0.17.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +232 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +358 -18
  5. data/Rakefile +13 -0
  6. data/lib/faraday/adapter/em_http.rb +97 -140
  7. data/lib/faraday/adapter/em_http_ssl_patch.rb +17 -23
  8. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +15 -18
  9. data/lib/faraday/adapter/em_synchrony.rb +60 -104
  10. data/lib/faraday/adapter/excon.rb +55 -100
  11. data/lib/faraday/adapter/httpclient.rb +39 -61
  12. data/lib/faraday/adapter/net_http.rb +51 -103
  13. data/lib/faraday/adapter/net_http_persistent.rb +28 -49
  14. data/lib/faraday/adapter/patron.rb +35 -54
  15. data/lib/faraday/adapter/rack.rb +12 -28
  16. data/lib/faraday/adapter/test.rb +53 -86
  17. data/lib/faraday/adapter/typhoeus.rb +1 -4
  18. data/lib/faraday/adapter.rb +22 -36
  19. data/lib/faraday/autoload.rb +36 -47
  20. data/lib/faraday/connection.rb +179 -321
  21. data/lib/faraday/deprecate.rb +109 -0
  22. data/lib/faraday/error.rb +79 -21
  23. data/lib/faraday/middleware.rb +28 -4
  24. data/lib/faraday/options.rb +183 -32
  25. data/lib/faraday/parameters.rb +197 -4
  26. data/lib/faraday/rack_builder.rb +55 -66
  27. data/lib/faraday/request/authorization.rb +30 -42
  28. data/lib/faraday/request/basic_authentication.rb +7 -14
  29. data/lib/faraday/request/instrumentation.rb +27 -45
  30. data/lib/faraday/request/multipart.rb +48 -79
  31. data/lib/faraday/request/retry.rb +171 -197
  32. data/lib/faraday/request/token_authentication.rb +10 -15
  33. data/lib/faraday/request/url_encoded.rb +23 -41
  34. data/lib/faraday/request.rb +36 -68
  35. data/lib/faraday/response/logger.rb +69 -22
  36. data/lib/faraday/response/raise_error.rb +18 -36
  37. data/lib/faraday/response.rb +13 -20
  38. data/lib/faraday/upload_io.rb +67 -0
  39. data/lib/faraday/utils.rb +245 -28
  40. data/lib/faraday.rb +174 -93
  41. data/spec/faraday/deprecate_spec.rb +147 -0
  42. data/spec/faraday/error_spec.rb +102 -0
  43. data/spec/faraday/response/raise_error_spec.rb +106 -0
  44. data/spec/spec_helper.rb +105 -0
  45. data/test/adapters/default_test.rb +14 -0
  46. data/test/adapters/em_http_test.rb +30 -0
  47. data/test/adapters/em_synchrony_test.rb +32 -0
  48. data/test/adapters/excon_test.rb +30 -0
  49. data/test/adapters/httpclient_test.rb +34 -0
  50. data/test/adapters/integration.rb +263 -0
  51. data/test/adapters/logger_test.rb +136 -0
  52. data/test/adapters/net_http_persistent_test.rb +114 -0
  53. data/test/adapters/net_http_test.rb +79 -0
  54. data/test/adapters/patron_test.rb +40 -0
  55. data/test/adapters/rack_test.rb +38 -0
  56. data/test/adapters/test_middleware_test.rb +157 -0
  57. data/test/adapters/typhoeus_test.rb +38 -0
  58. data/test/authentication_middleware_test.rb +65 -0
  59. data/test/composite_read_io_test.rb +109 -0
  60. data/test/connection_test.rb +738 -0
  61. data/test/env_test.rb +268 -0
  62. data/test/helper.rb +75 -0
  63. data/test/live_server.rb +67 -0
  64. data/test/middleware/instrumentation_test.rb +88 -0
  65. data/test/middleware/retry_test.rb +282 -0
  66. data/test/middleware_stack_test.rb +260 -0
  67. data/test/multibyte.txt +1 -0
  68. data/test/options_test.rb +333 -0
  69. data/test/parameters_test.rb +157 -0
  70. data/test/request_middleware_test.rb +126 -0
  71. data/test/response_middleware_test.rb +72 -0
  72. data/test/strawberry.rb +2 -0
  73. data/test/utils_test.rb +98 -0
  74. metadata +50 -25
  75. data/lib/faraday/adapter_registry.rb +0 -28
  76. data/lib/faraday/dependency_loader.rb +0 -37
  77. data/lib/faraday/encoders/flat_params_encoder.rb +0 -94
  78. data/lib/faraday/encoders/nested_params_encoder.rb +0 -171
  79. data/lib/faraday/file_part.rb +0 -128
  80. data/lib/faraday/logging/formatter.rb +0 -92
  81. data/lib/faraday/middleware_registry.rb +0 -129
  82. data/lib/faraday/options/connection_options.rb +0 -22
  83. data/lib/faraday/options/env.rb +0 -181
  84. data/lib/faraday/options/proxy_options.rb +0 -28
  85. data/lib/faraday/options/request_options.rb +0 -21
  86. data/lib/faraday/options/ssl_options.rb +0 -59
  87. data/lib/faraday/param_part.rb +0 -53
  88. data/lib/faraday/utils/headers.rb +0 -139
  89. data/lib/faraday/utils/params_hash.rb +0 -61
  90. data/spec/external_adapters/faraday_specs_setup.rb +0 -14
@@ -1,56 +1,51 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Faraday
4
2
  class Adapter
5
- # @example
3
+ # Examples
4
+ #
6
5
  # test = Faraday::Connection.new do
7
6
  # use Faraday::Adapter::Test do |stub|
8
- # # Define matcher to match the request
7
+ # # simply define matcher to match the request
9
8
  # stub.get '/resource.json' do
10
9
  # # return static content
11
10
  # [200, {'Content-Type' => 'application/json'}, 'hi world']
12
11
  # end
13
- #
12
+ #
14
13
  # # response with content generated based on request
15
14
  # stub.get '/showget' do |env|
16
15
  # [200, {'Content-Type' => 'text/plain'}, env[:method].to_s]
17
16
  # end
18
- #
19
- # # A regular expression can be used as matching filter
17
+ #
18
+ # # regular expression can be used as matching filter
20
19
  # stub.get /\A\/items\/(\d+)\z/ do |env, meta|
21
- # # in case regular expression is used, an instance of MatchData
22
- # # can be received
23
- # [200,
24
- # {'Content-Type' => 'text/plain'},
25
- # "showing item: #{meta[:match_data][1]}"
26
- # ]
20
+ # # in case regular expression is used an instance of MatchData can be received
21
+ # [200, {'Content-Type' => 'text/plain'}, "showing item: #{meta[:match_data][1]}"]
27
22
  # end
28
23
  # end
29
24
  # end
30
- #
25
+ #
31
26
  # resp = test.get '/resource.json'
32
27
  # resp.body # => 'hi world'
33
- #
28
+ #
34
29
  # resp = test.get '/showget'
35
30
  # resp.body # => 'get'
36
- #
31
+ #
37
32
  # resp = test.get '/items/1'
38
33
  # resp.body # => 'showing item: 1'
39
- #
34
+ #
40
35
  # resp = test.get '/items/2'
41
36
  # resp.body # => 'showing item: 2'
37
+ #
38
+
42
39
  class Test < Faraday::Adapter
43
40
  attr_accessor :stubs
44
41
 
45
- # A stack of Stubs
46
42
  class Stubs
47
43
  class NotFound < StandardError
48
44
  end
49
45
 
50
46
  def initialize
51
- # { get: [Stub, Stub] }
52
- @stack = {}
53
- @consumed = {}
47
+ # {:get => [Stub, Stub]}
48
+ @stack, @consumed = {}, {}
54
49
  yield(self) if block_given?
55
50
  end
56
51
 
@@ -59,8 +54,7 @@ module Faraday
59
54
  end
60
55
 
61
56
  def match(request_method, host, path, headers, body)
62
- return false unless @stack.key?(request_method)
63
-
57
+ return false if !@stack.key?(request_method)
64
58
  stack = @stack[request_method]
65
59
  consumed = (@consumed[request_method] ||= [])
66
60
 
@@ -80,15 +74,15 @@ module Faraday
80
74
  new_stub(:head, path, headers, &block)
81
75
  end
82
76
 
83
- def post(path, body = nil, headers = {}, &block)
77
+ def post(path, body=nil, headers = {}, &block)
84
78
  new_stub(:post, path, headers, body, &block)
85
79
  end
86
80
 
87
- def put(path, body = nil, headers = {}, &block)
81
+ def put(path, body=nil, headers = {}, &block)
88
82
  new_stub(:put, path, headers, body, &block)
89
83
  end
90
84
 
91
- def patch(path, body = nil, headers = {}, &block)
85
+ def patch(path, body=nil, headers = {}, &block)
92
86
  new_stub(:patch, path, headers, body, &block)
93
87
  end
94
88
 
@@ -104,32 +98,26 @@ module Faraday
104
98
  def verify_stubbed_calls
105
99
  failed_stubs = []
106
100
  @stack.each do |method, stubs|
107
- next if stubs.empty?
108
-
109
- failed_stubs.concat(
110
- stubs.map do |stub|
101
+ unless stubs.size == 0
102
+ failed_stubs.concat(stubs.map {|stub|
111
103
  "Expected #{method} #{stub}."
112
- end
113
- )
104
+ })
105
+ end
114
106
  end
115
- raise failed_stubs.join(' ') unless failed_stubs.empty?
107
+ raise failed_stubs.join(" ") unless failed_stubs.size == 0
116
108
  end
117
109
 
118
110
  protected
119
111
 
120
- def new_stub(request_method, path, headers = {}, body = nil, &block)
112
+ def new_stub(request_method, path, headers = {}, body=nil, &block)
121
113
  normalized_path, host =
122
114
  if path.is_a?(Regexp)
123
115
  path
124
116
  else
125
- [
126
- Faraday::Utils.normalize_path(path),
127
- Faraday::Utils.URI(path).host
128
- ]
117
+ [Faraday::Utils.normalize_path(path), Faraday::Utils.URI(path).host]
129
118
  end
130
119
 
131
- stub = Stub.new(host, normalized_path, headers, body, block)
132
- (@stack[request_method] ||= []) << stub
120
+ (@stack[request_method] ||= []) << Stub.new(host, normalized_path, headers, body, block)
133
121
  end
134
122
 
135
123
  def matches?(stack, host, path, headers, body)
@@ -141,42 +129,32 @@ module Faraday
141
129
  end
142
130
  end
143
131
 
144
- # Stub request
145
- # rubocop:disable Style/StructInheritance
146
132
  class Stub < Struct.new(:host, :path, :params, :headers, :body, :block)
147
- # rubocop:enable Style/StructInheritance
148
133
  def initialize(host, full, headers, body, block)
149
- path, query = full.respond_to?(:split) ? full.split('?') : full
150
- params =
151
- if query
152
- Faraday::Utils.parse_nested_query(query)
153
- else
154
- {}
155
- end
156
-
134
+ path, query = full.respond_to?(:split) ? full.split("?") : full
135
+ params = query ?
136
+ Faraday::Utils.parse_nested_query(query) :
137
+ {}
157
138
  super(host, path, params, headers, body, block)
158
139
  end
159
140
 
160
141
  def matches?(request_host, request_uri, request_headers, request_body)
161
142
  request_path, request_query = request_uri.split('?')
162
- request_params =
163
- if request_query
164
- Faraday::Utils.parse_nested_query(request_query)
165
- else
166
- {}
167
- end
168
- # meta is a hash used as carrier
143
+ request_params = request_query ?
144
+ Faraday::Utils.parse_nested_query(request_query) :
145
+ {}
146
+ # meta is a hash use as carrier
169
147
  # that will be yielded to consumer block
170
148
  meta = {}
171
- [(host.nil? || host == request_host) &&
149
+ return (host.nil? || host == request_host) &&
172
150
  path_match?(request_path, meta) &&
173
151
  params_match?(request_params) &&
174
152
  (body.to_s.size.zero? || request_body == body) &&
175
- headers_match?(request_headers), meta]
153
+ headers_match?(request_headers), meta
176
154
  end
177
155
 
178
156
  def path_match?(request_path, meta)
179
- if path.is_a?(Regexp)
157
+ if path.is_a? Regexp
180
158
  !!(meta[:match_data] = path.match(request_path))
181
159
  else
182
160
  path == request_path
@@ -200,7 +178,7 @@ module Faraday
200
178
  end
201
179
  end
202
180
 
203
- def initialize(app, stubs = nil, &block)
181
+ def initialize(app, stubs=nil, &block)
204
182
  super(app)
205
183
  @stubs = stubs || Stubs.new
206
184
  configure(&block) if block
@@ -214,31 +192,20 @@ module Faraday
214
192
  super
215
193
  host = env[:url].host
216
194
  normalized_path = Faraday::Utils.normalize_path(env[:url])
217
- params_encoder = env.request.params_encoder ||
218
- Faraday::Utils.default_params_encoder
219
-
220
- stub, meta = stubs.match(env[:method], host, normalized_path,
221
- env.request_headers, env[:body])
222
-
223
- unless stub
224
- raise Stubs::NotFound, "no stubbed request for #{env[:method]} "\
225
- "#{normalized_path} #{env[:body]}"
226
- end
227
-
228
- env[:params] = if (query = env[:url].query)
229
- params_encoder.decode(query)
230
- else
231
- {}
232
- end
233
- block_arity = stub.block.arity
234
- status, headers, body =
235
- if block_arity >= 0
236
- stub.block.call(*[env, meta].take(block_arity))
237
- else
195
+ params_encoder = env.request.params_encoder || Faraday::Utils.default_params_encoder
196
+
197
+ stub, meta = stubs.match(env[:method], host, normalized_path, env.request_headers, env[:body])
198
+ if stub
199
+ env[:params] = (query = env[:url].query) ?
200
+ params_encoder.decode(query) : {}
201
+ block_arity = stub.block.arity
202
+ status, headers, body = (block_arity >= 0) ?
203
+ stub.block.call(*[env, meta].take(block_arity)) :
238
204
  stub.block.call(env, meta)
239
- end
240
- save_response(env, status, body, headers)
241
-
205
+ save_response(env, status, body, headers)
206
+ else
207
+ raise Stubs::NotFound, "no stubbed request for #{env[:method]} #{normalized_path} #{env[:body]}"
208
+ end
242
209
  @app.call(env)
243
210
  end
244
211
  end
@@ -1,9 +1,6 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Faraday
4
2
  class Adapter
5
- # Typhoeus adapter. This class is just a stub, the real adapter is in
6
- # https://github.com/philsturgeon/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
3
+ # This class is just a stub, the real adapter is in https://github.com/philsturgeon/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
7
4
  class Typhoeus < Faraday::Adapter
8
5
  # Needs to define this method in order to support Typhoeus <= 1.3.0
9
6
  def call; end
@@ -1,54 +1,43 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Faraday
4
- # Base class for all Faraday adapters. Adapters are
2
+ # Public: This is a base class for all Faraday adapters. Adapters are
5
3
  # responsible for fulfilling a Faraday request.
6
- class Adapter
7
- extend MiddlewareRegistry
8
- extend DependencyLoader
9
-
10
- CONTENT_LENGTH = 'Content-Length'
11
-
12
- register_middleware File.expand_path('adapter', __dir__),
13
- test: [:Test, 'test'],
14
- net_http: [:NetHttp, 'net_http'],
15
- net_http_persistent: [
16
- :NetHttpPersistent,
17
- 'net_http_persistent'
18
- ],
19
- typhoeus: [:Typhoeus, 'typhoeus'],
20
- patron: [:Patron, 'patron'],
21
- em_synchrony: [:EMSynchrony, 'em_synchrony'],
22
- em_http: [:EMHttp, 'em_http'],
23
- excon: [:Excon, 'excon'],
24
- rack: [:Rack, 'rack'],
25
- httpclient: [:HTTPClient, 'httpclient']
26
-
27
- # This module marks an Adapter as supporting parallel requests.
4
+ class Adapter < Middleware
5
+ CONTENT_LENGTH = 'Content-Length'.freeze
6
+
7
+ register_middleware File.expand_path('../adapter', __FILE__),
8
+ :test => [:Test, 'test'],
9
+ :net_http => [:NetHttp, 'net_http'],
10
+ :net_http_persistent => [:NetHttpPersistent, 'net_http_persistent'],
11
+ :typhoeus => [:Typhoeus, 'typhoeus'],
12
+ :patron => [:Patron, 'patron'],
13
+ :em_synchrony => [:EMSynchrony, 'em_synchrony'],
14
+ :em_http => [:EMHttp, 'em_http'],
15
+ :excon => [:Excon, 'excon'],
16
+ :rack => [:Rack, 'rack'],
17
+ :httpclient => [:HTTPClient, 'httpclient']
18
+
19
+ # Public: This module marks an Adapter as supporting parallel requests.
28
20
  module Parallelism
29
21
  attr_writer :supports_parallel
30
- def supports_parallel?
31
- @supports_parallel
32
- end
22
+ def supports_parallel?() @supports_parallel end
33
23
 
34
24
  def inherited(subclass)
35
25
  super
36
- subclass.supports_parallel = supports_parallel?
26
+ subclass.supports_parallel = self.supports_parallel?
37
27
  end
38
28
  end
39
29
 
40
30
  extend Parallelism
41
31
  self.supports_parallel = false
42
32
 
43
- def initialize(_app = nil, opts = {}, &block)
44
- @app = ->(env) { env.response }
33
+ def initialize(app = nil, opts = {}, &block)
34
+ super(app)
45
35
  @connection_options = opts
46
36
  @config_block = block
47
37
  end
48
38
 
49
39
  def call(env)
50
40
  env.clear_body if env.needs_body?
51
- env.response = Response.new
52
41
  end
53
42
 
54
43
  private
@@ -56,14 +45,11 @@ module Faraday
56
45
  def save_response(env, status, body, headers = nil, reason_phrase = nil)
57
46
  env.status = status
58
47
  env.body = body
59
- env.reason_phrase = reason_phrase&.to_s&.strip
48
+ env.reason_phrase = reason_phrase && reason_phrase.to_s.strip
60
49
  env.response_headers = Utils::Headers.new.tap do |response_headers|
61
50
  response_headers.update headers unless headers.nil?
62
51
  yield(response_headers) if block_given?
63
52
  end
64
-
65
- env.response.finish(env) unless env.parallel?
66
- env.response
67
53
  end
68
54
  end
69
55
  end
@@ -1,95 +1,84 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Faraday
4
- # Adds the ability for other modules to manage autoloadable
2
+ # Internal: Adds the ability for other modules to manage autoloadable
5
3
  # constants.
6
- #
7
- # @api private
8
4
  module AutoloadHelper
9
- # Registers the constants to be auto loaded.
5
+ # Internal: Registers the constants to be auto loaded.
10
6
  #
11
- # @param prefix [String] The require prefix. If the path is inside Faraday,
12
- # then it will be prefixed with the root path of this loaded
13
- # Faraday version.
14
- # @param options [{ Symbol => String }] library names.
7
+ # prefix - The String require prefix. If the path is inside Faraday, then
8
+ # it will be prefixed with the root path of this loaded Faraday
9
+ # version.
10
+ # options - Hash of Symbol => String library names.
15
11
  #
16
- # @example
12
+ # Examples.
17
13
  #
18
14
  # Faraday.autoload_all 'faraday/foo',
19
- # Bar: 'bar'
15
+ # :Bar => 'bar'
20
16
  #
21
17
  # # requires faraday/foo/bar to load Faraday::Bar.
22
18
  # Faraday::Bar
23
19
  #
24
- # @return [void]
20
+ #
21
+ # Returns nothing.
25
22
  def autoload_all(prefix, options)
26
- if prefix =~ %r{^faraday(/|$)}i
23
+ if prefix =~ /^faraday(\/|$)/i
27
24
  prefix = File.join(Faraday.root_path, prefix)
28
25
  end
29
-
30
26
  options.each do |const_name, path|
31
27
  autoload const_name, File.join(prefix, path)
32
28
  end
33
29
  end
34
30
 
35
- # Loads each autoloaded constant. If thread safety is a concern,
31
+ # Internal: Loads each autoloaded constant. If thread safety is a concern,
36
32
  # wrap this in a Mutex.
37
33
  #
38
- # @return [void]
34
+ # Returns nothing.
39
35
  def load_autoloaded_constants
40
36
  constants.each do |const|
41
37
  const_get(const) if autoload?(const)
42
38
  end
43
39
  end
44
40
 
45
- # Filters the module's contents with those that have been already
41
+ # Internal: Filters the module's contents with those that have been already
46
42
  # autoloaded.
47
43
  #
48
- # @return [Array<Class, Module>]
44
+ # Returns an Array of Class/Module objects.
49
45
  def all_loaded_constants
50
- constants
51
- .map { |c| const_get(c) }
52
- .select { |a| a.respond_to?(:loaded?) && a.loaded? }
46
+ constants.map { |c| const_get(c) }.
47
+ select { |a| a.respond_to?(:loaded?) && a.loaded? }
53
48
  end
54
49
  end
55
50
 
56
- # Adapter is the base class for all Faraday adapters.
57
- # @see lib/faraday/adapter.rb Original class location
58
51
  class Adapter
59
52
  extend AutoloadHelper
60
53
  autoload_all 'faraday/adapter',
61
- NetHttp: 'net_http',
62
- NetHttpPersistent: 'net_http_persistent',
63
- EMSynchrony: 'em_synchrony',
64
- EMHttp: 'em_http',
65
- Typhoeus: 'typhoeus',
66
- Patron: 'patron',
67
- Excon: 'excon',
68
- Test: 'test',
69
- Rack: 'rack',
70
- HTTPClient: 'httpclient'
54
+ :NetHttp => 'net_http',
55
+ :NetHttpPersistent => 'net_http_persistent',
56
+ :EMSynchrony => 'em_synchrony',
57
+ :EMHttp => 'em_http',
58
+ :Typhoeus => 'typhoeus',
59
+ :Patron => 'patron',
60
+ :Excon => 'excon',
61
+ :Test => 'test',
62
+ :Rack => 'rack',
63
+ :HTTPClient => 'httpclient'
71
64
  end
72
65
 
73
- # Request represents a single HTTP request for a Faraday adapter to make.
74
- # @see lib/faraday/request.rb Original class location
75
66
  class Request
76
67
  extend AutoloadHelper
77
68
  autoload_all 'faraday/request',
78
- UrlEncoded: 'url_encoded',
79
- Multipart: 'multipart',
80
- Retry: 'retry',
81
- Authorization: 'authorization',
82
- BasicAuthentication: 'basic_authentication',
83
- TokenAuthentication: 'token_authentication',
84
- Instrumentation: 'instrumentation'
69
+ :UrlEncoded => 'url_encoded',
70
+ :Multipart => 'multipart',
71
+ :Retry => 'retry',
72
+ :Authorization => 'authorization',
73
+ :BasicAuthentication => 'basic_authentication',
74
+ :TokenAuthentication => 'token_authentication',
75
+ :Instrumentation => 'instrumentation'
85
76
  end
86
77
 
87
- # Response represents the returned value of a sent Faraday request.
88
- # @see lib/faraday/response.rb Original class location
89
78
  class Response
90
79
  extend AutoloadHelper
91
80
  autoload_all 'faraday/response',
92
- RaiseError: 'raise_error',
93
- Logger: 'logger'
81
+ :RaiseError => 'raise_error',
82
+ :Logger => 'logger'
94
83
  end
95
84
  end