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
|
|
|
$this->assertTrue( |
64
|
|
|
$request->isDELETE(), |
65
|
|
|
'POST with valid method override to DELETE' |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$request = new HTTPRequest( |
69
|
|
|
'POST', |
70
|
|
|
'admin/crm', |
71
|
|
|
array(), |
72
|
|
|
array('_method' => 'put') |
73
|
|
|
); |
74
|
|
|
$this->assertTrue( |
75
|
|
|
$request->isPUT(), |
76
|
|
|
'POST with valid method override to PUT' |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$request = new HTTPRequest( |
80
|
|
|
'POST', |
81
|
|
|
'admin/crm', |
82
|
|
|
array(), |
83
|
|
|
array('_method' => 'head') |
84
|
|
|
); |
85
|
|
|
$this->assertTrue( |
86
|
|
|
$request->isHEAD(), |
87
|
|
|
'POST with valid method override to HEAD ' |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$request = new HTTPRequest( |
91
|
|
|
'POST', |
92
|
|
|
'admin/crm', |
93
|
|
|
array(), |
94
|
|
|
array('_method' => 'head') |
95
|
|
|
); |
96
|
|
|
$this->assertTrue( |
97
|
|
|
$request->isHEAD(), |
98
|
|
|
'POST with valid method override to HEAD' |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$request = new HTTPRequest( |
102
|
|
|
'POST', |
103
|
|
|
'admin/crm', |
104
|
|
|
array('_method' => 'head') |
105
|
|
|
); |
106
|
|
|
$this->assertTrue( |
107
|
|
|
$request->isPOST(), |
108
|
|
|
'POST with invalid method override by GET parameters to HEAD' |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testRequestVars() |
113
|
|
|
{ |
114
|
|
|
$getVars = array( |
115
|
|
|
'first' => 'a', |
116
|
|
|
'second' => 'b', |
117
|
|
|
); |
118
|
|
|
$postVars = array( |
119
|
|
|
'third' => 'c', |
120
|
|
|
'fourth' => 'd', |
121
|
|
|
); |
122
|
|
|
$requestVars = array( |
123
|
|
|
'first' => 'a', |
124
|
|
|
'second' => 'b', |
125
|
|
|
'third' => 'c', |
126
|
|
|
'fourth' => 'd', |
127
|
|
|
); |
128
|
|
|
$request = new HTTPRequest( |
129
|
|
|
'POST', |
130
|
|
|
'admin/crm', |
131
|
|
|
$getVars, |
132
|
|
|
$postVars |
133
|
|
|
); |
134
|
|
|
$this->assertEquals( |
135
|
|
|
$requestVars, |
136
|
|
|
$request->requestVars(), |
137
|
|
|
'GET parameters should supplement POST parameters' |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$getVars = array( |
141
|
|
|
'first' => 'a', |
142
|
|
|
'second' => 'b', |
143
|
|
|
); |
144
|
|
|
$postVars = array( |
145
|
|
|
'first' => 'c', |
146
|
|
|
'third' => 'd', |
147
|
|
|
); |
148
|
|
|
$requestVars = array( |
149
|
|
|
'first' => 'c', |
150
|
|
|
'second' => 'b', |
151
|
|
|
'third' => 'd', |
152
|
|
|
); |
153
|
|
|
$request = new HTTPRequest( |
154
|
|
|
'POST', |
155
|
|
|
'admin/crm', |
156
|
|
|
$getVars, |
157
|
|
|
$postVars |
158
|
|
|
); |
159
|
|
|
$this->assertEquals( |
160
|
|
|
$requestVars, |
161
|
|
|
$request->requestVars(), |
162
|
|
|
'POST parameters should override GET parameters' |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
$getVars = array( |
166
|
|
|
'first' => array( |
167
|
|
|
'first' => 'a', |
168
|
|
|
), |
169
|
|
|
'second' => array( |
170
|
|
|
'second' => 'b', |
171
|
|
|
), |
172
|
|
|
); |
173
|
|
|
$postVars = array( |
174
|
|
|
'first' => array( |
175
|
|
|
'first' => 'c', |
176
|
|
|
), |
177
|
|
|
'third' => array( |
178
|
|
|
'third' => 'd', |
179
|
|
|
), |
180
|
|
|
); |
181
|
|
|
$requestVars = array( |
182
|
|
|
'first' => array( |
183
|
|
|
'first' => 'c', |
184
|
|
|
), |
185
|
|
|
'second' => array( |
186
|
|
|
'second' => 'b', |
187
|
|
|
), |
188
|
|
|
'third' => array( |
189
|
|
|
'third' => 'd', |
190
|
|
|
), |
191
|
|
|
); |
192
|
|
|
$request = new HTTPRequest( |
193
|
|
|
'POST', |
194
|
|
|
'admin/crm', |
195
|
|
|
$getVars, |
196
|
|
|
$postVars |
197
|
|
|
); |
198
|
|
|
$this->assertEquals( |
199
|
|
|
$requestVars, |
200
|
|
|
$request->requestVars(), |
201
|
|
|
'Nested POST parameters should override GET parameters' |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
$getVars = array( |
205
|
|
|
'first' => array( |
206
|
|
|
'first' => 'a', |
207
|
|
|
), |
208
|
|
|
'second' => array( |
209
|
|
|
'second' => 'b', |
210
|
|
|
), |
211
|
|
|
); |
212
|
|
|
$postVars = array( |
213
|
|
|
'first' => array( |
214
|
|
|
'second' => 'c', |
215
|
|
|
), |
216
|
|
|
'third' => array( |
217
|
|
|
'third' => 'd', |
218
|
|
|
), |
219
|
|
|
); |
220
|
|
|
$requestVars = array( |
221
|
|
|
'first' => array( |
222
|
|
|
'first' => 'a', |
223
|
|
|
'second' => 'c', |
224
|
|
|
), |
225
|
|
|
'second' => array( |
226
|
|
|
'second' => 'b', |
227
|
|
|
), |
228
|
|
|
'third' => array( |
229
|
|
|
'third' => 'd', |
230
|
|
|
), |
231
|
|
|
); |
232
|
|
|
$request = new HTTPRequest( |
233
|
|
|
'POST', |
234
|
|
|
'admin/crm', |
235
|
|
|
$getVars, |
236
|
|
|
$postVars |
237
|
|
|
); |
238
|
|
|
$this->assertEquals( |
239
|
|
|
$requestVars, |
240
|
|
|
$request->requestVars(), |
241
|
|
|
'Nested GET parameters should supplement POST parameters' |
242
|
|
|
); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function testIsAjax() |
246
|
|
|
{ |
247
|
|
|
$req = new HTTPRequest('GET', '/', array('ajax' => 0)); |
248
|
|
|
$this->assertFalse($req->isAjax()); |
249
|
|
|
|
250
|
|
|
$req = new HTTPRequest('GET', '/', array('ajax' => 1)); |
251
|
|
|
$this->assertTrue($req->isAjax()); |
252
|
|
|
|
253
|
|
|
$req = new HTTPRequest('GET', '/'); |
254
|
|
|
$req->addHeader('X-Requested-With', 'XMLHttpRequest'); |
255
|
|
|
$this->assertTrue($req->isAjax()); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testGetURL() |
259
|
|
|
{ |
260
|
|
|
$req = new HTTPRequest('GET', '/'); |
261
|
|
|
$this->assertEquals('', $req->getURL()); |
262
|
|
|
|
263
|
|
|
$req = new HTTPRequest('GET', '/assets/somefile.gif'); |
264
|
|
|
$this->assertEquals('assets/somefile.gif', $req->getURL()); |
265
|
|
|
|
266
|
|
|
$req = new HTTPRequest('GET', '/home?test=1'); |
267
|
|
|
$this->assertEquals('home?test=1', $req->getURL(true)); |
268
|
|
|
$this->assertEquals('home', $req->getURL()); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function testSetIPFromHeaderValue() |
272
|
|
|
{ |
273
|
|
|
$req = new TrustedProxyMiddleware(); |
274
|
|
|
$reflectionMethod = new ReflectionMethod($req, 'getIPFromHeaderValue'); |
275
|
|
|
$reflectionMethod->setAccessible(true); |
276
|
|
|
|
277
|
|
|
$headers = array( |
278
|
|
|
'80.79.208.21, 149.126.76.1, 10.51.0.68' => '80.79.208.21', |
279
|
|
|
'52.19.19.103, 10.51.0.49' => '52.19.19.103', |
280
|
|
|
'10.51.0.49, 52.19.19.103' => '52.19.19.103', |
281
|
|
|
'10.51.0.49' => '10.51.0.49', |
282
|
|
|
'127.0.0.1, 10.51.0.49' => '127.0.0.1', |
283
|
|
|
); |
284
|
|
|
|
285
|
|
|
foreach ($headers as $header => $ip) { |
286
|
|
|
$this->assertEquals($ip, $reflectionMethod->invoke($req, $header)); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|