1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
<?php
Prado::using('System.Web.THttpRequest');
Prado::using('System.Security.TSecurityManager');
/**
* @package System.Web
*/
class THttpRequestTest extends PHPUnit_Framework_TestCase {
public static $app = null;
public function setUp() {
// Fake environment variables
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['SERVER_NAME'] = 'localhost';
$_SERVER['SERVER_PORT'] = '80';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/demos/personal/index.php?page=Links';
$_SERVER['SCRIPT_NAME'] = '/demos/personal/index.php';
$_SERVER['PHP_SELF'] = '/demos/personal/index.php';
$_SERVER['QUERY_STRING'] = 'page=Links';
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
$_SERVER['PATH_INFO'] = __FILE__;
$_SERVER['HTTP_REFERER'] = 'https://github.com/pradosoft/prado';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3';
$_SERVER['REMOTE_HOST'] = 'localhost';
$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr,en-us;q=0.8,fr-fr;q=0.5,en;q=0.3';
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip,deflate';
$_SERVER['HTTP_ACCEPT_CHARSET'] = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7';
$_COOKIE['phpsessid']='0123456789abcdef';
$_FILES['userfile']=array ('name' => 'test.jpg','type' => 'image/jpg', 'size' => 10240, 'tmp_name' => 'tmpXXAZECZ', 'error'=>0);
if(self::$app === null) {
self::$app = new TApplication(dirname(__FILE__).'/app');
}
}
public function testInit() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals('', $request->getUrlManager());
// Try with unsetted REQUEST_URL & PATH_INFO
unset($_SERVER['REQUEST_URI']);
unset($_SERVER['PATH_INFO']);
$request = new THttpRequest();
$request->init(null);
self::assertEquals('', $request->getUrlManager());
}
public function testStripSlashes() {
$request = new THttpRequest();
$data = 'some\\text\\with\\slashes';
self::assertEquals('sometextwithslashes', $request->stripSlashes($data));
}
public function testGetUrl() {
$request = new THttpRequest();
$request->init(null);
self::assertInstanceOf('TUri', $request->getUrl());
// Try with $_SERVER['HTTP_HOST'] empty
$request=null;
$request = new THttpRequest();
$request->init(null);
$_SERVER['HTTP_HOST']='';
self::assertInstanceOf('TUri', $request->getUrl());
}
public function testGetUrlManager() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals(null, $request->getUrlManager());
}
public function testSetUrlManager() {
$request = new THttpRequest();
// Try invalid manager id
$request->setUrlManager('invalidManagerId');
try {
$request->init(null);
$request->getUrlManagerModule();
self::fail ('httprequest_urlmanager_inexist exception not thrown');
} catch (TConfigurationException $e) {
}
$request=null;
// Try with valid module id, but not instance of TUrlManager
$module=new TAssetManager();
self::$app->setModule('badmanager',$module);
$request = new THttpRequest();
$request->setUrlManager('badmanager');
try {
$request->init(null);
$request->getUrlManagerModule();
self::fail ('httprequest_urlmanager_invalid exception not thrown');
} catch (TConfigurationException $e) {
}
$request=null;
// Finally, try with a valid manager
$module=new TUrlManager ();
self::$app->setModule('goodmanager',$module);
$request = new THttpRequest();
$request->setUrlManager('goodmanager');
$request->init(null);
self::assertEquals ('goodmanager', $request->getUrlManager());
self::assertInstanceOf ('TUrlManager',$request->getUrlManagerModule());
}
public function testSetUrlFormat() {
$request = new THttpRequest();
$request->setUrlFormat('Path');
self::assertEquals('Path', $request->getUrlFormat());
// Test invalid
try {
$request->setUrlFormat('Bad');
self::fail ('Bad Value exception not thrown');
} catch (TInvalidDataValueException $e) {}
}
public function testGetRequestType() {
$request = new THttpRequest();
self::assertEquals('GET', $request->getRequestType());
}
public function testGetIsSecureConnection() {
$request = new THttpRequest();
self::assertEquals(false, $request->getIsSecureConnection());
}
public function testGetPathInfo() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals(__FILE__, $request->getPathInfo());
}
public function testGetQueryString() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals('page=Links', $request->getQueryString());
}
public function testGetRequestUri() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals('/demos/personal/index.php?page=Links', $request->getRequestUri());
}
public function testGetBaseUrl() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals('http://localhost', $request->getBaseUrl());
}
public function testGetApplicationUrl() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals('/demos/personal/index.php', $request->getApplicationUrl());
}
public function testGetAbsoluteApplicationUrl() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals('http://localhost/demos/personal/index.php', $request->getAbsoluteApplicationUrl());
}
public function testGetApplicationFilePath() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals(__FILE__, $request->getApplicationFilePath());
}
public function testGetServerName() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals('localhost', $request->getServerName());
}
public function testGetServerPort() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals('80', $request->getServerPort());
}
public function testGetUrlReferrer() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals('https://github.com/pradosoft/prado', $request->getUrlReferrer());
}
public function testGetBrowser() {
/*
// requires browscap configuration in php.ini
$request = new THttpRequest();
$request->init(null);
// Reset UserAgent, because constructor of THttpRequest unset it if called from cli !
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3';
try {
$browser=$request->getBrowser();
self::assertInstanceOf ('array', $browser);
self::assertEquals('Firefox', $browser['browser']);
self::assertEquals('2.0.0.3', $browser['version']);
} catch (TPhpErrorException $e) {
// If not supported, skip test
if (strstr($e->getMessage(),'browscap ini directive not set'))
self::markTestSkipped('browscap ini directive not set in php.ini');
else
self::fail ('Exception raised : '.$e->getMessage());
}*/
throw new PHPUnit_Framework_IncompleteTestError();
}
public function testGetUserAgent() {
$request = new THttpRequest();
self::assertEquals('Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3', $request->getUserAgent());
}
public function testGetUserHostAddress() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals('127.0.0.1', $request->getUserHostAddress());
}
public function testGetUserHost() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals('localhost', $request->getUserHost());
}
public function testGetAcceptTypes() {
$request = new THttpRequest();
$request->init(null);
self::assertEquals('text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5', $request->getAcceptTypes());
}
public function testGetUserLanguages() {
/*
// this actually doesn't work because:
// - THttpRequest::getUserLanguages() is a wrapper for PradoBase::getUserLanguages()
// - PradoBase is using a static variable to hold the user languages array
// - PradoBase exists before we set $_SERVER['HTTP_ACCEPT_LANGUAGE']
$request = new THttpRequest();
$request->init(null);
// Browser sent fr,en-us;q=0.8,fr-fr;q=0.5,en;q=0.3
// that means that browser want fr (1) first, next en-us (0.8), then fr-fr(0.5)n and last en (0.3)
// So, we expect method to return an array with these languages, and this order
$acceptLanguages=array('fr', 'en-us','fr-fr','en');
self::assertEquals($acceptLanguages, $request->getUserLanguages());
*/
throw new PHPUnit_Framework_IncompleteTestError();
}
public function testSetEnableCookieValidation() {
$request = new THttpRequest();
$request->init(null);
$request->setEnableCookieValidation (true);
self::assertEquals(true, $request->getEnableCookieValidation());
}
public function testGetCookies() {
$request = new THttpRequest ();
$request->init (null);
$request->setEnableCookieValidation (false);
$cookies=$request->getCookies();
self::assertInstanceOf('THttpCookieCollection', $cookies);
self::assertEquals('0123456789abcdef', $cookies->itemAt('phpsessid')->getValue());
$request = null;
// Test with cookie validation
$security=new TSecurityManager();
self::$app->setModule ('security', $security);
$_COOKIE['phpsessid']=$security->hashData('0123456789abcdef');
$request = new THttpRequest ();
$request->init (null);
$request->setEnableCookieValidation (true);
$cookies=$request->getCookies();
self::assertInstanceOf('THttpCookieCollection', $cookies);
self::assertEquals('0123456789abcdef', $cookies->itemAt('phpsessid')->getValue());
}
public function testGetUploadedFiles() {
$request = new THttpRequest ();
$request->init (null);
self::assertEquals($_FILES, $request->getUploadedFiles());
}
public function testGetServerVariables() {
$request = new THttpRequest ();
$request->init (null);
self::assertEquals($_SERVER, $request->getServerVariables());
}
public function testGetEnvironmentVariables() {
$request = new THttpRequest ();
$request->init (null);
self::assertEquals($_ENV, $request->getEnvironmentVariables());
}
public function testConstructUrl() {
$request = new THttpRequest ();
$request->init (null);
// Try to construct an url to the pageservice with some parameters
$url=$request->constructURL('page','Home',array('param1'=>'value1','param2'=>'value2'), true);
self::assertEquals('/demos/personal/index.php?page=Home&param1=value1&param2=value2', $url);
// Try without encode &
$url=$request->constructURL('page','Home',array('param1'=>'value1','param2'=>'value2'), false);
self::assertEquals('/demos/personal/index.php?page=Home¶m1=value1¶m2=value2', $url);
// Try to use HTTPS even if the current request is sent via HTTP
//$url=$request->constructURL('page','Home',array('param1'=>'value1','param2'=>'value2'), false, false, true);
//self::assertEquals('https', substr($url,0,5));
}
public function testSetServiceID() {
$request = new THttpRequest ();
$request->setServiceId('page');
self::assertEquals('page', $request->getServiceId());
}
public function testGetIterator() {
$request = new THttpRequest ();
$request->init(null);
self::assertInstanceOf ('TMapIterator', $request->getIterator());
}
public function testGetCount() {
$request = new THttpRequest ();
$request->init(null);
// Simulate a request with just a service
$_GET['page']='Home';
$request->resolveRequest(array('page'));
// Should return 1 (service param, and no get param)
self::assertEquals(1, $request->getCount());
self::assertEquals(1, $request->count());
}
public function testGetKeys() {
$request = new THttpRequest ();
$request->init(null);
// Simulate a request with just a service
$_GET['page']='Home';
$request->resolveRequest(array('page'));
self::assertEquals(array('page'), $request->getKeys());
}
public function testItemAt() {
$request = new THttpRequest ();
$request->init(null);
// Simulate a request with just a service
$_GET['page']='Home';
$request->resolveRequest(array('page'));
self::assertEquals('Home', $request->itemAt('page'));
}
public function testAdd() {
$request = new THttpRequest ();
$request->init(null);
// Simulate a request with just a service
$_GET['page']='Home';
$request->resolveRequest(array('page'));
$request->Add('param1','value1');
self::assertEquals('value1', $request->itemAt('param1'));
}
public function testRemove() {
$request = new THttpRequest ();
$request->init(null);
// Simulate a request with just a service
$_GET['page']='Home';
$request->resolveRequest(array('page'));
// Remove an unknow key
self::assertNull($request->remove('param1','value1'));
// Remove a key
self::assertEquals('Home', $request->remove('page'));
}
public function testContains() {
$request = new THttpRequest ();
$request->init(null);
// Simulate a request with just a service
$_GET['page']='Home';
$request->resolveRequest(array('page'));
self::assertTrue($request->contains('page'));
self::assertFalse($request->contains('param'));
}
public function testClear() {
$request = new THttpRequest ();
$request->init(null);
// Simulate a request with just a service
$_GET['page']='Home';
$request->resolveRequest(array('page'));
$request->clear();
self::assertEquals(0,$request->getCount());
}
public function testToArray() {
$request = new THttpRequest ();
$request->init(null);
// Simulate a request with just a service
$_GET['page']='Home';
$request->resolveRequest(array('page'));
self::assertEquals(array ('page'=>'Home'), $request->toArray());
}
public function testOffsetExists() {
$request = new THttpRequest ();
$request->init (null);
// should not exists
self::assertFalse($request->offsetExists(0));
}
public function testOffsetGet() {
$request = new THttpRequest ();
$request->init (null);
// should not exists
self::assertNull($request->offsetGet(0));
}
public function testOffsetSet() {
$request = new THttpRequest ();
$request->init (null);
$request->offsetSet(0, 'test');
// should not exists
self::assertEquals('test',$request->offsetGet(0));
}
public function testOffsetUnset() {
$request = new THttpRequest ();
$request->init (null);
$request->offsetSet(0, 'test');
// Count should be 1
self::assertEquals (1, $request->count());
$request->offsetUnset(0);
// Now, count should be zero
self::assertEquals (0, $request->count());
}
public function testGetSetID() {
$request=new THttpRequest();
$request->init(null);
$request->setID('testId');
self::assertEquals('testId', $request->getID());
}
public function testGetSetUrlParamSeparator() {
$request=new THttpRequest();
$request->init(null);
// Try an invalid separator
try {
$request->setUrlParamSeparator('&&');
self::fail('httprequest_separator_invalid exception not thrown');
} catch (TInvalidDataValueException $e) {
}
// Try valid one
$request->setUrlParamSeparator('&');
self::assertEquals('&', $request->getUrlParamSeparator());
}
public function testGetServiceParameter() {
$request=new THttpRequest();
$request->init(null);
$_GET['page']='Home';
$request->resolveRequest(array('page'));
self::assertEquals('Home', $request->getServiceParameter());
}
public function testGetRequestResolved() {
$request=new THttpRequest();
$request->init(null);
self::assertFalse($request->getRequestResolved());
$_GET['page']='Home';
$request->resolveRequest(array('page'));
self::assertTrue($request->getRequestResolved());
}
public function testRequestWithUrlMapping () {
Prado::Using ('System.Web.TUrlMapping');
$confstr='<config><url ServiceId="testService" ServiceParameter="testServiceParam" pattern="test/{param}/?" parameters.param="\w+"/></config>';
$config=new TXmlDocument('1.0','utf8');
$config->loadFromString($confstr);
$module=new TUrlMapping ();
self::$app->setModule('friendly-url',$module);
if (isset ($_GET['page'])) unset ($_GET['page']); // Remove service from a previous test !
$_SERVER['REQUEST_URI'] = '/index.php/test/value2';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['PHP_SELF'] = '/index.php/test/value2';
$_SERVER['QUERY_STRING'] = '';
$_SERVER['PATH_INFO'] = '/test/value2';
$request = new THttpRequest();
$request->setUrlManager('friendly-url');
$request->setUrlFormat(THttpRequestUrlFormat::Path);
$request->init(null);
$module->init ($config);
self::assertEquals('testService', $request->resolveRequest(array ('page', 'testService')));
}
}
|