From faf501b2773c005fe3f06ba36545d8d00a94f153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 20 Aug 2015 22:25:47 -0700 Subject: [PATCH] Fix default port numbers for Net::HTTP adapters When Addressable::URI is used, the `port` property will be nil unless port was explicitly specified. This made Addressable unusable in HTTPS scenarios with Net::HTTP adapters, which would default to port 80 even when the scheme was "https". --- lib/faraday/adapter/net_http.rb | 2 +- test/adapters/net_http_test.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/faraday/adapter/net_http.rb b/lib/faraday/adapter/net_http.rb index 6baac8b4..d248a826 100644 --- a/lib/faraday/adapter/net_http.rb +++ b/lib/faraday/adapter/net_http.rb @@ -92,7 +92,7 @@ module Faraday Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password]) else Net::HTTP - end.new(env[:url].host, env[:url].port) + end.new(env[:url].host, env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)) end def configure_ssl(http, ssl) diff --git a/test/adapters/net_http_test.rb b/test/adapters/net_http_test.rb index 810fad42..1bd60dba 100644 --- a/test/adapters/net_http_test.rb +++ b/test/adapters/net_http_test.rb @@ -1,4 +1,6 @@ require File.expand_path('../integration', __FILE__) +require 'ostruct' +require 'uri' module Adapters class NetHttpTest < Faraday::TestCase @@ -10,5 +12,34 @@ module Adapters Integration.apply(self, *behaviors) + def test_no_explicit_http_port_number + url = URI('http://example.com') + url.port = nil + + adapter = Faraday::Adapter::NetHttp.new + http = adapter.net_http_connection(:url => url, :request => {}) + + assert_equal 80, http.port + end + + def test_no_explicit_https_port_number + url = URI('https://example.com') + url.port = nil + + adapter = Faraday::Adapter::NetHttp.new + http = adapter.net_http_connection(:url => url, :request => {}) + + assert_equal 443, http.port + end + + def test_explicit_port_number + url = URI('https://example.com:1234') + + adapter = Faraday::Adapter::NetHttp.new + http = adapter.net_http_connection(:url => url, :request => {}) + + assert_equal 1234, http.port + end + end end