faraday 1.4.2 → 1.6.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b0077d079fbfe478a57bbc4f804b9e8761addcad6266587434f99534d9ca935
4
- data.tar.gz: 465fc3bb1ad67259f4e6515ff3ea006c833d4dca43946cb79da683311eccf4e7
3
+ metadata.gz: 1c84d273b85bc4643cccee381fc7a1190305143d5dc89447fe5ac41e1db7a89e
4
+ data.tar.gz: 6d9409101febc5167e3c98f33c4b5b15f0dcc68c4e3fc13198930b76dd83a39e
5
5
  SHA512:
6
- metadata.gz: 63deab2286691410961536ed63f36a5d23917227327e2ccfbc919621ba86dae8e397e8fbe3b29826a309e988a8924a8943583ac42b515aa07dd6dccacdc323a2
7
- data.tar.gz: 5827629437871707807bcfc405f02bc6a9a19fc99cc655bb44de37fbc4aaa5c277dd29895153cb506e0718d841229c6086ef00314c262ebda7e2ac68b5f90ce6
6
+ metadata.gz: a8e6280590ce1eb1b41311351e8e13b4de8f6571523f383d1651d63589db040a07eb0d0110a2d441f3fa275468b6eca6767924b8948c2e0f6cbfb2e1ef20b39a
7
+ data.tar.gz: b9df5e99b07e33d19b8d2fca7ba5357e38a95fa662c960c34876f3fe43348ffe15b5fd95abc90ee0f811885dcddaac3d7f28a4f09046f6450c5202153af4f8c0
data/lib/faraday.rb CHANGED
@@ -27,11 +27,16 @@ require 'faraday/error'
27
27
  require 'faraday/file_part'
28
28
  require 'faraday/param_part'
29
29
 
30
- require 'faraday/em_http'
31
- require 'faraday/em_synchrony'
30
+ unless defined?(JRUBY_VERSION)
31
+ require 'faraday/em_http'
32
+ require 'faraday/em_synchrony'
33
+ end
32
34
  require 'faraday/excon'
35
+ require 'faraday/httpclient'
33
36
  require 'faraday/net_http'
34
37
  require 'faraday/net_http_persistent'
38
+ require 'faraday/patron'
39
+ require 'faraday/rack'
35
40
 
36
41
  # This is the main namespace for Faraday.
37
42
  #
@@ -11,10 +11,7 @@ module Faraday
11
11
 
12
12
  register_middleware File.expand_path('adapter', __dir__),
13
13
  test: [:Test, 'test'],
14
- typhoeus: [:Typhoeus, 'typhoeus'],
15
- patron: [:Patron, 'patron'],
16
- rack: [:Rack, 'rack'],
17
- httpclient: [:HTTPClient, 'httpclient']
14
+ typhoeus: [:Typhoeus, 'typhoeus']
18
15
 
19
16
  # This module marks an Adapter as supporting parallel requests.
20
17
  module Parallelism
@@ -59,10 +59,7 @@ module Faraday
59
59
  extend AutoloadHelper
60
60
  autoload_all 'faraday/adapter',
61
61
  Typhoeus: 'typhoeus',
62
- Patron: 'patron',
63
- Test: 'test',
64
- Rack: 'rack',
65
- HTTPClient: 'httpclient'
62
+ Test: 'test'
66
63
  end
67
64
 
68
65
  # Request represents a single HTTP request for a Faraday adapter to make.
@@ -26,7 +26,7 @@ module Faraday
26
26
  # Connection. This includes a default host name, scheme, port, and path.
27
27
  attr_reader :url_prefix
28
28
 
29
- # @return [Faraday::Builder] Builder for this Connection.
29
+ # @return [Faraday::RackBuilder] Builder for this Connection.
30
30
  attr_reader :builder
31
31
 
32
32
  # @return [Hash] SSL options.
@@ -73,6 +73,7 @@ module Faraday
73
73
  @options = options.request
74
74
  @ssl = options.ssl
75
75
  @default_parallel_manager = options.parallel_manager
76
+ @manual_proxy = nil
76
77
 
77
78
  @builder = options.builder || begin
78
79
  # pass an empty block to Builder so it doesn't assume default middleware
@@ -519,9 +520,8 @@ module Faraday
519
520
  # @return [URI]
520
521
  def build_exclusive_url(url = nil, params = nil, params_encoder = nil)
521
522
  url = nil if url.respond_to?(:empty?) && url.empty?
522
- base = url_prefix
523
+ base = url_prefix.dup
523
524
  if url && base.path && base.path !~ %r{/$}
524
- base = base.dup
525
525
  base.path = "#{base.path}/" # ensure trailing slash
526
526
  end
527
527
  url = url && URI.parse(url.to_s).opaque ? url.to_s.gsub(':', '%3A') : url
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- VERSION = '1.4.2'
4
+ VERSION = '1.6.0'
5
5
  end
@@ -1,47 +1,49 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe Faraday::Adapter::EMHttp, unless: defined?(JRUBY_VERSION) do
4
- features :request_body_on_query_methods, :reason_phrase_parse, :trace_method,
5
- :skip_response_body_on_head, :parallel, :local_socket_binding
3
+ unless defined?(JRUBY_VERSION)
4
+ RSpec.describe Faraday::Adapter::EMHttp do
5
+ features :request_body_on_query_methods, :reason_phrase_parse, :trace_method,
6
+ :skip_response_body_on_head, :parallel, :local_socket_binding
6
7
 
7
- it_behaves_like 'an adapter'
8
+ it_behaves_like 'an adapter'
8
9
 
9
- it 'allows to provide adapter specific configs' do
10
- url = URI('https://example.com:1234')
11
- adapter = described_class.new nil, inactivity_timeout: 20
12
- req = adapter.create_request(url: url, request: {})
10
+ it 'allows to provide adapter specific configs' do
11
+ url = URI('https://example.com:1234')
12
+ adapter = described_class.new nil, inactivity_timeout: 20
13
+ req = adapter.create_request(url: url, request: {})
13
14
 
14
- expect(req.connopts.inactivity_timeout).to eq(20)
15
- end
16
-
17
- context 'Options' do
18
- let(:request) { Faraday::RequestOptions.new }
19
- let(:env) { { request: request } }
20
- let(:options) { {} }
21
- let(:adapter) { Faraday::Adapter::EMHttp.new }
22
-
23
- it 'configures timeout' do
24
- request.timeout = 5
25
- adapter.configure_timeout(options, env)
26
- expect(options[:inactivity_timeout]).to eq(5)
27
- expect(options[:connect_timeout]).to eq(5)
28
- end
29
-
30
- it 'configures timeout and open_timeout' do
31
- request.timeout = 5
32
- request.open_timeout = 1
33
- adapter.configure_timeout(options, env)
34
- expect(options[:inactivity_timeout]).to eq(5)
35
- expect(options[:connect_timeout]).to eq(1)
15
+ expect(req.connopts.inactivity_timeout).to eq(20)
36
16
  end
37
17
 
38
- it 'configures all timeout settings' do
39
- request.timeout = 5
40
- request.read_timeout = 3
41
- request.open_timeout = 1
42
- adapter.configure_timeout(options, env)
43
- expect(options[:inactivity_timeout]).to eq(3)
44
- expect(options[:connect_timeout]).to eq(1)
18
+ context 'Options' do
19
+ let(:request) { Faraday::RequestOptions.new }
20
+ let(:env) { { request: request } }
21
+ let(:options) { {} }
22
+ let(:adapter) { Faraday::Adapter::EMHttp.new }
23
+
24
+ it 'configures timeout' do
25
+ request.timeout = 5
26
+ adapter.configure_timeout(options, env)
27
+ expect(options[:inactivity_timeout]).to eq(5)
28
+ expect(options[:connect_timeout]).to eq(5)
29
+ end
30
+
31
+ it 'configures timeout and open_timeout' do
32
+ request.timeout = 5
33
+ request.open_timeout = 1
34
+ adapter.configure_timeout(options, env)
35
+ expect(options[:inactivity_timeout]).to eq(5)
36
+ expect(options[:connect_timeout]).to eq(1)
37
+ end
38
+
39
+ it 'configures all timeout settings' do
40
+ request.timeout = 5
41
+ request.read_timeout = 3
42
+ request.open_timeout = 1
43
+ adapter.configure_timeout(options, env)
44
+ expect(options[:inactivity_timeout]).to eq(3)
45
+ expect(options[:connect_timeout]).to eq(1)
46
+ end
45
47
  end
46
48
  end
47
49
  end
@@ -1,16 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe Faraday::Adapter::EMSynchrony, unless: defined?(JRUBY_VERSION) do
4
- features :request_body_on_query_methods, :reason_phrase_parse,
5
- :skip_response_body_on_head, :parallel, :local_socket_binding
3
+ unless defined?(JRUBY_VERSION)
4
+ RSpec.describe Faraday::Adapter::EMSynchrony do
5
+ features :request_body_on_query_methods, :reason_phrase_parse,
6
+ :skip_response_body_on_head, :parallel, :local_socket_binding
6
7
 
7
- it_behaves_like 'an adapter'
8
+ it_behaves_like 'an adapter'
8
9
 
9
- it 'allows to provide adapter specific configs' do
10
- url = URI('https://example.com:1234')
11
- adapter = described_class.new nil, inactivity_timeout: 20
12
- req = adapter.create_request(url: url, request: {})
10
+ it 'allows to provide adapter specific configs' do
11
+ url = URI('https://example.com:1234')
12
+ adapter = described_class.new nil, inactivity_timeout: 20
13
+ req = adapter.create_request(url: url, request: {})
13
14
 
14
- expect(req.connopts.inactivity_timeout).to eq(20)
15
+ expect(req.connopts.inactivity_timeout).to eq(20)
16
+ end
15
17
  end
16
18
  end
@@ -253,6 +253,13 @@ RSpec.describe Faraday::Connection do
253
253
  expect(uri.path).to eq('/sake.html')
254
254
  end
255
255
 
256
+ it 'always returns new URI instance' do
257
+ conn.url_prefix = 'http://sushi.com'
258
+ uri1 = conn.build_exclusive_url(nil)
259
+ uri2 = conn.build_exclusive_url(nil)
260
+ expect(uri1).not_to equal(uri2)
261
+ end
262
+
256
263
  context 'with url_prefixed connection' do
257
264
  let(:url) { 'http://sushi.com/sushi/' }
258
265
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "@technoweenie"
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-05-22 00:00:00.000000000 Z
13
+ date: 2021-08-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday-em_http
@@ -54,6 +54,20 @@ dependencies:
54
54
  - - "~>"
55
55
  - !ruby/object:Gem::Version
56
56
  version: '1.1'
57
+ - !ruby/object:Gem::Dependency
58
+ name: faraday-httpclient
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: 1.0.1
64
+ type: :runtime
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: 1.0.1
57
71
  - !ruby/object:Gem::Dependency
58
72
  name: faraday-net_http
59
73
  requirement: !ruby/object:Gem::Requirement
@@ -82,6 +96,34 @@ dependencies:
82
96
  - - "~>"
83
97
  - !ruby/object:Gem::Version
84
98
  version: '1.1'
99
+ - !ruby/object:Gem::Dependency
100
+ name: faraday-patron
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: '1.0'
106
+ type: :runtime
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - "~>"
111
+ - !ruby/object:Gem::Version
112
+ version: '1.0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: faraday-rack
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '1.0'
120
+ type: :runtime
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - "~>"
125
+ - !ruby/object:Gem::Version
126
+ version: '1.0'
85
127
  - !ruby/object:Gem::Dependency
86
128
  name: multipart-post
87
129
  requirement: !ruby/object:Gem::Requirement
@@ -130,9 +172,6 @@ files:
130
172
  - examples/client_test.rb
131
173
  - lib/faraday.rb
132
174
  - lib/faraday/adapter.rb
133
- - lib/faraday/adapter/httpclient.rb
134
- - lib/faraday/adapter/patron.rb
135
- - lib/faraday/adapter/rack.rb
136
175
  - lib/faraday/adapter/test.rb
137
176
  - lib/faraday/adapter/typhoeus.rb
138
177
  - lib/faraday/adapter_registry.rb
@@ -221,7 +260,7 @@ licenses:
221
260
  - MIT
222
261
  metadata:
223
262
  homepage_uri: https://lostisland.github.io/faraday
224
- changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.4.2
263
+ changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.6.0
225
264
  source_code_uri: https://github.com/lostisland/faraday
226
265
  bug_tracker_uri: https://github.com/lostisland/faraday/issues
227
266
  post_install_message:
@@ -1,152 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Adapter
5
- # HTTPClient adapter.
6
- class HTTPClient < Faraday::Adapter
7
- dependency 'httpclient'
8
-
9
- def build_connection(env)
10
- @client ||= ::HTTPClient.new.tap do |cli|
11
- # enable compression
12
- cli.transparent_gzip_decompression = true
13
- end
14
-
15
- if (req = env[:request])
16
- if (proxy = req[:proxy])
17
- configure_proxy @client, proxy
18
- end
19
-
20
- if (bind = req[:bind])
21
- configure_socket @client, bind
22
- end
23
-
24
- configure_timeouts @client, req
25
- end
26
-
27
- if env[:url].scheme == 'https' && (ssl = env[:ssl])
28
- configure_ssl @client, ssl
29
- end
30
-
31
- configure_client @client
32
-
33
- @client
34
- end
35
-
36
- def call(env)
37
- super
38
-
39
- # TODO: Don't stream yet.
40
- # https://github.com/nahi/httpclient/pull/90
41
- env[:body] = env[:body].read if env[:body].respond_to? :read
42
-
43
- connection(env) do |http|
44
- resp = http.request env[:method], env[:url],
45
- body: env[:body],
46
- header: env[:request_headers]
47
-
48
- if (req = env[:request]).stream_response?
49
- warn "Streaming downloads for #{self.class.name} " \
50
- 'are not yet implemented.'
51
- req.on_data.call(resp.body, resp.body.bytesize)
52
- end
53
- save_response env, resp.status, resp.body, resp.headers, resp.reason
54
-
55
- @app.call env
56
- end
57
- rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
58
- raise Faraday::TimeoutError, $ERROR_INFO
59
- rescue ::HTTPClient::BadResponseError => e
60
- if e.message.include?('status 407')
61
- raise Faraday::ConnectionFailed,
62
- %(407 "Proxy Authentication Required ")
63
- end
64
-
65
- raise Faraday::ClientError, $ERROR_INFO
66
- rescue Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED, IOError, SocketError
67
- raise Faraday::ConnectionFailed, $ERROR_INFO
68
- rescue StandardError => e
69
- if defined?(::OpenSSL::SSL::SSLError) && \
70
- e.is_a?(::OpenSSL::SSL::SSLError)
71
- raise Faraday::SSLError, e
72
- end
73
-
74
- raise
75
- end
76
-
77
- # @param bind [Hash]
78
- def configure_socket(client, bind)
79
- client.socket_local.host = bind[:host]
80
- client.socket_local.port = bind[:port]
81
- end
82
-
83
- # Configure proxy URI and any user credentials.
84
- #
85
- # @param proxy [Hash]
86
- def configure_proxy(client, proxy)
87
- client.proxy = proxy[:uri]
88
- return unless proxy[:user] && proxy[:password]
89
-
90
- client.set_proxy_auth(proxy[:user], proxy[:password])
91
- end
92
-
93
- # @param ssl [Hash]
94
- def configure_ssl(client, ssl)
95
- ssl_config = client.ssl_config
96
- ssl_config.verify_mode = ssl_verify_mode(ssl)
97
- ssl_config.cert_store = ssl_cert_store(ssl)
98
-
99
- ssl_config.add_trust_ca ssl[:ca_file] if ssl[:ca_file]
100
- ssl_config.add_trust_ca ssl[:ca_path] if ssl[:ca_path]
101
- ssl_config.client_cert = ssl[:client_cert] if ssl[:client_cert]
102
- ssl_config.client_key = ssl[:client_key] if ssl[:client_key]
103
- ssl_config.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
104
- end
105
-
106
- # @param req [Hash]
107
- def configure_timeouts(client, req)
108
- if (sec = request_timeout(:open, req))
109
- client.connect_timeout = sec
110
- end
111
-
112
- if (sec = request_timeout(:write, req))
113
- client.send_timeout = sec
114
- end
115
-
116
- return unless (sec = request_timeout(:read, req))
117
-
118
- client.receive_timeout = sec
119
- end
120
-
121
- def configure_client(client)
122
- @config_block&.call(client)
123
- end
124
-
125
- # @param ssl [Hash]
126
- # @return [OpenSSL::X509::Store]
127
- def ssl_cert_store(ssl)
128
- return ssl[:cert_store] if ssl[:cert_store]
129
-
130
- # Memoize the cert store so that the same one is passed to
131
- # HTTPClient each time, to avoid resyncing SSL sessions when
132
- # it's changed
133
- @ssl_cert_store ||= begin
134
- # Use the default cert store by default, i.e. system ca certs
135
- OpenSSL::X509::Store.new.tap(&:set_default_paths)
136
- end
137
- end
138
-
139
- # @param ssl [Hash]
140
- def ssl_verify_mode(ssl)
141
- ssl[:verify_mode] || begin
142
- if ssl.fetch(:verify, true)
143
- OpenSSL::SSL::VERIFY_PEER |
144
- OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
145
- else
146
- OpenSSL::SSL::VERIFY_NONE
147
- end
148
- end
149
- end
150
- end
151
- end
152
- end
@@ -1,132 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Adapter
5
- # Patron adapter.
6
- class Patron < Faraday::Adapter
7
- dependency 'patron'
8
-
9
- def build_connection(env)
10
- session = ::Patron::Session.new
11
- @config_block&.call(session)
12
- if (env[:url].scheme == 'https') && env[:ssl]
13
- configure_ssl(session, env[:ssl])
14
- end
15
-
16
- if (req = env[:request])
17
- configure_timeouts(session, req)
18
- configure_proxy(session, req[:proxy])
19
- end
20
-
21
- session
22
- end
23
-
24
- def call(env)
25
- super
26
- # TODO: support streaming requests
27
- env[:body] = env[:body].read if env[:body].respond_to? :read
28
-
29
- response = connection(env) do |session|
30
- begin
31
- data = env[:body] ? env[:body].to_s : nil
32
- session.request(env[:method], env[:url].to_s,
33
- env[:request_headers], data: data)
34
- rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
35
- raise Faraday::ConnectionFailed, $ERROR_INFO
36
- end
37
- end
38
-
39
- if (req = env[:request]).stream_response?
40
- warn "Streaming downloads for #{self.class.name} " \
41
- 'are not yet implemented.'
42
- req.on_data.call(response.body, response.body.bytesize)
43
- end
44
- # Remove the "HTTP/1.1 200", leaving just the reason phrase
45
- reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')
46
-
47
- save_response(env, response.status, response.body,
48
- response.headers, reason_phrase)
49
-
50
- @app.call env
51
- rescue ::Patron::TimeoutError => e
52
- if connection_timed_out_message?(e.message)
53
- raise Faraday::ConnectionFailed, e
54
- end
55
-
56
- raise Faraday::TimeoutError, e
57
- rescue ::Patron::Error => e
58
- if e.message.include?('code 407')
59
- raise Faraday::ConnectionFailed,
60
- %(407 "Proxy Authentication Required ")
61
- end
62
-
63
- raise Faraday::ConnectionFailed, e
64
- end
65
-
66
- if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
67
- # HAX: helps but doesn't work completely
68
- # https://github.com/toland/patron/issues/34
69
- ::Patron::Request::VALID_ACTIONS.tap do |actions|
70
- if actions[0].is_a?(Symbol)
71
- actions << :patch unless actions.include? :patch
72
- actions << :options unless actions.include? :options
73
- else
74
- # Patron 0.4.20 and up
75
- actions << 'PATCH' unless actions.include? 'PATCH'
76
- actions << 'OPTIONS' unless actions.include? 'OPTIONS'
77
- end
78
- end
79
- end
80
-
81
- def configure_ssl(session, ssl)
82
- if ssl.fetch(:verify, true)
83
- session.cacert = ssl[:ca_file]
84
- else
85
- session.insecure = true
86
- end
87
- end
88
-
89
- def configure_timeouts(session, req)
90
- return unless req
91
-
92
- if (sec = request_timeout(:read, req))
93
- session.timeout = sec
94
- end
95
-
96
- return unless (sec = request_timeout(:open, req))
97
-
98
- session.connect_timeout = sec
99
- end
100
-
101
- def configure_proxy(session, proxy)
102
- return unless proxy
103
-
104
- proxy_uri = proxy[:uri].dup
105
- proxy_uri.user = proxy[:user] &&
106
- Utils.escape(proxy[:user]).gsub('+', '%20')
107
- proxy_uri.password = proxy[:password] &&
108
- Utils.escape(proxy[:password]).gsub('+', '%20')
109
- session.proxy = proxy_uri.to_s
110
- end
111
-
112
- private
113
-
114
- CURL_TIMEOUT_MESSAGES = [
115
- 'Connection time-out',
116
- 'Connection timed out',
117
- 'Timed out before name resolve',
118
- 'server connect has timed out',
119
- 'Resolving timed out',
120
- 'name lookup timed out',
121
- 'timed out before SSL',
122
- 'connect() timed out'
123
- ].freeze
124
-
125
- def connection_timed_out_message?(message)
126
- CURL_TIMEOUT_MESSAGES.any? do |curl_message|
127
- message.include?(curl_message)
128
- end
129
- end
130
- end
131
- end
132
- end
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Adapter
5
- # Sends requests to a Rack app.
6
- #
7
- # @example
8
- #
9
- # class MyRackApp
10
- # def call(env)
11
- # [200, {'Content-Type' => 'text/html'}, ["hello world"]]
12
- # end
13
- # end
14
- #
15
- # Faraday.new do |conn|
16
- # conn.adapter :rack, MyRackApp.new
17
- # end
18
- class Rack < Faraday::Adapter
19
- dependency 'rack/test'
20
-
21
- # not prefixed with "HTTP_"
22
- SPECIAL_HEADERS = %w[CONTENT_LENGTH CONTENT_TYPE].freeze
23
-
24
- def initialize(faraday_app, rack_app)
25
- super(faraday_app)
26
- mock_session = ::Rack::MockSession.new(rack_app)
27
- @session = ::Rack::Test::Session.new(mock_session)
28
- end
29
-
30
- def call(env)
31
- super
32
- rack_env = build_rack_env(env)
33
-
34
- env[:request_headers]&.each do |name, value|
35
- name = name.upcase.tr('-', '_')
36
- name = "HTTP_#{name}" unless SPECIAL_HEADERS.include? name
37
- rack_env[name] = value
38
- end
39
-
40
- timeout = request_timeout(:open, env[:request])
41
- timeout ||= request_timeout(:read, env[:request])
42
- response = if timeout
43
- Timer.timeout(timeout, Faraday::TimeoutError) do
44
- execute_request(env, rack_env)
45
- end
46
- else
47
- execute_request(env, rack_env)
48
- end
49
-
50
- if (req = env[:request]).stream_response?
51
- warn "Streaming downloads for #{self.class.name} " \
52
- 'are not yet implemented.'
53
- req.on_data.call(response.body, response.body.bytesize)
54
- end
55
-
56
- save_response(env, response.status, response.body, response.headers)
57
- @app.call env
58
- end
59
-
60
- private
61
-
62
- def execute_request(env, rack_env)
63
- @session.request(env[:url].to_s, rack_env)
64
- end
65
-
66
- def build_rack_env(env)
67
- {
68
- method: env[:method],
69
- input: env[:body].respond_to?(:read) ? env[:body].read : env[:body],
70
- 'rack.url_scheme' => env[:url].scheme
71
- }
72
- end
73
- end
74
- end
75
- end