Updating RequestHandler and its tests to use the features moved to CakeRequest. Marking a number of methods as deprecated.

This commit is contained in:
Mark Story 2010-05-08 00:59:40 -04:00
parent 78bd7300a2
commit 5040eb7227
2 changed files with 71 additions and 187 deletions

View file

@ -57,12 +57,12 @@ class RequestHandlerComponent extends Object {
private $__responseTypeSet = null; private $__responseTypeSet = null;
/** /**
* Holds the copy of Controller::$params * Holds the copy of Controller::$request
* *
* @var array * @var CakeRequest
* @access public * @access public
*/ */
public $params = array(); public $request;
/** /**
* Friendly content-type mappings used to set response types and determine * Friendly content-type mappings used to set response types and determine
@ -102,37 +102,6 @@ class RequestHandlerComponent extends Object {
'tar' => 'application/x-tar' 'tar' => 'application/x-tar'
); );
/**
* List of regular expressions for matching mobile device's user agent string
*
* @var array
* @access public
*/
public $mobileUA = array(
'Android',
'AvantGo',
'BlackBerry',
'DoCoMo',
'iPod',
'iPhone',
'J2ME',
'MIDP',
'NetFront',
'Nokia',
'Opera Mini',
'PalmOS',
'PalmSource',
'portalmmm',
'Plucker',
'ReqwirelessWeb',
'SonyEricsson',
'Symbian',
'UP\.Browser',
'webOS',
'Windows CE',
'Xiino'
);
/** /**
* Content-types accepted by the client. If extension parsing is enabled in the * Content-types accepted by the client. If extension parsing is enabled in the
* Router, and an extension is detected, the corresponding content-type will be * Router, and an extension is detected, the corresponding content-type will be
@ -201,6 +170,7 @@ class RequestHandlerComponent extends Object {
if (isset($controller->params['url']['ext'])) { if (isset($controller->params['url']['ext'])) {
$this->ext = $controller->params['url']['ext']; $this->ext = $controller->params['url']['ext'];
} }
$this->request = $controller->request;
$this->_set($settings); $this->_set($settings);
} }
@ -222,12 +192,8 @@ class RequestHandlerComponent extends Object {
* @return void * @return void
*/ */
public function startup(&$controller) { public function startup(&$controller) {
if (!$this->enabled) {
return;
}
$this->__initializeTypes(); $this->__initializeTypes();
$controller->params['isAjax'] = $this->isAjax(); $controller->request->params['isAjax'] = $this->request->is('ajax');
$isRecognized = ( $isRecognized = (
!in_array($this->ext, array('html', 'htm')) && !in_array($this->ext, array('html', 'htm')) &&
in_array($this->ext, array_keys($this->__requestContent)) in_array($this->ext, array_keys($this->__requestContent))
@ -235,7 +201,7 @@ class RequestHandlerComponent extends Object {
if (!empty($this->ext) && $isRecognized) { if (!empty($this->ext) && $isRecognized) {
$this->renderAs($controller, $this->ext); $this->renderAs($controller, $this->ext);
} elseif ($this->isAjax()) { } elseif ($this->request->is('ajax')) {
$this->renderAs($controller, 'ajax'); $this->renderAs($controller, 'ajax');
} }
@ -279,7 +245,7 @@ class RequestHandlerComponent extends Object {
* @return boolean True if call is Ajax * @return boolean True if call is Ajax
*/ */
public function isAjax() { public function isAjax() {
return env('HTTP_X_REQUESTED_WITH') === "XMLHttpRequest"; return $this->request->is('ajax');
} }
/** /**
@ -288,7 +254,7 @@ class RequestHandlerComponent extends Object {
* @return boolean True if call is from Flash * @return boolean True if call is from Flash
*/ */
public function isFlash() { public function isFlash() {
return (preg_match('/^(Shockwave|Adobe) Flash/', env('HTTP_USER_AGENT')) == 1); return $this->request->is('flash');
} }
/** /**
@ -297,7 +263,7 @@ class RequestHandlerComponent extends Object {
* @return bool True if call is over HTTPS * @return bool True if call is over HTTPS
*/ */
public function isSSL() { public function isSSL() {
return env('HTTPS'); return $this->request->is('ssl');
} }
/** /**
@ -332,20 +298,9 @@ class RequestHandlerComponent extends Object {
* client accepts WAP content. * client accepts WAP content.
* *
* @return boolean True if user agent is a mobile web browser * @return boolean True if user agent is a mobile web browser
* @access public
* @deprecated Use of constant REQUEST_MOBILE_UA is deprecated and will be removed in future versions
*/ */
function isMobile() { function isMobile() {
if (defined('REQUEST_MOBILE_UA')) { return $this->request->is('mobile') || $this->accepts('wap');
$regex = '/' . REQUEST_MOBILE_UA . '/i';
} else {
$regex = '/' . implode('|', $this->mobileUA) . '/i';
}
if (preg_match($regex, env('HTTP_USER_AGENT')) || $this->accepts('wap')) {
return true;
}
return false;
} }
/** /**
@ -361,36 +316,40 @@ class RequestHandlerComponent extends Object {
* Returns true if the current call a POST request * Returns true if the current call a POST request
* *
* @return boolean True if call is a POST * @return boolean True if call is a POST
* @deprecated Use $this->request->is('post'); from your controller.
*/ */
public function isPost() { public function isPost() {
return (strtolower(env('REQUEST_METHOD')) == 'post'); return $this->request->is('post');
} }
/** /**
* Returns true if the current call a PUT request * Returns true if the current call a PUT request
* *
* @return boolean True if call is a PUT * @return boolean True if call is a PUT
* @deprecated Use $this->request->is('put'); from your controller.
*/ */
public function isPut() { public function isPut() {
return (strtolower(env('REQUEST_METHOD')) == 'put'); return $this->request->is('put');
} }
/** /**
* Returns true if the current call a GET request * Returns true if the current call a GET request
* *
* @return boolean True if call is a GET * @return boolean True if call is a GET
* @deprecated Use $this->request->is('get'); from your controller.
*/ */
public function isGet() { public function isGet() {
return (strtolower(env('REQUEST_METHOD')) == 'get'); return $this->request->is('get');
} }
/** /**
* Returns true if the current call a DELETE request * Returns true if the current call a DELETE request
* *
* @return boolean True if call is a DELETE * @return boolean True if call is a DELETE
* @deprecated Use $this->request->is('delete'); from your controller.
*/ */
public function isDelete() { public function isDelete() {
return (strtolower(env('REQUEST_METHOD')) == 'delete'); return $this->request->is('delete');
} }
/** /**
@ -429,42 +388,20 @@ class RequestHandlerComponent extends Object {
* Gets the server name from which this request was referred * Gets the server name from which this request was referred
* *
* @return string Server address * @return string Server address
* @deprecated use $this->request->referer() from your controller instead
*/ */
public function getReferer() { public function getReferer() {
if (env('HTTP_HOST') != null) { return $this->request->referer(false);
$sessHost = env('HTTP_HOST');
}
if (env('HTTP_X_FORWARDED_HOST') != null) {
$sessHost = env('HTTP_X_FORWARDED_HOST');
}
return trim(preg_replace('/(?:\:.*)/', '', $sessHost));
} }
/** /**
* Gets remote client IP * Gets remote client IP
* *
* @return string Client IP address * @return string Client IP address
* @deprecated use $this->request->clientIp() from your controller instead.
*/ */
public function getClientIP($safe = true) { public function getClientIP($safe = true) {
if (!$safe && env('HTTP_X_FORWARDED_FOR') != null) { return $this->request->clientIp($safe);
$ipaddr = preg_replace('/(?:,.*)/', '', env('HTTP_X_FORWARDED_FOR'));
} else {
if (env('HTTP_CLIENT_IP') != null) {
$ipaddr = env('HTTP_CLIENT_IP');
} else {
$ipaddr = env('REMOTE_ADDR');
}
}
if (env('HTTP_CLIENTADDRESS') != null) {
$tmpipaddr = env('HTTP_CLIENTADDRESS');
if (!empty($tmpipaddr)) {
$ipaddr = preg_replace('/(?:,.*)/', '', $tmpipaddr);
}
}
return trim($ipaddr);
} }
/** /**

View file

@ -21,6 +21,7 @@ App::import('Controller', 'Controller', false);
App::import('Component', array('RequestHandler')); App::import('Component', array('RequestHandler'));
Mock::generatePartial('RequestHandlerComponent', 'NoStopRequestHandler', array('_stop')); Mock::generatePartial('RequestHandlerComponent', 'NoStopRequestHandler', array('_stop'));
Mock::generate('CakeRequest', 'RequestHandlerMockCakeRequest');
/** /**
* RequestHandlerTestController class * RequestHandlerTestController class
@ -53,11 +54,11 @@ class RequestHandlerTestController extends Controller {
* @access private * @access private
* @return void * @return void
*/ */
function __construct($params = array()) { function __construct($request, $params = array()) {
foreach ($params as $key => $val) { foreach ($params as $key => $val) {
$this->{$key} = $val; $this->{$key} = $val;
} }
parent::__construct(); parent::__construct($request);
} }
/** /**
@ -103,11 +104,11 @@ class RequestHandlerTestDisabledController extends Controller {
* @access private * @access private
* @return void * @return void
*/ */
function __construct($params = array()) { function __construct($request, $params = array()) {
foreach ($params as $key => $val) { foreach ($params as $key => $val) {
$this->{$key} = $val; $this->{$key} = $val;
} }
parent::__construct(); parent::__construct($request);
} }
/** /**
@ -151,6 +152,7 @@ class RequestHandlerComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function startTest() { function startTest() {
$this->_server = $_SERVER;
$this->_init(); $this->_init();
} }
@ -161,9 +163,10 @@ class RequestHandlerComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function _init() { function _init() {
$this->Controller = new RequestHandlerTestController(array('components' => array('RequestHandler'))); $request = new CakeRequest('controller_posts/index');
$this->Controller->constructClasses(); $this->Controller = new RequestHandlerTestController($request);
$this->RequestHandler =& $this->Controller->RequestHandler; $this->RequestHandler = new RequestHandlerComponent();
$this->RequestHandler->request = $request;
} }
/** /**
@ -178,6 +181,7 @@ class RequestHandlerComponentTest extends CakeTestCase {
if (!headers_sent()) { if (!headers_sent()) {
header('Content-type: text/html'); //reset content type. header('Content-type: text/html'); //reset content type.
} }
$_SERVER = $this->_server;
App::build(); App::build();
} }
@ -191,7 +195,7 @@ class RequestHandlerComponentTest extends CakeTestCase {
$this->assertNull($this->RequestHandler->ext); $this->assertNull($this->RequestHandler->ext);
$this->_init(); $this->_init();
$this->Controller->params['url']['ext'] = 'rss'; $this->Controller->request->params['url']['ext'] = 'rss';
$this->RequestHandler->initialize($this->Controller); $this->RequestHandler->initialize($this->Controller);
$this->assertEqual($this->RequestHandler->ext, 'rss'); $this->assertEqual($this->RequestHandler->ext, 'rss');
@ -211,18 +215,10 @@ class RequestHandlerComponentTest extends CakeTestCase {
function testDisabling() { function testDisabling() {
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
$this->_init(); $this->_init();
$this->Controller->Component->initialize($this->Controller); $this->RequestHandler->initialize($this->Controller);
$this->Controller->beforeFilter(); $this->Controller->beforeFilter();
$this->Controller->Component->startup($this->Controller); $this->RequestHandler->startup($this->Controller);
$this->assertEqual($this->Controller->params, array('isAjax' => true)); $this->assertEqual($this->Controller->params['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']);
} }
/** /**
@ -233,7 +229,7 @@ class RequestHandlerComponentTest extends CakeTestCase {
*/ */
function testAutoResponseType() { function testAutoResponseType() {
$this->Controller->ext = '.thtml'; $this->Controller->ext = '.thtml';
$this->Controller->params['url']['ext'] = 'rss'; $this->Controller->request->params['url']['ext'] = 'rss';
$this->RequestHandler->initialize($this->Controller); $this->RequestHandler->initialize($this->Controller);
$this->RequestHandler->startup($this->Controller); $this->RequestHandler->startup($this->Controller);
$this->assertEqual($this->Controller->ext, '.ctp'); $this->assertEqual($this->Controller->ext, '.ctp');
@ -320,19 +316,10 @@ class RequestHandlerComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testRequestClientTypes() { 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'; $_SERVER['HTTP_X_PROTOTYPE_VERSION'] = '1.5';
$this->assertTrue($this->RequestHandler->isAjax());
$this->assertEqual($this->RequestHandler->getAjaxVersion(), '1.5'); $this->assertEqual($this->RequestHandler->getAjaxVersion(), '1.5');
unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']); unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']);
$this->assertFalse($this->RequestHandler->isAjax());
$this->assertFalse($this->RequestHandler->getAjaxVersion()); $this->assertFalse($this->RequestHandler->getAjaxVersion());
} }
@ -343,23 +330,12 @@ class RequestHandlerComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testFlashDetection() { function testFlashDetection() {
$_agent = env('HTTP_USER_AGENT'); $request = new RequestHandlerMockCakeRequest();
$_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash'; $request->setReturnValue('is', array(true), array('flash'));
$request->expectOnce('is', array('flash'));
$this->RequestHandler->request = $request;
$this->assertTrue($this->RequestHandler->isFlash()); $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;
} }
/** /**
@ -425,15 +401,12 @@ class RequestHandlerComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMobileDeviceDetection() { function testMobileDeviceDetection() {
$this->assertFalse($this->RequestHandler->isMobile()); $request = new RequestHandlerMockCakeRequest();
$request->setReturnValue('is', array(true), array('mobile'));
$request->expectOnce('is', array('mobile'));
$_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->RequestHandler->request = $request;
$this->assertTrue($this->RequestHandler->isMobile()); $this->assertTrue($this->RequestHandler->isMobile());
$_SERVER['HTTP_USER_AGENT'] = 'Some imaginary UA';
$this->RequestHandler->mobileUA []= 'imaginary';
$this->assertTrue($this->RequestHandler->isMobile());
array_pop($this->RequestHandler->mobileUA);
} }
/** /**
@ -443,17 +416,12 @@ class RequestHandlerComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testRequestProperties() { function testRequestProperties() {
$_SERVER['HTTPS'] = 'on'; $request = new RequestHandlerMockCakeRequest();
$this->assertTrue($this->RequestHandler->isSSL()); $request->setReturnValue('is', array(true), array('ssl'));
$request->expectOnce('is', array('ssl'));
unset($_SERVER['HTTPS']); $this->RequestHandler->request = $request;
$this->assertFalse($this->RequestHandler->isSSL()); $this->assertTrue($this->RequestHandler->isSsl());
$_ENV['SCRIPT_URI'] = 'https://localhost/';
$s = $_SERVER;
$_SERVER = array();
$this->assertTrue($this->RequestHandler->isSSL());
$_SERVER = $s;
} }
/** /**
@ -463,28 +431,17 @@ class RequestHandlerComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testRequestMethod() { function testRequestMethod() {
$_SERVER['REQUEST_METHOD'] = 'GET'; $request = new RequestHandlerMockCakeRequest();
$request->setReturnValue('is', array(true), array('get'));
$request->setReturnValue('is', array(false), array('post'));
$request->setReturnValue('is', array(true), array('delete'));
$request->setReturnValue('is', array(false), array('put'));
$request->expectCallCount('is', 4);
$this->RequestHandler->request = $request;
$this->assertTrue($this->RequestHandler->isGet()); $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->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->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()); $this->assertTrue($this->RequestHandler->isDelete());
} }
@ -543,26 +500,13 @@ class RequestHandlerComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testClientProperties() { function testClientProperties() {
$_SERVER['HTTP_HOST'] = 'localhost:80'; $request = new RequestHandlerMockCakeRequest();
$this->assertEqual($this->RequestHandler->getReferer(), 'localhost'); $request->expectOnce('referer');
$_SERVER['HTTP_HOST'] = null; $request->expectOnce('clientIp', array(false));
$_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org'; $this->RequestHandler->request = $request;
$this->assertEqual($this->RequestHandler->getReferer(), 'cakephp.org');
$_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com'; $this->RequestHandler->getReferer();
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2'; $this->RequestHandler->getClientIP(false);
$_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');
} }
/** /**
@ -577,7 +521,9 @@ class RequestHandlerComponentTest extends CakeTestCase {
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS) 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
), true); ), true);
$this->Controller->request = new CakeRequest('posts/index');
$this->Controller->RequestHandler = new NoStopRequestHandler($this); $this->Controller->RequestHandler = new NoStopRequestHandler($this);
$this->Controller->RequestHandler->request = $this->Controller->request;
$this->Controller->RequestHandler->expectOnce('_stop'); $this->Controller->RequestHandler->expectOnce('_stop');
ob_start(); ob_start();
@ -610,6 +556,7 @@ class RequestHandlerComponentTest extends CakeTestCase {
)); ));
$RequestHandler =& new NoStopRequestHandler(); $RequestHandler =& new NoStopRequestHandler();
$RequestHandler->request = new CakeRequest('posts/index');
ob_start(); ob_start();
$RequestHandler->beforeRedirect( $RequestHandler->beforeRedirect(