Browse Source

Implement withMaxAge()

Mark Story 9 years ago
parent
commit
c4bfdbb21d
2 changed files with 27 additions and 1 deletions
  1. 18 0
      src/Network/Response.php
  2. 9 1
      tests/TestCase/Network/ResponseTest.php

+ 18 - 0
src/Network/Response.php

@@ -1334,6 +1334,24 @@ class Response implements ResponseInterface
     }
 
     /**
+     * Create an instance with Cache-Control max-age directive set.
+     *
+     * The max-age is the number of seconds after which the response should no longer be considered
+     * a good candidate to be fetched from the local (client) cache.
+     *
+     * @param int $seconds The seconds a cached response can be considered valid
+     * @return static
+     */
+    public function withMaxAge($seconds)
+    {
+        $new = clone $this;
+        $new->_cacheDirectives['max-age'] = $seconds;
+        $new->_setCacheControl();
+
+        return $new;
+    }
+
+    /**
      * Sets the Cache-Control must-revalidate directive.
      * must-revalidate indicates that the response should not be served
      * stale by a cache under any circumstance without first revalidating

+ 9 - 1
tests/TestCase/Network/ResponseTest.php

@@ -879,7 +879,15 @@ class ResponseTest extends TestCase
      */
     public function testWithMaxAge()
     {
-        $this->markTestIncomplete();
+        $response = new Response();
+        $this->assertFalse($response->hasHeader('Cache-Control'));
+
+        $new = $response->withMaxAge(3600);
+        $this->assertEquals('max-age=3600', $new->getHeaderLine('Cache-Control'));
+
+        $new = $response->withMaxAge(3600)
+            ->withSharable(false);
+        $this->assertEquals('max-age=3600, private', $new->getHeaderLine('Cache-Control'));
     }
 
     /**