1package favicon
2
3import "testing"
4
5func TestKeyCandidates(t *testing.T) {
6 tests := []struct {
7 name string
8 raw string
9 want []Key
10 }{
11 {name: "walks path before public host to registrable domain", raw: "https://app.docs.example.com/path", want: []Key{"app.docs.example.com/path", "app.docs.example.com", "docs.example.com", "example.com"}},
12 {name: "strips www", raw: "https://www.example.com", want: []Key{"example.com"}},
13 {name: "localhost with port is exact", raw: "http://localhost:1455/path", want: []Key{"localhost:1455/path", "localhost:1455"}},
14 {name: "ip with port is exact", raw: "http://127.0.0.1:8080/path", want: []Key{"127.0.0.1:8080/path", "127.0.0.1:8080"}},
15 {name: "ipv6 with port is exact", raw: "http://[::1]:8080/path", want: []Key{"[::1]:8080/path", "[::1]:8080"}},
16 {name: "uses public suffix for co uk", raw: "https://news.bbc.co.uk/story", want: []Key{"news.bbc.co.uk/story", "news.bbc.co.uk", "bbc.co.uk"}},
17 {name: "path candidates walk from exact path to host", raw: "https://github.com/bnema/gordon/pull/123", want: []Key{"github.com/bnema/gordon/pull/123", "github.com/bnema/gordon/pull", "github.com/bnema/gordon", "github.com/bnema", "github.com"}},
18 {name: "trailing slash normalizes to parent path key", raw: "https://github.com/bnema/gordon/", want: []Key{"github.com/bnema/gordon", "github.com/bnema", "github.com"}},
19 {name: "path normalization removes dot segments and query fragments", raw: "https://github.com/bnema/./gordon/issues/../pull/123?x#y", want: []Key{"github.com/bnema/gordon/pull/123", "github.com/bnema/gordon/pull", "github.com/bnema/gordon", "github.com/bnema", "github.com"}},
20 {name: "host-like string preserves path before host fallback", raw: "github.com/bnema?tab=repositories", want: []Key{"github.com/bnema", "github.com"}},
21
22 {name: "drops default https port", raw: "https://example.com:443/path", want: []Key{"example.com/path", "example.com"}},
23 {name: "non default url port is exact", raw: "https://example.com:8443/path", want: []Key{"example.com:8443/path", "example.com:8443"}},
24 {name: "host-like string lowercases and trims trailing dot", raw: "Docs.Example.COM.", want: []Key{"docs.example.com", "example.com"}},
25 {name: "strips www before preserving non default port", raw: "https://www.example.com:8443", want: []Key{"example.com:8443"}},
26 {name: "host-like string with port preserves port exact", raw: "example.com:443", want: []Key{"example.com:443"}},
27 {name: "unicode host normalizes to punycode", raw: "https://bücher.example/path", want: []Key{"xn--bcher-kva.example/path", "xn--bcher-kva.example"}},
28 {name: "punycode equivalent matches unicode", raw: "https://xn--bcher-kva.example/path", want: []Key{"xn--bcher-kva.example/path", "xn--bcher-kva.example"}},
29 {name: "empty input has no candidates", raw: "", want: nil},
30 {name: "about url has no candidates", raw: "about:blank", want: nil},
31 {name: "unsupported scheme has no candidates", raw: "file:///tmp/icon.html", want: nil},
32 }
33
34 for _, tt := range tests {
35 t.Run(tt.name, func(t *testing.T) {
36 got := Candidates(tt.raw)
37 if len(got) != len(tt.want) {
38 t.Fatalf("Candidates(%q) len = %d (%v), want %d (%v)", tt.raw, len(got), got, len(tt.want), tt.want)
39 }
40 for i := range got {
41 if got[i] != tt.want[i] {
42 t.Fatalf("Candidates(%q)[%d] = %q, want %q (all got %v)", tt.raw, i, got[i], tt.want[i], got)
43 }
44 }
45 })
46 }
47}
48
49func TestCanonicalKey(t *testing.T) {
50 key, ok := CanonicalKey("https://www.Example.COM.:443/path")
51 if !ok {
52 t.Fatal("CanonicalKey returned false")
53 }
54 if key != "example.com/path" {
55 t.Fatalf("CanonicalKey = %q, want example.com/path", key)
56 }
57
58 if key, ok := CanonicalKey("about:blank"); ok || key != "" {
59 t.Fatalf("CanonicalKey(about:blank) = %q, %v; want empty false", key, ok)
60 }
61}
62
63func TestCanonicalHostKey(t *testing.T) {
64 key, ok := CanonicalHostKey("https://www.Example.COM.:443/path")
65 if !ok {
66 t.Fatal("CanonicalHostKey returned false")
67 }
68 if key != "example.com" {
69 t.Fatalf("CanonicalHostKey = %q, want example.com", key)
70 }
71
72 if key, ok := CanonicalHostKey("about:blank"); ok || key != "" {
73 t.Fatalf("CanonicalHostKey(about:blank) = %q, %v; want empty false", key, ok)
74 }
75}