Преглед изворни кода

[HttpFoundation] fix Cache-Control header when forcing the Response to have an Expires header field. The RFC2616 Bis indicates that if the Response has both max-age (or s-maxage) and Expires header field, the Cache-Control maxage wins over Expires. The previous code in ResponseHeaderBag always returned "private, max-age=0, must-revalidate" even if the Expires header field was forced.

This commit breaks functional tests in Symfony\Component\HttpKernel\HttpCache\HttpCacheTest.php. I tried to fix functional tests but I didn\'t manage to. For your information, the "try { } catch" block in the HttpKernel\HttpCache::lookup() method seems strange because I suspect line 274 to never leverage any exception...
hhamon пре 14 година
родитељ
комит
f985da5a9c

+ 2 - 2
src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php

@@ -118,7 +118,7 @@ class ResponseHeaderBag extends HeaderBag
 
         if (!$this->cacheControl) {
             // conservative by default
-            return 'private, max-age=0, must-revalidate';
+            return 'private, must-revalidate';
         }
 
         $header = $this->getCacheControlHeader();
@@ -133,4 +133,4 @@ class ResponseHeaderBag extends HeaderBag
 
         return $header;
     }
-}
+}

+ 13 - 4
tests/Symfony/Tests/Component/HttpFoundation/ResponseHeaderBagTest.php

@@ -26,16 +26,25 @@ class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($bag->hasCacheControlDirective('public'));
 
         $bag = new ResponseHeaderBag(array('ETag' => 'abcde'));
-        $this->assertEquals('private, max-age=0, must-revalidate', $bag->get('Cache-Control'));
+        $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
         $this->assertTrue($bag->hasCacheControlDirective('private'));
         $this->assertTrue($bag->hasCacheControlDirective('must-revalidate'));
-        $this->assertEquals(0, $bag->getCacheControlDirective('max-age'));
+        $this->assertFalse($bag->hasCacheControlDirective('max-age'));
+
+        $bag = new ResponseHeaderBag(array('Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT'));
+        $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
+
+        $bag = new ResponseHeaderBag(array(
+            'Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT',
+            'Cache-Control' => 'max-age=3600'
+        ));
+        $this->assertEquals('max-age=3600, private', $bag->get('Cache-Control'));
 
         $bag = new ResponseHeaderBag(array('Last-Modified' => 'abcde'));
-        $this->assertEquals('private, max-age=0, must-revalidate', $bag->get('Cache-Control'));
+        $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
 
         $bag = new ResponseHeaderBag(array('Etag' => 'abcde', 'Last-Modified' => 'abcde'));
-        $this->assertEquals('private, max-age=0, must-revalidate', $bag->get('Cache-Control'));
+        $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
 
         $bag = new ResponseHeaderBag(array('cache-control' => 'max-age=100'));
         $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));