Starting to update RequestHandler methods to use CakeRequest. RequestHandler will provide alias/mapped access to features in CakeRequest. Tests updated and expanded.

This commit is contained in:
mark_story 2010-07-01 00:13:13 -04:00
parent 6e516cae73
commit 8d32ad2821
2 changed files with 79 additions and 31 deletions

View file

@ -414,7 +414,17 @@ class RequestHandlerComponent extends Object {
/**
* Determines which content types the client accepts. Acceptance is based on
* the file extension parsed by the Router (if present), and by the HTTP_ACCEPT
* header.
* header. Unlike CakeRequest::accepts() this method deals entirely with mapped content types.
*
* Usage:
*
* `$this->RequestHandler->accepts(array('xml', 'html', 'json'));`
*
* Returns true if the client accepts any of the supplied types.
*
* `$this->RequestHandler->accepts('xml');`
*
* Returns true if the client accepts xml.
*
* @param mixed $type Can be null (or no parameter), a string type name, or an
* array of types
@ -428,35 +438,24 @@ class RequestHandlerComponent extends Object {
function accepts($type = null) {
$this->__initializeTypes();
if ($type == null) {
return $this->mapType($this->__acceptTypes);
$accepted = $this->request->accepts();
if ($type == null) {
return $this->mapType($accepted);
} elseif (is_array($type)) {
foreach ($type as $t) {
if ($this->accepts($t) == true) {
$t = $this->mapAlias($t);
if (in_array($t, $accepted)) {
return true;
}
}
return false;
} elseif (is_string($type)) {
if (!isset($this->__requestContent[$type])) {
return false;
}
$content = $this->__requestContent[$type];
if (is_array($content)) {
foreach ($content as $c) {
if (in_array($c, $this->__acceptTypes)) {
return true;
}
}
} else {
if (in_array($content, $this->__acceptTypes)) {
return true;
}
$type = $this->mapAlias($type);
if (in_array($type, $accepted)) {
return true;
}
return false;
}
return false;
}
@ -695,11 +694,7 @@ class RequestHandlerComponent extends Object {
*/
public function mapType($ctype) {
if (is_array($ctype)) {
$out = array();
foreach ($ctype as $t) {
$out[] = $this->mapType($t);
}
return $out;
return array_map(array($this, 'mapType'), $ctype);
} else {
$keys = array_keys($this->__requestContent);
$count = count($keys);
@ -718,6 +713,27 @@ class RequestHandlerComponent extends Object {
}
}
/**
* Maps a content type alias back to its mime-type(s)
*
* @param mixed $alias String alias to convert back into a content type. Or an array of aliases to map.
* @return mixed Null on an undefined alias. String value of the mapped alias type. If an
* alias maps to more than one content type, the first one will be returned.
*/
public function mapAlias($alias) {
if (is_array($alias)) {
return array_map(array($this, 'mapAlias'), $alias);
}
if (isset($this->__requestContent[$alias])) {
$types = $this->__requestContent[$alias];
if (is_array($types)) {
return $types[0];
}
return $types;
}
return null;
}
/**
* Initializes MIME types
*

View file

@ -512,13 +512,49 @@ class RequestHandlerComponentTest extends CakeTestCase {
$this->assertFalse($this->RequestHandler->isPut());
}
/**
* test that map alias converts aliases to content types.
*
* @return void
*/
function testMapAlias() {
$result = $this->RequestHandler->mapAlias('xml');
$this->assertEquals('application/xml', $result);
$result = $this->RequestHandler->mapAlias('text/html');
$this->assertNull($result);
$result = $this->RequestHandler->mapAlias('wap');
$this->assertEquals('text/vnd.wap.wml', $result);
$result = $this->RequestHandler->mapAlias(array('xml', 'js', 'json'));
$expected = array('application/xml', 'text/javascript', 'application/json');
$this->assertEquals($expected, $result);
}
/**
* test accepts() on the component
*
* @return void
*/
function testAccepts() {
$_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->accepts(array('js', 'xml', 'html')), 'xml');
$this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
$_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
$this->_init();
$this->assertFalse($this->RequestHandler->accepts('rss'));
}
/**
* test accepts and prefers methods.
*
* @access public
* @return void
*/
function testAcceptsAndPrefers() {
function testPrefers() {
$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
$this->_init();
$this->assertNotEqual($this->RequestHandler->prefers(), 'rss');
@ -526,19 +562,15 @@ class RequestHandlerComponentTest extends CakeTestCase {
$this->assertEqual($this->RequestHandler->prefers(), 'rss');
$this->assertFalse($this->RequestHandler->prefers('xml'));
$this->assertEqual($this->RequestHandler->prefers(array('js', 'xml', 'xhtml')), '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'));
}
/**