1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Middleware\TrustedProxyMiddleware; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
8
|
|
|
use ReflectionMethod; |
9
|
|
|
|
10
|
|
|
class HTTPRequestTest extends SapphireTest |
11
|
|
|
{ |
12
|
|
|
protected static $fixture_file = null; |
13
|
|
|
|
14
|
|
|
public function testMatch() |
15
|
|
|
{ |
16
|
|
|
$request = new HTTPRequest("GET", "admin/crm/add"); |
17
|
|
|
|
18
|
|
|
/* When a rule matches, but has no variables, array("_matched" => true) is returned. */ |
19
|
|
|
$this->assertEquals(array("_matched" => true), $request->match('admin/crm', true)); |
20
|
|
|
|
21
|
|
|
/* Becasue we shifted admin/crm off the stack, just "add" should be remaining */ |
22
|
|
|
$this->assertEquals("add", $request->remaining()); |
23
|
|
|
|
24
|
|
|
$this->assertEquals(array("_matched" => true), $request->match('add', true)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testHttpMethodOverrides() |
28
|
|
|
{ |
29
|
|
|
$request = new HTTPRequest( |
30
|
|
|
'GET', |
31
|
|
|
'admin/crm' |
32
|
|
|
); |
33
|
|
|
$this->assertTrue( |
34
|
|
|
$request->isGET(), |
35
|
|
|
'GET with no method override' |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$request = new HTTPRequest( |
39
|
|
|
'POST', |
40
|
|
|
'admin/crm' |
41
|
|
|
); |
42
|
|
|
$this->assertTrue( |
43
|
|
|
$request->isPOST(), |
44
|
|
|
'POST with no method override' |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
$request = new HTTPRequest( |
48
|
|
|
'GET', |
49
|
|
|
'admin/crm', |
50
|
|
|
array('_method' => 'DELETE') |
51
|
|
|
); |
52
|
|
|
$this->assertTrue( |
53
|
|
|
$request->isGET(), |
54
|
|
|
'GET with invalid POST method override' |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$request = new HTTPRequest( |
58
|
|
|
'POST', |
59
|
|
|
'admin/crm', |
60
|
|
|
array(), |
61
|
|
|
array('_method' => 'DELETE') |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$this->assertTrue( |
65
|
|
|
$request->isPOST(), |
66
|
|
|
'_method override is no longer honored' |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$this->assertFalse( |
70
|
|
|
$request->isDELETE(), |
71
|
|
|
'DELETE _method override is not honored' |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$request = new HTTPRequest( |
75
|
|
|
'POST', |
76
|
|
|
'admin/crm', |
77
|
|
|
array(), |
78
|
|
|
array('_method' => 'put') |
79
|
|
|
); |
80
|
|
|
$this->assertFalse( |
81
|
|
|
$request->isPUT(), |
82
|
|
|
'PUT _method override is not honored' |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$request = new HTTPRequest( |
86
|
|
|
'POST', |
87
|
|
|
'admin/crm', |
88
|
|
|
array(), |
89
|
|
|
array('_method' => 'head') |
90
|
|
|
); |
91
|
|
|
$this->assertFalse( |
92
|
|
|
$request->isHEAD(), |
93
|
|
|
'HEAD _method override is not honored' |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function detectMethodDataProvider() |
98
|
|
|
{ |
99
|
|
|
return [ |
100
|
|
|
'Plain POST request' => ['POST', [], 'POST'], |
101
|
|
|
'Plain GET request' => ['GET', [], 'GET'], |
102
|
|
|
'Plain DELETE request' => ['DELETE', [], 'DELETE'], |
103
|
|
|
'Plain PUT request' => ['PUT', [], 'PUT'], |
104
|
|
|
'Plain HEAD request' => ['HEAD', [], 'HEAD'], |
105
|
|
|
|
106
|
|
|
'Request with GET method override' => ['POST', ['_method' => 'GET'], 'GET'], |
107
|
|
|
'Request with HEAD method override' => ['POST', ['_method' => 'HEAD'], 'HEAD'], |
108
|
|
|
'Request with DELETE method override' => ['POST', ['_method' => 'DELETE'], 'DELETE'], |
109
|
|
|
'Request with PUT method override' => ['POST', ['_method' => 'PUT'], 'PUT'], |
110
|
|
|
'Request with POST method override' => ['POST', ['_method' => 'POST'], 'POST'], |
111
|
|
|
|
112
|
|
|
'Request with mixed case method override' => ['POST', ['_method' => 'gEt'], 'GET'] |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @dataProvider detectMethodDataProvider |
118
|
|
|
*/ |
119
|
|
|
public function testDetectMethod($realMethod, $post, $expected) |
120
|
|
|
{ |
121
|
|
|
$actual = HTTPRequest::detect_method($realMethod, $post); |
|
|
|
|
122
|
|
|
$this->assertEquals($expected, $actual); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @expectedException PHPUnit_Framework_Error |
127
|
|
|
*/ |
128
|
|
|
public function testBadDetectMethod() |
129
|
|
|
{ |
130
|
|
|
HTTPRequest::detect_method('POST', ['_method' => 'Boom']); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function setHttpMethodDataProvider() |
134
|
|
|
{ |
135
|
|
|
return [ |
136
|
|
|
'POST request' => ['POST','POST'], |
137
|
|
|
'GET request' => ['GET', 'GET'], |
138
|
|
|
'DELETE request' => ['DELETE', 'DELETE'], |
139
|
|
|
'PUT request' => ['PUT', 'PUT'], |
140
|
|
|
'HEAD request' => ['HEAD', 'HEAD'], |
141
|
|
|
'Mixed case POST' => ['gEt', 'GET'], |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @dataProvider setHttpMethodDataProvider |
147
|
|
|
*/ |
148
|
|
|
public function testSetHttpMethod($method, $expected) |
149
|
|
|
{ |
150
|
|
|
$request = new HTTPRequest('GET', '/hello'); |
151
|
|
|
$returnedRequest = $request->setHttpMethod($method); |
152
|
|
|
$this->assertEquals($expected, $request->httpMethod()); |
153
|
|
|
$this->assertEquals($request, $returnedRequest); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @expectedException PHPUnit_Framework_Error |
158
|
|
|
*/ |
159
|
|
|
public function testBadSetHttpMethod() |
160
|
|
|
{ |
161
|
|
|
$request = new HTTPRequest('GET', '/hello'); |
162
|
|
|
$request->setHttpMethod('boom'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testRequestVars() |
166
|
|
|
{ |
167
|
|
|
$getVars = array( |
168
|
|
|
'first' => 'a', |
169
|
|
|
'second' => 'b', |
170
|
|
|
); |
171
|
|
|
$postVars = array( |
172
|
|
|
'third' => 'c', |
173
|
|
|
'fourth' => 'd', |
174
|
|
|
); |
175
|
|
|
$requestVars = array( |
176
|
|
|
'first' => 'a', |
177
|
|
|
'second' => 'b', |
178
|
|
|
'third' => 'c', |
179
|
|
|
'fourth' => 'd', |
180
|
|
|
); |
181
|
|
|
$request = new HTTPRequest( |
182
|
|
|
'POST', |
183
|
|
|
'admin/crm', |
184
|
|
|
$getVars, |
185
|
|
|
$postVars |
186
|
|
|
); |
187
|
|
|
$this->assertEquals( |
188
|
|
|
$requestVars, |
189
|
|
|
$request->requestVars(), |
190
|
|
|
'GET parameters should supplement POST parameters' |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
$getVars = array( |
194
|
|
|
'first' => 'a', |
195
|
|
|
'second' => 'b', |
196
|
|
|
); |
197
|
|
|
$postVars = array( |
198
|
|
|
'first' => 'c', |
199
|
|
|
'third' => 'd', |
200
|
|
|
); |
201
|
|
|
$requestVars = array( |
202
|
|
|
'first' => 'c', |
203
|
|
|
'second' => 'b', |
204
|
|
|
'third' => 'd', |
205
|
|
|
); |
206
|
|
|
$request = new HTTPRequest( |
207
|
|
|
'POST', |
208
|
|
|
'admin/crm', |
209
|
|
|
$getVars, |
210
|
|
|
$postVars |
211
|
|
|
); |
212
|
|
|
$this->assertEquals( |
213
|
|
|
$requestVars, |
214
|
|
|
$request->requestVars(), |
215
|
|
|
'POST parameters should override GET parameters' |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$getVars = array( |
219
|
|
|
'first' => array( |
220
|
|
|
'first' => 'a', |
221
|
|
|
), |
222
|
|
|
'second' => array( |
223
|
|
|
'second' => 'b', |
224
|
|
|
), |
225
|
|
|
); |
226
|
|
|
$postVars = array( |
227
|
|
|
'first' => array( |
228
|
|
|
'first' => 'c', |
229
|
|
|
), |
230
|
|
|
'third' => array( |
231
|
|
|
'third' => 'd', |
232
|
|
|
), |
233
|
|
|
); |
234
|
|
|
$requestVars = array( |
235
|
|
|
'first' => array( |
236
|
|
|
'first' => 'c', |
237
|
|
|
), |
238
|
|
|
'second' => array( |
239
|
|
|
'second' => 'b', |
240
|
|
|
), |
241
|
|
|
'third' => array( |
242
|
|
|
'third' => 'd', |
243
|
|
|
), |
244
|
|
|
); |
245
|
|
|
$request = new HTTPRequest( |
246
|
|
|
'POST', |
247
|
|
|
'admin/crm', |
248
|
|
|
$getVars, |
249
|
|
|
$postVars |
250
|
|
|
); |
251
|
|
|
$this->assertEquals( |
252
|
|
|
$requestVars, |
253
|
|
|
$request->requestVars(), |
254
|
|
|
'Nested POST parameters should override GET parameters' |
255
|
|
|
); |
256
|
|
|
|
257
|
|
|
$getVars = array( |
258
|
|
|
'first' => array( |
259
|
|
|
'first' => 'a', |
260
|
|
|
), |
261
|
|
|
'second' => array( |
262
|
|
|
'second' => 'b', |
263
|
|
|
), |
264
|
|
|
); |
265
|
|
|
$postVars = array( |
266
|
|
|
'first' => array( |
267
|
|
|
'second' => 'c', |
268
|
|
|
), |
269
|
|
|
'third' => array( |
270
|
|
|
'third' => 'd', |
271
|
|
|
), |
272
|
|
|
); |
273
|
|
|
$requestVars = array( |
274
|
|
|
'first' => array( |
275
|
|
|
'first' => 'a', |
276
|
|
|
'second' => 'c', |
277
|
|
|
), |
278
|
|
|
'second' => array( |
279
|
|
|
'second' => 'b', |
280
|
|
|
), |
281
|
|
|
'third' => array( |
282
|
|
|
'third' => 'd', |
283
|
|
|
), |
284
|
|
|
); |
285
|
|
|
$request = new HTTPRequest( |
286
|
|
|
'POST', |
287
|
|
|
'admin/crm', |
288
|
|
|
$getVars, |
289
|
|
|
$postVars |
290
|
|
|
); |
291
|
|
|
$this->assertEquals( |
292
|
|
|
$requestVars, |
293
|
|
|
$request->requestVars(), |
294
|
|
|
'Nested GET parameters should supplement POST parameters' |
295
|
|
|
); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
public function testIsAjax() |
299
|
|
|
{ |
300
|
|
|
$req = new HTTPRequest('GET', '/', array('ajax' => 0)); |
301
|
|
|
$this->assertFalse($req->isAjax()); |
302
|
|
|
|
303
|
|
|
$req = new HTTPRequest('GET', '/', array('ajax' => 1)); |
304
|
|
|
$this->assertTrue($req->isAjax()); |
305
|
|
|
|
306
|
|
|
$req = new HTTPRequest('GET', '/'); |
307
|
|
|
$req->addHeader('X-Requested-With', 'XMLHttpRequest'); |
308
|
|
|
$this->assertTrue($req->isAjax()); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function testGetURL() |
312
|
|
|
{ |
313
|
|
|
$req = new HTTPRequest('GET', '/'); |
314
|
|
|
$this->assertEquals('', $req->getURL()); |
315
|
|
|
|
316
|
|
|
$req = new HTTPRequest('GET', '/assets/somefile.gif'); |
317
|
|
|
$this->assertEquals('assets/somefile.gif', $req->getURL()); |
318
|
|
|
|
319
|
|
|
$req = new HTTPRequest('GET', '/home?test=1'); |
320
|
|
|
$this->assertEquals('home?test=1', $req->getURL(true)); |
321
|
|
|
$this->assertEquals('home', $req->getURL()); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function testSetIPFromHeaderValue() |
325
|
|
|
{ |
326
|
|
|
$req = new TrustedProxyMiddleware(); |
327
|
|
|
$reflectionMethod = new ReflectionMethod($req, 'getIPFromHeaderValue'); |
328
|
|
|
$reflectionMethod->setAccessible(true); |
329
|
|
|
|
330
|
|
|
$headers = array( |
331
|
|
|
'80.79.208.21, 149.126.76.1, 10.51.0.68' => '80.79.208.21', |
332
|
|
|
'52.19.19.103, 10.51.0.49' => '52.19.19.103', |
333
|
|
|
'10.51.0.49, 52.19.19.103' => '52.19.19.103', |
334
|
|
|
'10.51.0.49' => '10.51.0.49', |
335
|
|
|
'127.0.0.1, 10.51.0.49' => '127.0.0.1', |
336
|
|
|
); |
337
|
|
|
|
338
|
|
|
foreach ($headers as $header => $ip) { |
339
|
|
|
$this->assertEquals($ip, $reflectionMethod->invoke($req, $header)); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.