From d6b5c675cfeaeccd91b581f2d6437bd079bec979 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Sun, 9 Aug 2015 11:07:27 +0300 Subject: [PATCH] Do not add 80 port to host string --- http_client.go | 10 ++++++++-- http_client_test.go | 8 ++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/http_client.go b/http_client.go index 917c084..f9918b7 100644 --- a/http_client.go +++ b/http_client.go @@ -42,7 +42,9 @@ func NewHTTPClient(baseURL string, config *HTTPClientConfig) *HTTPClient { u, _ := url.Parse(baseURL) if !strings.Contains(u.Host, ":") { - u.Host += ":" + defaultPorts[u.Scheme] + if u.Scheme != "http" { + u.Host += ":" + defaultPorts[u.Scheme] + } } if config.Timeout.Nanoseconds() == 0 { @@ -66,7 +68,11 @@ func NewHTTPClient(baseURL string, config *HTTPClientConfig) *HTTPClient { func (c *HTTPClient) Connect() (err error) { c.Disconnect() - c.conn, err = net.Dial("tcp", c.host) + if !strings.Contains(c.host, ":") { + c.conn, err = net.Dial("tcp", c.host + ":80") + } else { + c.conn, err = net.Dial("tcp", c.host) + } if c.scheme == "https" { tlsConn := tls.Client(c.conn, &tls.Config{InsecureSkipVerify: true}) diff --git a/http_client_test.go b/http_client_test.go index d14d5be..6109a90 100644 --- a/http_client_test.go +++ b/http_client_test.go @@ -15,8 +15,8 @@ import ( func TestHTTPClientURLPort(t *testing.T) { c1 := NewHTTPClient("http://example.com", &HTTPClientConfig{}) - if c1.baseURL != "http://example.com:80" { - t.Error("Sould add 80 port for http:", c1.baseURL) + if c1.baseURL != "http://example.com" { + t.Error("Sould not add 80 port for http:", c1.baseURL) } c2 := NewHTTPClient("https://example.com", &HTTPClientConfig{}) @@ -30,8 +30,8 @@ func TestHTTPClientURLPort(t *testing.T) { } c4 := NewHTTPClient("example.com", &HTTPClientConfig{}) - if c4.baseURL != "http://example.com:80" { - t.Error("Sould add default protocol:", c4.baseURL) + if c4.baseURL != "http://example.com" { + t.Error("Sould not add default protocol:", c4.baseURL) } }