|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class HTTPCacheControlIntegrationTest extends FunctionalTest { |
|
4
|
|
|
|
|
5
|
|
|
public function setUp() { |
|
6
|
|
|
parent::setUp(); |
|
7
|
|
|
Config::inst()->remove('HTTP', 'disable_http_cache'); |
|
8
|
|
|
HTTPCacheControl::reset(); |
|
9
|
|
|
} |
|
10
|
|
|
|
|
11
|
|
|
public function testFormCSRF() { |
|
12
|
|
|
// CSRF sets caching to disabled |
|
13
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_SessionController/showform'); |
|
14
|
|
|
$header = $response->getHeader('Cache-Control'); |
|
15
|
|
|
$this->assertFalse($response->isError()); |
|
16
|
|
|
$this->assertNotContains('public', $header); |
|
17
|
|
|
$this->assertNotContains('private', $header); |
|
18
|
|
|
$this->assertContains('no-cache', $header); |
|
19
|
|
|
$this->assertContains('no-store', $header); |
|
20
|
|
|
$this->assertContains('must-revalidate', $header); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testPublicForm() { |
|
24
|
|
|
// Public forms (http get) allow public caching |
|
25
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_SessionController/showpublicform'); |
|
26
|
|
|
$header = $response->getHeader('Cache-Control'); |
|
27
|
|
|
$this->assertFalse($response->isError()); |
|
28
|
|
|
$this->assertContains('public', $header); |
|
29
|
|
|
$this->assertContains('must-revalidate', $header); |
|
30
|
|
|
$this->assertNotContains('no-cache', $response->getHeader('Cache-Control')); |
|
31
|
|
|
$this->assertNotContains('no-store', $response->getHeader('Cache-Control')); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testPrivateActionsError() |
|
35
|
|
|
{ |
|
36
|
|
|
// disallowed private actions don't cache |
|
37
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_SessionController/privateaction'); |
|
38
|
|
|
$header = $response->getHeader('Cache-Control'); |
|
39
|
|
|
$this->assertTrue($response->isError()); |
|
40
|
|
|
$this->assertContains('no-cache', $header); |
|
41
|
|
|
$this->assertContains('no-store', $header); |
|
42
|
|
|
$this->assertContains('must-revalidate', $header); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testPrivateActionsAuthenticated() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->logInWithPermission('ADMIN'); |
|
48
|
|
|
// Authenticated actions are private cache |
|
49
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_SessionController/privateaction'); |
|
50
|
|
|
$header = $response->getHeader('Cache-Control'); |
|
51
|
|
|
$this->assertFalse($response->isError()); |
|
52
|
|
|
$this->assertContains('private', $header); |
|
53
|
|
|
$this->assertContains('must-revalidate', $header); |
|
54
|
|
|
$this->assertNotContains('no-cache', $header); |
|
55
|
|
|
$this->assertNotContains('no-store', $header); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testPrivateCache() { |
|
59
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_RuleController/privateaction'); |
|
60
|
|
|
$header = $response->getHeader('Cache-Control'); |
|
61
|
|
|
$this->assertFalse($response->isError()); |
|
62
|
|
|
$this->assertContains('private', $header); |
|
63
|
|
|
$this->assertContains('must-revalidate', $header); |
|
64
|
|
|
$this->assertNotContains('no-cache', $header); |
|
65
|
|
|
$this->assertNotContains('no-store', $header); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testPublicCache() { |
|
69
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_RuleController/publicaction'); |
|
70
|
|
|
$header = $response->getHeader('Cache-Control'); |
|
71
|
|
|
$this->assertFalse($response->isError()); |
|
72
|
|
|
$this->assertContains('public', $header); |
|
73
|
|
|
$this->assertContains('must-revalidate', $header); |
|
74
|
|
|
$this->assertNotContains('no-cache', $header); |
|
75
|
|
|
$this->assertNotContains('no-store', $header); |
|
76
|
|
|
$this->assertContains('max-age=9000', $header); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testDisabledCache() { |
|
80
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_RuleController/disabledaction'); |
|
81
|
|
|
$header = $response->getHeader('Cache-Control'); |
|
82
|
|
|
$this->assertFalse($response->isError()); |
|
83
|
|
|
$this->assertNotContains('public', $header); |
|
84
|
|
|
$this->assertNotContains('private', $header); |
|
85
|
|
|
$this->assertContains('no-cache', $header); |
|
86
|
|
|
$this->assertContains('no-store', $header); |
|
87
|
|
|
$this->assertContains('must-revalidate', $header); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Test caching based on session |
|
93
|
|
|
*/ |
|
94
|
|
|
class HTTPCacheControlIntegrationTest_SessionController extends Controller implements TestOnly |
|
95
|
|
|
{ |
|
96
|
|
|
private static $allowed_actions = array( |
|
97
|
|
|
'showform', |
|
98
|
|
|
'privateaction', |
|
99
|
|
|
'publicaction', |
|
100
|
|
|
'showpublicform', |
|
101
|
|
|
'Form', |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
public function init() |
|
105
|
|
|
{ |
|
106
|
|
|
parent::init(); |
|
107
|
|
|
// Prefer public by default |
|
108
|
|
|
HTTPCacheControl::singleton()->publicCache(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function getContent() |
|
112
|
|
|
{ |
|
113
|
|
|
return '<p>Hello world</p>'; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function showform() |
|
117
|
|
|
{ |
|
118
|
|
|
// Form should be set to private due to CSRF |
|
119
|
|
|
SecurityToken::enable(); |
|
120
|
|
|
return $this->renderWith('BlankPage'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function showpublicform() |
|
124
|
|
|
{ |
|
125
|
|
|
// Public form doesn't use CSRF and thus no session usage |
|
126
|
|
|
SecurityToken::disable(); |
|
127
|
|
|
return $this->renderWith('BlankPage'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function privateaction() |
|
131
|
|
|
{ |
|
132
|
|
|
if (!Permission::check('ANYCODE')) { |
|
133
|
|
|
$this->httpError(403, 'Not allowed'); |
|
134
|
|
|
} |
|
135
|
|
|
return 'ok'; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function publicaction() |
|
139
|
|
|
{ |
|
140
|
|
|
return 'Hello!'; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function Form() |
|
144
|
|
|
{ |
|
145
|
|
|
$form = new Form( |
|
146
|
|
|
$this, |
|
147
|
|
|
'Form', |
|
148
|
|
|
new FieldList(new TextField('Name')), |
|
149
|
|
|
new FieldList(new FormAction('submit', 'Submit')) |
|
150
|
|
|
); |
|
151
|
|
|
$form->setFormMethod('GET'); |
|
152
|
|
|
return $form; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Test caching based on specific http caching directives |
|
158
|
|
|
*/ |
|
159
|
|
|
class HTTPCacheControlIntegrationTest_RuleController extends Controller implements TestOnly |
|
160
|
|
|
{ |
|
161
|
|
|
private static $allowed_actions = array( |
|
162
|
|
|
'privateaction', |
|
163
|
|
|
'publicaction', |
|
164
|
|
|
'disabledaction', |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
public function init() |
|
168
|
|
|
{ |
|
169
|
|
|
parent::init(); |
|
170
|
|
|
// Prefer public by default |
|
171
|
|
|
HTTPCacheControl::singleton()->publicCache(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function privateaction() { |
|
175
|
|
|
HTTPCacheControl::singleton()->privateCache(); |
|
176
|
|
|
return 'private content'; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function publicaction() { |
|
180
|
|
|
HTTPCacheControl::singleton() |
|
181
|
|
|
->publicCache() |
|
182
|
|
|
->setMaxAge(9000); |
|
183
|
|
|
return 'public content'; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function disabledaction() { |
|
187
|
|
|
HTTPCacheControl::singleton()->disableCache(); |
|
188
|
|
|
return 'uncached content'; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|