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