Skip to content

Commit

Permalink
Do not assume CONTENT_TYPE is available.
Browse files Browse the repository at this point in the history
In some server environments notably the CLI server, _SERVER['CONTENT_TYPE'] is not available.
In these cases, fall back to the HTTP_CONTENT_TYPE header.

Refs #GH-1661
  • Loading branch information
sime authored and markstory committed Oct 1, 2013
1 parent ad1b806 commit c998888
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Cake/Controller/Component/RequestHandlerComponent.php
Expand Up @@ -518,6 +518,9 @@ public function requestedWith($type = null) {
}

list($contentType) = explode(';', env('CONTENT_TYPE'));
if ($contentType === '') {
list($contentType) = explode(';', CakeRequest::header('CONTENT_TYPE'));
}
if (!$type) {
return $this->mapType($contentType);
}
Expand Down
Expand Up @@ -603,6 +603,16 @@ public function testRequestContentTypes() {
$result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
$this->assertFalse($result);

$_SERVER['REQUEST_METHOD'] = 'POST';
unset($_SERVER['CONTENT_TYPE']);
$_SERVER['HTTP_CONTENT_TYPE'] = 'application/json';

$result = $this->RequestHandler->requestedWith(array('json', 'xml'));
$this->assertEquals('json', $result);

$result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
$this->assertFalse($result);

$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
$this->assertTrue($this->RequestHandler->isXml());
$this->assertFalse($this->RequestHandler->isAtom());
Expand Down

0 comments on commit c998888

Please sign in to comment.