Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 542 lines (507 sloc) 16.704 kb
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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
<?php
/* SVN FILE: $Id$ */
/**
* RequestHandlerComponentTest file
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.cases.libs.controller.components
* @since CakePHP(tm) v 1.2.0.5435
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Core', array('Controller'));
App::import('Component', array('RequestHandler'));

Mock::generatePartial('RequestHandlerComponent', 'NoStopRequestHandler', array('_stop'));
/**
* RequestHandlerTestController class
*
* @package cake
* @subpackage cake.tests.cases.libs.controller.components
*/
class RequestHandlerTestController extends Controller {
/**
* name property
*
* @var string
* @access public
**/
var $name = 'RequestHandlerTest';
/**
* uses property
*
* @var mixed null
* @access public
*/
var $uses = null;
/**
* construct method
*
* @param array $params
* @access private
* @return void
*/
function __construct($params = array()) {
foreach ($params as $key => $val) {
$this->{$key} = $val;
}
parent::__construct();
}
/**
* test method for ajax redirection
*
* @return void
**/
function destination() {
$this->viewPath = 'posts';
$this->render('index');
}
}
/**
* RequestHandlerTestDisabledController class
*
* @package cake
* @subpackage cake.tests.cases.libs.controller.components
*/
class RequestHandlerTestDisabledController extends Controller {
/**
* uses property
*
* @var mixed null
* @access public
*/
var $uses = null;
/**
* construct method
*
* @param array $params
* @access private
* @return void
*/
function __construct($params = array()) {
foreach ($params as $key => $val) {
$this->{$key} = $val;
}
parent::__construct();
}
/**
* beforeFilter method
*
* @return void
* @access public
*/
function beforeFilter() {
$this->RequestHandler->enabled = false;
}
}
/**
* RequestHandlerComponentTest class
*
* @package cake
* @subpackage cake.tests.cases.libs.controller.components
*/
class RequestHandlerComponentTest extends CakeTestCase {
/**
* Controller property
*
* @var RequestHandlerTestController
* @access public
*/
var $Controller;
/**
* RequestHandler property
*
* @var RequestHandlerComponent
* @access public
*/
var $RequestHandler;
/**
* startTest method
*
* @access public
* @return void
*/
function startTest() {
$this->_init();
}
/**
* init method
*
* @access protected
* @return void
*/
function _init() {
$this->Controller = new RequestHandlerTestController(array('components' => array('RequestHandler')));
$this->Controller->constructClasses();
$this->RequestHandler =& $this->Controller->RequestHandler;
}
/**
* endTest method
*
* @access public
* @return void
*/
function endTest() {
unset($this->RequestHandler);
unset($this->Controller);
if (!headers_sent()) {
header('Content-type: text/html'); //reset content type.
}
}
/**
* testInitializeCallback method
*
* @access public
* @return void
*/
function testInitializeCallback() {
$this->assertNull($this->RequestHandler->ext);

$this->_init();
$this->Controller->params['url']['ext'] = 'rss';
$this->RequestHandler->initialize($this->Controller);
$this->assertEqual($this->RequestHandler->ext, 'rss');
}
/**
* testDisabling method
*
* @access public
* @return void
*/
function testDisabling() {
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
$this->_init();
$this->Controller->Component->initialize($this->Controller);
$this->Controller->beforeFilter();
$this->Controller->Component->startup($this->Controller);
$this->assertEqual($this->Controller->params, array('isAjax' => true));

$this->Controller = new RequestHandlerTestDisabledController(array('components' => array('RequestHandler')));
$this->Controller->constructClasses();
$this->Controller->Component->initialize($this->Controller);
$this->Controller->beforeFilter();
$this->Controller->Component->startup($this->Controller);
$this->assertEqual($this->Controller->params, array());
unset($_SERVER['HTTP_X_REQUESTED_WITH']);
}
/**
* testAutoResponseType method
*
* @access public
* @return void
*/
function testAutoResponseType() {
$this->Controller->ext = '.thtml';
$this->Controller->params['url']['ext'] = 'rss';
$this->RequestHandler->initialize($this->Controller);
$this->RequestHandler->startup($this->Controller);
$this->assertEqual($this->Controller->ext, '.ctp');
}
/**
* testStartupCallback method
*
* @access public
* @return void
*/
function testStartupCallback() {
$_SERVER['REQUEST_METHOD'] = 'PUT';
$_SERVER['CONTENT_TYPE'] = 'application/xml';
$this->RequestHandler->startup($this->Controller);
$this->assertTrue(is_object($this->Controller->data));
$this->assertEqual(strtolower(get_class($this->Controller->data)), 'xml');
}
/**
* testStartupCallback with charset.
*
* @return void
**/
function testStartupCallbackCharset() {
$_SERVER['REQUEST_METHOD'] = 'PUT';
$_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
$this->RequestHandler->startup($this->Controller);
$this->assertTrue(is_object($this->Controller->data));
$this->assertEqual(strtolower(get_class($this->Controller->data)), 'xml');
}
/**
* testNonAjaxRedirect method
*
* @access public
* @return void
*/
function testNonAjaxRedirect() {
$this->RequestHandler->initialize($this->Controller);
$this->RequestHandler->startup($this->Controller);
$this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, '/'));
}
/**
* testRenderAs method
*
* @access public
* @return void
*/
function testRenderAs() {
$this->assertFalse(in_array('Xml', $this->Controller->helpers));
$this->RequestHandler->renderAs($this->Controller, 'xml');
$this->assertTrue(in_array('Xml', $this->Controller->helpers));
}
/**
* test that calling renderAs() more than once continues to work.
*
* @link #6466
* @return void
**/
function testRenderAsCalledTwice() {
$this->RequestHandler->renderAs($this->Controller, 'xml');
$this->assertEqual($this->Controller->viewPath, 'request_handler_test' . DS . 'xml');
$this->assertEqual($this->Controller->layoutPath, 'xml');

$this->assertTrue(in_array('Xml', $this->Controller->helpers));

$this->RequestHandler->renderAs($this->Controller, 'js');
$this->assertEqual($this->Controller->viewPath, 'request_handler_test' . DS . 'js');
$this->assertEqual($this->Controller->layoutPath, 'js');
$this->assertTrue(in_array('Js', $this->Controller->helpers));
}
/**
* testRequestClientTypes method
*
* @access public
* @return void
*/
function testRequestClientTypes() {
$this->assertFalse($this->RequestHandler->isFlash());
$_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
$this->assertTrue($this->RequestHandler->isFlash());
unset($_SERVER['HTTP_USER_AGENT'], $_SERVER['HTTP_X_REQUESTED_WITH']);

$this->assertFalse($this->RequestHandler->isAjax());
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
$_SERVER['HTTP_X_PROTOTYPE_VERSION'] = '1.5';
$this->assertTrue($this->RequestHandler->isAjax());
$this->assertEqual($this->RequestHandler->getAjaxVersion(), '1.5');

unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']);
$this->assertFalse($this->RequestHandler->isAjax());
$this->assertFalse($this->RequestHandler->getAjaxVersion());
}
/**
* Tests the detection of various Flash versions
*
* @access public
* @return void
*/
function testFlashDetection() {
$_agent = env('HTTP_USER_AGENT');
$_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
$this->assertTrue($this->RequestHandler->isFlash());

$_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
$this->assertTrue($this->RequestHandler->isFlash());

$_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player 9';
$this->assertTrue($this->RequestHandler->isFlash());

$_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player 10';
$this->assertTrue($this->RequestHandler->isFlash());

$_SERVER['HTTP_USER_AGENT'] = 'Shock Flash';
$this->assertFalse($this->RequestHandler->isFlash());

$_SERVER['HTTP_USER_AGENT'] = $_agent;
}
/**
* testRequestContentTypes method
*
* @access public
* @return void
*/
function testRequestContentTypes() {
$_SERVER['REQUEST_METHOD'] = 'GET';
$this->assertNull($this->RequestHandler->requestedWith());

$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['CONTENT_TYPE'] = 'application/json';
$this->assertEqual($this->RequestHandler->requestedWith(), 'json');

$result = $this->RequestHandler->requestedWith(array('json', 'xml'));
$this->assertEqual($result, 'json');

$result =$this->RequestHandler->requestedWith(array('rss', 'atom'));
$this->assertFalse($result);

$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
$this->_init();
$this->assertTrue($this->RequestHandler->isXml());
$this->assertFalse($this->RequestHandler->isAtom());
$this->assertFalse($this->RequestHandler->isRSS());

$_SERVER['HTTP_ACCEPT'] = 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
$this->_init();
$this->assertTrue($this->RequestHandler->isAtom());
$this->assertFalse($this->RequestHandler->isRSS());

$_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
$this->_init();
$this->assertFalse($this->RequestHandler->isAtom());
$this->assertTrue($this->RequestHandler->isRSS());

$this->assertFalse($this->RequestHandler->isWap());
$_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*';
$this->_init();
$this->assertTrue($this->RequestHandler->isWap());

$_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
}
/**
* testResponseContentType method
*
* @access public
* @return void
*/
function testResponseContentType() {
$this->assertNull($this->RequestHandler->responseType());
$this->assertTrue($this->RequestHandler->respondAs('atom'));
$this->assertEqual($this->RequestHandler->responseType(), 'atom');
}
/**
* testMobileDeviceDetection method
*
* @access public
* @return void
*/
function testMobileDeviceDetection() {
$this->assertFalse($this->RequestHandler->isMobile());
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3';
$this->assertTrue($this->RequestHandler->isMobile());
}
/**
* testRequestProperties method
*
* @access public
* @return void
*/
function testRequestProperties() {
$_SERVER['HTTPS'] = 'on';
$this->assertTrue($this->RequestHandler->isSSL());

unset($_SERVER['HTTPS']);
$this->assertFalse($this->RequestHandler->isSSL());

$_ENV['SCRIPT_URI'] = 'https://localhost/';
$s = $_SERVER;
$_SERVER = array();
$this->assertTrue($this->RequestHandler->isSSL());
$_SERVER = $s;
}
/**
* testRequestMethod method
*
* @access public
* @return void
*/
function testRequestMethod() {
$_SERVER['REQUEST_METHOD'] = 'GET';
$this->assertTrue($this->RequestHandler->isGet());
$this->assertFalse($this->RequestHandler->isPost());
$this->assertFalse($this->RequestHandler->isPut());
$this->assertFalse($this->RequestHandler->isDelete());

$_SERVER['REQUEST_METHOD'] = 'POST';
$this->assertFalse($this->RequestHandler->isGet());
$this->assertTrue($this->RequestHandler->isPost());
$this->assertFalse($this->RequestHandler->isPut());
$this->assertFalse($this->RequestHandler->isDelete());

$_SERVER['REQUEST_METHOD'] = 'PUT';
$this->assertFalse($this->RequestHandler->isGet());
$this->assertFalse($this->RequestHandler->isPost());
$this->assertTrue($this->RequestHandler->isPut());
$this->assertFalse($this->RequestHandler->isDelete());

$_SERVER['REQUEST_METHOD'] = 'DELETE';
$this->assertFalse($this->RequestHandler->isGet());
$this->assertFalse($this->RequestHandler->isPost());
$this->assertFalse($this->RequestHandler->isPut());
$this->assertTrue($this->RequestHandler->isDelete());
}
/**
* testClientContentPreference method
*
* @access public
* @return void
*/
function testClientContentPreference() {
$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
$this->_init();
$this->assertNotEqual($this->RequestHandler->prefers(), 'rss');
$this->RequestHandler->ext = 'rss';
$this->assertEqual($this->RequestHandler->prefers(), 'rss');
$this->assertFalse($this->RequestHandler->prefers('xml'));
$this->assertEqual($this->RequestHandler->prefers(array('js', 'xml', 'xhtml')), 'xml');
$this->assertTrue($this->RequestHandler->accepts('xml'));

$_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';
$this->_init();
$this->assertEqual($this->RequestHandler->prefers(), 'xml');
$this->assertEqual($this->RequestHandler->accepts(array('js', 'xml', 'html')), 'xml');
$this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));

$_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
$this->_init();
$this->assertEqual($this->RequestHandler->prefers(), 'html');
$this->assertFalse($this->RequestHandler->prefers('rss'));
$this->assertFalse($this->RequestHandler->accepts('rss'));
}
/**
* testCustomContent method
*
* @access public
* @return void
*/
function testCustomContent() {
$_SERVER['HTTP_ACCEPT'] = 'text/x-mobile,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5';
$this->_init();
$this->RequestHandler->setContent('mobile', 'text/x-mobile');
$this->RequestHandler->startup($this->Controller);
$this->assertEqual($this->RequestHandler->prefers(), 'mobile');

$this->_init();
$this->RequestHandler->setContent(array('mobile' => 'text/x-mobile'));
$this->RequestHandler->startup($this->Controller);
$this->assertEqual($this->RequestHandler->prefers(), 'mobile');
}
/**
* testClientProperties method
*
* @access public
* @return void
*/
function testClientProperties() {
$_SERVER['HTTP_HOST'] = 'localhost:80';
$this->assertEqual($this->RequestHandler->getReferrer(), 'localhost');
$_SERVER['HTTP_HOST'] = null;
$_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
$this->assertEqual($this->RequestHandler->getReferrer(), 'cakephp.org');

$_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
$_SERVER['REMOTE_ADDR'] = '192.168.1.3';
$this->assertEqual($this->RequestHandler->getClientIP(false), '192.168.1.5');
$this->assertEqual($this->RequestHandler->getClientIP(), '192.168.1.2');

unset($_SERVER['HTTP_X_FORWARDED_FOR']);
$this->assertEqual($this->RequestHandler->getClientIP(), '192.168.1.2');

unset($_SERVER['HTTP_CLIENT_IP']);
$this->assertEqual($this->RequestHandler->getClientIP(), '192.168.1.3');

$_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
$this->assertEqual($this->RequestHandler->getClientIP(), '10.0.1.2');
}
/**
* test that ajax requests involving redirects trigger requestAction instead.
*
* @return void
**/
function testAjaxRedirectAsRequestAction() {
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
$this->_init();
$_paths = Configure::read('viewPaths');
$testDir = array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS);
Configure::write('viewPaths', array_merge($testDir, $_paths));

$this->Controller->RequestHandler = new NoStopRequestHandler($this);
$this->Controller->RequestHandler->expectOnce('_stop');

ob_start();
$this->Controller->RequestHandler->beforeRedirect(
$this->Controller, array('controller' => 'request_handler_test', 'action' => 'destination')
);
$result = ob_get_clean();
$this->assertPattern('/posts index/', $result, 'RequestAction redirect failed.');

Configure::write('viewPaths', $_paths);
unset($_SERVER['HTTP_X_REQUESTED_WITH']);
}
}
?>
Something went wrong with that request. Please try again.