|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Tests the {@link HTTP} class |
|
4
|
|
|
* |
|
5
|
|
|
* @package framework |
|
6
|
|
|
* @subpackage tests |
|
7
|
|
|
*/ |
|
8
|
|
|
class HTTPTest extends FunctionalTest { |
|
9
|
|
|
|
|
10
|
|
|
public function testAddCacheHeaders() { |
|
11
|
|
|
$body = "<html><head></head><body><h1>Mysite</h1></body></html>"; |
|
12
|
|
|
$response = new SS_HTTPResponse($body, 200); |
|
13
|
|
|
$this->assertEmpty($response->getHeader('Cache-Control')); |
|
14
|
|
|
|
|
15
|
|
|
HTTP::set_cache_age(30); |
|
16
|
|
|
|
|
17
|
|
|
HTTP::add_cache_headers($response); |
|
18
|
|
|
$this->assertNotEmpty($response->getHeader('Cache-Control')); |
|
19
|
|
|
|
|
20
|
|
|
// Ensure max-age is zero for development. |
|
21
|
|
|
Config::inst()->update('Director', 'environment_type', 'dev'); |
|
22
|
|
|
$response = new SS_HTTPResponse($body, 200); |
|
23
|
|
|
HTTP::add_cache_headers($response); |
|
24
|
|
|
$this->assertContains('max-age=0', $response->getHeader('Cache-Control')); |
|
25
|
|
|
|
|
26
|
|
|
// Ensure max-age setting is respected in production. |
|
27
|
|
|
Config::inst()->update('Director', 'environment_type', 'live'); |
|
28
|
|
|
$response = new SS_HTTPResponse($body, 200); |
|
29
|
|
|
HTTP::add_cache_headers($response); |
|
30
|
|
|
$this->assertContains('max-age=30', explode(', ', $response->getHeader('Cache-Control'))); |
|
31
|
|
|
$this->assertNotContains('max-age=0', $response->getHeader('Cache-Control')); |
|
32
|
|
|
|
|
33
|
|
|
// Still "live": Ensure header's aren't overridden if already set (using purposefully different values). |
|
34
|
|
|
$headers = array( |
|
35
|
|
|
'Vary' => '*', |
|
36
|
|
|
'Pragma' => 'no-cache', |
|
37
|
|
|
'Cache-Control' => 'max-age=0, no-cache, no-store', |
|
38
|
|
|
); |
|
39
|
|
|
$response = new SS_HTTPResponse($body, 200); |
|
40
|
|
|
foreach($headers as $name => $value) { |
|
41
|
|
|
$response->addHeader($name, $value); |
|
42
|
|
|
} |
|
43
|
|
|
HTTP::add_cache_headers($response); |
|
44
|
|
|
foreach($headers as $name => $value) { |
|
45
|
|
|
$this->assertEquals($value, $response->getHeader($name)); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
public function testConfigVary() { |
|
51
|
|
|
$body = "<html><head></head><body><h1>Mysite</h1></body></html>"; |
|
52
|
|
|
$response = new SS_HTTPResponse($body, 200); |
|
53
|
|
|
Config::inst()->update('Director', 'environment_type', 'live'); |
|
54
|
|
|
HTTP::set_cache_age(30); |
|
55
|
|
|
HTTP::add_cache_headers($response); |
|
56
|
|
|
|
|
57
|
|
|
$v = $response->getHeader('Vary'); |
|
58
|
|
|
$this->assertNotEmpty($v); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertContains("Cookie", $v); |
|
61
|
|
|
$this->assertContains("X-Forwarded-Protocol", $v); |
|
62
|
|
|
$this->assertContains("User-Agent", $v); |
|
63
|
|
|
$this->assertContains("Accept", $v); |
|
64
|
|
|
|
|
65
|
|
|
Config::inst()->update('HTTP', 'vary', ''); |
|
66
|
|
|
|
|
67
|
|
|
$response = new SS_HTTPResponse($body, 200); |
|
68
|
|
|
HTTP::add_cache_headers($response); |
|
69
|
|
|
|
|
70
|
|
|
$v = $response->getHeader('Vary'); |
|
71
|
|
|
$this->assertEmpty($v); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Tests {@link HTTP::getLinksIn()} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function testGetLinksIn() { |
|
78
|
|
|
$content = ' |
|
79
|
|
|
<h2><a href="/">My Cool Site</a></h2> |
|
80
|
|
|
|
|
81
|
|
|
<p> |
|
82
|
|
|
A boy went <a href="home/">home</a> to see his <span><a href="mother/">mother</a></span>. This |
|
83
|
|
|
involved a short <a href="$Journey">journey</a>, as well as some <a href="space travel">space travel</a> |
|
84
|
|
|
and <a href=unquoted>unquoted</a> events, as well as a <a href=\'single quote\'>single quote</a> from |
|
85
|
|
|
his <a href="/father">father</a>. |
|
86
|
|
|
</p> |
|
87
|
|
|
|
|
88
|
|
|
<p> |
|
89
|
|
|
There were also some elements with extra <a class=attribute href=\'attributes\'>attributes</a> which |
|
90
|
|
|
played a part in his <a href=journey"extra id="JourneyLink">journey</a>. HE ALSO DISCOVERED THE |
|
91
|
|
|
<A HREF="CAPS LOCK">KEY</a>. Later he got his <a href="quotes \'mixed\' up">mixed up</a>. |
|
92
|
|
|
</p> |
|
93
|
|
|
'; |
|
94
|
|
|
|
|
95
|
|
|
$expected = array ( |
|
96
|
|
|
'/', 'home/', 'mother/', '$Journey', 'space travel', 'unquoted', 'single quote', '/father', 'attributes', |
|
97
|
|
|
'journey', 'CAPS LOCK', 'quotes \'mixed\' up' |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$result = HTTP::getLinksIn($content); |
|
101
|
|
|
|
|
102
|
|
|
// Results don't neccesarily come out in the order they are in the $content param. |
|
103
|
|
|
sort($result); |
|
104
|
|
|
sort($expected); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertTrue(is_array($result)); |
|
107
|
|
|
$this->assertEquals($expected, $result, 'Test that all links within the content are found.'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Tests {@link HTTP::setGetVar()} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function testSetGetVar() { |
|
|
|
|
|
|
114
|
|
|
// Hackery to work around volatile URL formats in test invocation, |
|
115
|
|
|
// and the inability of Director::absoluteBaseURL() to produce consistent URLs. |
|
116
|
|
|
$origURI = $_SERVER['REQUEST_URI']; |
|
117
|
|
|
$_SERVER['REQUEST_URI'] = 'relative/url/'; |
|
118
|
|
|
$this->assertContains( |
|
119
|
|
|
'relative/url/?foo=bar', |
|
120
|
|
|
HTTP::setGetVar('foo', 'bar'), |
|
121
|
|
|
'Omitting a URL falls back to current URL' |
|
122
|
|
|
); |
|
123
|
|
|
$_SERVER['REQUEST_URI'] = $origURI; |
|
124
|
|
|
|
|
125
|
|
|
$this->assertEquals( |
|
126
|
|
|
'relative/url?foo=bar', |
|
127
|
|
|
HTTP::setGetVar('foo', 'bar', 'relative/url'), |
|
128
|
|
|
'Relative URL without existing query params'); |
|
129
|
|
|
|
|
130
|
|
|
$this->assertEquals( |
|
131
|
|
|
'relative/url?baz=buz&foo=bar', |
|
132
|
|
|
HTTP::setGetVar('foo', 'bar', '/relative/url?baz=buz'), |
|
133
|
|
|
'Relative URL with existing query params, and new added key' |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
|
|
$this->assertEquals( |
|
137
|
|
|
'http://test.com/?foo=new&buz=baz', |
|
138
|
|
|
HTTP::setGetVar('foo', 'new', 'http://test.com/?foo=old&buz=baz'), |
|
139
|
|
|
'Absolute URL without path and multipe existing query params, overwriting an existing parameter' |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
$this->assertContains( |
|
143
|
|
|
'http://test.com/?foo=new', |
|
144
|
|
|
HTTP::setGetVar('foo', 'new', 'http://test.com/?foo=&foo=old'), |
|
145
|
|
|
'Absolute URL and empty query param' |
|
146
|
|
|
); |
|
147
|
|
|
// http_build_query() escapes angular brackets, they should be correctly urldecoded by the browser client |
|
148
|
|
|
$this->assertEquals( |
|
149
|
|
|
'http://test.com/?foo%5Btest%5D=one&foo%5Btest%5D=two', |
|
150
|
|
|
HTTP::setGetVar('foo[test]', 'two', 'http://test.com/?foo[test]=one'), |
|
151
|
|
|
'Absolute URL and PHP array query string notation' |
|
152
|
|
|
); |
|
153
|
|
|
|
|
154
|
|
|
$urls = array( |
|
155
|
|
|
'http://www.test.com:8080', |
|
156
|
|
|
'http://test.com:3000/', |
|
157
|
|
|
'http://test.com:3030/baz/', |
|
158
|
|
|
'http://baz:[email protected]', |
|
159
|
|
|
'http://[email protected]/', |
|
160
|
|
|
'http://baz:[email protected]:8080', |
|
161
|
|
|
'http://[email protected]:8080' |
|
162
|
|
|
); |
|
163
|
|
|
|
|
164
|
|
|
foreach($urls as $testURL) { |
|
165
|
|
|
$this->assertEquals( |
|
166
|
|
|
$testURL .'?foo=bar', |
|
167
|
|
|
HTTP::setGetVar('foo', 'bar', $testURL), |
|
168
|
|
|
'Absolute URL and Port Number' |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Test that the the get_mime_type() works correctly |
|
175
|
|
|
* |
|
176
|
|
|
*/ |
|
177
|
|
|
public function testGetMimeType() { |
|
178
|
|
|
$this->assertEquals('text/plain', HTTP::get_mime_type(FRAMEWORK_DIR.'/tests/control/files/file.csv')); |
|
179
|
|
|
$this->assertEquals('image/gif', HTTP::get_mime_type(FRAMEWORK_DIR.'/tests/control/files/file.gif')); |
|
180
|
|
|
$this->assertEquals('text/html', HTTP::get_mime_type(FRAMEWORK_DIR.'/tests/control/files/file.html')); |
|
181
|
|
|
$this->assertEquals('image/jpeg', HTTP::get_mime_type(FRAMEWORK_DIR.'/tests/control/files/file.jpg')); |
|
182
|
|
|
$this->assertEquals('image/png', HTTP::get_mime_type(FRAMEWORK_DIR.'/tests/control/files/file.png')); |
|
183
|
|
|
$this->assertEquals('image/vnd.adobe.photoshop', |
|
184
|
|
|
HTTP::get_mime_type(FRAMEWORK_DIR.'/tests/control/files/file.psd')); |
|
185
|
|
|
$this->assertEquals('audio/x-wav', HTTP::get_mime_type(FRAMEWORK_DIR.'/tests/control/files/file.wav')); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Test that absoluteURLs correctly transforms urls within CSS to absolute |
|
190
|
|
|
*/ |
|
191
|
|
|
public function testAbsoluteURLsCSS() { |
|
192
|
|
|
$this->withBaseURL('http://www.silverstripe.org/', function($test){ |
|
193
|
|
|
|
|
194
|
|
|
// background-image |
|
195
|
|
|
// Note that using /./ in urls is absolutely acceptable |
|
196
|
|
|
$test->assertEquals( |
|
197
|
|
|
'<div style="background-image: url(\'http://www.silverstripe.org/./images/mybackground.gif\');">'. |
|
198
|
|
|
'Content</div>', |
|
199
|
|
|
HTTP::absoluteURLs('<div style="background-image: url(\'./images/mybackground.gif\');">Content</div>') |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
// background |
|
203
|
|
|
$test->assertEquals( |
|
204
|
|
|
'<div style="background: url(\'http://www.silverstripe.org/images/mybackground.gif\');">Content</div>', |
|
205
|
|
|
HTTP::absoluteURLs('<div style="background: url(\'images/mybackground.gif\');">Content</div>') |
|
206
|
|
|
); |
|
207
|
|
|
|
|
208
|
|
|
// list-style-image |
|
209
|
|
|
$test->assertEquals( |
|
210
|
|
|
'<div style=\'background: url(http://www.silverstripe.org/list.png);\'>Content</div>', |
|
211
|
|
|
HTTP::absoluteURLs('<div style=\'background: url(list.png);\'>Content</div>') |
|
212
|
|
|
); |
|
213
|
|
|
|
|
214
|
|
|
// list-style |
|
215
|
|
|
$test->assertEquals( |
|
216
|
|
|
'<div style=\'background: url("http://www.silverstripe.org/./assets/list.png");\'>Content</div>', |
|
217
|
|
|
HTTP::absoluteURLs('<div style=\'background: url("./assets/list.png");\'>Content</div>') |
|
218
|
|
|
); |
|
219
|
|
|
}); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Test that absoluteURLs correctly transforms urls within html attributes to absolute |
|
224
|
|
|
*/ |
|
225
|
|
|
public function testAbsoluteURLsAttributes() { |
|
226
|
|
|
$this->withBaseURL('http://www.silverstripe.org/', function($test){ |
|
227
|
|
|
//empty links |
|
228
|
|
|
$test->assertEquals( |
|
229
|
|
|
'<a href="http://www.silverstripe.org/">test</a>', |
|
230
|
|
|
HTTP::absoluteURLs('<a href="">test</a>') |
|
231
|
|
|
); |
|
232
|
|
|
|
|
233
|
|
|
$test->assertEquals( |
|
234
|
|
|
'<a href="http://www.silverstripe.org/">test</a>', |
|
235
|
|
|
HTTP::absoluteURLs('<a href="/">test</a>') |
|
236
|
|
|
); |
|
237
|
|
|
|
|
238
|
|
|
//relative |
|
239
|
|
|
$test->assertEquals( |
|
240
|
|
|
'<a href="http://www.silverstripe.org/">test</a>', |
|
241
|
|
|
HTTP::absoluteURLs('<a href="./">test</a>') |
|
242
|
|
|
); |
|
243
|
|
|
$test->assertEquals( |
|
244
|
|
|
'<a href="http://www.silverstripe.org/">test</a>', |
|
245
|
|
|
HTTP::absoluteURLs('<a href=".">test</a>') |
|
246
|
|
|
); |
|
247
|
|
|
|
|
248
|
|
|
// links |
|
249
|
|
|
$test->assertEquals( |
|
250
|
|
|
'<a href=\'http://www.silverstripe.org/blog/\'>SS Blog</a>', |
|
251
|
|
|
HTTP::absoluteURLs('<a href=\'/blog/\'>SS Blog</a>') |
|
252
|
|
|
); |
|
253
|
|
|
|
|
254
|
|
|
// background |
|
255
|
|
|
// Note that using /./ in urls is absolutely acceptable |
|
256
|
|
|
$test->assertEquals( |
|
257
|
|
|
'<div background="http://www.silverstripe.org/./themes/silverstripe/images/nav-bg-repeat-2.png">'. |
|
258
|
|
|
'SS Blog</div>', |
|
259
|
|
|
HTTP::absoluteURLs('<div background="./themes/silverstripe/images/nav-bg-repeat-2.png">SS Blog</div>') |
|
260
|
|
|
); |
|
261
|
|
|
|
|
262
|
|
|
//check dot segments |
|
263
|
|
|
// Assumption: dots are not removed |
|
264
|
|
|
//if they were, the url should be: http://www.silverstripe.org/abc |
|
265
|
|
|
$test->assertEquals( |
|
266
|
|
|
'<a href="http://www.silverstripe.org/test/page/../../abc">Test</a>', |
|
267
|
|
|
HTTP::absoluteURLs('<a href="test/page/../../abc">Test</a>') |
|
268
|
|
|
); |
|
269
|
|
|
|
|
270
|
|
|
// image |
|
271
|
|
|
$test->assertEquals( |
|
272
|
|
|
'<img src=\'http://www.silverstripe.org/themes/silverstripe/images/logo-org.png\' />', |
|
273
|
|
|
HTTP::absoluteURLs('<img src=\'themes/silverstripe/images/logo-org.png\' />') |
|
274
|
|
|
); |
|
275
|
|
|
|
|
276
|
|
|
// link |
|
277
|
|
|
$test->assertEquals( |
|
278
|
|
|
'<link href=http://www.silverstripe.org/base.css />', |
|
279
|
|
|
HTTP::absoluteURLs('<link href=base.css />') |
|
280
|
|
|
); |
|
281
|
|
|
}); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Make sure URI schemes are not rewritten |
|
286
|
|
|
*/ |
|
287
|
|
|
public function testURISchemes() { |
|
288
|
|
|
$this->withBaseURL('http://www.silverstripe.org/', function($test){ |
|
289
|
|
|
|
|
290
|
|
|
// mailto |
|
291
|
|
|
$test->assertEquals( |
|
292
|
|
|
'<a href=\'mailto:[email protected]\'>Email Us</a>', |
|
293
|
|
|
HTTP::absoluteURLs('<a href=\'mailto:[email protected]\'>Email Us</a>'), |
|
294
|
|
|
'Email links are not rewritten' |
|
295
|
|
|
); |
|
296
|
|
|
|
|
297
|
|
|
// data uri |
|
298
|
|
|
$test->assertEquals( |
|
299
|
|
|
'<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38'. |
|
300
|
|
|
'GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />', |
|
301
|
|
|
HTTP::absoluteURLs('<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAH'. |
|
302
|
|
|
'ElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />'), |
|
303
|
|
|
'Data URI links are not rewritten' |
|
304
|
|
|
); |
|
305
|
|
|
|
|
306
|
|
|
// call |
|
307
|
|
|
$test->assertEquals( |
|
308
|
|
|
'<a href="callto:12345678" />', |
|
309
|
|
|
HTTP::absoluteURLs('<a href="callto:12345678" />'), |
|
310
|
|
|
'Call to links are not rewritten' |
|
311
|
|
|
); |
|
312
|
|
|
}); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
} |
|
316
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: