Adding a few more ways to detect HTTP headers, extensions and the accept header.

This commit is contained in:
Florian Krämer 2014-11-27 01:00:44 +01:00
parent 4ff07b745a
commit 43f7fcc735
2 changed files with 79 additions and 24 deletions

View file

@ -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;

View file

@ -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'));
}
/**