diff --git a/lib/Cake/Network/CakeRequest.php b/lib/Cake/Network/CakeRequest.php index 307066299..01179cb9d 100644 --- a/lib/Cake/Network/CakeRequest.php +++ b/lib/Cake/Network/CakeRequest.php @@ -112,8 +112,8 @@ class CakeRequest implements ArrayAccess { 'webOS', 'Windows CE', 'Windows Phone OS', 'Xiino' )), 'requested' => array('param' => 'requested', 'value' => 1), - 'json' => array('header' => array('application/json')), - 'xml' => array('header' => array('application/xml', 'text/xml')), + 'json' => array('accept' => array('application/json'), 'param' => 'ext', 'value' => 'json'), + 'xml' => array('accept' => array('application/xml', 'text/xml'), 'param' => 'ext', 'value' => 'xml'), ); /** @@ -506,6 +506,9 @@ class CakeRequest implements ArrayAccess { if (isset($detect['header']) && $this->_headerDetector($detect)) { return true; } + if (isset($detect['accept']) && $this->_acceptHeaderDetector($detect)) { + return true; + } if (isset($detect['param']) && $this->_paramDetector($detect)) { return true; } @@ -528,6 +531,38 @@ class CakeRequest implements ArrayAccess { return $headers; } +/** + * Detects if an URL extension is present. + * + * @param array $detect Detector options array. + * @return bool Whether or not the request is the type you are checking. + */ + protected function _extensionDetector($detect) { + if (is_string($detect['extension'])) { + $detect['extension'] = array($detect['extension']); + } + if (in_array($this->params['ext'], $detect['extension'])) { + return true; + } + return false; + } + +/** + * Detects if a specific accept header is present. + * + * @param array $detect Detector options array. + * @return bool Whether or not the request is the type you are checking. + */ + protected function _acceptHeaderDetector($detect) { + $acceptHeaders = $this->getAcceptHeaders(); + foreach ($detect['accept'] as $header) { + if (in_array($header, $acceptHeaders)) { + return true; + } + } + return false; + } + /** * Detects if a specific header is present. * @@ -535,10 +570,13 @@ class CakeRequest implements ArrayAccess { * @return bool Whether or not the request is the type you are checking. */ protected function _headerDetector($detect) { - $acceptHeaders = $this->getAcceptHeaders(); - foreach ($detect['header'] as $header) { - if (in_array($header, $acceptHeaders)) { - return true; + foreach ($detect['header'] as $header => $value) { + $header = 'HTTP_' . strtoupper($header); + if (isset($_SERVER[$header])) { + if (is_callable($value)) { + return call_user_func($value, $_SERVER[$header]); + } + return ($_SERVER[$header] === $value); } } return false; diff --git a/lib/Cake/Test/Case/Network/CakeRequestTest.php b/lib/Cake/Test/Case/Network/CakeRequestTest.php index 14a3d63f4..f9d8c5842 100644 --- a/lib/Cake/Test/Case/Network/CakeRequestTest.php +++ b/lib/Cake/Test/Case/Network/CakeRequestTest.php @@ -52,16 +52,6 @@ class TestCakeRequest extends CakeRequest { $this->here = $this->base . '/' . $this->url; } -/** - * Detects if a specific header is present. - * - * @param $detect Detector options array. - * @return bool Whether or not the request is the type you are checking. - */ - public function headerDetector($detect) { - return $this->_headerDetector($detect); - } - } /** @@ -105,15 +95,42 @@ class CakeRequestTest extends CakeTestCase { * @return void */ public function testHeaderDetector() { - $request = $this->getMock('TestCakeRequest', array('getAcceptHeaders')); + $request = new CakeRequest('some/path'); + $request->addDetector('host', array('header' => array('host' => 'cakephp.org'))); + + $_SERVER['HTTP_HOST'] = 'cakephp.org'; + $this->assertTrue($request->is('host')); + + $_SERVER['HTTP_HOST'] = 'php.net'; + $this->assertFalse($request->is('host')); + } + +/** + * Test the accept header detector. + * + * @return void + */ + public function testExtensionDetector() { + $request = new CakeRequest('some/path'); + $request->params['ext'] = 'json'; + $this->assertTrue($request->is('json')); + + $request->params['ext'] = 'xml'; + $this->assertFalse($request->is('json')); + } + +/** + * Test the accept header detector. + * + * @return void + */ + public function testAcceptHeaderDetector() { + $request = new CakeRequest('some/path'); $_SERVER['HTTP_ACCEPT'] = 'application/json, text/plain, */*'; - $detector = array('header' => array('application/json'), 'param' => 'ext', 'value' => 'json'); - $request->expects($this->once()) - ->method('getAcceptHeaders') - ->will($this->returnValue(array( - 'application/json' - ))); - $this->assertTrue($request->headerDetector($detector)); + $this->assertTrue($request->is('json')); + + $_SERVER['HTTP_ACCEPT'] = 'text/plain, */*'; + $this->assertFalse($request->is('json')); } /**