pomerium/internal/urlutil/url_test.go
Bobby DeSimone 7558d5b0de
internal/config: refactor option parsing
- authorize: build whitelist from policy's URLs instead of strings.
- internal/httputil: merged httputil and https package.
- internal/config: merged config and policy packages.
- internal/metrics: removed unused measure struct.
- proxy/clients: refactor Addr fields to be urls.
- proxy: remove unused extend deadline function.
- proxy: use handler middleware for reverse proxy leg.
- proxy: change the way websocket requests are made (route based).

General improvements
- omitted value from range in several cases where for loop could be simplified.
- added error checking to many tests.
- standardize url parsing.
- remove unnecessary return statements.

- proxy: add self-signed certificate support. #179
- proxy: add skip tls certificate verification. #179
- proxy: Refactor websocket support to be route based. #204
2019-07-07 09:39:31 -07:00

61 lines
1.7 KiB
Go

package urlutil
import (
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
)
func Test_StripPort(t *testing.T) {
t.Parallel()
tests := []struct {
name string
hostport string
want string
}{
{"localhost", "localhost", "localhost"},
{"localhost with port", "localhost:443", "localhost"},
{"IPv6 localhost", "[::1]:80", "::1"},
{"IPv6 localhost without port", "[::1]", "::1"},
{"domain with port", "example.org:8080", "example.org"},
{"domain without port", "example.org", "example.org"},
{"long domain with port", "some.super.long.domain.example.org:8080", "some.super.long.domain.example.org"},
{"IPv6 with port", "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000", "2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
{"IPv6 without port", "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", "2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := StripPort(tt.hostport); got != tt.want {
t.Errorf("StripPort() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseAndValidateURL(t *testing.T) {
tests := []struct {
name string
rawurl string
want *url.URL
wantErr bool
}{
{"good", "https://some.example", &url.URL{Scheme: "https", Host: "some.example"}, false},
{"bad schema", "//some.example", nil, true},
{"bad hostname", "https://", nil, true},
{"bad parse", "https://^", nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseAndValidateURL(tt.rawurl)
if (err != nil) != tt.wantErr {
t.Errorf("ParseAndValidateURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("TestParseAndValidateURL() = %s", diff)
}
})
}
}