|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Tests\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPResponse; |
|
6
|
|
|
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; |
|
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
8
|
|
|
|
|
9
|
|
|
class HTTPCacheControlMiddlewareTest extends SapphireTest |
|
10
|
|
|
{ |
|
11
|
|
|
protected function setUp() |
|
12
|
|
|
{ |
|
13
|
|
|
parent::setUp(); |
|
14
|
|
|
// Set to disabled at null forcing level |
|
15
|
|
|
HTTPCacheControlMiddleware::config() |
|
16
|
|
|
->set('defaultState', HTTPCacheControlMiddleware::STATE_ENABLED) |
|
17
|
|
|
->set('defaultForcingLevel', 0); |
|
18
|
|
|
HTTPCacheControlMiddleware::reset(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function provideCacheStates() |
|
22
|
|
|
{ |
|
23
|
|
|
return [ |
|
24
|
|
|
['enableCache', false], |
|
25
|
|
|
['publicCache', false], |
|
26
|
|
|
['privateCache', false], |
|
27
|
|
|
['disableCache', true], |
|
28
|
|
|
]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @dataProvider provideCacheStates |
|
33
|
|
|
*/ |
|
34
|
|
|
public function testCheckDefaultStates($state, $immutable) |
|
35
|
|
|
{ |
|
36
|
|
|
$cc = HTTPCacheControlMiddleware::singleton(); |
|
37
|
|
|
$cc->{$state}(); |
|
38
|
|
|
|
|
39
|
|
|
$response = new HTTPResponse(); |
|
40
|
|
|
$cc->applyToResponse($response); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertContains('must-revalidate', $response->getHeader('cache-control')); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @dataProvider provideCacheStates |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testSetMaxAge($state, $immutable) |
|
49
|
|
|
{ |
|
50
|
|
|
$cc = HTTPCacheControlMiddleware::singleton(); |
|
51
|
|
|
$cc->{$state}(); |
|
52
|
|
|
|
|
53
|
|
|
$originalResponse = new HTTPResponse(); |
|
54
|
|
|
$cc->applyToResponse($originalResponse); |
|
55
|
|
|
|
|
56
|
|
|
$cc->setMaxAge('300'); |
|
57
|
|
|
|
|
58
|
|
|
$response = new HTTPResponse(); |
|
59
|
|
|
|
|
60
|
|
|
$cc->applyToResponse($response); |
|
61
|
|
|
|
|
62
|
|
|
if ($immutable) { |
|
63
|
|
|
$this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control')); |
|
64
|
|
|
} else { |
|
65
|
|
|
$this->assertContains('max-age=300', $response->getHeader('cache-control')); |
|
66
|
|
|
$this->assertNotContains('no-cache', $response->getHeader('cache-control')); |
|
67
|
|
|
$this->assertNotContains('no-store', $response->getHeader('cache-control')); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @dataProvider provideCacheStates |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testSetNoStore($state, $immutable) |
|
75
|
|
|
{ |
|
76
|
|
|
$cc = HTTPCacheControlMiddleware::singleton(); |
|
77
|
|
|
$cc->setMaxAge('300'); |
|
78
|
|
|
$cc->setSharedMaxAge('300'); |
|
79
|
|
|
|
|
80
|
|
|
$cc->{$state}(); |
|
81
|
|
|
|
|
82
|
|
|
$originalResponse = new HTTPResponse(); |
|
83
|
|
|
$cc->applyToResponse($originalResponse); |
|
84
|
|
|
|
|
85
|
|
|
$cc->setNoStore(); |
|
86
|
|
|
|
|
87
|
|
|
$response = new HTTPResponse(); |
|
88
|
|
|
|
|
89
|
|
|
$cc->applyToResponse($response); |
|
90
|
|
|
|
|
91
|
|
|
if ($immutable) { |
|
92
|
|
|
$this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control')); |
|
93
|
|
|
} else { |
|
94
|
|
|
$this->assertContains('no-store', $response->getHeader('cache-control')); |
|
95
|
|
|
$this->assertNotContains('max-age', $response->getHeader('cache-control')); |
|
96
|
|
|
$this->assertNotContains('s-maxage', $response->getHeader('cache-control')); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @dataProvider provideCacheStates |
|
102
|
|
|
*/ |
|
103
|
|
|
public function testSetNoCache($state, $immutable) |
|
104
|
|
|
{ |
|
105
|
|
|
$cc = HTTPCacheControlMiddleware::singleton(); |
|
106
|
|
|
$cc->setMaxAge('300'); |
|
107
|
|
|
$cc->setSharedMaxAge('300'); |
|
108
|
|
|
|
|
109
|
|
|
$cc->{$state}(); |
|
110
|
|
|
|
|
111
|
|
|
$originalResponse = new HTTPResponse(); |
|
112
|
|
|
$cc->applyToResponse($originalResponse); |
|
113
|
|
|
|
|
114
|
|
|
$cc->setNoCache(); |
|
115
|
|
|
|
|
116
|
|
|
$response = new HTTPResponse(); |
|
117
|
|
|
|
|
118
|
|
|
$cc->applyToResponse($response); |
|
119
|
|
|
|
|
120
|
|
|
if ($immutable) { |
|
121
|
|
|
$this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control')); |
|
122
|
|
|
} else { |
|
123
|
|
|
$this->assertContains('no-cache', $response->getHeader('cache-control')); |
|
124
|
|
|
$this->assertNotContains('max-age', $response->getHeader('cache-control')); |
|
125
|
|
|
$this->assertNotContains('s-maxage', $response->getHeader('cache-control')); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @dataProvider provideCacheStates |
|
131
|
|
|
*/ |
|
132
|
|
|
public function testSetSharedMaxAge($state, $immutable) |
|
133
|
|
|
{ |
|
134
|
|
|
$cc = HTTPCacheControlMiddleware::singleton(); |
|
135
|
|
|
|
|
136
|
|
|
$cc->{$state}(); |
|
137
|
|
|
|
|
138
|
|
|
$originalResponse = new HTTPResponse(); |
|
139
|
|
|
$cc->applyToResponse($originalResponse); |
|
140
|
|
|
|
|
141
|
|
|
$cc->setSharedMaxAge('300'); |
|
142
|
|
|
|
|
143
|
|
|
$response = new HTTPResponse(); |
|
144
|
|
|
|
|
145
|
|
|
$cc->applyToResponse($response); |
|
146
|
|
|
|
|
147
|
|
|
if ($immutable) { |
|
148
|
|
|
$this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control')); |
|
149
|
|
|
} else { |
|
150
|
|
|
$this->assertContains('s-maxage=300', $response->getHeader('cache-control')); |
|
151
|
|
|
$this->assertNotContains('no-cache', $response->getHeader('cache-control')); |
|
152
|
|
|
$this->assertNotContains('no-store', $response->getHeader('cache-control')); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @dataProvider provideCacheStates |
|
158
|
|
|
*/ |
|
159
|
|
|
public function testSetMustRevalidate($state, $immutable) |
|
160
|
|
|
{ |
|
161
|
|
|
$cc = HTTPCacheControlMiddleware::singleton(); |
|
162
|
|
|
|
|
163
|
|
|
$cc->{$state}(); |
|
164
|
|
|
|
|
165
|
|
|
$originalResponse = new HTTPResponse(); |
|
166
|
|
|
$cc->applyToResponse($originalResponse); |
|
167
|
|
|
|
|
168
|
|
|
$cc->setMustRevalidate(); |
|
169
|
|
|
|
|
170
|
|
|
$response = new HTTPResponse(); |
|
171
|
|
|
|
|
172
|
|
|
$cc->applyToResponse($response); |
|
173
|
|
|
|
|
174
|
|
|
if ($immutable) { |
|
175
|
|
|
$this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control')); |
|
176
|
|
|
} else { |
|
177
|
|
|
$this->assertContains('must-revalidate', $response->getHeader('cache-control')); |
|
178
|
|
|
$this->assertNotContains('max-age', $response->getHeader('cache-control')); |
|
179
|
|
|
$this->assertNotContains('s-maxage', $response->getHeader('cache-control')); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function testCachingPriorities() |
|
184
|
|
|
{ |
|
185
|
|
|
$hcc = new HTTPCacheControlMiddleware(); |
|
186
|
|
|
$this->assertFalse($this->isDisabled($hcc), 'caching starts as disabled'); |
|
187
|
|
|
|
|
188
|
|
|
$hcc->enableCache(); |
|
189
|
|
|
$this->assertFalse($this->isDisabled($hcc)); |
|
190
|
|
|
|
|
191
|
|
|
$hcc->publicCache(); |
|
192
|
|
|
$this->assertTrue($this->isPublic($hcc), 'public can be set at start'); |
|
193
|
|
|
|
|
194
|
|
|
$hcc->privateCache(); |
|
195
|
|
|
$this->assertTrue($this->isPrivate($hcc), 'private overrides public'); |
|
196
|
|
|
|
|
197
|
|
|
$hcc->publicCache(); |
|
198
|
|
|
$this->assertFalse($this->isPublic($hcc), 'public does not overrides private'); |
|
199
|
|
|
|
|
200
|
|
|
$hcc->disableCache(); |
|
201
|
|
|
$this->assertTrue($this->isDisabled($hcc), 'disabled overrides private'); |
|
202
|
|
|
|
|
203
|
|
|
$hcc->privateCache(); |
|
204
|
|
|
$this->assertFalse($this->isPrivate($hcc), 'private does not override disabled'); |
|
205
|
|
|
|
|
206
|
|
|
$hcc->enableCache(true); |
|
207
|
|
|
$this->assertFalse($this->isDisabled($hcc)); |
|
208
|
|
|
|
|
209
|
|
|
$hcc->publicCache(true); |
|
210
|
|
|
$this->assertTrue($this->isPublic($hcc), 'force-public overrides disabled'); |
|
211
|
|
|
|
|
212
|
|
|
$hcc->privateCache(); |
|
213
|
|
|
$this->assertFalse($this->isPrivate($hcc), 'private does not overrdie force-public'); |
|
214
|
|
|
|
|
215
|
|
|
$hcc->privateCache(true); |
|
216
|
|
|
$this->assertTrue($this->isPrivate($hcc), 'force-private overrides force-public'); |
|
217
|
|
|
|
|
218
|
|
|
$hcc->publicCache(true); |
|
219
|
|
|
$this->assertFalse($this->isPublic($hcc), 'force-public does not override force-private'); |
|
220
|
|
|
|
|
221
|
|
|
$hcc->disableCache(true); |
|
222
|
|
|
$this->assertTrue($this->isDisabled($hcc), 'force-disabled overrides force-private'); |
|
223
|
|
|
|
|
224
|
|
|
$hcc->publicCache(true); |
|
225
|
|
|
$this->assertFalse($this->isPublic($hcc), 'force-public does not overrides force-disabled'); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
protected function isPrivate(HTTPCacheControlMiddleware $hcc) |
|
229
|
|
|
{ |
|
230
|
|
|
return $hcc->hasDirective('private') && !$hcc->hasDirective('public') && !$hcc->hasDirective('no-cache'); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
protected function isPublic(HTTPCacheControlMiddleware $hcc) |
|
234
|
|
|
{ |
|
235
|
|
|
return $hcc->hasDirective('public') && !$hcc->hasDirective('private') && !$hcc->hasDirective('no-cache'); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
protected function isDisabled(HTTPCacheControlMiddleware $hcc) |
|
239
|
|
|
{ |
|
240
|
|
|
return $hcc->hasDirective('no-store') && !$hcc->hasDirective('private') && !$hcc->hasDirective('public'); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|