Skip to content
Permalink
Browse files

Implement withSharable()

  • Loading branch information...
markstory committed Dec 2, 2016
1 parent 9d7c22f commit 681a38765b324f3dc9993e39158bde56596209f0
Showing with 37 additions and 1 deletion.
  1. +24 −0 src/Network/Response.php
  2. +13 −1 tests/TestCase/Network/ResponseTest.php
@@ -1264,6 +1264,30 @@ public function sharable($public = null, $time = null)
return (bool)$public;
}
/**
* Create a new instace with the public/private Cache-Control directive set.
*
* @param bool $public If set to true, the Cache-Control header will be set as public
* if set to false, the response will be set to private.
* @param int|null $time time in seconds after which the response should no longer be considered fresh.
* @return static
*/
public function withSharable($public, $time = null)
{
$new = clone $this;
unset($new->_cacheDirectives['private'], $new->_cacheDirectives['public']);
$key = $public ? 'public' : 'private';
$new->_cacheDirectives[$key] = true;
if ($time !== null) {
$new->_cacheDirectives['max-age'] = $time;
}
$new->_setCacheControl();
return $new;
}
/**
* Sets the Cache-Control s-maxage directive.
*
@@ -838,7 +838,19 @@ public function testSharable()
*/
public function testWithSharable()
{
$this->markTestIncomplete();
$response = new Response();
$new = $response->withSharable(true);
$this->assertFalse($response->hasHeader('Cache-Control'), 'old instance unchanged');
$this->assertEquals('public', $new->getHeaderLine('Cache-Control'));
$new = $response->withSharable(false);
$this->assertEquals('private', $new->getHeaderLine('Cache-Control'));
$new = $response->withSharable(true, 3600);
$this->assertEquals('public, max-age=3600', $new->getHeaderLine('Cache-Control'));
$new = $response->withSharable(false, 3600);
$this->assertEquals('private, max-age=3600', $new->getHeaderLine('Cache-Control'));
}
/**

0 comments on commit 681a387

Please sign in to comment.
You can’t perform that action at this time.