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