faraday 1.7.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +193 -3
  3. data/README.md +11 -9
  4. data/examples/client_spec.rb +19 -19
  5. data/examples/client_test.rb +22 -22
  6. data/lib/faraday/adapter/test.rb +10 -4
  7. data/lib/faraday/adapter.rb +2 -5
  8. data/lib/faraday/connection.rb +17 -93
  9. data/lib/faraday/encoders/nested_params_encoder.rb +2 -2
  10. data/lib/faraday/error.rb +3 -8
  11. data/lib/faraday/logging/formatter.rb +1 -0
  12. data/lib/faraday/middleware.rb +0 -1
  13. data/lib/faraday/middleware_registry.rb +17 -63
  14. data/lib/faraday/options.rb +3 -3
  15. data/lib/faraday/rack_builder.rb +23 -20
  16. data/lib/faraday/request/authorization.rb +32 -38
  17. data/lib/faraday/request/instrumentation.rb +2 -0
  18. data/lib/faraday/request/json.rb +55 -0
  19. data/lib/faraday/request/url_encoded.rb +2 -0
  20. data/lib/faraday/request.rb +11 -31
  21. data/lib/faraday/response/json.rb +54 -0
  22. data/lib/faraday/response/logger.rb +4 -4
  23. data/lib/faraday/response/raise_error.rb +9 -1
  24. data/lib/faraday/response.rb +8 -19
  25. data/lib/faraday/utils/headers.rb +1 -1
  26. data/lib/faraday/utils.rb +10 -5
  27. data/lib/faraday/version.rb +1 -1
  28. data/lib/faraday.rb +11 -39
  29. data/spec/faraday/connection_spec.rb +136 -85
  30. data/spec/faraday/middleware_registry_spec.rb +31 -0
  31. data/spec/faraday/options/env_spec.rb +2 -2
  32. data/spec/faraday/rack_builder_spec.rb +26 -54
  33. data/spec/faraday/request/authorization_spec.rb +19 -24
  34. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  35. data/spec/faraday/request/json_spec.rb +111 -0
  36. data/spec/faraday/request/url_encoded_spec.rb +0 -1
  37. data/spec/faraday/request_spec.rb +4 -15
  38. data/spec/faraday/response/json_spec.rb +117 -0
  39. data/spec/faraday/response/raise_error_spec.rb +7 -4
  40. data/spec/faraday/utils/headers_spec.rb +2 -2
  41. data/spec/faraday/utils_spec.rb +62 -1
  42. data/spec/support/fake_safe_buffer.rb +1 -1
  43. data/spec/support/helper_methods.rb +0 -37
  44. data/spec/support/shared_examples/adapter.rb +0 -1
  45. data/spec/support/shared_examples/request_method.rb +5 -18
  46. metadata +12 -147
  47. data/lib/faraday/adapter/typhoeus.rb +0 -15
  48. data/lib/faraday/autoload.rb +0 -87
  49. data/lib/faraday/dependency_loader.rb +0 -37
  50. data/lib/faraday/file_part.rb +0 -128
  51. data/lib/faraday/param_part.rb +0 -53
  52. data/lib/faraday/request/basic_authentication.rb +0 -20
  53. data/lib/faraday/request/multipart.rb +0 -106
  54. data/lib/faraday/request/retry.rb +0 -239
  55. data/lib/faraday/request/token_authentication.rb +0 -20
  56. data/spec/faraday/adapter/em_http_spec.rb +0 -49
  57. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
  58. data/spec/faraday/adapter/excon_spec.rb +0 -49
  59. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  60. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  61. data/spec/faraday/adapter/patron_spec.rb +0 -18
  62. data/spec/faraday/adapter/rack_spec.rb +0 -8
  63. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  64. data/spec/faraday/composite_read_io_spec.rb +0 -80
  65. data/spec/faraday/request/multipart_spec.rb +0 -302
  66. data/spec/faraday/request/retry_spec.rb +0 -242
  67. data/spec/faraday/response/middleware_spec.rb +0 -68
  68. data/spec/support/webmock_rack_app.rb +0 -68
@@ -1,239 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Request
5
- # Catches exceptions and retries each request a limited number of times.
6
- #
7
- # By default, it retries 2 times and handles only timeout exceptions. It can
8
- # be configured with an arbitrary number of retries, a list of exceptions to
9
- # handle, a retry interval, a percentage of randomness to add to the retry
10
- # interval, and a backoff factor.
11
- #
12
- # @example Configure Retry middleware using intervals
13
- # Faraday.new do |conn|
14
- # conn.request(:retry, max: 2,
15
- # interval: 0.05,
16
- # interval_randomness: 0.5,
17
- # backoff_factor: 2,
18
- # exceptions: [CustomException, 'Timeout::Error'])
19
- #
20
- # conn.adapter(:net_http) # NB: Last middleware must be the adapter
21
- # end
22
- #
23
- # This example will result in a first interval that is random between 0.05
24
- # and 0.075 and a second interval that is random between 0.1 and 0.125.
25
- class Retry < Faraday::Middleware
26
- DEFAULT_EXCEPTIONS = [
27
- Errno::ETIMEDOUT, 'Timeout::Error',
28
- Faraday::TimeoutError, Faraday::RetriableResponse
29
- ].freeze
30
- IDEMPOTENT_METHODS = %i[delete get head options put].freeze
31
-
32
- # Options contains the configurable parameters for the Retry middleware.
33
- class Options < Faraday::Options.new(:max, :interval, :max_interval,
34
- :interval_randomness,
35
- :backoff_factor, :exceptions,
36
- :methods, :retry_if, :retry_block,
37
- :retry_statuses)
38
-
39
- DEFAULT_CHECK = ->(_env, _exception) { false }
40
-
41
- def self.from(value)
42
- if value.is_a?(Integer)
43
- new(value)
44
- else
45
- super(value)
46
- end
47
- end
48
-
49
- def max
50
- (self[:max] ||= 2).to_i
51
- end
52
-
53
- def interval
54
- (self[:interval] ||= 0).to_f
55
- end
56
-
57
- def max_interval
58
- (self[:max_interval] ||= Float::MAX).to_f
59
- end
60
-
61
- def interval_randomness
62
- (self[:interval_randomness] ||= 0).to_f
63
- end
64
-
65
- def backoff_factor
66
- (self[:backoff_factor] ||= 1).to_f
67
- end
68
-
69
- def exceptions
70
- Array(self[:exceptions] ||= DEFAULT_EXCEPTIONS)
71
- end
72
-
73
- def methods
74
- Array(self[:methods] ||= IDEMPOTENT_METHODS)
75
- end
76
-
77
- def retry_if
78
- self[:retry_if] ||= DEFAULT_CHECK
79
- end
80
-
81
- def retry_block
82
- self[:retry_block] ||= proc {}
83
- end
84
-
85
- def retry_statuses
86
- Array(self[:retry_statuses] ||= [])
87
- end
88
- end
89
-
90
- # @param app [#call]
91
- # @param options [Hash]
92
- # @option options [Integer] :max (2) Maximum number of retries
93
- # @option options [Integer] :interval (0) Pause in seconds between retries
94
- # @option options [Integer] :interval_randomness (0) The maximum random
95
- # interval amount expressed as a float between
96
- # 0 and 1 to use in addition to the interval.
97
- # @option options [Integer] :max_interval (Float::MAX) An upper limit
98
- # for the interval
99
- # @option options [Integer] :backoff_factor (1) The amount to multiply
100
- # each successive retry's interval amount by in order to provide backoff
101
- # @option options [Array] :exceptions ([ Errno::ETIMEDOUT,
102
- # 'Timeout::Error', Faraday::TimeoutError, Faraday::RetriableResponse])
103
- # The list of exceptions to handle. Exceptions can be given as
104
- # Class, Module, or String.
105
- # @option options [Array] :methods (the idempotent HTTP methods
106
- # in IDEMPOTENT_METHODS) A list of HTTP methods to retry without
107
- # calling retry_if. Pass an empty Array to call retry_if
108
- # for all exceptions.
109
- # @option options [Block] :retry_if (false) block that will receive
110
- # the env object and the exception raised
111
- # and should decide if the code should retry still the action or
112
- # not independent of the retry count. This would be useful
113
- # if the exception produced is non-recoverable or if the
114
- # the HTTP method called is not idempotent.
115
- # @option options [Block] :retry_block block that is executed before
116
- # every retry. Request environment, middleware options, current number
117
- # of retries and the exception is passed to the block as parameters.
118
- # @option options [Array] :retry_statuses Array of Integer HTTP status
119
- # codes or a single Integer value that determines whether to raise
120
- # a Faraday::RetriableResponse exception based on the HTTP status code
121
- # of an HTTP response.
122
- def initialize(app, options = nil)
123
- super(app)
124
- @options = Options.from(options)
125
- @errmatch = build_exception_matcher(@options.exceptions)
126
- end
127
-
128
- def calculate_sleep_amount(retries, env)
129
- retry_after = calculate_retry_after(env)
130
- retry_interval = calculate_retry_interval(retries)
131
-
132
- return if retry_after && retry_after > @options.max_interval
133
-
134
- if retry_after && retry_after >= retry_interval
135
- retry_after
136
- else
137
- retry_interval
138
- end
139
- end
140
-
141
- # @param env [Faraday::Env]
142
- def call(env)
143
- retries = @options.max
144
- request_body = env[:body]
145
- begin
146
- # after failure env[:body] is set to the response body
147
- env[:body] = request_body
148
- @app.call(env).tap do |resp|
149
- if @options.retry_statuses.include?(resp.status)
150
- raise Faraday::RetriableResponse.new(nil, resp)
151
- end
152
- end
153
- rescue @errmatch => e
154
- if retries.positive? && retry_request?(env, e)
155
- retries -= 1
156
- rewind_files(request_body)
157
- @options.retry_block.call(env, @options, retries, e)
158
- if (sleep_amount = calculate_sleep_amount(retries + 1, env))
159
- sleep sleep_amount
160
- retry
161
- end
162
- end
163
-
164
- raise unless e.is_a?(Faraday::RetriableResponse)
165
-
166
- e.response
167
- end
168
- end
169
-
170
- # An exception matcher for the rescue clause can usually be any object
171
- # that responds to `===`, but for Ruby 1.8 it has to be a Class or Module.
172
- #
173
- # @param exceptions [Array]
174
- # @api private
175
- # @return [Module] an exception matcher
176
- def build_exception_matcher(exceptions)
177
- matcher = Module.new
178
- (
179
- class << matcher
180
- self
181
- end).class_eval do
182
- define_method(:===) do |error|
183
- exceptions.any? do |ex|
184
- if ex.is_a? Module
185
- error.is_a? ex
186
- else
187
- error.class.to_s == ex.to_s
188
- end
189
- end
190
- end
191
- end
192
- matcher
193
- end
194
-
195
- private
196
-
197
- def retry_request?(env, exception)
198
- @options.methods.include?(env[:method]) ||
199
- @options.retry_if.call(env, exception)
200
- end
201
-
202
- def rewind_files(body)
203
- return unless body.is_a?(Hash)
204
-
205
- body.each do |_, value|
206
- value.rewind if value.is_a?(UploadIO)
207
- end
208
- end
209
-
210
- # MDN spec for Retry-After header:
211
- # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
212
- def calculate_retry_after(env)
213
- response_headers = env[:response_headers]
214
- return unless response_headers
215
-
216
- retry_after_value = env[:response_headers]['Retry-After']
217
-
218
- # Try to parse date from the header value
219
- begin
220
- datetime = DateTime.rfc2822(retry_after_value)
221
- datetime.to_time - Time.now.utc
222
- rescue ArgumentError
223
- retry_after_value.to_f
224
- end
225
- end
226
-
227
- def calculate_retry_interval(retries)
228
- retry_index = @options.max - retries
229
- current_interval = @options.interval *
230
- (@options.backoff_factor**retry_index)
231
- current_interval = [current_interval, @options.max_interval].min
232
- random_interval = rand * @options.interval_randomness.to_f *
233
- @options.interval
234
-
235
- current_interval + random_interval
236
- end
237
- end
238
- end
239
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Request
5
- # TokenAuthentication is a middleware that adds a 'Token' header to a
6
- # Faraday request.
7
- class TokenAuthentication < load_middleware(:authorization)
8
- # Public
9
- def self.header(token, options = nil)
10
- options ||= {}
11
- options[:token] = token
12
- super(:Token, options)
13
- end
14
-
15
- def initialize(app, token, options = nil)
16
- super(app, token, options)
17
- end
18
- end
19
- end
20
- end
@@ -1,49 +0,0 @@
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
@@ -1,18 +0,0 @@
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
@@ -1,49 +0,0 @@
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
@@ -1,73 +0,0 @@
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
@@ -1,64 +0,0 @@
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
@@ -1,18 +0,0 @@
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
@@ -1,8 +0,0 @@
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
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Faraday::Adapter::Typhoeus do
4
- features :request_body_on_query_methods, :parallel, :trace_method
5
-
6
- it_behaves_like 'an adapter'
7
- end
@@ -1,80 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'stringio'
4
-
5
- RSpec.describe Faraday::CompositeReadIO do
6
- Part = Struct.new(:to_io) do
7
- def length
8
- to_io.string.length
9
- end
10
- end
11
-
12
- def part(str)
13
- Part.new StringIO.new(str)
14
- end
15
-
16
- def composite_io(*parts)
17
- Faraday::CompositeReadIO.new(*parts)
18
- end
19
-
20
- context 'with empty composite_io' do
21
- subject { composite_io }
22
-
23
- it { expect(subject.length).to eq(0) }
24
- it { expect(subject.read).to eq('') }
25
- it { expect(subject.read(1)).to be_nil }
26
- end
27
-
28
- context 'with empty parts' do
29
- subject { composite_io(part(''), part('')) }
30
-
31
- it { expect(subject.length).to eq(0) }
32
- it { expect(subject.read).to eq('') }
33
- it { expect(subject.read(1)).to be_nil }
34
- end
35
-
36
- context 'with 2 parts' do
37
- subject { composite_io(part('abcd'), part('1234')) }
38
-
39
- it { expect(subject.length).to eq(8) }
40
- it { expect(subject.read).to eq('abcd1234') }
41
- it 'allows to read in chunks' do
42
- expect(subject.read(3)).to eq('abc')
43
- expect(subject.read(3)).to eq('d12')
44
- expect(subject.read(3)).to eq('34')
45
- expect(subject.read(3)).to be_nil
46
- end
47
- it 'allows to rewind while reading in chunks' do
48
- expect(subject.read(3)).to eq('abc')
49
- expect(subject.read(3)).to eq('d12')
50
- subject.rewind
51
- expect(subject.read(3)).to eq('abc')
52
- expect(subject.read(5)).to eq('d1234')
53
- expect(subject.read(3)).to be_nil
54
- subject.rewind
55
- expect(subject.read(2)).to eq('ab')
56
- end
57
- end
58
-
59
- context 'with mix of empty and non-empty parts' do
60
- subject { composite_io(part(''), part('abcd'), part(''), part('1234'), part('')) }
61
-
62
- it 'allows to read in chunks' do
63
- expect(subject.read(6)).to eq('abcd12')
64
- expect(subject.read(6)).to eq('34')
65
- expect(subject.read(6)).to be_nil
66
- end
67
- end
68
-
69
- context 'with utf8 multibyte part' do
70
- subject { composite_io(part("\x86"), part('ファイル')) }
71
-
72
- it { expect(subject.read).to eq(String.new("\x86\xE3\x83\x95\xE3\x82\xA1\xE3\x82\xA4\xE3\x83\xAB", encoding: 'BINARY')) }
73
- it 'allows to read in chunks' do
74
- expect(subject.read(3)).to eq(String.new("\x86\xE3\x83", encoding: 'BINARY'))
75
- expect(subject.read(3)).to eq(String.new("\x95\xE3\x82", encoding: 'BINARY'))
76
- expect(subject.read(8)).to eq(String.new("\xA1\xE3\x82\xA4\xE3\x83\xAB", encoding: 'BINARY'))
77
- expect(subject.read(3)).to be_nil
78
- end
79
- end
80
- end