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