faraday 0.17.4 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
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 +97 -0
  7. data/examples/client_test.rb +118 -0
  8. data/lib/faraday/adapter/test.rb +118 -69
  9. data/lib/faraday/adapter/typhoeus.rb +4 -1
  10. data/lib/faraday/adapter.rb +72 -22
  11. data/lib/faraday/adapter_registry.rb +30 -0
  12. data/lib/faraday/autoload.rb +39 -36
  13. data/lib/faraday/connection.rb +343 -185
  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 +29 -35
  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 +36 -191
  29. data/lib/faraday/param_part.rb +53 -0
  30. data/lib/faraday/parameters.rb +4 -197
  31. data/lib/faraday/rack_builder.rb +76 -64
  32. data/lib/faraday/request/authorization.rb +51 -30
  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 +86 -48
  36. data/lib/faraday/request/retry.rb +197 -171
  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 +86 -44
  40. data/lib/faraday/response/logger.rb +22 -69
  41. data/lib/faraday/response/raise_error.rb +49 -18
  42. data/lib/faraday/response.rb +24 -20
  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 -247
  46. data/lib/faraday/version.rb +5 -0
  47. data/lib/faraday.rb +127 -189
  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 +12 -54
  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 +78 -15
  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 +63 -36
  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 +194 -48
  94. data/lib/faraday/adapter/em_http.rb +0 -243
  95. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  96. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  97. data/lib/faraday/adapter/em_synchrony.rb +0 -106
  98. data/lib/faraday/adapter/excon.rb +0 -82
  99. data/lib/faraday/adapter/httpclient.rb +0 -128
  100. data/lib/faraday/adapter/net_http.rb +0 -153
  101. data/lib/faraday/adapter/net_http_persistent.rb +0 -68
  102. data/lib/faraday/adapter/patron.rb +0 -95
  103. data/lib/faraday/adapter/rack.rb +0 -58
  104. data/lib/faraday/deprecate.rb +0 -109
  105. data/lib/faraday/upload_io.rb +0 -67
  106. data/spec/faraday/deprecate_spec.rb +0 -147
  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,153 +0,0 @@
1
- begin
2
- require 'net/https'
3
- rescue LoadError
4
- warn "Warning: no such file to load -- net/https. Make sure openssl is installed if you want ssl support"
5
- require 'net/http'
6
- end
7
- require 'zlib'
8
-
9
- module Faraday
10
- class Adapter
11
- class NetHttp < Faraday::Adapter
12
- NET_HTTP_EXCEPTIONS = [
13
- IOError,
14
- Errno::EADDRNOTAVAIL,
15
- Errno::ECONNABORTED,
16
- Errno::ECONNREFUSED,
17
- Errno::ECONNRESET,
18
- Errno::EHOSTUNREACH,
19
- Errno::EINVAL,
20
- Errno::ENETUNREACH,
21
- Errno::EPIPE,
22
- Net::HTTPBadResponse,
23
- Net::HTTPHeaderSyntaxError,
24
- Net::ProtocolError,
25
- SocketError,
26
- Zlib::GzipFile::Error,
27
- ]
28
-
29
- NET_HTTP_EXCEPTIONS << OpenSSL::SSL::SSLError if defined?(OpenSSL)
30
- NET_HTTP_EXCEPTIONS << Net::OpenTimeout if defined?(Net::OpenTimeout)
31
-
32
- def initialize(app = nil, opts = {}, &block)
33
- @cert_store = nil
34
- super(app, opts, &block)
35
- end
36
-
37
- def call(env)
38
- super
39
- with_net_http_connection(env) do |http|
40
- configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
41
- configure_request(http, env[:request])
42
-
43
- begin
44
- http_response = perform_request(http, env)
45
- rescue *NET_HTTP_EXCEPTIONS => err
46
- if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
47
- raise Faraday::SSLError, err
48
- else
49
- raise Faraday::ConnectionFailed, err
50
- end
51
- end
52
-
53
- save_response(env, http_response.code.to_i, http_response.body || '', nil, http_response.message) do |response_headers|
54
- http_response.each_header do |key, value|
55
- response_headers[key] = value
56
- end
57
- end
58
- end
59
-
60
- @app.call env
61
- rescue Timeout::Error, Errno::ETIMEDOUT => err
62
- raise Faraday::TimeoutError, err
63
- end
64
-
65
- private
66
-
67
- def create_request(env)
68
- request = Net::HTTPGenericRequest.new \
69
- env[:method].to_s.upcase, # request method
70
- !!env[:body], # is there request body
71
- :head != env[:method], # is there response body
72
- env[:url].request_uri, # request uri path
73
- env[:request_headers] # request headers
74
-
75
- if env[:body].respond_to?(:read)
76
- request.body_stream = env[:body]
77
- else
78
- request.body = env[:body]
79
- end
80
- request
81
- end
82
-
83
- def perform_request(http, env)
84
- if :get == env[:method] and !env[:body]
85
- # prefer `get` to `request` because the former handles gzip (ruby 1.9)
86
- http.get env[:url].request_uri, env[:request_headers]
87
- else
88
- http.request create_request(env)
89
- end
90
- end
91
-
92
- def with_net_http_connection(env)
93
- yield net_http_connection(env)
94
- end
95
-
96
- def net_http_connection(env)
97
- if proxy = env[:request][:proxy]
98
- Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port, proxy[:user], proxy[:password])
99
- else
100
- Net::HTTP
101
- end.new(env[:url].hostname, env[:url].port || (env[:url].scheme == 'https' ? 443 : 80))
102
- end
103
-
104
- def configure_ssl(http, ssl)
105
- http.use_ssl = true
106
- http.verify_mode = ssl_verify_mode(ssl)
107
- http.cert_store = ssl_cert_store(ssl)
108
-
109
- http.cert = ssl[:client_cert] if ssl[:client_cert]
110
- http.key = ssl[:client_key] if ssl[:client_key]
111
- http.ca_file = ssl[:ca_file] if ssl[:ca_file]
112
- http.ca_path = ssl[:ca_path] if ssl[:ca_path]
113
- http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
114
- http.ssl_version = ssl[:version] if ssl[:version]
115
- http.min_version = ssl[:min_version] if ssl[:min_version]
116
- http.max_version = ssl[:max_version] if ssl[:max_version]
117
- end
118
-
119
- def configure_request(http, req)
120
- if req[:timeout]
121
- http.read_timeout = req[:timeout]
122
- http.open_timeout = req[:timeout]
123
- http.write_timeout = req[:timeout] if http.respond_to?(:write_timeout=)
124
- end
125
- http.open_timeout = req[:open_timeout] if req[:open_timeout]
126
- http.write_timeout = req[:write_timeout] if req[:write_timeout] && http.respond_to?(:write_timeout=)
127
- # Only set if Net::Http supports it, since Ruby 2.5.
128
- http.max_retries = 0 if http.respond_to?(:max_retries=)
129
-
130
- @config_block.call(http) if @config_block
131
- end
132
-
133
- def ssl_cert_store(ssl)
134
- return ssl[:cert_store] if ssl[:cert_store]
135
- return @cert_store if @cert_store
136
- # Use the default cert store by default, i.e. system ca certs
137
- @cert_store = OpenSSL::X509::Store.new
138
- @cert_store.set_default_paths
139
- @cert_store
140
- end
141
-
142
- def ssl_verify_mode(ssl)
143
- ssl[:verify_mode] || begin
144
- if ssl.fetch(:verify, true)
145
- OpenSSL::SSL::VERIFY_PEER
146
- else
147
- OpenSSL::SSL::VERIFY_NONE
148
- end
149
- end
150
- end
151
- end
152
- end
153
- end
@@ -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,95 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- class Patron < Faraday::Adapter
4
- dependency 'patron'
5
-
6
- def call(env)
7
- super
8
- # TODO: support streaming requests
9
- env[:body] = env[:body].read if env[:body].respond_to? :read
10
-
11
- session = ::Patron::Session.new
12
- @config_block.call(session) if @config_block
13
- configure_ssl(session, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
14
-
15
- if req = env[:request]
16
- session.timeout = session.connect_timeout = req[:timeout] if req[:timeout]
17
- session.connect_timeout = req[:open_timeout] if req[:open_timeout]
18
-
19
- if proxy = req[:proxy]
20
- proxy_uri = proxy[:uri].dup
21
- proxy_uri.user = proxy[:user] && Utils.escape(proxy[:user]).gsub('+', '%20')
22
- proxy_uri.password = proxy[:password] && Utils.escape(proxy[:password]).gsub('+', '%20')
23
- session.proxy = proxy_uri.to_s
24
- end
25
- end
26
-
27
- response = begin
28
- data = env[:body] ? env[:body].to_s : nil
29
- session.request(env[:method], env[:url].to_s, env[:request_headers], :data => data)
30
- rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
31
- raise Faraday::ConnectionFailed, $!
32
- end
33
-
34
- # Remove the "HTTP/1.1 200", leaving just the reason phrase
35
- reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')
36
-
37
- save_response(env, response.status, response.body, response.headers, reason_phrase)
38
-
39
- @app.call env
40
- rescue ::Patron::TimeoutError => err
41
- if connection_timed_out_message?(err.message)
42
- raise Faraday::ConnectionFailed, err
43
- else
44
- raise Faraday::TimeoutError, err
45
- end
46
- rescue ::Patron::Error => err
47
- if err.message.include?("code 407")
48
- raise Faraday::ConnectionFailed, %{407 "Proxy Authentication Required "}
49
- else
50
- raise Faraday::ConnectionFailed, err
51
- end
52
- end
53
-
54
- if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
55
- # HAX: helps but doesn't work completely
56
- # https://github.com/toland/patron/issues/34
57
- ::Patron::Request::VALID_ACTIONS.tap do |actions|
58
- if actions[0].is_a?(Symbol)
59
- actions << :patch unless actions.include? :patch
60
- actions << :options unless actions.include? :options
61
- else
62
- # Patron 0.4.20 and up
63
- actions << "PATCH" unless actions.include? "PATCH"
64
- actions << "OPTIONS" unless actions.include? "OPTIONS"
65
- end
66
- end
67
- end
68
-
69
- def configure_ssl(session, ssl)
70
- if ssl.fetch(:verify, true)
71
- session.cacert = ssl[:ca_file]
72
- else
73
- session.insecure = true
74
- end
75
- end
76
-
77
- private
78
-
79
- CURL_TIMEOUT_MESSAGES = [ "Connection time-out",
80
- "Connection timed out",
81
- "Timed out before name resolve",
82
- "server connect has timed out",
83
- "Resolving timed out",
84
- "name lookup timed out",
85
- "timed out before SSL",
86
- "connect() timed out"
87
- ].freeze
88
-
89
- def connection_timed_out_message?(message)
90
- CURL_TIMEOUT_MESSAGES.any? { |curl_message| message.include?(curl_message) }
91
- end
92
-
93
- end
94
- end
95
- end
@@ -1,58 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- # Sends requests to a Rack app.
4
- #
5
- # Examples
6
- #
7
- # class MyRackApp
8
- # def call(env)
9
- # [200, {'Content-Type' => 'text/html'}, ["hello world"]]
10
- # end
11
- # end
12
- #
13
- # Faraday.new do |conn|
14
- # conn.adapter :rack, MyRackApp.new
15
- # end
16
- class Rack < Faraday::Adapter
17
- dependency 'rack/test'
18
-
19
- # not prefixed with "HTTP_"
20
- SPECIAL_HEADERS = %w[ CONTENT_LENGTH CONTENT_TYPE ]
21
-
22
- def initialize(faraday_app, rack_app)
23
- super(faraday_app)
24
- mock_session = ::Rack::MockSession.new(rack_app)
25
- @session = ::Rack::Test::Session.new(mock_session)
26
- end
27
-
28
- def call(env)
29
- super
30
- rack_env = {
31
- :method => env[:method],
32
- :input => env[:body].respond_to?(:read) ? env[:body].read : env[:body],
33
- 'rack.url_scheme' => env[:url].scheme
34
- }
35
-
36
- env[:request_headers].each do |name, value|
37
- name = name.upcase.tr('-', '_')
38
- name = "HTTP_#{name}" unless SPECIAL_HEADERS.include? name
39
- rack_env[name] = value
40
- end if env[:request_headers]
41
-
42
- timeout = env[:request][:timeout] || env[:request][:open_timeout]
43
- response = if timeout
44
- Timer.timeout(timeout, Faraday::TimeoutError) { execute_request(env, rack_env) }
45
- else
46
- execute_request(env, rack_env)
47
- end
48
-
49
- save_response(env, response.status, response.body, response.headers)
50
- @app.call env
51
- end
52
-
53
- def execute_request(env, rack_env)
54
- @session.request(env[:url].to_s, rack_env)
55
- end
56
- end
57
- end
58
- end
@@ -1,109 +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
- const_set("ORIG_CLASS", origclass)
13
-
14
- class << self
15
- extend Faraday::Deprecate
16
-
17
- def ===(other)
18
- (superclass == const_get("ORIG_CLASS") && other.is_a?(superclass)) || super
19
- end
20
- end
21
- end
22
- proxy.singleton_class.send(:deprecate, :new, "#{origclass}.new", ver)
23
- proxy.singleton_class.send(:deprecate, :inherited, origclass.name, ver)
24
- proxy
25
- end
26
- end
27
-
28
- # Deprecation using semver instead of date, based on Gem::Deprecate
29
- # Provides a single method +deprecate+ to be used to declare when
30
- # something is going away.
31
- #
32
- # class Legacy
33
- # def self.klass_method
34
- # # ...
35
- # end
36
- #
37
- # def instance_method
38
- # # ...
39
- # end
40
- #
41
- # extend Faraday::Deprecate
42
- # deprecate :instance_method, "X.z", '1.0'
43
- #
44
- # class << self
45
- # extend Faraday::Deprecate
46
- # deprecate :klass_method, :none, '1.0'
47
- # end
48
- # end
49
- module Deprecate
50
- def self.skip # :nodoc:
51
- @skip ||= begin
52
- case ENV['FARADAY_DEPRECATE'].to_s.downcase
53
- when '1', 'warn' then :warn
54
- else :skip
55
- end
56
- end
57
- @skip == :skip
58
- end
59
-
60
- def self.skip=(value) # :nodoc:
61
- @skip = value ? :skip : :warn
62
- end
63
-
64
- # Temporarily turn off warnings. Intended for tests only.
65
- def skip_during
66
- original = Faraday::Deprecate.skip
67
- Faraday::Deprecate.skip, = true
68
- yield
69
- ensure
70
- Faraday::Deprecate.skip = original
71
- end
72
-
73
- # Simple deprecation method that deprecates +name+ by wrapping it up
74
- # in a dummy method. It warns on each call to the dummy method
75
- # telling the user of +repl+ (unless +repl+ is :none) and the
76
- # semver that it is planned to go away.
77
- # @param name [Symbol] the method symbol to deprecate
78
- # @param repl [#to_s, :none] the replacement to use, when `:none` it will
79
- # alert the user that no replacemtent is present.
80
- # @param ver [String] the semver the method will be removed.
81
- def deprecate(name, repl, ver)
82
- class_eval do
83
- gem_ver = Gem::Version.new(ver)
84
- old = "_deprecated_#{name}"
85
- alias_method old, name
86
- define_method name do |*args, &block|
87
- mod = is_a? Module
88
- target = mod ? "#{self}." : "#{self.class}#"
89
- target_message = if name == :inherited
90
- "Inheriting #{self}"
91
- else
92
- "#{target}#{name}"
93
- end
94
-
95
- msg = [
96
- "NOTE: #{target_message} is deprecated",
97
- repl == :none ? ' with no replacement' : "; use #{repl} instead. ",
98
- "It will be removed in or after version #{gem_ver}",
99
- "\n#{target}#{name} called from #{Gem.location_of_caller.join(':')}"
100
- ]
101
- warn "#{msg.join}." unless Faraday::Deprecate.skip
102
- send old, *args, &block
103
- end
104
- end
105
- end
106
-
107
- module_function :deprecate, :skip_during
108
- end
109
- 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,147 +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
- describe 'match behavior' do
60
- class SampleDeprecatedClassA < SampleDeprecatedClass; end
61
- class SampleDeprecatedClassB < SampleDeprecatedClass; end
62
-
63
- class SampleDeprecatedClassAX < SampleDeprecatedClassA; end
64
-
65
- class SampleClassA < SampleClass; end
66
-
67
- describe 'undeprecated class' do
68
- it 'is === to instance of deprecated class' do
69
- expect(SampleClass === SampleDeprecatedClass.new).to be true
70
- end
71
-
72
- it 'is === to instance of subclass of deprecated class' do
73
- expect(SampleClass === SampleDeprecatedClassA.new).to be true
74
- end
75
-
76
- it 'is === to instance of subclass of subclass of deprecated class' do
77
- expect(SampleClass === SampleDeprecatedClassAX.new).to be true
78
- end
79
- end
80
-
81
- describe 'subclass of undeprecated class' do
82
- it 'is not === to instance of undeprecated class' do
83
- expect(SampleClassA === SampleClass.new).to be false
84
- end
85
-
86
- it 'is not === to instance of deprecated class' do
87
- expect(SampleClassA === SampleDeprecatedClass.new).to be false
88
- end
89
- end
90
-
91
- describe 'deprecated class' do
92
- it 'is === to instance of undeprecated class' do
93
- expect(SampleDeprecatedClass === SampleClass.new).to be true
94
- end
95
-
96
- it 'is === to instance of subclass of undeprecated class' do
97
- expect(SampleDeprecatedClass === SampleClassA.new).to be true
98
- end
99
-
100
- it 'is === to instance of subclass of deprecated class' do
101
- expect(SampleDeprecatedClass === SampleDeprecatedClassA.new).to be true
102
- end
103
-
104
- it 'is === to instance of subclass of subclass of deprecated class' do
105
- expect(SampleDeprecatedClass === SampleDeprecatedClassAX.new).to be true
106
- end
107
- end
108
-
109
- describe 'subclass of deprecated class' do
110
- it 'is not === to instance of subclass of undeprecated class' do
111
- expect(SampleDeprecatedClassA === SampleClass.new).to be false
112
- end
113
-
114
- it 'is not === to instance of another subclass of deprecated class' do
115
- expect(SampleDeprecatedClassA === SampleDeprecatedClassB.new).to be false
116
- end
117
-
118
- it 'is === to instance of its subclass' do
119
- expect(SampleDeprecatedClassA === SampleDeprecatedClassAX.new).to be true
120
- end
121
-
122
- it 'is === to instance of deprecated class' do
123
- expect(SampleDeprecatedClass === SampleDeprecatedClassB.new).to be true
124
- end
125
- end
126
-
127
- describe 'subclass of subclass of deprecated class' do
128
- it 'is not === to instance of subclass of another subclass of deprecated class' do
129
- expect(SampleDeprecatedClassAX === SampleDeprecatedClassB.new).to be false
130
- end
131
-
132
- it 'is not === to instance of its superclass' do
133
- expect(SampleDeprecatedClassA === SampleDeprecatedClass.new).to be false
134
- end
135
- end
136
- end
137
-
138
- def with_warn_squelching
139
- stderr_catcher = StringIO.new
140
- original_stderr = $stderr
141
- $stderr = stderr_catcher
142
- result = yield if block_given?
143
- [stderr_catcher.tap(&:rewind).string, result]
144
- ensure
145
- $stderr = original_stderr
146
- end
147
- end