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