This repository has been archived by the owner on Apr 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
rules_test.go
157 lines (123 loc) · 4.92 KB
/
rules_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
package cache
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func makeRequest(URL string, headers http.Header) *http.Request {
r, err := http.NewRequest("GET", URL, nil)
if err != nil {
panic(err)
}
copyHeaders(headers, r.Header)
return r
}
func makeResponse(code int, headers http.Header) *Response {
return &Response{
Code: code,
snapHeader: headers,
}
}
func makeHeader(key string, value string) http.Header {
h := http.Header{}
h.Add(key, value)
return h
}
func TestCacheableStatus(t *testing.T) {
c := emptyConfig()
c.DefaultMaxAge = time.Duration(1) * time.Second
c.LockTimeout = time.Duration(5) * time.Hour
c.CacheRules = []CacheRule{
&PathCacheRule{Path: "/public"},
}
testTime := time.Now()
now = func() time.Time {
return testTime
}
t.Run("it should handle parsing error", func(t *testing.T) {
request := makeRequest("/", http.Header{})
response := makeResponse(200, makeHeader("Cache-Control", "max-age=ss"))
isPublic, expiration := getCacheableStatus(request, response, c)
require.False(t, isPublic)
require.Equal(t, time.Time{}, expiration)
})
t.Run("it should return lockTimeout if response is private", func(t *testing.T) {
request := makeRequest("/", http.Header{})
response := makeResponse(200, makeHeader("Cache-control", "private"))
isPublic, expiration := getCacheableStatus(request, response, c)
require.False(t, isPublic)
require.Equal(t, testTime.Add(c.LockTimeout), expiration)
})
t.Run("it should return lockTimeout if response has Vary: *", func(t *testing.T) {
request := makeRequest("/", http.Header{})
response := makeResponse(200, makeHeader("Vary", "*"))
isPublic, expiration := getCacheableStatus(request, response, c)
require.False(t, isPublic)
require.Equal(t, testTime.Add(c.LockTimeout), expiration)
})
t.Run("should return public = false if does not have explicit expiration", func(t *testing.T) {
request := makeRequest("/", http.Header{})
response := makeResponse(200, http.Header{})
isPublic, expiration := getCacheableStatus(request, response, c)
require.False(t, isPublic)
require.Equal(t, testTime.Add(c.LockTimeout), expiration)
})
t.Run("should return public = false if the status code is 502", func(t *testing.T) {
request := makeRequest("/", http.Header{})
response := makeResponse(502, http.Header{})
isPublic, _ := getCacheableStatus(request, response, c)
require.False(t, isPublic)
})
t.Run("should return public = false if the status code is 304", func(t *testing.T) {
request := makeRequest("/", http.Header{})
response := makeResponse(304, makeHeader("Cache-control", "max-age=5"))
isPublic, _ := getCacheableStatus(request, response, c)
require.False(t, isPublic)
})
t.Run("should return public = true if it has explicit expiration", func(t *testing.T) {
request := makeRequest("/", http.Header{})
response := makeResponse(200, makeHeader("Cache-control", "max-age=5"))
isPublic, expiration := getCacheableStatus(request, response, c)
require.True(t, isPublic)
// Round is required because cachecontrol library uses time.Now() inside
require.Equal(t, testTime.Add(time.Duration(5)*time.Second).UTC().Round(time.Second), expiration.UTC().Round(time.Second))
})
t.Run("should use default max age if rules matches and no expiration specified", func(t *testing.T) {
request := makeRequest("/public", http.Header{})
response := makeResponse(200, http.Header{})
isPublic, expiration := getCacheableStatus(request, response, c)
require.True(t, isPublic)
require.Equal(t, testTime.Add(c.DefaultMaxAge), expiration)
})
t.Run("should use specified expiration if rules matches and expiration is set", func(t *testing.T) {
request := makeRequest("/public", http.Header{})
response := makeResponse(200, makeHeader("Cache-control", "max-age=50"))
isPublic, expiration := getCacheableStatus(request, response, c)
require.True(t, isPublic)
// Round is required because cachecontrol library uses time.Now() inside
require.Equal(t, testTime.Add(time.Duration(50)*time.Second).UTC().Round(time.Second), expiration.UTC().Round(time.Second))
})
}
func TestHeaderCacheRule(t *testing.T) {
r := &HeaderCacheRule{
Header: "Content-Type",
Value: []string{"image/png", "image/jpg"},
}
t.Run("should match equal header and value", func(t *testing.T) {
r := &HeaderCacheRule{
Header: "Content-Type",
Value: []string{"image/jpg"},
}
matched := r.matches(makeRequest("/", http.Header{}), 200, makeHeader("Content-Type", "image/jpg"))
require.True(t, matched)
})
t.Run("should match equal header and 1 value", func(t *testing.T) {
matched := r.matches(makeRequest("/", http.Header{}), 200, makeHeader("Content-Type", "image/png"))
require.True(t, matched)
})
t.Run("should not match different header but same", func(t *testing.T) {
matched := r.matches(makeRequest("/", http.Header{}), 200, makeHeader("X-Custom-Header", "image/png"))
require.False(t, matched)
})
}