Permalink
Browse files

Adding a test for the refactored CakeRequest code.

  • Loading branch information...
burzum committed Nov 20, 2014
1 parent 88bfa70 commit 728764c5432e44fa3f168711250e732b6635b79b
Showing with 49 additions and 8 deletions.
  1. +23 −8 lib/Cake/Network/CakeRequest.php
  2. +26 −0 lib/Cake/Test/Case/Network/CakeRequestTest.php
@@ -522,19 +522,34 @@ public function is($type) {
}
/**
* Detects if a specific header is present
* Gets the accept header from the current request.
*
* @return bool Returns an array of accept header values.
*/
public function getAcceptHeaders() {
$headers = array();
if (function_exists('getallheaders')) {
$headers = getallheaders();
$headers = explode(',', $headers['Accept']);
} else {
if (isset($_SERVER['HTTP_ACCEPT'])) {
$headers = explode(',', $_SERVER['HTTP_ACCEPT']);
}
}
return $headers;
}
/**
* 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.
*/
protected function _headerDetector($detect) {
$headers = getallheaders();
if (isset($headers['Accept'])) {
$headers = explode(',', $headers['Accept']);
foreach ($detect['header'] as $header) {
if (in_array($header, $headers)) {
return true;
}
$headers = $this->getAcceptHeaders();
foreach ($detect['header'] as $header) {
if (in_array($header, $headers)) {
return true;
}
}
return false;
@@ -52,6 +52,15 @@ 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);
}
}
/**
@@ -89,6 +98,23 @@ public function tearDown() {
Configure::write('App', $this->_app);
}
/**
* Test the header detector.
*
* @return void
*/
public function testHeaderDetector() {
$request = $this->getMock('TestCakeRequest', array('getAcceptHeaders'));
$_SERVER['HTTP_ACCEPT'] = 'application/json';
$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));
}
/**
* Test that the autoparse = false constructor works.
*

0 comments on commit 728764c

Please sign in to comment.