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) { function prefers($type = null) {
$this->__initializeTypes(); $this->__initializeTypes();
$accept = $this->accepts();
if ($type == null) { if ($type == null) {
if (empty($this->ext)) { if (empty($this->ext)) {
$accept = $this->accepts(null);
if (is_array($accept)) { if (is_array($accept)) {
return $accept[0]; return $accept[0];
} }
@ -510,19 +511,30 @@ class RequestHandlerComponent extends Object {
} }
return $this->ext; 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(); $accepts = array();
foreach ($types as $type) { foreach ($types as $type) {
if ($this->accepts($type)) { if (in_array($type, $accept)) {
$accepts[] = $type; $accepts[] = $type;
} }
} }
if (count($accepts) == 0) { if (count($accepts) === 0) {
return false; return false;
} elseif (count($accepts) == 1) { } elseif (count($types) === 1) {
return ($types[0] === $accepts[0]);
} elseif (count($accepts) === 1) {
return $accepts[0]; return $accepts[0];
} }

View file

@ -360,17 +360,20 @@ class RequestHandlerComponentTest extends CakeTestCase {
$this->assertNotEqual($this->RequestHandler->prefers(), 'rss'); $this->assertNotEqual($this->RequestHandler->prefers(), 'rss');
$this->RequestHandler->ext = 'rss'; $this->RequestHandler->ext = 'rss';
$this->assertEqual($this->RequestHandler->prefers(), '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'; $_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->_init();
$this->assertEqual($this->RequestHandler->prefers(), 'xml'); $this->assertEqual($this->RequestHandler->prefers(), 'xml');
$this->assertEqual($this->RequestHandler->accepts(array('js', 'xml', 'html')), 'xml'); $this->assertEqual($this->RequestHandler->accepts(array('js', 'xml', 'html')), 'xml');
$this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo'))); $this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
$_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5'; $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
$this->_init(); $this->_init();
$this->assertEqual($this->RequestHandler->prefers(), 'html'); $this->assertEqual($this->RequestHandler->prefers(), 'html');
$this->assertFalse($this->RequestHandler->prefers('rss'));
$this->assertFalse($this->RequestHandler->accepts('rss'));
} }
/** /**
* testCustomContent method * testCustomContent method