Skip to content

Commit

Permalink
Adding trimming of Accept headers to CakeRequest.
Browse files Browse the repository at this point in the history
Fixes #1684
  • Loading branch information
markstory committed Aug 26, 2011
1 parent f060fda commit 5955cc6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/Cake/Network/CakeRequest.php
Expand Up @@ -611,6 +611,7 @@ public function subdomains($tldLength = 1) {
*/
public function accepts($type = null) {
$acceptTypes = explode(',', $this->header('accept'));
$acceptTypes = array_map('trim', $acceptTypes);
foreach ($acceptTypes as $i => $accepted) {
if (strpos($accepted, ';') !== false) {
list($accepted) = explode(';', $accepted);
Expand Down
21 changes: 19 additions & 2 deletions lib/Cake/Test/Case/Network/CakeRequestTest.php
Expand Up @@ -788,14 +788,31 @@ public function testAccepts() {
'text/xml', 'application/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png'
);
$this->assertEquals($expected, $result, 'Content types differ.');

$result = $request->accepts('text/html');
$this->assertTrue($result);

$result = $request->accepts('image/gif');
$this->assertFalse($result);
}

/**
* Test that accept header types are trimmed for comparisons.
*
* @return void
*/
public function testAcceptWithWhitespace() {
$_SERVER['HTTP_ACCEPT'] = 'text/xml , text/html , text/plain,image/png';
$request = new CakeRequest('/', false);
$result = $request->accepts();
$expected = array(
'text/xml', 'text/html', 'text/plain', 'image/png'
);
$this->assertEquals($expected, $result, 'Content types differ.');

$this->assertTrue($request->accepts('text/html'));
}

/**
* testBaseUrlAndWebrootWithModRewrite method
*
Expand Down

0 comments on commit 5955cc6

Please sign in to comment.