-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathparse_test.go
73 lines (69 loc) · 1.87 KB
/
parse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package registryurl
import (
"errors"
"testing"
)
// TestHelperParseURL verifies that a // "scheme" is added to URLs,
// and that invalid URLs produce an error.
func TestHelperParseURL(t *testing.T) {
tests := []struct {
url string
expectedURL string
err error
}{
{
url: "foobar.example.com",
expectedURL: "//foobar.example.com",
},
{
url: "foobar.example.com:2376",
expectedURL: "//foobar.example.com:2376",
},
{
url: "//foobar.example.com:2376",
expectedURL: "//foobar.example.com:2376",
},
{
url: "http://foobar.example.com:2376",
expectedURL: "http://foobar.example.com:2376",
},
{
url: "https://foobar.example.com:2376",
expectedURL: "https://foobar.example.com:2376",
},
{
url: "https://foobar.example.com:2376/some/path",
expectedURL: "https://foobar.example.com:2376/some/path",
},
{
url: "https://foobar.example.com:2376/some/other/path?foo=bar",
expectedURL: "https://foobar.example.com:2376/some/other/path",
},
{
url: "/foobar.example.com",
err: errors.New("no hostname in URL"),
},
{
url: "ftp://foobar.example.com:2376",
err: errors.New("unsupported scheme: ftp"),
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.url, func(t *testing.T) {
u, err := Parse(tc.url)
if tc.err == nil && err != nil {
t.Fatalf("Error: failed to parse URL %q: %s", tc.url, err)
}
if tc.err != nil && err == nil {
t.Fatalf("Error: expected error %q, got none when parsing URL %q", tc.err, tc.url)
}
if tc.err != nil && err.Error() != tc.err.Error() {
t.Fatalf("Error: expected error %q, got %q when parsing URL %q", tc.err, err, tc.url)
}
if u != nil && u.String() != tc.expectedURL {
t.Errorf("Error: expected URL: %q, but got %q for URL: %q", tc.expectedURL, u.String(), tc.url)
}
})
}
}