mirror of https://github.com/perkeep/perkeep.git
123 lines
2.2 KiB
Go
123 lines
2.2 KiB
Go
/*
|
|
Copyright 2014 The Camlistore Authors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package netutil
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestHostPort(t *testing.T) {
|
|
tests := []struct {
|
|
baseURL string
|
|
wantNetAddr string
|
|
}{
|
|
// IPv4, no prefix
|
|
{
|
|
baseURL: "http://foo.com/",
|
|
wantNetAddr: "foo.com:80",
|
|
},
|
|
|
|
{
|
|
baseURL: "https://foo.com/",
|
|
wantNetAddr: "foo.com:443",
|
|
},
|
|
|
|
{
|
|
baseURL: "http://foo.com:8080/",
|
|
wantNetAddr: "foo.com:8080",
|
|
},
|
|
|
|
{
|
|
baseURL: "https://foo.com:8080/",
|
|
wantNetAddr: "foo.com:8080",
|
|
},
|
|
|
|
// IPv4, with prefix
|
|
{
|
|
baseURL: "http://foo.com/pics/",
|
|
wantNetAddr: "foo.com:80",
|
|
},
|
|
|
|
{
|
|
baseURL: "https://foo.com/pics/",
|
|
wantNetAddr: "foo.com:443",
|
|
},
|
|
|
|
{
|
|
baseURL: "http://foo.com:8080/pics/",
|
|
wantNetAddr: "foo.com:8080",
|
|
},
|
|
|
|
{
|
|
baseURL: "https://foo.com:8080/pics/",
|
|
wantNetAddr: "foo.com:8080",
|
|
},
|
|
|
|
// IPv6, no prefix
|
|
{
|
|
baseURL: "http://[::1]/",
|
|
wantNetAddr: "[::1]:80",
|
|
},
|
|
|
|
{
|
|
baseURL: "https://[::1]/",
|
|
wantNetAddr: "[::1]:443",
|
|
},
|
|
|
|
{
|
|
baseURL: "http://[::1]:8080/",
|
|
wantNetAddr: "[::1]:8080",
|
|
},
|
|
|
|
{
|
|
baseURL: "https://[::1]:8080/",
|
|
wantNetAddr: "[::1]:8080",
|
|
},
|
|
|
|
// IPv6, with prefix
|
|
{
|
|
baseURL: "http://[::1]/pics/",
|
|
wantNetAddr: "[::1]:80",
|
|
},
|
|
|
|
{
|
|
baseURL: "https://[::1]/pics/",
|
|
wantNetAddr: "[::1]:443",
|
|
},
|
|
|
|
{
|
|
baseURL: "http://[::1]:8080/pics/",
|
|
wantNetAddr: "[::1]:8080",
|
|
},
|
|
|
|
{
|
|
baseURL: "https://[::1]:8080/pics/",
|
|
wantNetAddr: "[::1]:8080",
|
|
},
|
|
}
|
|
for _, v := range tests {
|
|
got, err := HostPort(v.baseURL)
|
|
if err != nil {
|
|
t.Error(err)
|
|
continue
|
|
}
|
|
if got != v.wantNetAddr {
|
|
t.Errorf("got: %v for %v, want: %v", got, v.baseURL, v.wantNetAddr)
|
|
}
|
|
}
|
|
}
|