forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
heartbeat: setup default ports in http monitors (elastic#3924)
* heartbeat: setup default ports in http monitors Default http to 80 and https to 443 Resolves elastic#3915 * Add entry to changelog * Add test Also refactor the code, turns out net.SplitHostPort() doesn't like hosts without ports.
- Loading branch information
1 parent
5443727
commit 77c5b74
Showing
4 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package http | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestSplitHostnamePort(t *testing.T) { | ||
var urlTests = []struct { | ||
scheme string | ||
host string | ||
expectedHost string | ||
expectedPort uint16 | ||
expectedError error | ||
}{ | ||
{ | ||
"http", | ||
"foo", | ||
"foo", | ||
80, | ||
nil, | ||
}, | ||
{ | ||
"http", | ||
"www.foo.com", | ||
"www.foo.com", | ||
80, | ||
nil, | ||
}, | ||
{ | ||
"http", | ||
"www.foo.com:8080", | ||
"www.foo.com", | ||
8080, | ||
nil, | ||
}, | ||
{ | ||
"https", | ||
"foo", | ||
"foo", | ||
443, | ||
nil, | ||
}, | ||
{ | ||
"http", | ||
"foo:81", | ||
"foo", | ||
81, | ||
nil, | ||
}, | ||
{ | ||
"https", | ||
"foo:444", | ||
"foo", | ||
444, | ||
nil, | ||
}, | ||
{ | ||
"httpz", | ||
"foo", | ||
"foo", | ||
81, | ||
&net.AddrError{}, | ||
}, | ||
} | ||
for _, test := range urlTests { | ||
url := &url.URL{ | ||
Scheme: test.scheme, | ||
Host: test.host, | ||
} | ||
request := &http.Request{ | ||
URL: url, | ||
} | ||
host, port, err := splitHostnamePort(request) | ||
if err != nil { | ||
if test.expectedError == nil { | ||
t.Error(err) | ||
} else if reflect.TypeOf(err) != reflect.TypeOf(test.expectedError) { | ||
t.Errorf("Expected %T but got %T", err, test.expectedError) | ||
} | ||
continue | ||
} | ||
if host != test.expectedHost { | ||
t.Errorf("Unexpected host for %#v: expected %q, got %q", request, test.expectedHost, host) | ||
} | ||
if port != test.expectedPort { | ||
t.Errorf("Unexpected port for %#v: expected %q, got %q", request, test.expectedPort, port) | ||
} | ||
} | ||
} |