/
ResponseTest.php
174 lines (145 loc) · 5.67 KB
/
ResponseTest.php
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* Munee: Optimising Your Assets
*
* @copyright Cody Lundquist 2012
* @license http://opensource.org/licenses/mit-license.php
*/
namespace Munee\Cases;
use Munee\Response;
use Munee\Mocks\MockAssetType;
/**
* Tests for the \Munee\Response Class
*
* @author Cody Lundquist
*/
class ResponseTest extends \PHPUnit_Framework_TestCase
{
/**
* @var int
*/
protected $_lastModifiedTime = 123456789;
/**
* Make sure the constructor is getting the correct Object passed to it.
*/
public function testConstruct()
{
$this->setExpectedException('Munee\ErrorException');
new Response(new \stdClass());
}
/**
* Test the initial request from a browser. Meaning they haven't requested the file before
*
* @runInSeparateProcess
*/
public function testNonCachedResponse()
{
$Response = new Response(new MockAssetType());
$Response->setHeaders();
$checkHeaders = array();
$checkHeaders['Cache-Control'] = 'must-revalidate';
$checkHeaders['Content-Type'] = 'text/test';
$checkHeaders['Last-Modified'] = gmdate('D, d M Y H:i:s', $this->_lastModifiedTime) . ' GMT';
// ETag is MD5 Hash of the content + last modified date
$checkHeaders['ETag'] = '00403b660c8f869d9f50c429f6dceb72';
$setHeaders = $this->_getHeaders();
$this->assertSame($checkHeaders['Cache-Control'], $setHeaders['Cache-Control']);
unset($setHeaders['Cache-Control']);
$this->assertSame($checkHeaders['Content-Type'], $setHeaders['Content-Type']);
unset($setHeaders['Content-Type']);
$this->assertSame($checkHeaders['Last-Modified'], $setHeaders['Last-Modified']);
unset($setHeaders['Last-Modified']);
$this->assertSame($checkHeaders['ETag'], $setHeaders['ETag']);
unset($setHeaders['ETag']);
$this->assertSame(0, count($setHeaders));
$this->assertFalse($Response->notModified);
header_remove('Cache-Control');
header_remove('Last-Modified');
header_remove('ETag');
header_remove('Content-Type');
}
/**
* Test the subsequent request by setting the correct $_SERVER variables and returning a
* 304 response. Unfortunately, xdebug_get_headers() does not return header response codes
* so I am testing a variable instead.
*
* @runInSeparateProcess
*/
public function testCachedResponse()
{
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s', $this->_lastModifiedTime) . ' GMT';
$_SERVER['HTTP_IF_NONE_MATCH'] = '00403b660c8f869d9f50c429f6dceb72';
$Response = new Response(new MockAssetType());
$Response->setHeaders();
$checkHeaders = array();
$setHeaders = $this->_getHeaders();
$this->assertSame($checkHeaders, $setHeaders);
$this->assertSame(0, count($setHeaders));
$this->assertTrue($Response->notModified);
unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
unset($_SERVER['HTTP_IF_NONE_MATCH']);
}
/**
* Test to see if what the browser request headers has is expired and return a new response
* with new caching headers.
*
* @runInSeparateProcess
*/
public function testExpiredRequestResponse()
{
$expiredTime = $this->_lastModifiedTime - 100; // removing 100 seconds
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s', $expiredTime) . ' GMT';
$_SERVER['HTTP_IF_NONE_MATCH'] = '00000000000000000000000000000000';
$Response = new Response(new MockAssetType());
$Response->setHeaders();
$checkHeaders = array();
$checkHeaders['Cache-Control'] = 'must-revalidate';
$checkHeaders['Content-Type'] = 'text/test';
$checkHeaders['Last-Modified'] = gmdate('D, d M Y H:i:s', $this->_lastModifiedTime) . ' GMT';
// ETag is MD5 Hash of the content + last modified date
$checkHeaders['ETag'] = '00403b660c8f869d9f50c429f6dceb72';
$setHeaders = $this->_getHeaders();
$this->assertSame($checkHeaders['Cache-Control'], $setHeaders['Cache-Control']);
unset($setHeaders['Cache-Control']);
$this->assertSame($checkHeaders['Content-Type'], $setHeaders['Content-Type']);
unset($setHeaders['Content-Type']);
$this->assertSame($checkHeaders['Last-Modified'], $setHeaders['Last-Modified']);
unset($setHeaders['Last-Modified']);
$this->assertSame($checkHeaders['ETag'], $setHeaders['ETag']);
unset($setHeaders['ETag']);
$this->assertSame(0, count($setHeaders));
$this->assertFalse($Response->notModified);
header_remove('Cache-Control');
header_remove('Last-Modified');
header_remove('ETag');
header_remove('Content-Type');
}
/**
* Make sure rendering is working properly
*/
public function testRender()
{
$Response = new Response(new MockAssetType());
$this->assertSame('foo', $Response->render());
}
/**
* Helper function to get the current headers set and weed out the ones that don't have a value
*
* @return array
*/
protected function _getHeaders()
{
$rawHeaders = xdebug_get_headers();
$ret = array();
foreach ($rawHeaders as $header) {
$headerParts = explode(':', $header, 2);
if (2 == count($headerParts)) {
$ret[$headerParts[0]] = trim($headerParts[1]);
} elseif (isset($ret[$headerParts[0]])) {
// If a header param is empty, make sure others with the same name are not set as well
unset($ret[$headerParts[0]]);
}
}
return $ret;
}
}