faraday 0.17.3 → 1.4.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 (135) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +156 -8
  3. data/LICENSE.md +1 -1
  4. data/README.md +16 -358
  5. data/Rakefile +1 -7
  6. data/examples/client_spec.rb +65 -0
  7. data/examples/client_test.rb +79 -0
  8. data/lib/faraday.rb +120 -189
  9. data/lib/faraday/adapter.rb +77 -22
  10. data/lib/faraday/adapter/em_http.rb +148 -102
  11. data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
  12. data/lib/faraday/adapter/em_synchrony.rb +110 -63
  13. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
  14. data/lib/faraday/adapter/httpclient.rb +83 -59
  15. data/lib/faraday/adapter/patron.rb +80 -43
  16. data/lib/faraday/adapter/rack.rb +30 -13
  17. data/lib/faraday/adapter/test.rb +86 -53
  18. data/lib/faraday/adapter/typhoeus.rb +4 -1
  19. data/lib/faraday/adapter_registry.rb +30 -0
  20. data/lib/faraday/autoload.rb +44 -36
  21. data/lib/faraday/connection.rb +313 -182
  22. data/lib/faraday/dependency_loader.rb +37 -0
  23. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  24. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  25. data/lib/faraday/error.rb +29 -35
  26. data/lib/faraday/file_part.rb +128 -0
  27. data/lib/faraday/logging/formatter.rb +105 -0
  28. data/lib/faraday/methods.rb +6 -0
  29. data/lib/faraday/middleware.rb +19 -25
  30. data/lib/faraday/middleware_registry.rb +129 -0
  31. data/lib/faraday/options.rb +36 -191
  32. data/lib/faraday/options/connection_options.rb +22 -0
  33. data/lib/faraday/options/env.rb +181 -0
  34. data/lib/faraday/options/proxy_options.rb +28 -0
  35. data/lib/faraday/options/request_options.rb +22 -0
  36. data/lib/faraday/options/ssl_options.rb +59 -0
  37. data/lib/faraday/param_part.rb +53 -0
  38. data/lib/faraday/parameters.rb +4 -197
  39. data/lib/faraday/rack_builder.rb +76 -64
  40. data/lib/faraday/request.rb +86 -44
  41. data/lib/faraday/request/authorization.rb +44 -30
  42. data/lib/faraday/request/basic_authentication.rb +14 -7
  43. data/lib/faraday/request/instrumentation.rb +45 -27
  44. data/lib/faraday/request/multipart.rb +86 -48
  45. data/lib/faraday/request/retry.rb +197 -171
  46. data/lib/faraday/request/token_authentication.rb +15 -10
  47. data/lib/faraday/request/url_encoded.rb +43 -23
  48. data/lib/faraday/response.rb +24 -20
  49. data/lib/faraday/response/logger.rb +22 -69
  50. data/lib/faraday/response/raise_error.rb +49 -18
  51. data/lib/faraday/utils.rb +38 -247
  52. data/lib/faraday/utils/headers.rb +139 -0
  53. data/lib/faraday/utils/params_hash.rb +61 -0
  54. data/lib/faraday/version.rb +5 -0
  55. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  56. data/spec/faraday/adapter/em_http_spec.rb +47 -0
  57. data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
  58. data/spec/faraday/adapter/excon_spec.rb +49 -0
  59. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  60. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  61. data/spec/faraday/adapter/patron_spec.rb +18 -0
  62. data/spec/faraday/adapter/rack_spec.rb +8 -0
  63. data/spec/faraday/adapter/test_spec.rb +260 -0
  64. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  65. data/spec/faraday/adapter_registry_spec.rb +28 -0
  66. data/spec/faraday/adapter_spec.rb +55 -0
  67. data/spec/faraday/composite_read_io_spec.rb +80 -0
  68. data/spec/faraday/connection_spec.rb +721 -0
  69. data/spec/faraday/error_spec.rb +12 -54
  70. data/spec/faraday/middleware_spec.rb +52 -0
  71. data/spec/faraday/options/env_spec.rb +70 -0
  72. data/spec/faraday/options/options_spec.rb +297 -0
  73. data/spec/faraday/options/proxy_options_spec.rb +37 -0
  74. data/spec/faraday/options/request_options_spec.rb +19 -0
  75. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  76. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  77. data/spec/faraday/rack_builder_spec.rb +345 -0
  78. data/spec/faraday/request/authorization_spec.rb +88 -0
  79. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  80. data/spec/faraday/request/multipart_spec.rb +302 -0
  81. data/spec/faraday/request/retry_spec.rb +242 -0
  82. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  83. data/spec/faraday/request_spec.rb +120 -0
  84. data/spec/faraday/response/logger_spec.rb +220 -0
  85. data/spec/faraday/response/middleware_spec.rb +68 -0
  86. data/spec/faraday/response/raise_error_spec.rb +78 -15
  87. data/spec/faraday/response_spec.rb +75 -0
  88. data/spec/faraday/utils/headers_spec.rb +82 -0
  89. data/spec/faraday/utils_spec.rb +56 -0
  90. data/spec/faraday_spec.rb +37 -0
  91. data/spec/spec_helper.rb +63 -36
  92. data/spec/support/disabling_stub.rb +14 -0
  93. data/spec/support/fake_safe_buffer.rb +15 -0
  94. data/spec/support/helper_methods.rb +133 -0
  95. data/spec/support/shared_examples/adapter.rb +105 -0
  96. data/spec/support/shared_examples/params_encoder.rb +18 -0
  97. data/spec/support/shared_examples/request_method.rb +262 -0
  98. data/spec/support/streaming_response_checker.rb +35 -0
  99. data/spec/support/webmock_rack_app.rb +68 -0
  100. metadata +124 -41
  101. data/lib/faraday/adapter/excon.rb +0 -82
  102. data/lib/faraday/adapter/net_http.rb +0 -152
  103. data/lib/faraday/adapter/net_http_persistent.rb +0 -68
  104. data/lib/faraday/deprecate.rb +0 -107
  105. data/lib/faraday/upload_io.rb +0 -67
  106. data/spec/faraday/deprecate_spec.rb +0 -69
  107. data/test/adapters/default_test.rb +0 -14
  108. data/test/adapters/em_http_test.rb +0 -30
  109. data/test/adapters/em_synchrony_test.rb +0 -32
  110. data/test/adapters/excon_test.rb +0 -30
  111. data/test/adapters/httpclient_test.rb +0 -34
  112. data/test/adapters/integration.rb +0 -263
  113. data/test/adapters/logger_test.rb +0 -136
  114. data/test/adapters/net_http_persistent_test.rb +0 -114
  115. data/test/adapters/net_http_test.rb +0 -79
  116. data/test/adapters/patron_test.rb +0 -40
  117. data/test/adapters/rack_test.rb +0 -38
  118. data/test/adapters/test_middleware_test.rb +0 -157
  119. data/test/adapters/typhoeus_test.rb +0 -38
  120. data/test/authentication_middleware_test.rb +0 -65
  121. data/test/composite_read_io_test.rb +0 -109
  122. data/test/connection_test.rb +0 -738
  123. data/test/env_test.rb +0 -268
  124. data/test/helper.rb +0 -75
  125. data/test/live_server.rb +0 -67
  126. data/test/middleware/instrumentation_test.rb +0 -88
  127. data/test/middleware/retry_test.rb +0 -282
  128. data/test/middleware_stack_test.rb +0 -260
  129. data/test/multibyte.txt +0 -1
  130. data/test/options_test.rb +0 -333
  131. data/test/parameters_test.rb +0 -157
  132. data/test/request_middleware_test.rb +0 -126
  133. data/test/response_middleware_test.rb +0 -72
  134. data/test/strawberry.rb +0 -2
  135. data/test/utils_test.rb +0 -98
@@ -1,68 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- class NetHttpPersistent < NetHttp
4
- dependency 'net/http/persistent'
5
-
6
- private
7
-
8
- def net_http_connection(env)
9
- @cached_connection ||=
10
- if Net::HTTP::Persistent.instance_method(:initialize).parameters.first == [:key, :name]
11
- options = {name: 'Faraday'}
12
- options[:pool_size] = @connection_options[:pool_size] if @connection_options.key?(:pool_size)
13
- Net::HTTP::Persistent.new(**options)
14
- else
15
- Net::HTTP::Persistent.new('Faraday')
16
- end
17
-
18
- proxy_uri = proxy_uri(env)
19
- @cached_connection.proxy = proxy_uri if @cached_connection.proxy_uri != proxy_uri
20
- @cached_connection
21
- end
22
-
23
- def proxy_uri(env)
24
- proxy_uri = nil
25
- if (proxy = env[:request][:proxy])
26
- proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
27
- proxy_uri.user = proxy_uri.password = nil
28
- # awful patch for net-http-persistent 2.8 not unescaping user/password
29
- (class << proxy_uri; self; end).class_eval do
30
- define_method(:user) { proxy[:user] }
31
- define_method(:password) { proxy[:password] }
32
- end if proxy[:user]
33
- end
34
- proxy_uri
35
- end
36
-
37
- def perform_request(http, env)
38
- http.request env[:url], create_request(env)
39
- rescue Errno::ETIMEDOUT => error
40
- raise Faraday::TimeoutError, error
41
- rescue Net::HTTP::Persistent::Error => error
42
- if error.message.include? 'Timeout'
43
- raise Faraday::TimeoutError, error
44
- elsif error.message.include? 'connection refused'
45
- raise Faraday::ConnectionFailed, error
46
- else
47
- raise
48
- end
49
- end
50
-
51
- def configure_ssl(http, ssl)
52
- http_set(http, :verify_mode, ssl_verify_mode(ssl))
53
- http_set(http, :cert_store, ssl_cert_store(ssl))
54
-
55
- http_set(http, :certificate, ssl[:client_cert]) if ssl[:client_cert]
56
- http_set(http, :private_key, ssl[:client_key]) if ssl[:client_key]
57
- http_set(http, :ca_file, ssl[:ca_file]) if ssl[:ca_file]
58
- http_set(http, :ssl_version, ssl[:version]) if ssl[:version]
59
- end
60
-
61
- def http_set(http, attr, value)
62
- if http.send(attr) != value
63
- http.send("#{attr}=", value)
64
- end
65
- end
66
- end
67
- end
68
- end
@@ -1,107 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- # @param new_klass [Class] new Klass to use
5
- #
6
- # @return [Class] A modified version of new_klass that warns on
7
- # usage about deprecation.
8
- # @see Faraday::Deprecate
9
- module DeprecatedClass
10
- def self.proxy_class(origclass, ver = '1.0')
11
- proxy = Class.new(origclass) do
12
- class << self
13
- extend Faraday::Deprecate
14
-
15
- def ===(other)
16
- other.is_a?(superclass) || super
17
- end
18
- end
19
- end
20
- proxy.singleton_class.send(:deprecate, :new, "#{origclass}.new", ver)
21
- proxy.singleton_class.send(:deprecate, :inherited, origclass.name, ver)
22
- proxy
23
- end
24
- end
25
-
26
- # Deprecation using semver instead of date, based on Gem::Deprecate
27
- # Provides a single method +deprecate+ to be used to declare when
28
- # something is going away.
29
- #
30
- # class Legacy
31
- # def self.klass_method
32
- # # ...
33
- # end
34
- #
35
- # def instance_method
36
- # # ...
37
- # end
38
- #
39
- # extend Faraday::Deprecate
40
- # deprecate :instance_method, "X.z", '1.0'
41
- #
42
- # class << self
43
- # extend Faraday::Deprecate
44
- # deprecate :klass_method, :none, '1.0'
45
- # end
46
- # end
47
- module Deprecate
48
- def self.skip # :nodoc:
49
- @skip ||= begin
50
- case ENV['FARADAY_DEPRECATE'].to_s.downcase
51
- when '1', 'warn' then :warn
52
- else :skip
53
- end
54
- end
55
- @skip == :skip
56
- end
57
-
58
- def self.skip=(value) # :nodoc:
59
- @skip = value ? :skip : :warn
60
- end
61
-
62
- # Temporarily turn off warnings. Intended for tests only.
63
- def skip_during
64
- original = Faraday::Deprecate.skip
65
- Faraday::Deprecate.skip, = true
66
- yield
67
- ensure
68
- Faraday::Deprecate.skip = original
69
- end
70
-
71
- # Simple deprecation method that deprecates +name+ by wrapping it up
72
- # in a dummy method. It warns on each call to the dummy method
73
- # telling the user of +repl+ (unless +repl+ is :none) and the
74
- # semver that it is planned to go away.
75
- # @param name [Symbol] the method symbol to deprecate
76
- # @param repl [#to_s, :none] the replacement to use, when `:none` it will
77
- # alert the user that no replacemtent is present.
78
- # @param ver [String] the semver the method will be removed.
79
- def deprecate(name, repl, ver)
80
- class_eval do
81
- gem_ver = Gem::Version.new(ver)
82
- old = "_deprecated_#{name}"
83
- alias_method old, name
84
- define_method name do |*args, &block|
85
- mod = is_a? Module
86
- target = mod ? "#{self}." : "#{self.class}#"
87
- target_message = if name == :inherited
88
- "Inheriting #{self}"
89
- else
90
- "#{target}#{name}"
91
- end
92
-
93
- msg = [
94
- "NOTE: #{target_message} is deprecated",
95
- repl == :none ? ' with no replacement' : "; use #{repl} instead. ",
96
- "It will be removed in or after version #{gem_ver}",
97
- "\n#{target}#{name} called from #{Gem.location_of_caller.join(':')}"
98
- ]
99
- warn "#{msg.join}." unless Faraday::Deprecate.skip
100
- send old, *args, &block
101
- end
102
- end
103
- end
104
-
105
- module_function :deprecate, :skip_during
106
- end
107
- end
@@ -1,67 +0,0 @@
1
- begin
2
- require 'composite_io'
3
- require 'parts'
4
- require 'stringio'
5
- rescue LoadError
6
- $stderr.puts "Install the multipart-post gem."
7
- raise
8
- end
9
-
10
- module Faraday
11
- # Similar but not compatible with ::CompositeReadIO provided by multipart-post.
12
- class CompositeReadIO
13
- def initialize(*parts)
14
- @parts = parts.flatten
15
- @ios = @parts.map { |part| part.to_io }
16
- @index = 0
17
- end
18
-
19
- def length
20
- @parts.inject(0) { |sum, part| sum + part.length }
21
- end
22
-
23
- def rewind
24
- @ios.each { |io| io.rewind }
25
- @index = 0
26
- end
27
-
28
- # Read from IOs in order until `length` bytes have been received.
29
- def read(length = nil, outbuf = nil)
30
- got_result = false
31
- outbuf = outbuf ? outbuf.replace("") : ""
32
-
33
- while io = current_io
34
- if result = io.read(length)
35
- got_result ||= !result.nil?
36
- result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
37
- outbuf << result
38
- length -= result.length if length
39
- break if length == 0
40
- end
41
- advance_io
42
- end
43
- (!got_result && length) ? nil : outbuf
44
- end
45
-
46
- def close
47
- @ios.each { |io| io.close }
48
- end
49
-
50
- def ensure_open_and_readable
51
- # Rubinius compatibility
52
- end
53
-
54
- private
55
-
56
- def current_io
57
- @ios[@index]
58
- end
59
-
60
- def advance_io
61
- @index += 1
62
- end
63
- end
64
-
65
- UploadIO = ::UploadIO
66
- Parts = ::Parts
67
- end
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Faraday::DeprecatedClass do
4
- class SampleClass < StandardError
5
- attr_accessor :foo
6
-
7
- def initialize(foo = nil)
8
- @foo = foo || :foo
9
- end
10
- end
11
-
12
- SampleDeprecatedClass = Faraday::DeprecatedClass.proxy_class(SampleClass)
13
-
14
- it 'does not raise error for deprecated classes but prints an error message' do
15
- error_message, foobar = with_warn_squelching { SampleDeprecatedClass.new(:foo_bar) }
16
- expect(foobar).to be_a(SampleClass)
17
- expect(foobar.foo).to eq(:foo_bar)
18
- expect(error_message).to match(
19
- Regexp.new(
20
- 'NOTE: SampleDeprecatedClass.new is deprecated; '\
21
- 'use SampleClass.new instead. It will be removed in or after version 1.0'
22
- )
23
- )
24
- end
25
-
26
- it 'does not raise an error for inherited error-namespaced classes but prints an error message' do
27
- error_message, = with_warn_squelching { Class.new(SampleDeprecatedClass) }
28
-
29
- expect(error_message).to match(
30
- Regexp.new(
31
- 'NOTE: Inheriting SampleDeprecatedClass is deprecated; '\
32
- 'use SampleClass instead. It will be removed in or after version 1.0'
33
- )
34
- )
35
- end
36
-
37
- it 'allows backward-compatible class to be subclassed' do
38
- expect {
39
- with_warn_squelching { Class.new(SampleDeprecatedClass) }
40
- }.not_to raise_error
41
- end
42
-
43
- it 'allows rescuing of a current error with a deprecated error' do
44
- expect { raise SampleClass, nil }.to raise_error(SampleDeprecatedClass)
45
- end
46
-
47
- it 'allows rescuing of a current error with a current error' do
48
- expect { raise SampleClass, nil }.to raise_error(SampleClass)
49
- end
50
-
51
- it 'allows rescuing of a deprecated error with a deprecated error' do
52
- expect { raise SampleDeprecatedClass, nil }.to raise_error(SampleDeprecatedClass)
53
- end
54
-
55
- it 'allows rescuing of a deprecated error with a current error' do
56
- expect { raise SampleDeprecatedClass, nil }.to raise_error(SampleClass)
57
- end
58
-
59
-
60
- def with_warn_squelching
61
- stderr_catcher = StringIO.new
62
- original_stderr = $stderr
63
- $stderr = stderr_catcher
64
- result = yield if block_given?
65
- [stderr_catcher.tap(&:rewind).string, result]
66
- ensure
67
- $stderr = original_stderr
68
- end
69
- end
@@ -1,14 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class DefaultTest < Faraday::TestCase
5
-
6
- def adapter() :default end
7
-
8
- Integration.apply(self, :NonParallel) do
9
- # default stack is not configured with Multipart
10
- undef :test_POST_sends_files
11
- end
12
-
13
- end
14
- end
@@ -1,30 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class EMHttpTest < Faraday::TestCase
5
-
6
- def adapter() :em_http end
7
-
8
- Integration.apply(self, :Parallel) do
9
- # https://github.com/eventmachine/eventmachine/pull/289
10
- undef :test_timeout
11
-
12
- def test_binds_local_socket
13
- host = '1.2.3.4'
14
- conn = create_connection :request => { :bind => { :host => host } }
15
- assert_equal host, conn.options[:bind][:host]
16
- end
17
- end unless jruby? and ssl_mode?
18
- # https://github.com/eventmachine/eventmachine/issues/180
19
-
20
- def test_custom_adapter_config
21
- url = URI('https://example.com:1234')
22
-
23
- adapter = Faraday::Adapter::EMHttp.new nil, inactivity_timeout: 20
24
-
25
- req = adapter.create_request(url: url, request: {})
26
-
27
- assert_equal 20, req.connopts.inactivity_timeout
28
- end
29
- end
30
- end
@@ -1,32 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class EMSynchronyTest < Faraday::TestCase
5
-
6
- def adapter() :em_synchrony end
7
-
8
- unless jruby?
9
- Integration.apply(self, :Parallel) do
10
- # https://github.com/eventmachine/eventmachine/pull/289
11
- undef :test_timeout
12
-
13
- def test_binds_local_socket
14
- host = '1.2.3.4'
15
- conn = create_connection :request => { :bind => { :host => host } }
16
- #put conn.get('/who-am-i').body
17
- assert_equal host, conn.options[:bind][:host]
18
- end
19
- end
20
- end
21
-
22
- def test_custom_adapter_config
23
- url = URI('https://example.com:1234')
24
-
25
- adapter = Faraday::Adapter::EMSynchrony.new nil, inactivity_timeout: 20
26
-
27
- req = adapter.create_request(url: url, request: {})
28
-
29
- assert_equal 20, req.connopts.inactivity_timeout
30
- end
31
- end
32
- end
@@ -1,30 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class ExconTest < Faraday::TestCase
5
-
6
- def adapter() :excon end
7
-
8
- Integration.apply(self, :NonParallel) do
9
- # https://github.com/geemus/excon/issues/126 ?
10
- undef :test_timeout if ssl_mode?
11
-
12
- # Excon lets OpenSSL::SSL::SSLError be raised without any way to
13
- # distinguish whether it happened because of a 407 proxy response
14
- undef :test_proxy_auth_fail if ssl_mode?
15
-
16
- # https://github.com/geemus/excon/issues/358
17
- undef :test_connection_error if RUBY_VERSION >= '2.1.0'
18
- end
19
-
20
- def test_custom_adapter_config
21
- url = URI('https://example.com:1234')
22
-
23
- adapter = Faraday::Adapter::Excon.new nil, debug_request: true
24
-
25
- conn = adapter.create_connection({url: url}, {})
26
-
27
- assert_equal true, conn.data[:debug_request]
28
- end
29
- end
30
- end
@@ -1,34 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class HttpclientTest < Faraday::TestCase
5
-
6
- def adapter() :httpclient end
7
-
8
- Integration.apply(self, :NonParallel, :Compression) do
9
- def setup
10
- require 'httpclient' unless defined?(HTTPClient)
11
- HTTPClient::NO_PROXY_HOSTS.delete('localhost')
12
- end
13
-
14
- def test_binds_local_socket
15
- host = '1.2.3.4'
16
- conn = create_connection :request => { :bind => { :host => host } }
17
- assert_equal host, conn.options[:bind][:host]
18
- end
19
-
20
- def test_custom_adapter_config
21
- adapter = Faraday::Adapter::HTTPClient.new do |client|
22
- client.keep_alive_timeout = 20
23
- client.ssl_config.timeout = 25
24
- end
25
-
26
- client = adapter.client
27
- adapter.configure_client
28
-
29
- assert_equal 20, client.keep_alive_timeout
30
- assert_equal 25, client.ssl_config.timeout
31
- end
32
- end
33
- end
34
- end