-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachen_test.go
144 lines (121 loc) · 4.07 KB
/
cachen_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
package cachen
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
//Helper
func findValue(dataset []string, key string) bool {
for _, v := range dataset {
if v == key {
return true
}
}
return false
}
func TestReusableRequestIsCachable(t *testing.T) {
cached := New()
data := cached.ReusableRequest(true).State()
if data.cachable == noStore {
t.Error("Reusable request cannot be " + noStore)
}
}
func TestRevalidateEachTimeNotSetOnNonReusable(t *testing.T) {
cached := New()
data := cached.ReusableRequest(false).RevalidateEachTime(true).State()
if data.cachable == noCache || data.cachable == "" {
t.Error("Non Reusable must be " + noStore + " and it's " + data.cachable)
}
}
func TestIntermediatesAllowedOrNot(t *testing.T) {
cached := New()
data := cached.ReusableRequest(true).
RevalidateEachTime(true).
IntermediatesAllowed(true).State()
if data.intermediate != public {
t.Error("intermediate must be " + public + " and it's" + data.intermediate)
}
ndata := cached.ReusableRequest(true).RevalidateEachTime(true).IntermediatesAllowed(false).State()
if ndata.intermediate != private {
t.Error("intermediate must be " + private + " and it's" + data.intermediate)
}
}
func TestMaxAge(t *testing.T) {
cached := New()
cached.ReusableRequest(true).
RevalidateEachTime(true).
IntermediatesAllowed(true).MaxAge(5 * SECONDS)
data := cached.State()
if data.maxAge != data.smaxAge || data.maxAge != 5*SECONDS {
t.Error("Max age cannot be different than smaxage or is not well set")
}
cached.ReusableRequest(true).
RevalidateEachTime(true).
IntermediatesAllowed(true).MaxAge(5*SECONDS, 1*MINUTES)
ndata := cached.State()
if ndata.maxAge == ndata.smaxAge || ndata.maxAge != 5*SECONDS || ndata.smaxAge != 1*MINUTES {
t.Error("Max age cannot be equal to smaxcache and must save values correctly")
}
}
func TestStaleAllowed(t *testing.T) {
cached := New()
cached.ReusableRequest(true).
RevalidateEachTime(true).
IntermediatesAllowed(true).MaxAge(5 * SECONDS).StaleAllowed(true)
ndata := cached.State()
if !findValue(ndata.cacheControl, revalidate) {
t.Error("Must appear inside cacheControl the property:" + revalidate)
}
if findValue(ndata.cacheControl, proxyRevalidate) {
t.Error("Cannot appear inside cacheControl the property:" + proxyRevalidate)
}
cached.ReusableRequest(true).
RevalidateEachTime(true).
IntermediatesAllowed(true).MaxAge(5*SECONDS).StaleAllowed(false, true)
ndata = cached.State()
if findValue(ndata.cacheControl, revalidate) {
t.Error("Cannot appear inside cacheControl the property:" + revalidate)
}
if !findValue(ndata.cacheControl, proxyRevalidate) {
t.Error("Must appear inside cacheControl the property:" + proxyRevalidate)
}
}
func TestEtag(t *testing.T) {
cached := New()
cached.ReusableRequest(true).
RevalidateEachTime(true).
IntermediatesAllowed(true).MaxAge(5 * SECONDS).StaleAllowed(true).Etag("rd2d")
ndata := cached.State()
if ndata.etag != "rd2d" {
t.Error("Must have an etag correctly set")
}
}
func TestHandlerReturn(t *testing.T) {
cached := New()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.HandlerFunc(cached.ReusableRequest(true).
RevalidateEachTime(true).
IntermediatesAllowed(true).MaxAge(5 * SECONDS).StaleAllowed(false).Etag("rd2d").Bind)
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
etag := rr.Header().Get("Etag")
if etag != "rd2d" {
t.Error("Etag is not returned as a header")
}
cc := rr.Header().Get("Cache-Control")
ccs := strings.Split(cc, ",")
if !findValue(ccs, "no-cache") || !findValue(ccs, "public") || !findValue(ccs, "maxage=5") || !findValue(ccs, "smaxage=5") {
t.Errorf("One or more cache keys are missing: %v", cc)
}
}