Skip to content

Commit

Permalink
Add withDisabledCache() header macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Nov 26, 2016
1 parent f4d7d35 commit 5326287
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Network/Response.php
Expand Up @@ -1166,6 +1166,7 @@ public function charset($charset = null)
* Sets the correct headers to instruct the client to not cache the response
*
* @return void
* @deprected 3.4.0 Use withDisabledCache() instead.
*/
public function disableCache()
{
Expand All @@ -1174,6 +1175,18 @@ public function disableCache()
$this->_setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
}

/**
* Create a new instance with headers to instruct the client to not cache the response
*
* @return static
*/
public function withDisabledCache()
{
return $this->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
->withHeader('Last-Modified', gmdate("D, d M Y H:i:s") . " GMT")
->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
}

/**
* Sets the correct headers to instruct the client to cache the response.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/Network/ResponseTest.php
Expand Up @@ -471,6 +471,26 @@ public function testDisableCache()
$this->assertEquals($expected, $response->header());
}

/**
* Tests the withDisabledCache method
*
* @return void
*/
public function testWithDisabledCache()
{
$response = new Response();
$expected = [
'Expires' => ['Mon, 26 Jul 1997 05:00:00 GMT'],
'Last-Modified' => [gmdate("D, d M Y H:i:s") . " GMT"],
'Cache-Control' => ['no-store, no-cache, must-revalidate, post-check=0, pre-check=0'],
'Content-Type' => ['text/html; charset=UTF-8'],
];
$new = $response->withDisabledCache();
$this->assertFalse($response->hasHeader('Expires'), 'Old instance not mutated.');

$this->assertEquals($expected, $new->getHeaders());
}

/**
* Tests the cache method
*
Expand Down

0 comments on commit 5326287

Please sign in to comment.