-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache_test.go
86 lines (75 loc) · 2.79 KB
/
cache_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
package cachewrapper
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestCacheControl(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "http://example.com/foo", nil)
handler := func(w http.ResponseWriter, r *http.Request) {}
opts := []optionFunc{MaxAge(time.Hour * 24 * 13), NoTransform()}
cached := Cached(http.HandlerFunc(handler), opts...)
cached.ServeHTTP(w, r)
wanted := "no-transform, max-age=1123200"
if pragmas := w.Header().Get("Cache-Control"); pragmas != wanted {
t.Fatalf("Cache-Control header: got %s, wanted '%s'", pragmas, wanted)
}
}
var cacheOptionTests = []struct {
o CacheOptions
h string
}{
{CacheOptions{Immutable: true, NoTransform: true}, "no-transform, max-age=31536000"},
{CacheOptions{Private: true, NoTransform: true}, "private, no-transform"},
{CacheOptions{MaxAge: time.Hour * 24 * 13, NoTransform: true}, "no-transform, max-age=1123200"},
{CacheOptions{NoCache: true, NoTransform: true}, "no-cache, no-transform"},
{CacheOptions{NoStore: true, NoTransform: true}, "no-store, no-transform"},
{CacheOptions{NoTransform: false}, ""},
{CacheOptions{MustRevalidate: true, NoTransform: true}, "no-transform, must-revalidate"},
{CacheOptions{ProxyRevalidate: true, NoTransform: true}, "no-transform, proxy-revalidate"},
{CacheOptions{SharedMaxAge: time.Hour * 13, NoTransform: true}, "no-transform, s-maxage=46800"},
}
func TestCacheOptions(t *testing.T) {
for _, tt := range cacheOptionTests {
if o := tt.o.String(); tt.h != o {
t.Errorf("%#v got '%s', wanted '%s'", tt.o, o, tt.h)
}
}
}
var cacheOptionFuncTests = []struct {
f optionFunc
h string
}{
{Immutable(), "max-age=31536000"},
{Private(), "private"},
{MaxAge(time.Hour * 24 * 13), "max-age=1123200"},
{NoCache(), "no-cache"},
{NoStore(), "no-store"},
{MustRevalidate(), "must-revalidate"},
{ProxyRevalidate(), "proxy-revalidate"},
{SharedMaxAge(time.Hour * 13), "s-maxage=46800"},
{Config(CacheOptions{MaxAge: time.Hour * 24 * 13, NoTransform: true}), "no-transform, max-age=1123200"},
}
func TestOptionFuncs(t *testing.T) {
for _, tt := range cacheOptionFuncTests {
co := CacheOptions{}
tt.f(&co)
if msg := co.String(); tt.h != msg {
t.Errorf("got '%s', wanted '%s'", msg, tt.h)
}
}
}
func TestMaintainsExistingCacheOptions(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "http://example.com/foo", nil)
handler := func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "must-revalidate") }
opts := []optionFunc{MaxAge(time.Hour * 24 * 13), NoTransform()}
cached := Cached(http.HandlerFunc(handler), opts...)
cached.ServeHTTP(w, r)
wanted := "must-revalidate"
if pragmas := w.Header().Get("Cache-Control"); pragmas != wanted {
t.Fatalf("Cache-Control header: got %s, wanted '%s'", pragmas, wanted)
}
}