Permalink
Browse files

Adding a few more ways to detect HTTP headers, extensions and the acc…

…ept header.
  • Loading branch information...
burzum committed Nov 27, 2014
1 parent 4ff07b7 commit 43f7fcc735ef2c64212638f9565cee0ed54dd785
Showing with 78 additions and 23 deletions.
  1. +43 −5 lib/Cake/Network/CakeRequest.php
  2. +35 −18 lib/Cake/Test/Case/Network/CakeRequestTest.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 @@ public function is($type) {
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;
}
@@ -529,21 +532,56 @@ public function getAcceptHeaders() {
}
/**
* Detects if a specific header is present.
* 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 _headerDetector($detect) {
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['header'] as $header) {
foreach ($detect['accept'] as $header) {
if (in_array($header, $acceptHeaders)) {
return true;
}
}
return false;
}
/**
* Detects if a specific header is present.
*
* @param array $detect Detector options array.
* @return bool Whether or not the request is the type you are checking.
*/
protected function _headerDetector($detect) {
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;
}
/**
* Detects if a specific request parameter is present.
*
@@ -52,16 +52,6 @@ public function reConstruct($url = 'some/path', $parseEnvironment = true) {
$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 @@ public function tearDown() {
* @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'));
}
/**

0 comments on commit 43f7fcc

Please sign in to comment.