Permalink
Browse files

Adding trimming of Accept headers to CakeRequest.

Fixes #1684
  • Loading branch information...
1 parent f060fda commit 5955cc6ed7b122aa47819bdd9f3344d7e09133c2 @markstory markstory committed Aug 26, 2011
Showing with 20 additions and 2 deletions.
  1. +1 −0 lib/Cake/Network/CakeRequest.php
  2. +19 −2 lib/Cake/Test/Case/Network/CakeRequestTest.php
@@ -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);
@@ -788,15 +788,32 @@ 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
*
* @return void

0 comments on commit 5955cc6

Please sign in to comment.