Skip to content

Commit

Permalink
Implement withMustRevalidate()
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 5, 2016
1 parent 966d3b7 commit f2f195f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/Network/Response.php
Expand Up @@ -1376,6 +1376,31 @@ public function mustRevalidate($enable = null)
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
Expand Down
51 changes: 48 additions & 3 deletions tests/TestCase/Network/ResponseTest.php
Expand Up @@ -555,6 +555,16 @@ public function testCache()
$this->assertEquals($expected, $response->header());
}

/**
* Tests the withCache method
*
* @return void
*/
public function testWithCache()
{
$this->markTestIncomplete();
}

/**
* Tests the compress method
*
Expand Down Expand Up @@ -776,6 +786,16 @@ public function testExpires()
$this->assertEquals($time->format($format) . ' GMT', $response->getHeaderLine('Expires'));
}

/**
* Tests the withExpires method
*
* @return void
*/
public function testWithExpires()
{
$this->markTestIncomplete();
}

/**
* Tests setting the modification date
*
Expand Down Expand Up @@ -804,6 +824,16 @@ public function testModified()
$this->assertEquals($time->format($format) . ' GMT', $response->getHeaderLine('Last-Modified'));
}

/**
* Tests the withModified method
*
* @return void
*/
public function testWithModified()
{
$this->markTestIncomplete();
}

/**
* Tests setting of public/private Cache-Control directives
*
Expand Down Expand Up @@ -916,9 +946,6 @@ public function testSharedMaxAge()
*/
public function testMustRevalidate()
{
$response = $this->getMockBuilder('Cake\Network\Response')
->setMethods(['_sendHeader', '_sendContent'])
->getMock();
$response = new Response();
$this->assertFalse($response->mustRevalidate());

Expand All @@ -935,6 +962,24 @@ public function testMustRevalidate()
$this->assertEquals('s-maxage=3600, must-revalidate', $response->getHeaderLine('Cache-Control'));
}

/**
* 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
*
Expand Down

0 comments on commit f2f195f

Please sign in to comment.