faraday 0.9.1 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +360 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +28 -195
  5. data/Rakefile +4 -68
  6. data/examples/client_spec.rb +97 -0
  7. data/examples/client_test.rb +118 -0
  8. data/lib/faraday/adapter/test.rb +158 -58
  9. data/lib/faraday/adapter/typhoeus.rb +7 -115
  10. data/lib/faraday/adapter.rb +79 -20
  11. data/lib/faraday/adapter_registry.rb +30 -0
  12. data/lib/faraday/autoload.rb +39 -36
  13. data/lib/faraday/connection.rb +378 -168
  14. data/lib/faraday/dependency_loader.rb +37 -0
  15. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  16. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  17. data/lib/faraday/error.rb +128 -29
  18. data/lib/faraday/file_part.rb +128 -0
  19. data/lib/faraday/logging/formatter.rb +105 -0
  20. data/lib/faraday/methods.rb +6 -0
  21. data/lib/faraday/middleware.rb +19 -25
  22. data/lib/faraday/middleware_registry.rb +129 -0
  23. data/lib/faraday/options/connection_options.rb +22 -0
  24. data/lib/faraday/options/env.rb +181 -0
  25. data/lib/faraday/options/proxy_options.rb +32 -0
  26. data/lib/faraday/options/request_options.rb +22 -0
  27. data/lib/faraday/options/ssl_options.rb +59 -0
  28. data/lib/faraday/options.rb +61 -193
  29. data/lib/faraday/param_part.rb +53 -0
  30. data/lib/faraday/parameters.rb +4 -180
  31. data/lib/faraday/rack_builder.rb +84 -47
  32. data/lib/faraday/request/authorization.rb +51 -31
  33. data/lib/faraday/request/basic_authentication.rb +14 -7
  34. data/lib/faraday/request/instrumentation.rb +45 -27
  35. data/lib/faraday/request/multipart.rb +88 -45
  36. data/lib/faraday/request/retry.rb +212 -121
  37. data/lib/faraday/request/token_authentication.rb +15 -10
  38. data/lib/faraday/request/url_encoded.rb +43 -23
  39. data/lib/faraday/request.rb +96 -32
  40. data/lib/faraday/response/logger.rb +22 -48
  41. data/lib/faraday/response/raise_error.rb +49 -14
  42. data/lib/faraday/response.rb +33 -25
  43. data/lib/faraday/utils/headers.rb +139 -0
  44. data/lib/faraday/utils/params_hash.rb +61 -0
  45. data/lib/faraday/utils.rb +38 -218
  46. data/lib/faraday/version.rb +5 -0
  47. data/lib/faraday.rb +130 -213
  48. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  49. data/spec/faraday/adapter/em_http_spec.rb +49 -0
  50. data/spec/faraday/adapter/em_synchrony_spec.rb +18 -0
  51. data/spec/faraday/adapter/excon_spec.rb +49 -0
  52. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  53. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  54. data/spec/faraday/adapter/patron_spec.rb +18 -0
  55. data/spec/faraday/adapter/rack_spec.rb +8 -0
  56. data/spec/faraday/adapter/test_spec.rb +377 -0
  57. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  58. data/spec/faraday/adapter_registry_spec.rb +28 -0
  59. data/spec/faraday/adapter_spec.rb +55 -0
  60. data/spec/faraday/composite_read_io_spec.rb +80 -0
  61. data/spec/faraday/connection_spec.rb +736 -0
  62. data/spec/faraday/error_spec.rb +60 -0
  63. data/spec/faraday/middleware_spec.rb +52 -0
  64. data/spec/faraday/options/env_spec.rb +70 -0
  65. data/spec/faraday/options/options_spec.rb +297 -0
  66. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  67. data/spec/faraday/options/request_options_spec.rb +19 -0
  68. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  69. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  70. data/spec/faraday/rack_builder_spec.rb +345 -0
  71. data/spec/faraday/request/authorization_spec.rb +96 -0
  72. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  73. data/spec/faraday/request/multipart_spec.rb +302 -0
  74. data/spec/faraday/request/retry_spec.rb +242 -0
  75. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  76. data/spec/faraday/request_spec.rb +120 -0
  77. data/spec/faraday/response/logger_spec.rb +220 -0
  78. data/spec/faraday/response/middleware_spec.rb +68 -0
  79. data/spec/faraday/response/raise_error_spec.rb +169 -0
  80. data/spec/faraday/response_spec.rb +75 -0
  81. data/spec/faraday/utils/headers_spec.rb +82 -0
  82. data/spec/faraday/utils_spec.rb +56 -0
  83. data/spec/faraday_spec.rb +37 -0
  84. data/spec/spec_helper.rb +132 -0
  85. data/spec/support/disabling_stub.rb +14 -0
  86. data/spec/support/fake_safe_buffer.rb +15 -0
  87. data/spec/support/helper_methods.rb +133 -0
  88. data/spec/support/shared_examples/adapter.rb +105 -0
  89. data/spec/support/shared_examples/params_encoder.rb +18 -0
  90. data/spec/support/shared_examples/request_method.rb +262 -0
  91. data/spec/support/streaming_response_checker.rb +35 -0
  92. data/spec/support/webmock_rack_app.rb +68 -0
  93. metadata +199 -98
  94. data/.document +0 -6
  95. data/CONTRIBUTING.md +0 -36
  96. data/Gemfile +0 -25
  97. data/faraday.gemspec +0 -34
  98. data/lib/faraday/adapter/em_http.rb +0 -237
  99. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  100. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  101. data/lib/faraday/adapter/em_synchrony.rb +0 -92
  102. data/lib/faraday/adapter/excon.rb +0 -80
  103. data/lib/faraday/adapter/httpclient.rb +0 -106
  104. data/lib/faraday/adapter/net_http.rb +0 -130
  105. data/lib/faraday/adapter/net_http_persistent.rb +0 -48
  106. data/lib/faraday/adapter/patron.rb +0 -72
  107. data/lib/faraday/adapter/rack.rb +0 -58
  108. data/lib/faraday/upload_io.rb +0 -67
  109. data/script/cached-bundle +0 -46
  110. data/script/console +0 -7
  111. data/script/generate_certs +0 -42
  112. data/script/package +0 -7
  113. data/script/proxy-server +0 -42
  114. data/script/release +0 -17
  115. data/script/s3-put +0 -71
  116. data/script/server +0 -36
  117. data/script/test +0 -172
  118. data/test/adapters/default_test.rb +0 -14
  119. data/test/adapters/em_http_test.rb +0 -20
  120. data/test/adapters/em_synchrony_test.rb +0 -20
  121. data/test/adapters/excon_test.rb +0 -20
  122. data/test/adapters/httpclient_test.rb +0 -21
  123. data/test/adapters/integration.rb +0 -254
  124. data/test/adapters/logger_test.rb +0 -82
  125. data/test/adapters/net_http_persistent_test.rb +0 -20
  126. data/test/adapters/net_http_test.rb +0 -14
  127. data/test/adapters/patron_test.rb +0 -20
  128. data/test/adapters/rack_test.rb +0 -31
  129. data/test/adapters/test_middleware_test.rb +0 -114
  130. data/test/adapters/typhoeus_test.rb +0 -28
  131. data/test/authentication_middleware_test.rb +0 -65
  132. data/test/composite_read_io_test.rb +0 -111
  133. data/test/connection_test.rb +0 -522
  134. data/test/env_test.rb +0 -218
  135. data/test/helper.rb +0 -81
  136. data/test/live_server.rb +0 -67
  137. data/test/middleware/instrumentation_test.rb +0 -88
  138. data/test/middleware/retry_test.rb +0 -177
  139. data/test/middleware_stack_test.rb +0 -173
  140. data/test/multibyte.txt +0 -1
  141. data/test/options_test.rb +0 -252
  142. data/test/parameters_test.rb +0 -64
  143. data/test/request_middleware_test.rb +0 -142
  144. data/test/response_middleware_test.rb +0 -72
  145. data/test/strawberry.rb +0 -2
  146. data/test/utils_test.rb +0 -58
@@ -0,0 +1,82 @@
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 '#parse' do
60
+ before { subject.parse(headers) }
61
+
62
+ context 'when response headers leave http status line out' do
63
+ let(:headers) { "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" }
64
+
65
+ it { expect(subject.keys).to eq(%w[Content-Type]) }
66
+ it { expect(subject['Content-Type']).to eq('text/html') }
67
+ it { expect(subject['content-type']).to eq('text/html') }
68
+ end
69
+
70
+ context 'when response headers values include a colon' do
71
+ let(:headers) { "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nLocation: http://sushi.com/\r\n\r\n" }
72
+
73
+ it { expect(subject['location']).to eq('http://sushi.com/') }
74
+ end
75
+
76
+ context 'when response headers include a blank line' do
77
+ let(:headers) { "HTTP/1.1 200 OK\r\n\r\nContent-Type: text/html\r\n\r\n" }
78
+
79
+ it { expect(subject['content-type']).to eq('text/html') }
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,56 @@
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
+ end
@@ -0,0 +1,37 @@
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
+ expect { Faraday.this_method_does_not_exist }.to raise_error(
22
+ NoMethodError,
23
+ "undefined method `this_method_does_not_exist' for Faraday:Module"
24
+ )
25
+ end
26
+
27
+ it 'proxied methods can be accessed' do
28
+ allow(mock_connection).to receive(:this_should_be_proxied)
29
+
30
+ expect(Faraday.method(:this_should_be_proxied)).to be_a(Method)
31
+ end
32
+
33
+ after do
34
+ Faraday.default_connection = nil
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,132 @@
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
+ # Ensure all /lib files are loaded
33
+ # so they will be included in the test coverage report.
34
+ Dir['./lib/**/*.rb'].sort.each { |file| require file }
35
+
36
+ require 'faraday'
37
+ require 'pry'
38
+
39
+ Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
40
+
41
+ RSpec.configure do |config|
42
+ # rspec-expectations config goes here. You can use an alternate
43
+ # assertion/expectation library such as wrong or the stdlib/minitest
44
+ # assertions if you prefer.
45
+ config.expect_with :rspec do |expectations|
46
+ # This option will default to `true` in RSpec 4. It makes the `description`
47
+ # and `failure_message` of custom matchers include text for helper methods
48
+ # defined using `chain`, e.g.:
49
+ # be_bigger_than(2).and_smaller_than(4).description
50
+ # # => "be bigger than 2 and smaller than 4"
51
+ # ...rather than:
52
+ # # => "be bigger than 2"
53
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
54
+ end
55
+
56
+ # rspec-mocks config goes here. You can use an alternate test double
57
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
58
+ config.mock_with :rspec do |mocks|
59
+ # Prevents you from mocking or stubbing a method that does not exist on
60
+ # a real object. This is generally recommended, and will default to
61
+ # `true` in RSpec 4.
62
+ mocks.verify_partial_doubles = true
63
+ end
64
+
65
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
66
+ # have no way to turn it off -- the option exists only for backwards
67
+ # compatibility in RSpec 3). It causes shared context metadata to be
68
+ # inherited by the metadata hash of host groups and examples, rather than
69
+ # triggering implicit auto-inclusion in groups with matching metadata.
70
+ config.shared_context_metadata_behavior = :apply_to_host_groups
71
+
72
+ # This allows you to limit a spec run to individual examples or groups
73
+ # you care about by tagging them with `:focus` metadata. When nothing
74
+ # is tagged with `:focus`, all examples get run. RSpec also provides
75
+ # aliases for `it`, `describe`, and `context` that include `:focus`
76
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
77
+ # config.filter_run_when_matching :focus
78
+
79
+ # Allows RSpec to persist some state between runs in order to support
80
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
81
+ # you configure your source control system to ignore this file.
82
+ # config.example_status_persistence_file_path = "spec/examples.txt"
83
+
84
+ # Limits the available syntax to the non-monkey patched syntax that is
85
+ # recommended. For more details, see:
86
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
87
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
88
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
89
+ # config.disable_monkey_patching!
90
+
91
+ # This setting enables warnings. It's recommended, but in some cases may
92
+ # be too noisy due to issues in dependencies.
93
+ # config.warnings = true
94
+
95
+ # Many RSpec users commonly either run the entire suite or an individual
96
+ # file, and it's useful to allow more verbose output when running an
97
+ # individual spec file.
98
+ # if config.files_to_run.one?
99
+ # # Use the documentation formatter for detailed output,
100
+ # # unless a formatter has already been configured
101
+ # # (e.g. via a command-line flag).
102
+ # config.default_formatter = "doc"
103
+ # end
104
+
105
+ # Print the 10 slowest examples and example groups at the
106
+ # end of the spec run, to help surface which specs are running
107
+ # particularly slow.
108
+ # config.profile_examples = 10
109
+
110
+ # Run specs in random order to surface order dependencies. If you find an
111
+ # order dependency and want to debug it, you can fix the order by providing
112
+ # the seed, which is printed after each run.
113
+ # --seed 1234
114
+ config.order = :random
115
+
116
+ # Seed global randomization in this process using the `--seed` CLI option.
117
+ # Setting this allows you to use `--seed` to deterministically reproduce
118
+ # test failures related to randomization by passing the same `--seed` value
119
+ # as the one that triggered the failure.
120
+ Kernel.srand config.seed
121
+
122
+ config.include Faraday::HelperMethods
123
+ end
124
+
125
+ # Extends RSpec DocumentationFormatter to hide skipped tests.
126
+ module FormatterOverrides
127
+ def example_pending(_arg); end
128
+
129
+ def dump_pending(_arg); end
130
+
131
+ RSpec::Core::Formatters::DocumentationFormatter.prepend self
132
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Allows to disable WebMock stubs
4
+ module DisablingStub
5
+ def disable
6
+ @disabled = true
7
+ end
8
+
9
+ def disabled?
10
+ @disabled
11
+ end
12
+
13
+ WebMock::RequestStub.prepend self
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # emulates ActiveSupport::SafeBuffer#gsub
4
+ FakeSafeBuffer = Struct.new(:string) do
5
+ def to_s
6
+ self
7
+ end
8
+
9
+ def gsub(regex)
10
+ string.gsub(regex) do
11
+ match, = $&, '' =~ /a/
12
+ yield(match)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'multipart_parser/reader'
4
+
5
+ module Faraday
6
+ module HelperMethods
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ def features(*features)
13
+ @features = features
14
+ end
15
+
16
+ def on_feature(name)
17
+ yield if block_given? && feature?(name)
18
+ end
19
+
20
+ def feature?(name)
21
+ if @features.nil?
22
+ superclass.feature?(name) if superclass.respond_to?(:feature?)
23
+ elsif @features.include?(name)
24
+ true
25
+ end
26
+ end
27
+
28
+ def method_with_body?(method)
29
+ METHODS_WITH_BODY.include?(method.to_s)
30
+ end
31
+ end
32
+
33
+ def ssl_mode?
34
+ ENV['SSL'] == 'yes'
35
+ end
36
+
37
+ def normalize(url)
38
+ Faraday::Utils::URI(url)
39
+ end
40
+
41
+ def with_default_uri_parser(parser)
42
+ old_parser = Faraday::Utils.default_uri_parser
43
+ begin
44
+ Faraday::Utils.default_uri_parser = parser
45
+ yield
46
+ ensure
47
+ Faraday::Utils.default_uri_parser = old_parser
48
+ end
49
+ end
50
+
51
+ def with_env(new_env)
52
+ old_env = {}
53
+
54
+ new_env.each do |key, value|
55
+ old_env[key] = ENV.fetch(key, false)
56
+ ENV[key] = value
57
+ end
58
+
59
+ begin
60
+ yield
61
+ ensure
62
+ old_env.each do |key, value|
63
+ value == false ? ENV.delete(key) : ENV[key] = value
64
+ end
65
+ end
66
+ end
67
+
68
+ def with_env_proxy_disabled
69
+ Faraday.ignore_env_proxy = true
70
+
71
+ begin
72
+ yield
73
+ ensure
74
+ Faraday.ignore_env_proxy = false
75
+ end
76
+ end
77
+
78
+ def capture_warnings
79
+ old = $stderr
80
+ $stderr = StringIO.new
81
+ begin
82
+ yield
83
+ $stderr.string
84
+ ensure
85
+ $stderr = old
86
+ end
87
+ end
88
+
89
+ def multipart_file
90
+ Faraday::FilePart.new(__FILE__, 'text/x-ruby')
91
+ end
92
+
93
+ # parse boundary out of a Content-Type header like:
94
+ # Content-Type: multipart/form-data; boundary=gc0p4Jq0M2Yt08jU534c0p
95
+ def parse_multipart_boundary(ctype)
96
+ MultipartParser::Reader.extract_boundary_value(ctype)
97
+ end
98
+
99
+ # parse a multipart MIME message, returning a hash of any multipart errors
100
+ def parse_multipart(boundary, body)
101
+ reader = MultipartParser::Reader.new(boundary)
102
+ result = { errors: [], parts: [] }
103
+ def result.part(name)
104
+ hash = self[:parts].detect { |h| h[:part].name == name }
105
+ [hash[:part], hash[:body].join]
106
+ end
107
+
108
+ reader.on_part do |part|
109
+ result[:parts] << thispart = {
110
+ part: part,
111
+ body: []
112
+ }
113
+ part.on_data do |chunk|
114
+ thispart[:body] << chunk
115
+ end
116
+ end
117
+ reader.on_error do |msg|
118
+ result[:errors] << msg
119
+ end
120
+ reader.write(body)
121
+ result
122
+ end
123
+
124
+ def method_with_body?(method)
125
+ self.class.method_with_body?(method)
126
+ end
127
+
128
+ def big_string
129
+ kb = 1024
130
+ (32..126).map(&:chr).cycle.take(50 * kb).join
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'an adapter' do |**options|
4
+ before { skip } if options[:skip]
5
+
6
+ context 'with SSL enabled' do
7
+ before { ENV['SSL'] = 'yes' }
8
+ include_examples 'adapter examples', options
9
+ end
10
+
11
+ context 'with SSL disabled' do
12
+ before { ENV['SSL'] = 'no' }
13
+ include_examples 'adapter examples', options
14
+ end
15
+ end
16
+
17
+ shared_examples 'adapter examples' do |**options|
18
+ include Faraday::StreamingResponseChecker
19
+
20
+ let(:adapter) { described_class.name.split('::').last }
21
+
22
+ let(:conn_options) { { headers: { 'X-Faraday-Adapter' => adapter } }.merge(options[:conn_options] || {}) }
23
+
24
+ let(:adapter_options) do
25
+ return [] unless options[:adapter_options]
26
+
27
+ if options[:adapter_options].is_a?(Array)
28
+ options[:adapter_options]
29
+ else
30
+ [options[:adapter_options]]
31
+ end
32
+ end
33
+
34
+ let(:protocol) { ssl_mode? ? 'https' : 'http' }
35
+ let(:remote) { "#{protocol}://example.com" }
36
+ let(:stub_remote) { remote }
37
+
38
+ let(:conn) do
39
+ conn_options[:ssl] ||= {}
40
+ conn_options[:ssl][:ca_file] ||= ENV['SSL_FILE']
41
+
42
+ Faraday.new(remote, conn_options) do |conn|
43
+ conn.request :multipart
44
+ conn.request :url_encoded
45
+ conn.response :raise_error
46
+ conn.adapter described_class, *adapter_options
47
+ end
48
+ end
49
+
50
+ let!(:request_stub) { stub_request(http_method, stub_remote) }
51
+
52
+ after do
53
+ expect(request_stub).to have_been_requested unless request_stub.disabled?
54
+ end
55
+
56
+ describe '#delete' do
57
+ let(:http_method) { :delete }
58
+
59
+ it_behaves_like 'a request method', :delete
60
+ end
61
+
62
+ describe '#get' do
63
+ let(:http_method) { :get }
64
+
65
+ it_behaves_like 'a request method', :get
66
+ end
67
+
68
+ describe '#head' do
69
+ let(:http_method) { :head }
70
+
71
+ it_behaves_like 'a request method', :head
72
+ end
73
+
74
+ describe '#options' do
75
+ let(:http_method) { :options }
76
+
77
+ it_behaves_like 'a request method', :options
78
+ end
79
+
80
+ describe '#patch' do
81
+ let(:http_method) { :patch }
82
+
83
+ it_behaves_like 'a request method', :patch
84
+ end
85
+
86
+ describe '#post' do
87
+ let(:http_method) { :post }
88
+
89
+ it_behaves_like 'a request method', :post
90
+ end
91
+
92
+ describe '#put' do
93
+ let(:http_method) { :put }
94
+
95
+ it_behaves_like 'a request method', :put
96
+ end
97
+
98
+ on_feature :trace_method do
99
+ describe '#trace' do
100
+ let(:http_method) { :trace }
101
+
102
+ it_behaves_like 'a request method', :trace
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'a params encoder' do
4
+ it 'escapes safe buffer' do
5
+ monies = FakeSafeBuffer.new('$32,000.00')
6
+ expect(subject.encode('a' => monies)).to eq('a=%2432%2C000.00')
7
+ end
8
+
9
+ it 'raises type error for empty string' do
10
+ expect { subject.encode('') }.to raise_error(TypeError) do |error|
11
+ expect(error.message).to eq("Can't convert String into Hash.")
12
+ end
13
+ end
14
+
15
+ it 'encodes nil' do
16
+ expect(subject.encode('a' => nil)).to eq('a')
17
+ end
18
+ end