-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfavicon_proxy_test.go
More file actions
74 lines (63 loc) · 1.94 KB
/
favicon_proxy_test.go
File metadata and controls
74 lines (63 loc) · 1.94 KB
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
package gobookmarks
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
// helper server providing favicon
func newFaviconServer(t *testing.T, icon []byte) (*httptest.Server, *int) {
hits := 0
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("<link rel='icon' href='/favicon.ico'>"))
})
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
hits++
w.Header().Set("Cache-Control", "max-age=1")
w.Header().Set("Content-Type", "image/png")
w.Write(icon)
})
return httptest.NewServer(mux), &hits
}
func TestFaviconDiskCacheExpiry(t *testing.T) {
icon := []byte{0x89, 'P', 'N', 'G'}
srv, hits := newFaviconServer(t, icon)
defer srv.Close()
Config.FaviconCacheDir = t.TempDir()
Config.FaviconCacheSize = 1024 * 1024
FaviconCache.cache = make(map[string]*FavIcon)
req := httptest.NewRequest("GET", "/proxy/favicon?url="+srv.URL, nil)
w := httptest.NewRecorder()
FaviconProxyHandler(w, req)
if *hits != 1 {
t.Fatalf("expected 1 hit, got %d", *hits)
}
req = httptest.NewRequest("GET", "/proxy/favicon?url="+srv.URL, nil)
w = httptest.NewRecorder()
FaviconProxyHandler(w, req)
if *hits != 1 {
t.Fatalf("expected cache hit, hits %d", *hits)
}
time.Sleep(1500 * time.Millisecond)
req = httptest.NewRequest("GET", "/proxy/favicon?url="+srv.URL, nil)
w = httptest.NewRecorder()
FaviconProxyHandler(w, req)
if *hits != 2 {
t.Fatalf("expected refetch after expiry, hits %d", *hits)
}
}
func TestFaviconMaxCacheCount(t *testing.T) {
icon := []byte{0x89, 'P', 'N', 'G'}
FaviconCache.cache = make(map[string]*FavIcon)
Config.FaviconMaxCacheCount = 2
// Add 3 items
cacheFavicon("url1", icon, "image/png")
cacheFavicon("url2", icon, "image/png")
cacheFavicon("url3", icon, "image/png")
FaviconCache.RLock()
defer FaviconCache.RUnlock()
if len(FaviconCache.cache) > 2 {
t.Fatalf("expected cache size <= 2, got %d", len(FaviconCache.cache))
}
}