Skip to content

Commit cc9772b

Browse files
committed
Merge pull request #514 from lostisland/no-port-number
Fix default port numbers for Net::HTTP adapters
2 parents 1b10688 + faf501b commit cc9772b

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/faraday/adapter/net_http.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def net_http_connection(env)
9292
Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password])
9393
else
9494
Net::HTTP
95-
end.new(env[:url].host, env[:url].port)
95+
end.new(env[:url].host, env[:url].port || (env[:url].scheme == 'https' ? 443 : 80))
9696
end
9797

9898
def configure_ssl(http, ssl)

test/adapters/net_http_test.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
require File.expand_path('../integration', __FILE__)
2+
require 'ostruct'
3+
require 'uri'
24

35
module Adapters
46
class NetHttpTest < Faraday::TestCase
@@ -10,5 +12,34 @@ def adapter() :net_http end
1012

1113
Integration.apply(self, *behaviors)
1214

15+
def test_no_explicit_http_port_number
16+
url = URI('http://example.com')
17+
url.port = nil
18+
19+
adapter = Faraday::Adapter::NetHttp.new
20+
http = adapter.net_http_connection(:url => url, :request => {})
21+
22+
assert_equal 80, http.port
23+
end
24+
25+
def test_no_explicit_https_port_number
26+
url = URI('https://example.com')
27+
url.port = nil
28+
29+
adapter = Faraday::Adapter::NetHttp.new
30+
http = adapter.net_http_connection(:url => url, :request => {})
31+
32+
assert_equal 443, http.port
33+
end
34+
35+
def test_explicit_port_number
36+
url = URI('https://example.com:1234')
37+
38+
adapter = Faraday::Adapter::NetHttp.new
39+
http = adapter.net_http_connection(:url => url, :request => {})
40+
41+
assert_equal 1234, http.port
42+
end
43+
1344
end
1445
end

0 commit comments

Comments
 (0)