Browse Source

Implement withMustRevalidate()

Mark Story 9 years ago
parent
commit
f2f195f346
2 changed files with 73 additions and 3 deletions
  1. 25 0
      src/Network/Response.php
  2. 48 3
      tests/TestCase/Network/ResponseTest.php

+ 25 - 0
src/Network/Response.php

@@ -1376,6 +1376,31 @@ class Response implements ResponseInterface
         return array_key_exists('must-revalidate', $this->_cacheDirectives);
     }
 
+
+    /**
+     * Create an instance with Cache-Control must-revalidate directive set.
+     *
+     * 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
+     * with the origin.
+     *
+     * @param bool $enable If boolean sets or unsets the directive.
+     * @return static
+     */
+    public function withMustRevalidate($enable)
+    {
+        $new = clone $this;
+        if ($enable) {
+            $new->_cacheDirectives['must-revalidate'] = true;
+        } else {
+            unset($new->_cacheDirectives['must-revalidate']);
+        }
+        $new->_setCacheControl();
+
+        return $new;
+    }
+
     /**
      * Helper method to generate a valid Cache-Control header from the options set
      * in other methods

+ 48 - 3
tests/TestCase/Network/ResponseTest.php

@@ -556,6 +556,16 @@ class ResponseTest extends TestCase
     }
 
     /**
+     * Tests the withCache method
+     *
+     * @return void
+     */
+    public function testWithCache()
+    {
+        $this->markTestIncomplete();
+    }
+
+    /**
      * Tests the compress method
      *
      * @return void
@@ -777,6 +787,16 @@ class ResponseTest extends TestCase
     }
 
     /**
+     * Tests the withExpires method
+     *
+     * @return void
+     */
+    public function testWithExpires()
+    {
+        $this->markTestIncomplete();
+    }
+
+    /**
      * Tests setting the modification date
      *
      * @return void
@@ -805,6 +825,16 @@ class ResponseTest extends TestCase
     }
 
     /**
+     * Tests the withModified method
+     *
+     * @return void
+     */
+    public function testWithModified()
+    {
+        $this->markTestIncomplete();
+    }
+
+    /**
      * Tests setting of public/private Cache-Control directives
      *
      * @return void
@@ -916,9 +946,6 @@ class ResponseTest extends TestCase
      */
     public function testMustRevalidate()
     {
-        $response = $this->getMockBuilder('Cake\Network\Response')
-            ->setMethods(['_sendHeader', '_sendContent'])
-            ->getMock();
         $response = new Response();
         $this->assertFalse($response->mustRevalidate());
 
@@ -936,6 +963,24 @@ class ResponseTest extends TestCase
     }
 
     /**
+     * Tests setting of must-revalidate Cache-Control directive
+     *
+     * @return void
+     */
+    public function testWithMustRevalidate()
+    {
+        $response = new Response();
+        $this->assertFalse($response->hasHeader('Cache-Control'));
+
+        $new = $response->withMustRevalidate(true);
+        $this->assertFalse($response->hasHeader('Cache-Control'));
+        $this->assertEquals('must-revalidate', $new->getHeaderLine('Cache-Control'));
+
+        $new = $new->withMustRevalidate(false);
+        $this->assertEmpty($new->getHeaderLine('Cache-Control'));
+    }
+
+    /**
      * Tests getting/setting the Vary header
      *
      * @return void