Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1219 Net::HTTP still uses env proxy #1221

Merged
merged 3 commits into from Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 18 additions & 17 deletions lib/faraday/adapter/net_http.rb
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
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
@@ -1,5 +1,19 @@
# frozen_string_literal: true

shared_examples 'proxy examples' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really, I like this shared one.

it 'handles requests with proxy' do
res = conn.public_send(http_method, '/')

expect(res.status).to eq(200)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A newline, yeah?

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