|
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; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\HttpCacheBundle\HttpCache; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @group legacy |
|
21
|
|
|
*/ |
|
22
|
|
|
class HttpCacheTest extends \PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @return \FOS\HttpCacheBundle\HttpCache|\PHPUnit_Framework_MockObject_MockObject |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function getHttpCachePartialMock(array $mockedMethods = null) |
|
28
|
|
|
{ |
|
29
|
|
|
// HttpKernelInterface does not work, as symfony HttpCache requires an isDebug method |
|
30
|
|
|
$mockKernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface'); |
|
31
|
|
|
$mock = $this->getMockBuilder('\FOS\HttpCacheBundle\HttpCache') |
|
32
|
|
|
->setConstructorArgs(array($mockKernel, sys_get_temp_dir())) |
|
33
|
|
|
->setMethods($mockedMethods) |
|
34
|
|
|
->getMock() |
|
35
|
|
|
; |
|
36
|
|
|
|
|
37
|
|
|
// Force setting options property since we can't use original constructor. |
|
38
|
|
|
$options = array( |
|
39
|
|
|
'debug' => false, |
|
40
|
|
|
'default_ttl' => 0, |
|
41
|
|
|
'private_headers' => array('Authorization', 'Cookie'), |
|
42
|
|
|
'allow_reload' => false, |
|
43
|
|
|
'allow_revalidate' => false, |
|
44
|
|
|
'stale_while_revalidate' => 2, |
|
45
|
|
|
'stale_if_error' => 60, |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
$refHttpCache = new \ReflectionClass('Symfony\Component\HttpKernel\HttpCache\HttpCache'); |
|
49
|
|
|
// Workaround for Symfony 2.3 where $options property is not defined. |
|
50
|
|
View Code Duplication |
if (!$refHttpCache->hasProperty('options')) { |
|
|
|
|
|
|
51
|
|
|
$mock->options = $options; |
|
52
|
|
|
} else { |
|
53
|
|
|
$refOptions = $refHttpCache |
|
54
|
|
|
->getProperty('options'); |
|
55
|
|
|
$refOptions->setAccessible(true); |
|
56
|
|
|
$refOptions->setValue($mock, $options); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $mock; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
View Code Duplication |
public function testGenerateUserHashNotAllowed() |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$request = new Request(); |
|
65
|
|
|
$request->headers->set('accept', HttpCache::USER_HASH_ACCEPT_HEADER); |
|
|
|
|
|
|
66
|
|
|
$httpCache = $this->getHttpCachePartialMock(); |
|
67
|
|
|
$response = $httpCache->handle($request); |
|
68
|
|
|
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response); |
|
69
|
|
|
$this->assertSame(400, $response->getStatusCode()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
public function testPassingUserHashNotAllowed() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$request = new Request(); |
|
75
|
|
|
$request->headers->set(HttpCache::USER_HASH_HEADER, 'foo'); |
|
|
|
|
|
|
76
|
|
|
$httpCache = $this->getHttpCachePartialMock(); |
|
77
|
|
|
$response = $httpCache->handle($request); |
|
78
|
|
|
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response); |
|
79
|
|
|
$this->assertSame(400, $response->getStatusCode()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testUserHashAnonymous() |
|
83
|
|
|
{ |
|
84
|
|
|
$request = new Request(); |
|
85
|
|
|
$catch = true; |
|
86
|
|
|
|
|
87
|
|
|
$httpCache = $this->getHttpCachePartialMock(array('lookup')); |
|
88
|
|
|
$response = new Response(); |
|
89
|
|
|
$httpCache |
|
|
|
|
|
|
90
|
|
|
->expects($this->once()) |
|
91
|
|
|
->method('lookup') |
|
92
|
|
|
->with($request, $catch) |
|
93
|
|
|
->will($this->returnValue($response)); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch)); |
|
96
|
|
|
$this->assertTrue($request->headers->has(HttpCache::USER_HASH_HEADER)); |
|
|
|
|
|
|
97
|
|
|
$this->assertSame(HttpCache::ANONYMOUS_HASH, $request->headers->get(HttpCache::USER_HASH_HEADER)); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testUserHashUserWithSession() |
|
101
|
|
|
{ |
|
102
|
|
|
$catch = true; |
|
103
|
|
|
$sessionId1 = 'my_session_id'; |
|
104
|
|
|
$sessionId2 = 'another_session_id'; |
|
105
|
|
|
$cookies = array( |
|
106
|
|
|
'PHPSESSID' => $sessionId1, |
|
107
|
|
|
'PHPSESSIDsdiuhsdf4535d4f' => $sessionId2, |
|
108
|
|
|
'foo' => 'bar', |
|
109
|
|
|
); |
|
110
|
|
|
$cookieString = "PHPSESSID=$sessionId1; foo=bar; PHPSESSIDsdiuhsdf4535d4f=$sessionId2"; |
|
111
|
|
|
$request = Request::create('/foo', 'GET', array(), $cookies, array(), array('Cookie' => $cookieString)); |
|
112
|
|
|
$response = new Response(); |
|
113
|
|
|
|
|
114
|
|
|
$hashRequest = Request::create(HttpCache::USER_HASH_URI, HttpCache::USER_HASH_METHOD, array(), array(), array(), $request->server->all()); |
|
|
|
|
|
|
115
|
|
|
$hashRequest->attributes->set('internalRequest', true); |
|
116
|
|
|
$hashRequest->headers->set('Accept', HttpCache::USER_HASH_ACCEPT_HEADER); |
|
|
|
|
|
|
117
|
|
|
$hashRequest->headers->set('Cookie', "PHPSESSID=$sessionId1; PHPSESSIDsdiuhsdf4535d4f=$sessionId2"); |
|
118
|
|
|
$hashRequest->cookies->set('PHPSESSID', $sessionId1); |
|
119
|
|
|
$hashRequest->cookies->set('PHPSESSIDsdiuhsdf4535d4f', $sessionId2); |
|
120
|
|
|
// Ensure request properties have been filled up. |
|
121
|
|
|
$hashRequest->getPathInfo(); |
|
122
|
|
|
$hashRequest->getMethod(); |
|
123
|
|
|
|
|
124
|
|
|
$expectedContextHash = 'my_generated_hash'; |
|
125
|
|
|
// Just avoid the response to modify the request object, otherwise it's impossible to test objects equality. |
|
126
|
|
|
/** @var \Symfony\Component\HttpFoundation\Response|\PHPUnit_Framework_MockObject_MockObject $hashResponse */ |
|
127
|
|
|
$hashResponse = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Response') |
|
128
|
|
|
->setMethods(array('prepare')) |
|
129
|
|
|
->getMock(); |
|
130
|
|
|
$hashResponse->headers->set(HttpCache::USER_HASH_HEADER, $expectedContextHash); |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
$httpCache = $this->getHttpCachePartialMock(array('lookup')); |
|
133
|
|
|
$httpCache |
|
|
|
|
|
|
134
|
|
|
->expects($this->at(0)) |
|
135
|
|
|
->method('lookup') |
|
136
|
|
|
->with($hashRequest, $catch) |
|
137
|
|
|
->will($this->returnValue($hashResponse)); |
|
138
|
|
|
$httpCache |
|
|
|
|
|
|
139
|
|
|
->expects($this->at(1)) |
|
140
|
|
|
->method('lookup') |
|
141
|
|
|
->with($request) |
|
142
|
|
|
->will($this->returnValue($response)); |
|
143
|
|
|
|
|
144
|
|
|
$this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch)); |
|
145
|
|
|
$this->assertTrue($request->headers->has(HttpCache::USER_HASH_HEADER)); |
|
|
|
|
|
|
146
|
|
|
$this->assertSame($expectedContextHash, $request->headers->get(HttpCache::USER_HASH_HEADER)); |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.