faraday 2.14.0 → 2.15.1

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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +1 -1
  3. data/README.md +1 -1
  4. data/Rakefile +11 -5
  5. data/examples/client_test.rb +1 -1
  6. data/lib/faraday/adapter/test.rb +8 -0
  7. data/lib/faraday/adapter.rb +7 -6
  8. data/lib/faraday/connection.rb +28 -18
  9. data/lib/faraday/encoders/flat_params_encoder.rb +1 -0
  10. data/lib/faraday/encoders/nested_params_encoder.rb +12 -2
  11. data/lib/faraday/error.rb +30 -3
  12. data/lib/faraday/logging/formatter.rb +2 -2
  13. data/lib/faraday/middleware.rb +1 -1
  14. data/lib/faraday/options/connection_options.rb +27 -0
  15. data/lib/faraday/options/env.rb +3 -3
  16. data/lib/faraday/options/proxy_options.rb +13 -3
  17. data/lib/faraday/options/request_options.rb +36 -1
  18. data/lib/faraday/options.rb +4 -3
  19. data/lib/faraday/rack_builder.rb +9 -9
  20. data/lib/faraday/response/json.rb +2 -2
  21. data/lib/faraday/response/raise_error.rb +7 -1
  22. data/lib/faraday/response.rb +5 -2
  23. data/lib/faraday/utils/headers.rb +46 -12
  24. data/lib/faraday/utils.rb +15 -3
  25. data/lib/faraday/version.rb +1 -1
  26. data/lib/faraday.rb +32 -0
  27. data/spec/external_adapters/faraday_specs_setup.rb +2 -0
  28. data/spec/support/integration/test_pki.rb +77 -0
  29. data/spec/support/integration/tls_server.rb +92 -0
  30. data/spec/support/integration_helper.rb +71 -0
  31. metadata +7 -31
  32. data/spec/faraday/adapter/test_spec.rb +0 -442
  33. data/spec/faraday/adapter_registry_spec.rb +0 -28
  34. data/spec/faraday/adapter_spec.rb +0 -55
  35. data/spec/faraday/connection_spec.rb +0 -808
  36. data/spec/faraday/error_spec.rb +0 -175
  37. data/spec/faraday/middleware_registry_spec.rb +0 -31
  38. data/spec/faraday/middleware_spec.rb +0 -213
  39. data/spec/faraday/options/env_spec.rb +0 -76
  40. data/spec/faraday/options/options_spec.rb +0 -297
  41. data/spec/faraday/options/proxy_options_spec.rb +0 -79
  42. data/spec/faraday/options/request_options_spec.rb +0 -19
  43. data/spec/faraday/params_encoders/flat_spec.rb +0 -42
  44. data/spec/faraday/params_encoders/nested_spec.rb +0 -151
  45. data/spec/faraday/rack_builder_spec.rb +0 -317
  46. data/spec/faraday/request/authorization_spec.rb +0 -118
  47. data/spec/faraday/request/instrumentation_spec.rb +0 -74
  48. data/spec/faraday/request/json_spec.rb +0 -199
  49. data/spec/faraday/request/url_encoded_spec.rb +0 -93
  50. data/spec/faraday/request_spec.rb +0 -110
  51. data/spec/faraday/response/json_spec.rb +0 -206
  52. data/spec/faraday/response/logger_spec.rb +0 -299
  53. data/spec/faraday/response/raise_error_spec.rb +0 -286
  54. data/spec/faraday/response_spec.rb +0 -84
  55. data/spec/faraday/utils/headers_spec.rb +0 -109
  56. data/spec/faraday/utils_spec.rb +0 -120
  57. data/spec/faraday_spec.rb +0 -43
  58. data/spec/spec_helper.rb +0 -133
@@ -1,109 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Faraday::Utils::Headers do
4
- subject { Faraday::Utils::Headers.new }
5
-
6
- context 'when Content-Type is set to application/json' do
7
- before { subject['Content-Type'] = 'application/json' }
8
-
9
- it { expect(subject.keys).to eq(['Content-Type']) }
10
- it { expect(subject['Content-Type']).to eq('application/json') }
11
- it { expect(subject['CONTENT-TYPE']).to eq('application/json') }
12
- it { expect(subject['content-type']).to eq('application/json') }
13
- it { is_expected.to include('content-type') }
14
- end
15
-
16
- context 'when Content-Type is set to application/xml' do
17
- before { subject['Content-Type'] = 'application/xml' }
18
-
19
- it { expect(subject.keys).to eq(['Content-Type']) }
20
- it { expect(subject['Content-Type']).to eq('application/xml') }
21
- it { expect(subject['CONTENT-TYPE']).to eq('application/xml') }
22
- it { expect(subject['content-type']).to eq('application/xml') }
23
- it { is_expected.to include('content-type') }
24
- end
25
-
26
- describe '#fetch' do
27
- before { subject['Content-Type'] = 'application/json' }
28
-
29
- it { expect(subject.fetch('Content-Type')).to eq('application/json') }
30
- it { expect(subject.fetch('CONTENT-TYPE')).to eq('application/json') }
31
- it { expect(subject.fetch(:content_type)).to eq('application/json') }
32
- it { expect(subject.fetch('invalid', 'default')).to eq('default') }
33
- it { expect(subject.fetch('invalid', false)).to eq(false) }
34
- it { expect(subject.fetch('invalid', nil)).to be_nil }
35
- it { expect(subject.fetch('Invalid') { |key| "#{key} key" }).to eq('Invalid key') }
36
- it 'calls a block when provided' do
37
- block_called = false
38
- expect(subject.fetch('content-type') { block_called = true }).to eq('application/json')
39
- expect(block_called).to be_falsey
40
- end
41
- it 'raises an error if key not found' do
42
- expected_error = defined?(KeyError) ? KeyError : IndexError
43
- expect { subject.fetch('invalid') }.to raise_error(expected_error)
44
- end
45
- end
46
-
47
- describe '#delete' do
48
- before do
49
- subject['Content-Type'] = 'application/json'
50
- @deleted = subject.delete('content-type')
51
- end
52
-
53
- it { expect(@deleted).to eq('application/json') }
54
- it { expect(subject.size).to eq(0) }
55
- it { is_expected.not_to include('content-type') }
56
- it { expect(subject.delete('content-type')).to be_nil }
57
- end
58
-
59
- describe '#dig' do
60
- before { subject['Content-Type'] = 'application/json' }
61
-
62
- it { expect(subject&.dig('Content-Type')).to eq('application/json') }
63
- it { expect(subject&.dig('CONTENT-TYPE')).to eq('application/json') }
64
- it { expect(subject&.dig(:content_type)).to eq('application/json') }
65
- it { expect(subject&.dig('invalid')).to be_nil }
66
- end
67
-
68
- describe '#parse' do
69
- context 'when response headers leave http status line out' do
70
- let(:headers) { "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" }
71
-
72
- before { subject.parse(headers) }
73
-
74
- it { expect(subject.keys).to eq(%w[Content-Type]) }
75
- it { expect(subject['Content-Type']).to eq('text/html') }
76
- it { expect(subject['content-type']).to eq('text/html') }
77
- end
78
-
79
- context 'when response headers values include a colon' do
80
- let(:headers) { "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nLocation: http://httpbingo.org/\r\n\r\n" }
81
-
82
- before { subject.parse(headers) }
83
-
84
- it { expect(subject['location']).to eq('http://httpbingo.org/') }
85
- end
86
-
87
- context 'when response headers include a blank line' do
88
- let(:headers) { "HTTP/1.1 200 OK\r\n\r\nContent-Type: text/html\r\n\r\n" }
89
-
90
- before { subject.parse(headers) }
91
-
92
- it { expect(subject['content-type']).to eq('text/html') }
93
- end
94
-
95
- context 'when response headers include already stored keys' do
96
- let(:headers) { "HTTP/1.1 200 OK\r\nX-Numbers: 123\r\n\r\n" }
97
-
98
- before do
99
- h = subject
100
- h[:x_numbers] = 8
101
- h.parse(headers)
102
- end
103
-
104
- it do
105
- expect(subject[:x_numbers]).to eq('8, 123')
106
- end
107
- end
108
- end
109
- end
@@ -1,120 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Faraday::Utils do
4
- describe 'headers parsing' do
5
- let(:multi_response_headers) do
6
- "HTTP/1.x 500 OK\r\nContent-Type: text/html; charset=UTF-8\r\n" \
7
- "HTTP/1.x 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n"
8
- end
9
-
10
- it 'parse headers for aggregated responses' do
11
- headers = Faraday::Utils::Headers.new
12
- headers.parse(multi_response_headers)
13
-
14
- result = headers.to_hash
15
-
16
- expect(result['Content-Type']).to eq('application/json; charset=UTF-8')
17
- end
18
- end
19
-
20
- describe 'URI parsing' do
21
- let(:url) { 'http://example.com/abc' }
22
-
23
- it 'escapes safe buffer' do
24
- str = FakeSafeBuffer.new('$32,000.00')
25
- expect(Faraday::Utils.escape(str)).to eq('%2432%2C000.00')
26
- end
27
-
28
- it 'parses with default parser' do
29
- with_default_uri_parser(nil) do
30
- uri = normalize(url)
31
- expect(uri.host).to eq('example.com')
32
- end
33
- end
34
-
35
- it 'parses with URI' do
36
- with_default_uri_parser(::URI) do
37
- uri = normalize(url)
38
- expect(uri.host).to eq('example.com')
39
- end
40
- end
41
-
42
- it 'parses with block' do
43
- with_default_uri_parser(->(u) { "booya#{'!' * u.size}" }) do
44
- expect(normalize(url)).to eq('booya!!!!!!!!!!!!!!!!!!!!!!')
45
- end
46
- end
47
-
48
- it 'replaces headers hash' do
49
- headers = Faraday::Utils::Headers.new('authorization' => 't0ps3cr3t!')
50
- expect(headers).to have_key('authorization')
51
-
52
- headers.replace('content-type' => 'text/plain')
53
- expect(headers).not_to have_key('authorization')
54
- end
55
- end
56
-
57
- describe '.deep_merge!' do
58
- let(:connection_options) { Faraday::ConnectionOptions.new }
59
- let(:url) do
60
- {
61
- url: 'http://example.com/abc',
62
- headers: { 'Mime-Version' => '1.0' },
63
- request: { oauth: { consumer_key: 'anonymous' } },
64
- ssl: { version: '2' }
65
- }
66
- end
67
-
68
- it 'recursively merges the headers' do
69
- connection_options.headers = { user_agent: 'My Agent 1.0' }
70
- deep_merge = Faraday::Utils.deep_merge!(connection_options, url)
71
-
72
- expect(deep_merge.headers).to eq('Mime-Version' => '1.0', user_agent: 'My Agent 1.0')
73
- end
74
-
75
- context 'when a target hash has an Options Struct value' do
76
- let(:request) do
77
- {
78
- params_encoder: nil,
79
- proxy: nil,
80
- bind: nil,
81
- timeout: nil,
82
- open_timeout: nil,
83
- read_timeout: nil,
84
- write_timeout: nil,
85
- boundary: nil,
86
- oauth: { consumer_key: 'anonymous' },
87
- context: nil,
88
- on_data: nil
89
- }
90
- end
91
- let(:ssl) do
92
- {
93
- verify: nil,
94
- ca_file: nil,
95
- ca_path: nil,
96
- verify_mode: nil,
97
- cert_store: nil,
98
- client_cert: nil,
99
- client_key: nil,
100
- certificate: nil,
101
- private_key: nil,
102
- verify_depth: nil,
103
- version: '2',
104
- min_version: nil,
105
- max_version: nil,
106
- verify_hostname: nil,
107
- hostname: nil,
108
- ciphers: nil
109
- }
110
- end
111
-
112
- it 'does not overwrite an Options Struct value' do
113
- deep_merge = Faraday::Utils.deep_merge!(connection_options, url)
114
-
115
- expect(deep_merge.request.to_h).to eq(request)
116
- expect(deep_merge.ssl.to_h).to eq(ssl)
117
- end
118
- end
119
- end
120
- end
data/spec/faraday_spec.rb DELETED
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Faraday do
4
- it 'has a version number' do
5
- expect(Faraday::VERSION).not_to be nil
6
- end
7
-
8
- context 'proxies to default_connection' do
9
- let(:mock_connection) { double('Connection') }
10
- before do
11
- Faraday.default_connection = mock_connection
12
- end
13
-
14
- it 'proxies methods that exist on the default_connection' do
15
- expect(mock_connection).to receive(:this_should_be_proxied)
16
-
17
- Faraday.this_should_be_proxied
18
- end
19
-
20
- it 'uses method_missing on Faraday if there is no proxyable method' do
21
- expected_message =
22
- if RUBY_VERSION >= '3.4'
23
- "undefined method 'this_method_does_not_exist' for module Faraday"
24
- elsif RUBY_VERSION >= '3.3'
25
- "undefined method `this_method_does_not_exist' for module Faraday"
26
- else
27
- "undefined method `this_method_does_not_exist' for Faraday:Module"
28
- end
29
-
30
- expect { Faraday.this_method_does_not_exist }.to raise_error(NoMethodError, expected_message)
31
- end
32
-
33
- it 'proxied methods can be accessed' do
34
- allow(mock_connection).to receive(:this_should_be_proxied)
35
-
36
- expect(Faraday.method(:this_should_be_proxied)).to be_a(Method)
37
- end
38
-
39
- after do
40
- Faraday.default_connection = nil
41
- end
42
- end
43
- end
data/spec/spec_helper.rb DELETED
@@ -1,133 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # This file was generated by the `rspec --init` command. Conventionally, all
4
- # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
- # The generated `.rspec` file contains `--require spec_helper` which will cause
6
- # this file to always be loaded, without a need to explicitly require it in any
7
- # files.
8
- #
9
- # Given that it is always loaded, you are encouraged to keep this file as
10
- # light-weight as possible. Requiring heavyweight dependencies from this file
11
- # will add to the boot time of your test suite on EVERY test run, even for an
12
- # individual file that may not need all of that loaded. Instead, consider making
13
- # a separate helper file that requires the additional dependencies and performs
14
- # the additional setup, and require it from the spec files that actually need
15
- # it.
16
- #
17
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18
-
19
- require 'simplecov'
20
- require 'coveralls'
21
- require 'webmock/rspec'
22
- WebMock.disable_net_connect!(allow_localhost: true)
23
-
24
- SimpleCov.formatters = [SimpleCov::Formatter::HTMLFormatter, Coveralls::SimpleCov::Formatter]
25
-
26
- SimpleCov.start do
27
- add_filter '/spec/'
28
- minimum_coverage 84
29
- minimum_coverage_by_file 26
30
- end
31
-
32
- require 'faraday'
33
- require 'pry'
34
-
35
- # Ensure all /lib files are loaded
36
- # so they will be included in the test coverage report.
37
- Dir['./lib/**/*.rb'].each { |file| require file }
38
-
39
- # Load all Rspec support files
40
- Dir['./spec/support/**/*.rb'].each { |file| require file }
41
-
42
- RSpec.configure do |config|
43
- # rspec-expectations config goes here. You can use an alternate
44
- # assertion/expectation library such as wrong or the stdlib/minitest
45
- # assertions if you prefer.
46
- config.expect_with :rspec do |expectations|
47
- # This option will default to `true` in RSpec 4. It makes the `description`
48
- # and `failure_message` of custom matchers include text for helper methods
49
- # defined using `chain`, e.g.:
50
- # be_bigger_than(2).and_smaller_than(4).description
51
- # # => "be bigger than 2 and smaller than 4"
52
- # ...rather than:
53
- # # => "be bigger than 2"
54
- expectations.include_chain_clauses_in_custom_matcher_descriptions = true
55
- end
56
-
57
- # rspec-mocks config goes here. You can use an alternate test double
58
- # library (such as bogus or mocha) by changing the `mock_with` option here.
59
- config.mock_with :rspec do |mocks|
60
- # Prevents you from mocking or stubbing a method that does not exist on
61
- # a real object. This is generally recommended, and will default to
62
- # `true` in RSpec 4.
63
- mocks.verify_partial_doubles = true
64
- end
65
-
66
- # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
67
- # have no way to turn it off -- the option exists only for backwards
68
- # compatibility in RSpec 3). It causes shared context metadata to be
69
- # inherited by the metadata hash of host groups and examples, rather than
70
- # triggering implicit auto-inclusion in groups with matching metadata.
71
- config.shared_context_metadata_behavior = :apply_to_host_groups
72
-
73
- # This allows you to limit a spec run to individual examples or groups
74
- # you care about by tagging them with `:focus` metadata. When nothing
75
- # is tagged with `:focus`, all examples get run. RSpec also provides
76
- # aliases for `it`, `describe`, and `context` that include `:focus`
77
- # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
78
- # config.filter_run_when_matching :focus
79
-
80
- # Allows RSpec to persist some state between runs in order to support
81
- # the `--only-failures` and `--next-failure` CLI options. We recommend
82
- # you configure your source control system to ignore this file.
83
- # config.example_status_persistence_file_path = "spec/examples.txt"
84
-
85
- # Limits the available syntax to the non-monkey patched syntax that is
86
- # recommended. For more details, see:
87
- # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
88
- # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
89
- # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
90
- # config.disable_monkey_patching!
91
-
92
- # This setting enables warnings. It's recommended, but in some cases may
93
- # be too noisy due to issues in dependencies.
94
- # config.warnings = true
95
-
96
- # Many RSpec users commonly either run the entire suite or an individual
97
- # file, and it's useful to allow more verbose output when running an
98
- # individual spec file.
99
- # if config.files_to_run.one?
100
- # # Use the documentation formatter for detailed output,
101
- # # unless a formatter has already been configured
102
- # # (e.g. via a command-line flag).
103
- # config.default_formatter = "doc"
104
- # end
105
-
106
- # Print the 10 slowest examples and example groups at the
107
- # end of the spec run, to help surface which specs are running
108
- # particularly slow.
109
- # config.profile_examples = 10
110
-
111
- # Run specs in random order to surface order dependencies. If you find an
112
- # order dependency and want to debug it, you can fix the order by providing
113
- # the seed, which is printed after each run.
114
- # --seed 1234
115
- config.order = :random
116
-
117
- # Seed global randomization in this process using the `--seed` CLI option.
118
- # Setting this allows you to use `--seed` to deterministically reproduce
119
- # test failures related to randomization by passing the same `--seed` value
120
- # as the one that triggered the failure.
121
- Kernel.srand config.seed
122
-
123
- config.include Faraday::HelperMethods
124
- end
125
-
126
- # Extends RSpec DocumentationFormatter to hide skipped tests.
127
- module FormatterOverrides
128
- def example_pending(_arg); end
129
-
130
- def dump_pending(_arg); end
131
-
132
- RSpec::Core::Formatters::DocumentationFormatter.prepend self
133
- end