1
16
17 package urlutil
18
19 import "testing"
20
21 func TestURLJoin(t *testing.T) {
22 tests := []struct {
23 name, url, expect string
24 paths []string
25 }{
26 {name: "URL, one path", url: "http://example.com", paths: []string{"hello"}, expect: "http://example.com/hello"},
27 {name: "Long URL, one path", url: "http://example.com/but/first", paths: []string{"slurm"}, expect: "http://example.com/but/first/slurm"},
28 {name: "URL, two paths", url: "http://example.com", paths: []string{"hello", "world"}, expect: "http://example.com/hello/world"},
29 {name: "URL, no paths", url: "http://example.com", paths: []string{}, expect: "http://example.com"},
30 {name: "basepath, two paths", url: "../example.com", paths: []string{"hello", "world"}, expect: "../example.com/hello/world"},
31 }
32
33 for _, tt := range tests {
34 if got, err := URLJoin(tt.url, tt.paths...); err != nil {
35 t.Errorf("%s: error %q", tt.name, err)
36 } else if got != tt.expect {
37 t.Errorf("%s: expected %q, got %q", tt.name, tt.expect, got)
38 }
39 }
40 }
41
42 func TestEqual(t *testing.T) {
43 for _, tt := range []struct {
44 a, b string
45 match bool
46 }{
47 {"http://example.com", "http://example.com", true},
48 {"http://example.com", "http://another.example.com", false},
49 {"https://example.com", "https://example.com", true},
50 {"http://example.com/", "http://example.com", true},
51 {"https://example.com", "http://example.com", false},
52 {"http://example.com/foo", "http://example.com/foo/", true},
53 {"http://example.com/foo//", "http://example.com/foo/", true},
54 {"http://example.com/./foo/", "http://example.com/foo/", true},
55 {"http://example.com/bar/../foo/", "http://example.com/foo/", true},
56 {"/foo", "/foo", true},
57 {"/foo", "/foo/", true},
58 {"/foo/.", "/foo/", true},
59 {"%/1234", "%/1234", true},
60 {"%/1234", "%/123", false},
61 {"/1234", "%/1234", false},
62 } {
63 if tt.match != Equal(tt.a, tt.b) {
64 t.Errorf("Expected %q==%q to be %t", tt.a, tt.b, tt.match)
65 }
66 }
67 }
68
69 func TestExtractHostname(t *testing.T) {
70 tests := map[string]string{
71 "http://example.com": "example.com",
72 "https://example.com/foo": "example.com",
73
74 "https://example.com:31337/not/with/a/bang/but/a/whimper": "example.com",
75 }
76 for start, expect := range tests {
77 if got, _ := ExtractHostname(start); got != expect {
78 t.Errorf("Got %q, expected %q", got, expect)
79 }
80 }
81 }
82
View as plain text