Fixing bug in RequestHandler::prefers() not respecting accept order for single items, test case updated

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7634 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-09-19 19:48:22 +00:00
parent 740bd4e769
commit 91838d6d5d
2 changed files with 23 additions and 8 deletions

View file

@ -500,9 +500,10 @@ class RequestHandlerComponent extends Object {
*/
function prefers($type = null) {
$this->__initializeTypes();
$accept = $this->accepts();
if ($type == null) {
if (empty($this->ext)) {
$accept = $this->accepts(null);
if (is_array($accept)) {
return $accept[0];
}
@ -510,20 +511,31 @@ class RequestHandlerComponent extends Object {
}
return $this->ext;
}
App::import('Core', 'Set');
$types = Set::normalize($type, false);
if (is_string($type)) {
$types = array($type);
}
if (count($types) === 1) {
if (!empty($this->ext)) {
return ($types[0] == $this->ext);
}
return ($types[0] == $accept[0]);
}
$accepts = array();
foreach ($types as $type) {
if ($this->accepts($type)) {
if (in_array($type, $accept)) {
$accepts[] = $type;
}
}
if (count($accepts) == 0) {
if (count($accepts) === 0) {
return false;
} elseif (count($accepts) == 1) {
return $accepts[0];
} elseif (count($types) === 1) {
return ($types[0] === $accepts[0]);
} elseif (count($accepts) === 1) {
return $accepts[0];
}
$accepts = array_intersect($this->__acceptTypes, $accepts);

View file

@ -360,17 +360,20 @@ class RequestHandlerComponentTest extends CakeTestCase {
$this->assertNotEqual($this->RequestHandler->prefers(), 'rss');
$this->RequestHandler->ext = 'rss';
$this->assertEqual($this->RequestHandler->prefers(), 'rss');
$this->assertFalse($this->RequestHandler->prefers('xml'));
$this->assertTrue($this->RequestHandler->accepts('xml'));
$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
$this->_init();
$this->assertEqual($this->RequestHandler->prefers(), 'xml');
$this->assertEqual($this->RequestHandler->accepts(array('js', 'xml', 'html')), 'xml');
$this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
$_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
$this->_init();
$this->assertEqual($this->RequestHandler->prefers(), 'html');
$this->assertFalse($this->RequestHandler->prefers('rss'));
$this->assertFalse($this->RequestHandler->accepts('rss'));
}
/**
* testCustomContent method