1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCacheBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCacheBundle\Tests\Unit\EventListener; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCache\UserContext\HashGenerator; |
15
|
|
|
use FOS\HttpCacheBundle\EventListener\UserContextListener; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
19
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
20
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
21
|
|
|
use Symfony\Component\Routing\Matcher\RequestMatcherInterface; |
22
|
|
|
|
23
|
|
|
class UserContextListenerTest extends \PHPUnit_Framework_TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @expectedException \InvalidArgumentException |
27
|
|
|
*/ |
28
|
|
|
public function testMisconfiguration() |
29
|
|
|
{ |
30
|
|
|
new UserContextListener( |
31
|
|
|
\Mockery::mock('\Symfony\Component\HttpFoundation\RequestMatcherInterface'), |
32
|
|
|
\Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator'), |
33
|
|
|
array() |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testOnKernelRequest() |
38
|
|
|
{ |
39
|
|
|
$request = new Request(); |
40
|
|
|
$request->setMethod('HEAD'); |
41
|
|
|
|
42
|
|
|
$requestMatcher = $this->getRequestMatcher($request, true); |
43
|
|
|
$hashGenerator = \Mockery::mock(HashGenerator::class); |
44
|
|
|
$hashGenerator->shouldReceive('generateHash')->andReturn('hash'); |
45
|
|
|
|
46
|
|
|
$userContextListener = new UserContextListener($requestMatcher, $hashGenerator, array('X-SessionId'), 'X-Hash'); |
|
|
|
|
47
|
|
|
$event = $this->getKernelRequestEvent($request); |
48
|
|
|
|
49
|
|
|
$userContextListener->onKernelRequest($event); |
50
|
|
|
|
51
|
|
|
$response = $event->getResponse(); |
52
|
|
|
|
53
|
|
|
$this->assertNotNull($response); |
54
|
|
|
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
55
|
|
|
$this->assertEquals('hash', $response->headers->get('X-Hash')); |
56
|
|
|
$this->assertNull($response->headers->get('Vary')); |
57
|
|
|
$this->assertEquals('max-age=0, no-cache, private', $response->headers->get('Cache-Control')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function testOnKernelRequestNonMaster() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$request = new Request(); |
63
|
|
|
$request->setMethod('HEAD'); |
64
|
|
|
|
65
|
|
|
$requestMatcher = $this->getRequestMatcher($request, true); |
66
|
|
|
$hashGenerator = \Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator'); |
67
|
|
|
$hashGenerator->shouldReceive('generateHash')->never(); |
68
|
|
|
|
69
|
|
|
$userContextListener = new UserContextListener($requestMatcher, $hashGenerator, array('X-SessionId'), 'X-Hash'); |
|
|
|
|
70
|
|
|
$event = $this->getKernelRequestEvent($request, HttpKernelInterface::SUB_REQUEST); |
71
|
|
|
|
72
|
|
|
$userContextListener->onKernelRequest($event); |
73
|
|
|
|
74
|
|
|
$this->assertNull($event->getResponse()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testOnKernelRequestCached() |
78
|
|
|
{ |
79
|
|
|
$request = new Request(); |
80
|
|
|
$request->setMethod('HEAD'); |
81
|
|
|
|
82
|
|
|
$requestMatcher = $this->getRequestMatcher($request, true); |
83
|
|
|
$hashGenerator = \Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator'); |
84
|
|
|
$hashGenerator->shouldReceive('generateHash')->andReturn('hash'); |
85
|
|
|
|
86
|
|
|
$userContextListener = new UserContextListener($requestMatcher, $hashGenerator, array('X-SessionId'), 'X-Hash', 30); |
|
|
|
|
87
|
|
|
$event = $this->getKernelRequestEvent($request); |
88
|
|
|
|
89
|
|
|
$userContextListener->onKernelRequest($event); |
90
|
|
|
|
91
|
|
|
$response = $event->getResponse(); |
92
|
|
|
|
93
|
|
|
$this->assertNotNull($response); |
94
|
|
|
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
95
|
|
|
$this->assertEquals('hash', $response->headers->get('X-Hash')); |
96
|
|
|
$this->assertEquals('X-SessionId', $response->headers->get('Vary')); |
97
|
|
|
$this->assertEquals('max-age=30, public', $response->headers->get('Cache-Control')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
public function testOnKernelRequestNotMatched() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$request = new Request(); |
103
|
|
|
$request->setMethod('HEAD'); |
104
|
|
|
|
105
|
|
|
$requestMatcher = $this->getRequestMatcher($request, false); |
106
|
|
|
$hashGenerator = \Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator'); |
107
|
|
|
$hashGenerator->shouldReceive('generateHash')->andReturn('hash'); |
108
|
|
|
|
109
|
|
|
$userContextListener = new UserContextListener($requestMatcher, $hashGenerator, array('X-SessionId'), 'X-Hash'); |
|
|
|
|
110
|
|
|
$event = $this->getKernelRequestEvent($request); |
111
|
|
|
|
112
|
|
|
$userContextListener->onKernelRequest($event); |
113
|
|
|
|
114
|
|
|
$response = $event->getResponse(); |
115
|
|
|
|
116
|
|
|
$this->assertNull($response); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testOnKernelResponse() |
120
|
|
|
{ |
121
|
|
|
$request = new Request(); |
122
|
|
|
$request->setMethod('HEAD'); |
123
|
|
|
$request->headers->set('X-Hash', 'hash'); |
124
|
|
|
|
125
|
|
|
$requestMatcher = $this->getRequestMatcher($request, false); |
126
|
|
|
$hashGenerator = \Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator'); |
127
|
|
|
|
128
|
|
|
$userContextListener = new UserContextListener($requestMatcher, $hashGenerator, array('X-SessionId'), 'X-Hash'); |
|
|
|
|
129
|
|
|
$event = $this->getKernelResponseEvent($request); |
130
|
|
|
|
131
|
|
|
$userContextListener->onKernelResponse($event); |
132
|
|
|
|
133
|
|
|
$this->assertTrue($event->getResponse()->headers->has('Vary'), 'Vary header must be set'); |
134
|
|
|
$this->assertContains('X-Hash', $event->getResponse()->headers->get('Vary')); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testOnKernelResponseNotMaster() |
138
|
|
|
{ |
139
|
|
|
$request = new Request(); |
140
|
|
|
$request->setMethod('HEAD'); |
141
|
|
|
$request->headers->set('X-Hash', 'hash'); |
142
|
|
|
|
143
|
|
|
$requestMatcher = $this->getRequestMatcher($request, false); |
144
|
|
|
$hashGenerator = \Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator'); |
145
|
|
|
|
146
|
|
|
$userContextListener = new UserContextListener($requestMatcher, $hashGenerator, array('X-SessionId'), 'X-Hash'); |
|
|
|
|
147
|
|
|
$event = $this->getKernelResponseEvent($request, null, HttpKernelInterface::SUB_REQUEST); |
148
|
|
|
|
149
|
|
|
$userContextListener->onKernelResponse($event); |
150
|
|
|
|
151
|
|
|
$this->assertFalse($event->getResponse()->headers->has('Vary')); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* If there is no hash in the request, vary on the user identifier. |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
public function testOnKernelResponseNotCached() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$request = new Request(); |
160
|
|
|
$request->setMethod('HEAD'); |
161
|
|
|
|
162
|
|
|
$requestMatcher = $this->getRequestMatcher($request, false); |
163
|
|
|
$hashGenerator = \Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator'); |
164
|
|
|
|
165
|
|
|
$userContextListener = new UserContextListener($requestMatcher, $hashGenerator, array('X-SessionId'), 'X-Hash'); |
|
|
|
|
166
|
|
|
$event = $this->getKernelResponseEvent($request); |
167
|
|
|
|
168
|
|
|
$userContextListener->onKernelResponse($event); |
169
|
|
|
|
170
|
|
|
$this->assertEquals('X-SessionId', $event->getResponse()->headers->get('Vary')); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* If there is no hash in the request, vary on the user identifier. |
175
|
|
|
*/ |
176
|
|
|
public function testFullRequestHashOk() |
177
|
|
|
{ |
178
|
|
|
$request = new Request(); |
179
|
|
|
$request->setMethod('GET'); |
180
|
|
|
$request->headers->set('X-Hash', 'hash'); |
181
|
|
|
|
182
|
|
|
$requestMatcher = $this->getRequestMatcher($request, false); |
183
|
|
|
$hashGenerator = \Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator'); |
184
|
|
|
$hashGenerator->shouldReceive('generateHash')->andReturn('hash'); |
185
|
|
|
|
186
|
|
|
// onKernelRequest |
187
|
|
|
$userContextListener = new UserContextListener($requestMatcher, $hashGenerator, array('X-SessionId'), 'X-Hash'); |
|
|
|
|
188
|
|
|
$event = $this->getKernelRequestEvent($request); |
189
|
|
|
|
190
|
|
|
$userContextListener->onKernelRequest($event); |
191
|
|
|
|
192
|
|
|
$response = $event->getResponse(); |
193
|
|
|
|
194
|
|
|
$this->assertNull($response); |
195
|
|
|
|
196
|
|
|
// onKernelResponse |
197
|
|
|
$event = $this->getKernelResponseEvent($request); |
198
|
|
|
$userContextListener->onKernelResponse($event); |
199
|
|
|
|
200
|
|
|
$this->assertContains('X-Hash', $event->getResponse()->headers->get('Vary')); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* If the request is an anonymous one, no hash should be generated/validated. |
205
|
|
|
*/ |
206
|
|
|
public function testFullAnonymousRequestHashNotGenerated() |
207
|
|
|
{ |
208
|
|
|
$request = new Request(); |
209
|
|
|
$request->setMethod('GET'); |
210
|
|
|
$request->headers->set('X-Hash', 'anonymous-hash'); |
211
|
|
|
|
212
|
|
|
$requestMatcher = $this->getRequestMatcher($request, false); |
213
|
|
|
$hashGenerator = \Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator'); |
214
|
|
|
$hashGenerator->shouldReceive('generateHash')->never(); |
215
|
|
|
|
216
|
|
|
$anonymousRequestMatcher = \Mockery::mock('\Symfony\Component\HttpFoundation\RequestMatcherInterface'); |
217
|
|
|
$anonymousRequestMatcher->shouldReceive('matches')->andReturn(true); |
218
|
|
|
|
219
|
|
|
// onKernelRequest |
220
|
|
|
$userContextListener = new UserContextListener($requestMatcher, $hashGenerator, array('X-SessionId'), 'X-Hash', 0, true, $anonymousRequestMatcher); |
|
|
|
|
221
|
|
|
$event = $this->getKernelRequestEvent($request); |
222
|
|
|
|
223
|
|
|
$userContextListener->onKernelRequest($event); |
224
|
|
|
|
225
|
|
|
$response = $event->getResponse(); |
226
|
|
|
|
227
|
|
|
$this->assertNull($response); |
228
|
|
|
|
229
|
|
|
// onKernelResponse |
230
|
|
|
$event = $this->getKernelResponseEvent($request); |
231
|
|
|
$userContextListener->onKernelResponse($event); |
232
|
|
|
|
233
|
|
|
$this->assertContains('X-Hash', $event->getResponse()->headers->get('Vary')); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* If there is no hash in the requests but session changed, prevent setting bad cache. |
238
|
|
|
*/ |
239
|
|
|
public function testFullRequestHashChanged() |
240
|
|
|
{ |
241
|
|
|
$request = new Request(); |
242
|
|
|
$request->setMethod('GET'); |
243
|
|
|
$request->headers->set('X-Hash', 'hash'); |
244
|
|
|
|
245
|
|
|
$requestMatcher = $this->getRequestMatcher($request, false); |
246
|
|
|
$hashGenerator = \Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator'); |
247
|
|
|
$hashGenerator->shouldReceive('generateHash')->andReturn('hash-changed'); |
248
|
|
|
|
249
|
|
|
// onKernelRequest |
250
|
|
|
$userContextListener = new UserContextListener($requestMatcher, $hashGenerator, array('X-SessionId'), 'X-Hash'); |
|
|
|
|
251
|
|
|
$event = $this->getKernelRequestEvent($request); |
252
|
|
|
|
253
|
|
|
$userContextListener->onKernelRequest($event); |
254
|
|
|
|
255
|
|
|
$response = $event->getResponse(); |
256
|
|
|
|
257
|
|
|
$this->assertNull($response); |
258
|
|
|
|
259
|
|
|
// onKernelResponse |
260
|
|
|
$event = $this->getKernelResponseEvent($request); |
261
|
|
|
$userContextListener->onKernelResponse($event); |
262
|
|
|
|
263
|
|
|
$this->assertFalse($event->getResponse()->headers->has('Vary')); |
264
|
|
|
$this->assertEquals('max-age=0, no-cache, private', $event->getResponse()->headers->get('Cache-Control')); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
protected function getKernelRequestEvent(Request $request, $type = HttpKernelInterface::MASTER_REQUEST) |
268
|
|
|
{ |
269
|
|
|
return new GetResponseEvent( |
270
|
|
|
\Mockery::mock('\Symfony\Component\HttpKernel\HttpKernelInterface'), |
271
|
|
|
$request, |
272
|
|
|
$type |
273
|
|
|
); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
View Code Duplication |
protected function getKernelResponseEvent(Request $request, Response $response = null, $type = HttpKernelInterface::MASTER_REQUEST) |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
return new FilterResponseEvent( |
279
|
|
|
\Mockery::mock('\Symfony\Component\HttpKernel\HttpKernelInterface'), |
280
|
|
|
$request, |
281
|
|
|
$type, |
282
|
|
|
$response ?: new Response() |
283
|
|
|
); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param Request $request |
288
|
|
|
* @param bool $match |
289
|
|
|
* |
290
|
|
|
* @return \Mockery\MockInterface|RequestMatcherInterface |
291
|
|
|
*/ |
292
|
|
|
private function getRequestMatcher(Request $request, $match) |
293
|
|
|
{ |
294
|
|
|
$requestMatcher = \Mockery::mock('\Symfony\Component\HttpFoundation\RequestMatcherInterface'); |
295
|
|
|
$requestMatcher->shouldReceive('matches')->with($request)->andReturn($match); |
296
|
|
|
|
297
|
|
|
return $requestMatcher; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: