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

Support proxy settings with user and password #5102

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/active_merchant/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Connection
RETRY_SAFE = false
RUBY_184_POST_HEADERS = { 'Content-Type' => 'application/x-www-form-urlencoded' }

attr_accessor :endpoint, :open_timeout, :read_timeout, :verify_peer, :ssl_version, :ca_file, :ca_path, :pem, :pem_password, :logger, :tag, :ignore_http_status, :max_retries, :proxy_address, :proxy_port
attr_accessor :endpoint, :open_timeout, :read_timeout, :verify_peer, :ssl_version, :ca_file, :ca_path, :pem, :pem_password, :logger, :tag, :ignore_http_status, :max_retries, :proxy_address, :proxy_port, :proxy_user, :proxy_password

if Net::HTTP.instance_methods.include?(:min_version=)
attr_accessor :min_version
Expand All @@ -44,6 +44,8 @@ def initialize(endpoint)
@ssl_connection = {}
@proxy_address = :ENV
@proxy_port = nil
@proxy_user = nil
@proxy_password = nil
end

def wiredump_device=(device)
Expand Down Expand Up @@ -111,7 +113,7 @@ def request(method, body, headers = {})

def http
@http ||= begin
http = Net::HTTP.new(endpoint.host, endpoint.port, proxy_address, proxy_port)
http = Net::HTTP.new(endpoint.host, endpoint.port, proxy_address, proxy_port, proxy_user, proxy_password)
configure_debugging(http)
configure_timeouts(http)
configure_ssl(http)
Expand Down
8 changes: 6 additions & 2 deletions lib/active_merchant/posts_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def self.included(base)

base.class_attribute :proxy_address
base.class_attribute :proxy_port
base.class_attribute :proxy_user
base.class_attribute :proxy_password
end

def ssl_get(endpoint, headers = {})
Expand Down Expand Up @@ -68,8 +70,10 @@ def raw_ssl_request(method, endpoint, data, headers = {})

connection.ignore_http_status = @options[:ignore_http_status] if @options

connection.proxy_address = proxy_address
connection.proxy_port = proxy_port
connection.proxy_address = proxy_address
connection.proxy_port = proxy_port
connection.proxy_user = proxy_user
connection.proxy_password = proxy_password

connection.request(method, data, headers)
end
Expand Down
6 changes: 4 additions & 2 deletions test/unit/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_connection_endpoint_raises_uri_error

def test_connection_passes_env_proxy_by_default
spy = Net::HTTP.new('example.com', 443)
Net::HTTP.expects(:new).with('example.com', 443, :ENV, nil).returns(spy)
Net::HTTP.expects(:new).with('example.com', 443, :ENV, nil, nil, nil).returns(spy)
spy.expects(:start).returns(true)
spy.expects(:get).with('/tx.php', { 'connection' => 'close' }).returns(@ok)
@connection.request(:get, nil, {})
Expand All @@ -36,8 +36,10 @@ def test_connection_passes_env_proxy_by_default
def test_connection_does_pass_requested_proxy
@connection.proxy_address = 'proxy.example.com'
@connection.proxy_port = 8080
@connection.proxy_user = 'user'
@connection.proxy_password = 'password'
spy = Net::HTTP.new('example.com', 443)
Net::HTTP.expects(:new).with('example.com', 443, 'proxy.example.com', 8080).returns(spy)
Net::HTTP.expects(:new).with('example.com', 443, 'proxy.example.com', 8080, 'user', 'password').returns(spy)
spy.expects(:start).returns(true)
spy.expects(:get).with('/tx.php', { 'connection' => 'close' }).returns(@ok)
@connection.request(:get, nil, {})
Expand Down
4 changes: 4 additions & 0 deletions test/unit/posts_data_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ def test_setting_timeouts
def test_setting_proxy_settings
@gateway.class.proxy_address = 'http://proxy.com'
@gateway.class.proxy_port = 1234
@gateway.class.proxy_user = 'user'
@gateway.class.proxy_password = 'password'
ActiveMerchant::Connection.any_instance.expects(:request).returns(@ok)
ActiveMerchant::Connection.any_instance.expects(:proxy_address=).with('http://proxy.com')
ActiveMerchant::Connection.any_instance.expects(:proxy_port=).with(1234)
ActiveMerchant::Connection.any_instance.expects(:proxy_user=).with('user')
ActiveMerchant::Connection.any_instance.expects(:proxy_password=).with('password')

assert_nothing_raised do
@gateway.ssl_post(@url, '')
Expand Down
Loading