Skip to content

Commit

Permalink
Fix HTTPS proxy support (#48)
Browse files Browse the repository at this point in the history
* Fix HTTPS proxy support

* Add specs for setting http/s proxy

Ensure proxy settings are set correctly.
Currently proxy_use_ssl is not exposed as a public method by net-http, so we need to check the instance variable.
  • Loading branch information
richardmarbach authored Nov 13, 2024
1 parent 724af4d commit e2ab196
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/faraday/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def net_http_connection(env)
Net::HTTP.new(env[:url].hostname, port,
proxy[:uri].hostname, proxy[:uri].port,
proxy[:user], proxy[:password],
proxy[:uri].scheme == 'https')
nil, proxy[:uri].scheme == 'https')
else
Net::HTTP.new(env[:url].hostname, port, nil)
end
Expand Down
48 changes: 48 additions & 0 deletions spec/faraday/adapter/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,52 @@
it { expect(http.extra_chain_cert).to be_nil }
end
end

context 'http proxy' do
let(:adapter) { described_class.new }
let(:url) { URI('https://example.com') }
let(:proxy) { URI('http://proxy.example.com') }
let(:proxy_options) do
{
uri: proxy,
user: 'proxy_user',
password: 'proxy_pass'
}
end
let(:http) { adapter.send(:connection, url: url, request: { proxy: proxy_options }) }

it { expect(http.proxy_address).to eq 'proxy.example.com' }

it { expect(http.proxy_port).to be 80 }

it { expect(http.proxy_user).to eq 'proxy_user' }

it { expect(http.proxy_pass).to eq 'proxy_pass' }

it { expect(http.instance_variable_get(:@proxy_use_ssl)).to be false }
end

context 'https proxy' do
let(:adapter) { described_class.new }
let(:url) { URI('https://example.com') }
let(:proxy) { URI('https://proxy.example.com') }
let(:proxy_options) do
{
uri: proxy,
user: 'proxy_user',
password: 'proxy_pass'
}
end
let(:http) { adapter.send(:connection, url: url, request: { proxy: proxy_options }) }

it { expect(http.proxy_address).to eq 'proxy.example.com' }

it { expect(http.proxy_port).to be 443 }

it { expect(http.proxy_user).to eq 'proxy_user' }

it { expect(http.proxy_pass).to eq 'proxy_pass' }

it { expect(http.instance_variable_get(:@proxy_use_ssl)).to be true }
end
end

0 comments on commit e2ab196

Please sign in to comment.