mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-04 00:02:03 -04:00
parent
e111db34c3
commit
97a3bc2386
@ -53,14 +53,15 @@ module Faraday
|
|||||||
end
|
end
|
||||||
|
|
||||||
def net_http_connection(env)
|
def net_http_connection(env)
|
||||||
klass = if (proxy = env[:request][:proxy])
|
proxy = env[:request][:proxy]
|
||||||
Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port,
|
|
||||||
proxy[:user], proxy[:password])
|
|
||||||
else
|
|
||||||
Net::HTTP
|
|
||||||
end
|
|
||||||
port = env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)
|
port = env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)
|
||||||
klass.new(env[:url].hostname, port)
|
if proxy
|
||||||
|
Net::HTTP.new(env[:url].hostname, port,
|
||||||
|
proxy[:uri].hostname, proxy[:uri].port,
|
||||||
|
proxy[:user], proxy[:password])
|
||||||
|
else
|
||||||
|
Net::HTTP.new(env[:url].hostname, port, nil)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
@ -182,7 +183,7 @@ module Faraday
|
|||||||
end
|
end
|
||||||
|
|
||||||
if (sec = http.respond_to?(:write_timeout=) &&
|
if (sec = http.respond_to?(:write_timeout=) &&
|
||||||
request_timeout(:write, req))
|
request_timeout(:write, req))
|
||||||
http.write_timeout = sec
|
http.write_timeout = sec
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -200,19 +201,19 @@ module Faraday
|
|||||||
return ssl[:cert_store] if ssl[:cert_store]
|
return ssl[:cert_store] if ssl[:cert_store]
|
||||||
|
|
||||||
@ssl_cert_store ||= begin
|
@ssl_cert_store ||= begin
|
||||||
# Use the default cert store by default, i.e. system ca certs
|
# Use the default cert store by default, i.e. system ca certs
|
||||||
OpenSSL::X509::Store.new.tap(&:set_default_paths)
|
OpenSSL::X509::Store.new.tap(&:set_default_paths)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def ssl_verify_mode(ssl)
|
def ssl_verify_mode(ssl)
|
||||||
ssl[:verify_mode] || begin
|
ssl[:verify_mode] || begin
|
||||||
if ssl.fetch(:verify, true)
|
if ssl.fetch(:verify, true)
|
||||||
OpenSSL::SSL::VERIFY_PEER
|
OpenSSL::SSL::VERIFY_PEER
|
||||||
else
|
else
|
||||||
OpenSSL::SSL::VERIFY_NONE
|
OpenSSL::SSL::VERIFY_NONE
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -33,6 +33,7 @@ shared_examples 'adapter examples' do |**options|
|
|||||||
|
|
||||||
let(:protocol) { ssl_mode? ? 'https' : 'http' }
|
let(:protocol) { ssl_mode? ? 'https' : 'http' }
|
||||||
let(:remote) { "#{protocol}://example.com" }
|
let(:remote) { "#{protocol}://example.com" }
|
||||||
|
let(:stub_remote) { remote }
|
||||||
|
|
||||||
let(:conn) do
|
let(:conn) do
|
||||||
conn_options[:ssl] ||= {}
|
conn_options[:ssl] ||= {}
|
||||||
@ -46,7 +47,7 @@ shared_examples 'adapter examples' do |**options|
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
let!(:request_stub) { stub_request(http_method, remote) }
|
let!(:request_stub) { stub_request(http_method, stub_remote) }
|
||||||
|
|
||||||
after do
|
after do
|
||||||
expect(request_stub).to have_been_requested unless request_stub.disabled?
|
expect(request_stub).to have_been_requested unless request_stub.disabled?
|
||||||
|
@ -1,5 +1,19 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
shared_examples 'proxy examples' do
|
||||||
|
it 'handles requests with proxy' do
|
||||||
|
res = conn.public_send(http_method, '/')
|
||||||
|
|
||||||
|
expect(res.status).to eq(200)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'handles proxy failures' do
|
||||||
|
request_stub.to_return(status: 407)
|
||||||
|
|
||||||
|
expect { conn.public_send(http_method, '/') }.to raise_error(Faraday::ProxyAuthError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
shared_examples 'a request method' do |http_method|
|
shared_examples 'a request method' do |http_method|
|
||||||
let(:query_or_body) { method_with_body?(http_method) ? :body : :query }
|
let(:query_or_body) { method_with_body?(http_method) ? :body : :query }
|
||||||
let(:response) { conn.public_send(http_method, '/') }
|
let(:response) { conn.public_send(http_method, '/') }
|
||||||
@ -218,17 +232,31 @@ shared_examples 'a request method' do |http_method|
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'handles requests with proxy' do
|
context 'when a proxy is provided as option' do
|
||||||
conn_options[:proxy] = 'http://google.co.uk'
|
before do
|
||||||
|
conn_options[:proxy] = 'http://env-proxy.com:80'
|
||||||
|
end
|
||||||
|
|
||||||
res = conn.public_send(http_method, '/')
|
include_examples 'proxy examples'
|
||||||
expect(res.status).to eq(200)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'handles proxy failures' do
|
context 'when http_proxy env variable is set' do
|
||||||
conn_options[:proxy] = 'http://google.co.uk'
|
let(:proxy_url) { 'http://env-proxy.com:80' }
|
||||||
request_stub.to_return(status: 407)
|
|
||||||
|
|
||||||
expect { conn.public_send(http_method, '/') }.to raise_error(Faraday::ProxyAuthError)
|
around do |example|
|
||||||
|
with_env 'http_proxy' => proxy_url do
|
||||||
|
example.run
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
include_examples 'proxy examples'
|
||||||
|
|
||||||
|
context 'when the env proxy is ignored' do
|
||||||
|
around do |example|
|
||||||
|
with_env_proxy_disabled(&example)
|
||||||
|
end
|
||||||
|
|
||||||
|
include_examples 'proxy examples'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user