Skip to content

Commit

Permalink
Fix #1219 Net::HTTP still uses env proxy (#1221)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMacTia authored Dec 23, 2020
1 parent e111db3 commit 97a3bc2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
35 changes: 18 additions & 17 deletions lib/faraday/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ def build_connection(env)
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)
Expand Down Expand Up @@ -182,7 +183,7 @@ def configure_request(http, req)
end

if (sec = http.respond_to?(:write_timeout=) &&
request_timeout(:write, req))
request_timeout(:write, req))
http.write_timeout = sec
end

Expand All @@ -200,19 +201,19 @@ def ssl_cert_store(ssl)
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
Expand Down
3 changes: 2 additions & 1 deletion spec/support/shared_examples/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

let(:protocol) { ssl_mode? ? 'https' : 'http' }
let(:remote) { "#{protocol}://example.com" }
let(:stub_remote) { remote }

let(:conn) do
conn_options[:ssl] ||= {}
Expand All @@ -46,7 +47,7 @@
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?
Expand Down
44 changes: 36 additions & 8 deletions spec/support/shared_examples/request_method.rb
Original file line number Diff line number Diff line change
@@ -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, '/') }
Expand Down Expand Up @@ -218,17 +232,31 @@
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

0 comments on commit 97a3bc2

Please sign in to comment.