faraday 1.1.0 → 2.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +299 -1
  3. data/LICENSE.md +1 -1
  4. data/README.md +18 -17
  5. data/examples/client_spec.rb +67 -13
  6. data/examples/client_test.rb +80 -15
  7. data/lib/faraday/adapter/test.rb +117 -52
  8. data/lib/faraday/adapter.rb +5 -20
  9. data/lib/faraday/connection.rb +64 -118
  10. data/lib/faraday/encoders/nested_params_encoder.rb +13 -6
  11. data/lib/faraday/error.rb +15 -8
  12. data/lib/faraday/logging/formatter.rb +19 -2
  13. data/lib/faraday/methods.rb +6 -0
  14. data/lib/faraday/middleware.rb +17 -5
  15. data/lib/faraday/middleware_registry.rb +17 -63
  16. data/lib/faraday/options/env.rb +31 -7
  17. data/lib/faraday/options/proxy_options.rb +4 -0
  18. data/lib/faraday/options/ssl_options.rb +11 -1
  19. data/lib/faraday/options.rb +3 -3
  20. data/lib/faraday/rack_builder.rb +23 -20
  21. data/lib/faraday/request/authorization.rb +37 -38
  22. data/lib/faraday/request/instrumentation.rb +2 -0
  23. data/lib/faraday/request/json.rb +55 -0
  24. data/lib/faraday/request/url_encoded.rb +5 -1
  25. data/lib/faraday/request.rb +12 -32
  26. data/lib/faraday/response/json.rb +54 -0
  27. data/lib/faraday/response/logger.rb +8 -4
  28. data/lib/faraday/response/raise_error.rb +9 -1
  29. data/lib/faraday/response.rb +10 -26
  30. data/lib/faraday/utils/headers.rb +7 -2
  31. data/lib/faraday/utils.rb +10 -5
  32. data/lib/faraday/version.rb +5 -0
  33. data/lib/faraday.rb +49 -58
  34. data/spec/faraday/adapter/test_spec.rb +182 -0
  35. data/spec/faraday/connection_spec.rb +207 -90
  36. data/spec/faraday/error_spec.rb +15 -0
  37. data/spec/faraday/middleware_registry_spec.rb +31 -0
  38. data/spec/faraday/middleware_spec.rb +50 -6
  39. data/spec/faraday/options/env_spec.rb +8 -2
  40. data/spec/faraday/options/proxy_options_spec.rb +7 -0
  41. data/spec/faraday/params_encoders/nested_spec.rb +8 -0
  42. data/spec/faraday/rack_builder_spec.rb +26 -54
  43. data/spec/faraday/request/authorization_spec.rb +54 -24
  44. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  45. data/spec/faraday/request/json_spec.rb +111 -0
  46. data/spec/faraday/request/url_encoded_spec.rb +12 -2
  47. data/spec/faraday/request_spec.rb +5 -15
  48. data/spec/faraday/response/json_spec.rb +117 -0
  49. data/spec/faraday/response/logger_spec.rb +28 -0
  50. data/spec/faraday/response/raise_error_spec.rb +37 -4
  51. data/spec/faraday/response_spec.rb +3 -1
  52. data/spec/faraday/utils/headers_spec.rb +22 -4
  53. data/spec/faraday/utils_spec.rb +63 -1
  54. data/spec/support/fake_safe_buffer.rb +1 -1
  55. data/spec/support/helper_methods.rb +0 -37
  56. data/spec/support/shared_examples/adapter.rb +4 -3
  57. data/spec/support/shared_examples/request_method.rb +58 -29
  58. metadata +19 -45
  59. data/lib/faraday/adapter/em_http.rb +0 -286
  60. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
  61. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
  62. data/lib/faraday/adapter/em_synchrony.rb +0 -150
  63. data/lib/faraday/adapter/excon.rb +0 -124
  64. data/lib/faraday/adapter/httpclient.rb +0 -152
  65. data/lib/faraday/adapter/net_http.rb +0 -219
  66. data/lib/faraday/adapter/net_http_persistent.rb +0 -91
  67. data/lib/faraday/adapter/patron.rb +0 -132
  68. data/lib/faraday/adapter/rack.rb +0 -75
  69. data/lib/faraday/adapter/typhoeus.rb +0 -15
  70. data/lib/faraday/autoload.rb +0 -95
  71. data/lib/faraday/dependency_loader.rb +0 -39
  72. data/lib/faraday/file_part.rb +0 -128
  73. data/lib/faraday/param_part.rb +0 -53
  74. data/lib/faraday/request/basic_authentication.rb +0 -20
  75. data/lib/faraday/request/multipart.rb +0 -106
  76. data/lib/faraday/request/retry.rb +0 -239
  77. data/lib/faraday/request/token_authentication.rb +0 -20
  78. data/spec/faraday/adapter/em_http_spec.rb +0 -47
  79. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -16
  80. data/spec/faraday/adapter/excon_spec.rb +0 -49
  81. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  82. data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
  83. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  84. data/spec/faraday/adapter/patron_spec.rb +0 -18
  85. data/spec/faraday/adapter/rack_spec.rb +0 -8
  86. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  87. data/spec/faraday/composite_read_io_spec.rb +0 -80
  88. data/spec/faraday/request/multipart_spec.rb +0 -302
  89. data/spec/faraday/request/retry_spec.rb +0 -242
  90. data/spec/faraday/response/middleware_spec.rb +0 -68
  91. data/spec/support/webmock_rack_app.rb +0 -68
@@ -44,13 +44,21 @@ module Faraday
44
44
  body: env.body,
45
45
  request: {
46
46
  method: env.method,
47
+ url: env.url,
47
48
  url_path: env.url.path,
48
- params: env.params,
49
+ params: query_params(env),
49
50
  headers: env.request_headers,
50
51
  body: env.request_body
51
52
  }
52
53
  }
53
54
  end
55
+
56
+ def query_params(env)
57
+ env.request.params_encoder ||= Faraday::Utils.default_params_encoder
58
+ env.params_encoder.decode(env.url.query)
59
+ end
54
60
  end
55
61
  end
56
62
  end
63
+
64
+ Faraday::Response.register_middleware(raise_error: Faraday::Response::RaiseError)
@@ -5,31 +5,9 @@ require 'forwardable'
5
5
  module Faraday
6
6
  # Response represents an HTTP response from making an HTTP request.
7
7
  class Response
8
- # Used for simple response middleware.
9
- class Middleware < Faraday::Middleware
10
- def call(env)
11
- @app.call(env).on_complete do |environment|
12
- on_complete(environment)
13
- end
14
- end
15
-
16
- # Override this to modify the environment after the response has finished.
17
- # Calls the `parse` method if defined
18
- # `parse` method can be defined as private, public and protected
19
- def on_complete(env)
20
- return unless respond_to?(:parse, true) && env.parse_body?
21
-
22
- env.body = parse(env.body)
23
- end
24
- end
25
-
26
8
  extend Forwardable
27
9
  extend MiddlewareRegistry
28
10
 
29
- register_middleware File.expand_path('response', __dir__),
30
- raise_error: [:RaiseError, 'raise_error'],
31
- logger: [:Logger, 'logger']
32
-
33
11
  def initialize(env = nil)
34
12
  @env = Env.from(env) if env
35
13
  @on_complete_callbacks = []
@@ -48,6 +26,7 @@ module Faraday
48
26
  def headers
49
27
  finished? ? env.response_headers : {}
50
28
  end
29
+
51
30
  def_delegator :headers, :[]
52
31
 
53
32
  def body
@@ -59,10 +38,10 @@ module Faraday
59
38
  end
60
39
 
61
40
  def on_complete(&block)
62
- if !finished?
63
- @on_complete_callbacks << block
64
- else
41
+ if finished?
65
42
  yield(env)
43
+ else
44
+ @on_complete_callbacks << block
66
45
  end
67
46
  self
68
47
  end
@@ -82,7 +61,8 @@ module Faraday
82
61
  def to_hash
83
62
  {
84
63
  status: env.status, body: env.body,
85
- response_headers: env.response_headers
64
+ response_headers: env.response_headers,
65
+ url: env.url
86
66
  }
87
67
  end
88
68
 
@@ -105,3 +85,7 @@ module Faraday
105
85
  end
106
86
  end
107
87
  end
88
+
89
+ require 'faraday/response/json'
90
+ require 'faraday/response/logger'
91
+ require 'faraday/response/raise_error'
@@ -111,7 +111,7 @@ module Faraday
111
111
  def parse(header_string)
112
112
  return unless header_string && !header_string.empty?
113
113
 
114
- headers = header_string.split(/\r\n/)
114
+ headers = header_string.split("\r\n")
115
115
 
116
116
  # Find the last set of response headers.
117
117
  start_index = headers.rindex { |x| x.start_with?('HTTP/') } || 0
@@ -132,7 +132,12 @@ module Faraday
132
132
 
133
133
  # Join multiple values with a comma.
134
134
  def add_parsed(key, value)
135
- self[key] ? self[key] << ', ' << value : self[key] = value
135
+ if key?(key)
136
+ self[key] = self[key].to_s
137
+ self[key] << ', ' << value
138
+ else
139
+ self[key] = value
140
+ end
136
141
  end
137
142
  end
138
143
  end
data/lib/faraday/utils.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'base64'
4
+ require 'uri'
3
5
  require 'faraday/utils/headers'
4
6
  require 'faraday/utils/params_hash'
5
7
 
@@ -51,6 +53,12 @@ module Faraday
51
53
  @default_params_encoder ||= NestedParamsEncoder
52
54
  end
53
55
 
56
+ def basic_header_from(login, pass)
57
+ value = Base64.encode64("#{login}:#{pass}")
58
+ value.delete!("\n")
59
+ "Basic #{value}"
60
+ end
61
+
54
62
  class << self
55
63
  attr_writer :default_params_encoder
56
64
  end
@@ -71,10 +79,7 @@ module Faraday
71
79
  end
72
80
 
73
81
  def default_uri_parser
74
- @default_uri_parser ||= begin
75
- require 'uri'
76
- Kernel.method(:URI)
77
- end
82
+ @default_uri_parser ||= Kernel.method(:URI)
78
83
  end
79
84
 
80
85
  def default_uri_parser=(parser)
@@ -96,7 +101,7 @@ module Faraday
96
101
  # Recursive hash update
97
102
  def deep_merge!(target, hash)
98
103
  hash.each do |key, value|
99
- 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))
100
105
  deep_merge(target[key], value)
101
106
  else
102
107
  value
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ VERSION = '2.7.4'
5
+ end
data/lib/faraday.rb CHANGED
@@ -1,11 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'cgi'
4
+ require 'date'
4
5
  require 'set'
5
6
  require 'forwardable'
7
+ require 'faraday/version'
8
+ require 'faraday/methods'
9
+ require 'faraday/error'
6
10
  require 'faraday/middleware_registry'
7
- require 'faraday/dependency_loader'
8
-
11
+ require 'faraday/utils'
12
+ require 'faraday/options'
13
+ require 'faraday/connection'
14
+ require 'faraday/rack_builder'
15
+ require 'faraday/parameters'
16
+ require 'faraday/middleware'
17
+ require 'faraday/adapter'
18
+ require 'faraday/request'
19
+ require 'faraday/response'
20
+ require 'faraday/net_http'
9
21
  # This is the main namespace for Faraday.
10
22
  #
11
23
  # It provides methods to create {Connection} objects, and HTTP-related
@@ -19,9 +31,7 @@ require 'faraday/dependency_loader'
19
31
  # conn.get '/'
20
32
  #
21
33
  module Faraday
22
- VERSION = '1.1.0'
23
- METHODS_WITH_QUERY = %w[get head delete trace].freeze
24
- METHODS_WITH_BODY = %w[post put patch].freeze
34
+ CONTENT_TYPE = 'Content-Type'
25
35
 
26
36
  class << self
27
37
  # The root path that Faraday is being loaded from.
@@ -44,6 +54,10 @@ module Faraday
44
54
  # @return [Symbol] the new default_adapter.
45
55
  attr_reader :default_adapter
46
56
 
57
+ # Option for the default_adapter
58
+ # @return [Hash] default_adapter options
59
+ attr_accessor :default_adapter_options
60
+
47
61
  # Documented below, see default_connection
48
62
  attr_writer :default_connection
49
63
 
@@ -80,23 +94,10 @@ module Faraday
80
94
  # params: { page: 1 }
81
95
  # # => Faraday::Connection to http://faraday.com?page=1
82
96
  def new(url = nil, options = {}, &block)
83
- options = default_connection_options.merge(options)
97
+ options = Utils.deep_merge(default_connection_options, options)
84
98
  Faraday::Connection.new(url, options, &block)
85
99
  end
86
100
 
87
- # @private
88
- # Internal: Requires internal Faraday libraries.
89
- #
90
- # @param libs [Array] one or more relative String names to Faraday classes.
91
- # @return [void]
92
- def require_libs(*libs)
93
- libs.each do |lib|
94
- require "#{lib_path}/#{lib}"
95
- end
96
- end
97
-
98
- alias require_lib require_libs
99
-
100
101
  # Documented elsewhere, see default_adapter reader
101
102
  def default_adapter=(adapter)
102
103
  @default_connection = nil
@@ -107,6 +108,34 @@ module Faraday
107
108
  default_connection.respond_to?(symbol, include_private) || super
108
109
  end
109
110
 
111
+ # @overload default_connection
112
+ # Gets the default connection used for simple scripts.
113
+ # @return [Faraday::Connection] a connection configured with
114
+ # the default_adapter.
115
+ # @overload default_connection=(connection)
116
+ # @param connection [Faraday::Connection]
117
+ # Sets the default {Faraday::Connection} for simple scripts that
118
+ # access the Faraday constant directly, such as
119
+ # <code>Faraday.get "https://faraday.com"</code>.
120
+ def default_connection
121
+ @default_connection ||= Connection.new(default_connection_options)
122
+ end
123
+
124
+ # Gets the default connection options used when calling {Faraday#new}.
125
+ #
126
+ # @return [Faraday::ConnectionOptions]
127
+ def default_connection_options
128
+ @default_connection_options ||= ConnectionOptions.new
129
+ end
130
+
131
+ # Sets the default options used when calling {Faraday#new}.
132
+ #
133
+ # @param options [Hash, Faraday::ConnectionOptions]
134
+ def default_connection_options=(options)
135
+ @default_connection = nil
136
+ @default_connection_options = ConnectionOptions.from(options)
137
+ end
138
+
110
139
  private
111
140
 
112
141
  # Internal: Proxies method calls on the Faraday constant to
@@ -124,43 +153,5 @@ module Faraday
124
153
  self.root_path = File.expand_path __dir__
125
154
  self.lib_path = File.expand_path 'faraday', __dir__
126
155
  self.default_adapter = :net_http
127
-
128
- # @overload default_connection
129
- # Gets the default connection used for simple scripts.
130
- # @return [Faraday::Connection] a connection configured with
131
- # the default_adapter.
132
- # @overload default_connection=(connection)
133
- # @param connection [Faraday::Connection]
134
- # Sets the default {Faraday::Connection} for simple scripts that
135
- # access the Faraday constant directly, such as
136
- # <code>Faraday.get "https://faraday.com"</code>.
137
- def self.default_connection
138
- @default_connection ||= Connection.new(default_connection_options)
139
- end
140
-
141
- # Gets the default connection options used when calling {Faraday#new}.
142
- #
143
- # @return [Faraday::ConnectionOptions]
144
- def self.default_connection_options
145
- @default_connection_options ||= ConnectionOptions.new
146
- end
147
-
148
- # Sets the default options used when calling {Faraday#new}.
149
- #
150
- # @param options [Hash, Faraday::ConnectionOptions]
151
- def self.default_connection_options=(options)
152
- @default_connection = nil
153
- @default_connection_options = ConnectionOptions.from(options)
154
- end
155
-
156
- unless defined?(::Faraday::Timer)
157
- require 'timeout'
158
- Timer = Timeout
159
- end
160
-
161
- require_libs 'utils', 'options', 'connection', 'rack_builder', 'parameters',
162
- 'middleware', 'adapter', 'request', 'response', 'error',
163
- 'file_part', 'param_part'
164
-
165
- require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
156
+ self.default_adapter_options = {}
166
157
  end
@@ -257,4 +257,186 @@ RSpec.describe Faraday::Adapter::Test do
257
257
  it { expect { request }.to raise_error described_class::Stubs::NotFound }
258
258
  end
259
259
  end
260
+
261
+ describe 'for request with non default params encoder' do
262
+ let(:connection) do
263
+ Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
264
+ builder.adapter :test, stubs
265
+ end
266
+ end
267
+ let(:stubs) do
268
+ described_class::Stubs.new do |stubs|
269
+ stubs.get('/path?a=x&a=y&a=z') { [200, {}, 'a'] }
270
+ end
271
+ end
272
+
273
+ context 'when all flat param values are correctly set' do
274
+ subject(:request) { connection.get('/path?a=x&a=y&a=z') }
275
+
276
+ it { expect(request.status).to eq 200 }
277
+ end
278
+
279
+ shared_examples 'raise NotFound when params do not satisfy the flat param values' do |params|
280
+ subject(:request) { connection.get('/path', params) }
281
+
282
+ context "with #{params.inspect}" do
283
+ it { expect { request }.to raise_error described_class::Stubs::NotFound }
284
+ end
285
+ end
286
+
287
+ it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x] }
288
+ it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x y] }
289
+ it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x z y] } # NOTE: The order of the value is also compared.
290
+ it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { b: %w[x y z] }
291
+ end
292
+
293
+ describe 'strict_mode' do
294
+ let(:stubs) do
295
+ described_class::Stubs.new(strict_mode: true) do |stubs|
296
+ stubs.get('/strict?a=12&b=xy', 'Authorization' => 'Bearer m_ck', 'X-C' => 'hello') { [200, {}, 'a'] }
297
+ stubs.get('/with_user_agent?a=12&b=xy', authorization: 'Bearer m_ck', 'User-Agent' => 'My Agent') { [200, {}, 'a'] }
298
+ end
299
+ end
300
+
301
+ context 'when params and headers are exactly set' do
302
+ subject(:request) { connection.get('/strict', { a: '12', b: 'xy' }, { authorization: 'Bearer m_ck', x_c: 'hello' }) }
303
+
304
+ it { expect(request.status).to eq 200 }
305
+ end
306
+
307
+ context 'when params and headers are exactly set with a custom user agent' do
308
+ subject(:request) { connection.get('/with_user_agent', { a: '12', b: 'xy' }, { authorization: 'Bearer m_ck', 'User-Agent' => 'My Agent' }) }
309
+
310
+ it { expect(request.status).to eq 200 }
311
+ end
312
+
313
+ shared_examples 'raise NotFound when params do not satisfy the strict check' do |params|
314
+ subject(:request) { connection.get('/strict', params, { 'Authorization' => 'Bearer m_ck', 'X-C' => 'hello' }) }
315
+
316
+ context "with #{params.inspect}" do
317
+ it { expect { request }.to raise_error described_class::Stubs::NotFound }
318
+ end
319
+ end
320
+
321
+ it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '12' }
322
+ it_behaves_like 'raise NotFound when params do not satisfy the strict check', { b: 'xy' }
323
+ it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '123', b: 'xy' }
324
+ it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '12', b: 'xyz' }
325
+ it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '12', b: 'xy', c: 'hello' }
326
+ it_behaves_like 'raise NotFound when params do not satisfy the strict check', { additional: 'special', a: '12', b: 'xy', c: 'hello' }
327
+
328
+ shared_examples 'raise NotFound when headers do not satisfy the strict check' do |path, headers|
329
+ subject(:request) { connection.get(path, { a: 12, b: 'xy' }, headers) }
330
+
331
+ context "with #{headers.inspect}" do
332
+ it { expect { request }.to raise_error described_class::Stubs::NotFound }
333
+ end
334
+ end
335
+
336
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck' }
337
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { 'X-C' => 'hello' }
338
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'Hi' }
339
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Basic m_ck', 'x-c': 'hello' }
340
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello', x_special: 'special' }
341
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck' }
342
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'Unknown' }
343
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'My Agent', x_special: 'special' }
344
+
345
+ context 'when strict_mode is disabled' do
346
+ before do
347
+ stubs.strict_mode = false
348
+ end
349
+
350
+ shared_examples 'does not raise NotFound even when params do not satisfy the strict check' do |params|
351
+ subject(:request) { connection.get('/strict', params, { 'Authorization' => 'Bearer m_ck', 'X-C' => 'hello' }) }
352
+
353
+ context "with #{params.inspect}" do
354
+ it { expect(request.status).to eq 200 }
355
+ end
356
+ end
357
+
358
+ it_behaves_like 'does not raise NotFound even when params do not satisfy the strict check', { a: '12', b: 'xy' }
359
+ it_behaves_like 'does not raise NotFound even when params do not satisfy the strict check', { a: '12', b: 'xy', c: 'hello' }
360
+ it_behaves_like 'does not raise NotFound even when params do not satisfy the strict check', { additional: 'special', a: '12', b: 'xy', c: 'hello' }
361
+
362
+ shared_examples 'does not raise NotFound even when headers do not satisfy the strict check' do |path, headers|
363
+ subject(:request) { connection.get(path, { a: 12, b: 'xy' }, headers) }
364
+
365
+ context "with #{headers.inspect}" do
366
+ it { expect(request.status).to eq 200 }
367
+ end
368
+ end
369
+
370
+ it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello' }
371
+ it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello', x_special: 'special' }
372
+ it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello', user_agent: 'Special Agent' }
373
+ it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'My Agent' }
374
+ it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'My Agent', x_special: 'special' }
375
+ end
376
+
377
+ describe 'body_match?' do
378
+ let(:stubs) do
379
+ described_class::Stubs.new do |stubs|
380
+ stubs.post('/no_check') { [200, {}, 'ok'] }
381
+ stubs.post('/with_string', 'abc') { [200, {}, 'ok'] }
382
+ stubs.post(
383
+ '/with_proc',
384
+ ->(request_body) { JSON.parse(request_body, symbolize_names: true) == { x: '!', a: [{ m: [{ a: true }], n: 123 }] } },
385
+ { content_type: 'application/json' }
386
+ ) do
387
+ [200, {}, 'ok']
388
+ end
389
+ end
390
+ end
391
+
392
+ context 'when trying without any args for body' do
393
+ subject(:without_body) { connection.post('/no_check') }
394
+
395
+ it { expect(without_body.status).to eq 200 }
396
+ end
397
+
398
+ context 'when trying with string body stubs' do
399
+ subject(:with_string) { connection.post('/with_string', 'abc') }
400
+
401
+ it { expect(with_string.status).to eq 200 }
402
+ end
403
+
404
+ context 'when trying with proc body stubs' do
405
+ subject(:with_proc) do
406
+ connection.post('/with_proc', JSON.dump(a: [{ n: 123, m: [{ a: true }] }], x: '!'), { 'Content-Type' => 'application/json' })
407
+ end
408
+
409
+ it { expect(with_proc.status).to eq 200 }
410
+ end
411
+ end
412
+ end
413
+
414
+ describe 'request timeout' do
415
+ subject(:request) do
416
+ connection.get('/sleep') do |req|
417
+ req.options.timeout = timeout
418
+ end
419
+ end
420
+
421
+ before do
422
+ stubs.get('/sleep') do
423
+ sleep(0.01)
424
+ [200, {}, '']
425
+ end
426
+ end
427
+
428
+ context 'when request is within timeout' do
429
+ let(:timeout) { 1 }
430
+
431
+ it { expect(request.status).to eq 200 }
432
+ end
433
+
434
+ context 'when request is too slow' do
435
+ let(:timeout) { 0.001 }
436
+
437
+ it 'raises an exception' do
438
+ expect { request }.to raise_error(Faraday::TimeoutError)
439
+ end
440
+ end
441
+ end
260
442
  end