Jelajahi Sumber

merged branch Seldaek/http (PR #1414)

Commits
-------

7db0b95 [HttpKernel] Removed unnecessary strtoupper
0891c57 [HttpKernel] Added test
1350645 [HttpKernel] Uppercased a few http methods
05c9906 [HttpKernel] Suppress response content for 304 responses out of the cache

Discussion
----------

HttpCache changes for 304 responses

Fixes #1413
Fabien Potencier 14 tahun lalu
induk
melakukan
f406e3d6a3

+ 4 - 4
src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php

@@ -55,7 +55,7 @@ class HttpCache implements HttpKernelInterface
      *
      *   * allow_revalidate       Specifies whether the client can force a cache revalidate by including
      *                            a Cache-Control "max-age=0" directive in the request. Set it to ``true``
-      *                            for compliance with RFC 2616. (default: false)
+     *                            for compliance with RFC 2616. (default: false)
      *
      *   * stale_while_revalidate Specifies the default number of seconds (the granularity is the second as the
      *                            Response TTL precision is a second) during which the cache can immediately return
@@ -317,7 +317,7 @@ class HttpCache implements HttpKernelInterface
         $subRequest = clone $request;
 
         // send no head requests because we want content
-        $subRequest->setMethod('get');
+        $subRequest->setMethod('GET');
 
         // add our cached last-modified validator
         $subRequest->headers->set('if_modified_since', $entry->headers->get('Last-Modified'));
@@ -378,7 +378,7 @@ class HttpCache implements HttpKernelInterface
         $subRequest = clone $request;
 
         // send no head requests because we want content
-        $subRequest->setMethod('get');
+        $subRequest->setMethod('GET');
 
         // avoid that the backend sends no content
         $subRequest->headers->remove('if_modified_since');
@@ -550,7 +550,7 @@ class HttpCache implements HttpKernelInterface
      */
     private function restoreResponseBody(Request $request, Response $response)
     {
-        if ('head' === strtolower($request->getMethod())) {
+        if ('HEAD' === $request->getMethod() || 304 === $response->getStatusCode()) {
             $response->setContent('');
             $response->headers->remove('X-Body-Eval');
             $response->headers->remove('X-Body-File');

+ 20 - 0
tests/Symfony/Tests/Component/HttpKernel/HttpCache/HttpCacheTest.php

@@ -763,6 +763,26 @@ class HttpCacheTest extends HttpCacheTestCase
         $this->assertEquals(strlen('Hello World'), $this->response->headers->get('Content-Length'));
     }
 
+    public function testSendsNoContentWhenFresh()
+    {
+        $time = \DateTime::createFromFormat('U', time());
+        $that = $this;
+        $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($that, $time)
+        {
+            $response->headers->set('Cache-Control', 'public, max-age=10');
+            $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
+        });
+
+        $this->request('GET', '/');
+        $this->assertHttpKernelIsCalled();
+        $this->assertEquals('Hello World', $this->response->getContent());
+
+        $this->request('GET', '/', array('HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
+        $this->assertHttpKernelIsNotCalled();
+        $this->assertEquals(304, $this->response->getStatusCode());
+        $this->assertEquals('', $this->response->getContent());
+    }
+
     public function testInvalidatesCachedResponsesOnPost()
     {
         $this->setNextResponse(200, array(), 'Hello World', function ($request, $response)