-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetFavicons_test.go
199 lines (186 loc) · 6.88 KB
/
GetFavicons_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package katsuragi
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
// GetFavicon()
func TestGetFavicons_AllInOne(t *testing.T) {
tests := []struct {
name string
url string
mockupServerNeed bool
responseBody string
expectedErr string
expectedResLength int
}{
{
name: "Invalid URL",
url: "255.255.255.0",
mockupServerNeed: false,
expectedErr: `Get "255.255.255.0": unsupported protocol scheme ""`,
expectedResLength: 0,
},
{
name: "No Favicons",
url: "",
mockupServerNeed: true,
responseBody: `<html><head></head><body></body></html>`,
expectedErr: "GetFavicon failed to find any favicons",
expectedResLength: 0,
},
{
name: "Multiple Favicons",
url: "",
mockupServerNeed: true,
responseBody: `<html><head>
<link rel="icon" href="favicon.ico" sizes="16x16">
<link rel="icon" href="favicon-32.png" sizes="32x32">
<link rel="apple-touch-icon" href="apple-touch-icon.png" sizes="180x180">
</head><body></body></html>`,
expectedResLength: 3,
},
{
name: "Icon Tag",
url: "",
mockupServerNeed: true,
responseBody: `<html><head><link rel="icon" href="/favicon.ico"></head><body></body></html>`,
expectedResLength: 1,
},
{
name: "Apple Touch Icon Tag",
url: "",
mockupServerNeed: true,
responseBody: `<html><head><link rel="apple-touch-icon" href="/apple-touch-icon.png"></head><body></body></html>`,
expectedResLength: 1,
},
{
name: "OG Image Tag - No Size Specified",
url: "",
mockupServerNeed: true,
responseBody: `<html><head><meta property="og:image" content="og-image.png"></head><body></body></html>`,
expectedErr: "GetFavicon failed to find any favicons",
expectedResLength: 0,
},
{
name: "OG Image Tag - Non 1:1 Aspect Ratio",
url: "",
mockupServerNeed: true,
responseBody: `<html><head><meta property="og:image" content="og-image.png"><meta property="og:image:type" content="image/png"><meta property="og:image:width" content="1200"><meta property="og:image:height" content="630"></head><body></body></html>`,
expectedErr: "GetFavicon failed to find any favicons",
expectedResLength: 0,
},
{
name: "OG Image Tag - 1:1 Aspect Ratio",
url: "",
mockupServerNeed: true,
responseBody: `<html><head><meta property="og:image" content="og-image.png"><meta property="og:image:type" content="image/png"><meta property="og:image:width" content="1200"><meta property="og:image:height" content="1200"></head><body></body></html>`,
expectedResLength: 1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
f := NewFetcher(&FetcherProps{Timeout: 3000, CacheCap: 10})
defer f.ClearCache()
var favicons []string
var err error
if test.mockupServerNeed {
server := MockServer(t, test.responseBody)
defer server.Close()
favicons, err = f.GetFavicons(server.URL)
} else {
favicons, err = f.GetFavicons(test.url)
}
if err != nil {
if test.expectedErr == "" {
t.Fatalf("Expected no error, got: %v", err)
}
if err.Error() != test.expectedErr {
t.Fatalf("Expected error: %s, got: %v", test.expectedErr, err)
}
} else {
if test.expectedErr != "" {
t.Fatalf("Expected error: %s, got none", test.expectedErr)
}
}
if len(favicons) > 0 && test.expectedResLength == 0 {
t.Fatalf("Expected no favicons, found %d", len(favicons))
}
})
}
}
func TestGetRootFaviconIco(t *testing.T) {
tests := []struct {
name string
serverResponse int
expectedErr string
expectedLength int
badURL bool
}{
{
name: "Favicon Found",
serverResponse: http.StatusOK,
expectedErr: "",
expectedLength: 1,
badURL: false,
},
{
name: "Favicon Not Found",
serverResponse: http.StatusNotFound,
expectedErr: "failed to fetch favicon.ico: favicon not found",
expectedLength: 0,
badURL: false,
},
{
name: "Bad URL",
serverResponse: http.StatusOK,
expectedErr: "failed to fetch favicon.ico: invalid url",
expectedLength: 0,
badURL: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var serverURL string
if test.badURL {
serverURL = "http://invalid-url"
} else {
// Create a mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/favicon.ico" {
w.WriteHeader(test.serverResponse)
} else {
w.WriteHeader(http.StatusOK)
}
}))
defer server.Close()
// Parse the server URL
parsedURL, err := url.Parse(server.URL)
if err != nil {
t.Fatalf("Failed to parse server URL: %v", err)
}
serverURL = parsedURL.String()
}
// Call getRootFaviconIco
var favicons []string
err := getRootFaviconIco(&favicons, serverURL)
// Verify the results
if err != nil {
if test.expectedErr == "" {
t.Fatalf("Expected no error, got: %v", err)
}
if err.Error() != test.expectedErr {
t.Fatalf("Expected error: %s, got: %v", test.expectedErr, err)
}
} else {
if test.expectedErr != "" {
t.Fatalf("Expected error: %s, got none", test.expectedErr)
}
}
if len(favicons) != test.expectedLength {
t.Fatalf("Expected %d favicons, found %d", test.expectedLength, len(favicons))
}
})
}
}