forked from caddyserver/cache-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpcache_test.go
More file actions
160 lines (144 loc) · 4.02 KB
/
Copy pathhttpcache_test.go
File metadata and controls
160 lines (144 loc) · 4.02 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
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
package httpcache
import (
"testing"
"time"
"github.com/caddyserver/caddy/v2/caddytest"
)
func TestMaxAge(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
order cache before rewrite
http_port 9080
https_port 9443
cache
}
localhost:9080 {
route /cache-max-age {
cache
header Cache-Control "max-age=60"
respond "Hello, max-age!"
}
}`, "caddyfile")
resp1, _ := tester.AssertGetResponse(`http://localhost:9080/cache-max-age`, 200, "Hello, max-age!")
if resp1.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored" {
t.Errorf("unexpected Cache-Status header %v", resp1.Header)
}
resp2, _ := tester.AssertGetResponse(`http://localhost:9080/cache-max-age`, 200, "Hello, max-age!")
if resp2.Header.Get("Cache-Status") != "Souin; hit; ttl=59" {
t.Errorf("unexpected Cache-Status header %v", resp2.Header.Get("Cache-Status"))
}
}
func TestSMaxAge(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
http_port 9080
https_port 9443
cache {
ttl 1000s
}
}
localhost:9080 {
route /cache-s-maxage {
cache
header Cache-Control "s-maxage=5"
respond "Hello, s-maxage!"
}
}`, "caddyfile")
resp1, _ := tester.AssertGetResponse(`http://localhost:9080/cache-s-maxage`, 200, "Hello, s-maxage!")
if resp1.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored" {
t.Errorf("unexpected Cache-Status header %v", resp1.Header.Get("Cache-Status"))
}
resp2, _ := tester.AssertGetResponse(`http://localhost:9080/cache-s-maxage`, 200, "Hello, s-maxage!")
if resp2.Header.Get("Cache-Status") != "Souin; hit; ttl=4" {
t.Errorf("unexpected Cache-Status header with %v", resp2.Header.Get("Cache-Status"))
}
}
func TestAgeHeader(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
http_port 9080
https_port 9443
cache {
ttl 1000s
}
}
localhost:9080 {
route /age-header {
cache
header Cache-Control "max-age=60"
respond "Hello, Age header!"
}
}`, "caddyfile")
resp1, _ := tester.AssertGetResponse(`http://localhost:9080/age-header`, 200, "Hello, Age header!")
if resp1.Header.Get("Age") != "" {
t.Errorf("unexpected Age header %v", resp1.Header.Get("Age"))
}
resp2, _ := tester.AssertGetResponse(`http://localhost:9080/age-header`, 200, "Hello, Age header!")
if resp2.Header.Get("Age") == "" {
t.Error("Age header should be present")
}
if resp2.Header.Get("Age") != "1" {
t.Error("Age header should be present")
}
time.Sleep(10 * time.Second)
resp3, _ := tester.AssertGetResponse(`http://localhost:9080/age-header`, 200, "Hello, Age header!")
if resp3.Header.Get("Age") != "11" {
t.Error("Age header should be present")
}
}
func TestExistingAgeHeader(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
http_port 9080
https_port 9443
cache {
ttl 1000s
}
}
localhost:9080 {
route /age-header {
cache
header Cache-Control "max-age=60"
header Age "max-age=5"
respond "Hello, Age header!"
}
}`, "caddyfile")
resp1, _ := tester.AssertGetResponse(`http://localhost:9080/age-header`, 200, "Hello, Age header!")
if resp1.Header.Get("Age") != "" {
t.Errorf("unexpected Age header %v", resp1.Header.Get("Age"))
}
resp2, _ := tester.AssertGetResponse(`http://localhost:9080/age-header`, 200, "Hello, Age header!")
if resp2.Header.Get("Age") != "1" {
t.Errorf("unexpected Age header value %v", resp2.Header.Get("Age"))
}
}
func TestNotHandledRoute(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
http_port 9080
https_port 9443
cache {
ttl 1000s
regex {
exclude ".*handled"
}
}
}
localhost:9080 {
route /not-handled {
cache
header Cache-Control "max-age=60"
header Age "max-age=5"
respond "Hello, Age header!"
}
}`, "caddyfile")
resp1, _ := tester.AssertGetResponse(`http://localhost:9080/not-handled`, 200, "Hello, Age header!")
if resp1.Header.Get("Cache-Status") != "Souin; fwd=uri-miss" {
t.Errorf("unexpected Cache-Status header value %v", resp1.Header.Get("Cache-Status"))
}
}