Use external Rack adapter (#1296)

This commit is contained in:
Matt 2021-08-01 09:01:13 +01:00 committed by GitHub
parent 66d5dae371
commit fd1007ae33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 4 additions and 79 deletions

View File

@ -22,6 +22,7 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
spec.add_dependency 'faraday-net_http', '~> 1.0'
spec.add_dependency 'faraday-net_http_persistent', '~> 1.1'
spec.add_dependency 'faraday-patron', '~> 1.0'
spec.add_dependency 'faraday-rack', '~> 1.0'
spec.add_dependency 'multipart-post', '>= 1.2', '< 3'
spec.add_dependency 'ruby2_keywords', '>= 0.0.4'

View File

@ -36,6 +36,7 @@ require 'faraday/httpclient'
require 'faraday/net_http'
require 'faraday/net_http_persistent'
require 'faraday/patron'
require 'faraday/rack'
# This is the main namespace for Faraday.
#

View File

@ -11,8 +11,7 @@ module Faraday
register_middleware File.expand_path('adapter', __dir__),
test: [:Test, 'test'],
typhoeus: [:Typhoeus, 'typhoeus'],
rack: [:Rack, 'rack']
typhoeus: [:Typhoeus, 'typhoeus']
# This module marks an Adapter as supporting parallel requests.
module Parallelism

View File

@ -1,75 +0,0 @@
# frozen_string_literal: true
module Faraday
class Adapter
# Sends requests to a Rack app.
#
# @example
#
# class MyRackApp
# def call(env)
# [200, {'Content-Type' => 'text/html'}, ["hello world"]]
# end
# end
#
# Faraday.new do |conn|
# conn.adapter :rack, MyRackApp.new
# end
class Rack < Faraday::Adapter
dependency 'rack/test'
# not prefixed with "HTTP_"
SPECIAL_HEADERS = %w[CONTENT_LENGTH CONTENT_TYPE].freeze
def initialize(faraday_app, rack_app)
super(faraday_app)
mock_session = ::Rack::MockSession.new(rack_app)
@session = ::Rack::Test::Session.new(mock_session)
end
def call(env)
super
rack_env = build_rack_env(env)
env[:request_headers]&.each do |name, value|
name = name.upcase.tr('-', '_')
name = "HTTP_#{name}" unless SPECIAL_HEADERS.include? name
rack_env[name] = value
end
timeout = request_timeout(:open, env[:request])
timeout ||= request_timeout(:read, env[:request])
response = if timeout
Timer.timeout(timeout, Faraday::TimeoutError) do
execute_request(env, rack_env)
end
else
execute_request(env, rack_env)
end
if (req = env[:request]).stream_response?
warn "Streaming downloads for #{self.class.name} " \
'are not yet implemented.'
req.on_data.call(response.body, response.body.bytesize)
end
save_response(env, response.status, response.body, response.headers)
@app.call env
end
private
def execute_request(env, rack_env)
@session.request(env[:url].to_s, rack_env)
end
def build_rack_env(env)
{
method: env[:method],
input: env[:body].respond_to?(:read) ? env[:body].read : env[:body],
'rack.url_scheme' => env[:url].scheme
}
end
end
end
end

View File

@ -59,8 +59,7 @@ module Faraday
extend AutoloadHelper
autoload_all 'faraday/adapter',
Typhoeus: 'typhoeus',
Test: 'test',
Rack: 'rack'
Test: 'test'
end
# Request represents a single HTTP request for a Faraday adapter to make.