Properly deprecate HTTP.cache_control

This commit is contained in:
Ingo Schommer 2018-07-19 12:25:27 +12:00 committed by Daniel Hensby
parent 0f5420b6a5
commit d12c2fe631
No known key found for this signature in database
GPG Key ID: D8DEBC4C8E7BC8B9
2 changed files with 108 additions and 1 deletions

View File

@ -71,6 +71,7 @@ class HTTP
/**
* List of names to add to the Cache-Control header.
*
* @deprecated 4.2..5.0 Handled by HTTPCacheControlMiddleware instead
* @see HTTPCacheControlMiddleware::__construct()
* @config
* @var array Keys are cache control names, values are boolean flags
@ -80,7 +81,7 @@ class HTTP
/**
* Vary string; A comma separated list of var header names
*
* @deprecated 4.2..5.0 Handled by HTTPCacheMiddleware instead
* @deprecated 4.2..5.0 Handled by HTTPCacheControlMiddleware instead
* @config
* @var string|null
*/
@ -473,6 +474,7 @@ class HTTP
* Ensure that all deprecated HTTP cache settings are respected
*
* @deprecated 4.2..5.0 Use HTTPCacheControlMiddleware instead
* @throws \LogicException
* @param HTTPRequest $request
* @param HTTPResponse $response
*/
@ -509,6 +511,37 @@ class HTTP
$cacheControlMiddleware->addVary($configVary);
}
// Pass cache_control to middleware
$configCacheControl = $config->get('cache_control');
if ($configCacheControl) {
Deprecation::notice('5.0', 'Use HTTPCacheControlMiddleware API instead');
$supportedDirectives = ['max-age', 'no-cache', 'no-store', 'must-revalidate'];
if ($foundUnsupported = array_diff(array_keys($configCacheControl), $supportedDirectives)) {
throw new \LogicException(
'Found unsupported legacy directives in HTTP.cache_control: ' .
implode(', ', $foundUnsupported) .
'. Please use HTTPCacheControlMiddleware API instead'
);
}
if (isset($configCacheControl['max-age'])) {
$cacheControlMiddleware->setMaxAge($configCacheControl['max-age']);
}
if (isset($configCacheControl['no-cache'])) {
$cacheControlMiddleware->setNoCache((bool)$configCacheControl['no-cache']);
}
if (isset($configCacheControl['no-store'])) {
$cacheControlMiddleware->setNoStore((bool)$configCacheControl['no-store']);
}
if (isset($configCacheControl['must-revalidate'])) {
$cacheControlMiddleware->setMustRevalidate((bool)$configCacheControl['must-revalidate']);
}
}
// Set modification date
if (self::$modification_date) {
Deprecation::notice('5.0', 'Use HTTPCacheControlMiddleware::registerModificationDate() instead');

View File

@ -9,6 +9,7 @@ use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\FunctionalTest;
/**
@ -112,6 +113,79 @@ class HTTPTest extends FunctionalTest
$this->assertEmpty($v);
}
public function testDeprecatedVaryHandling()
{
/** @var Config */
Config::modify()->set(
HTTP::class,
'vary',
'X-Foo'
);
$response = new HTTPResponse('', 200);
$this->addCacheHeaders($response);
$header = $response->getHeader('Vary');
$this->assertContains('X-Foo', $header);
}
public function testDeprecatedCacheControlHandling()
{
HTTPCacheControlMiddleware::singleton()->publicCache();
/** @var Config */
Config::modify()->set(
HTTP::class,
'cache_control',
[
'no-store' => true,
'no-cache' => true,
]
);
$response = new HTTPResponse('', 200);
$this->addCacheHeaders($response);
$header = $response->getHeader('Cache-Control');
$this->assertContains('no-store', $header);
$this->assertContains('no-cache', $header);
}
public function testDeprecatedCacheControlHandlingOnMaxAge()
{
HTTPCacheControlMiddleware::singleton()->publicCache();
/** @var Config */
Config::modify()->set(
HTTP::class,
'cache_control',
[
// Needs to be separate from no-cache and no-store,
// since that would unset max-age
'max-age' => 99,
]
);
$response = new HTTPResponse('', 200);
$this->addCacheHeaders($response);
$header = $response->getHeader('Cache-Control');
$this->assertContains('max-age=99', $header);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessageRegExp /Found unsupported legacy directives in HTTP\.cache_control: unknown/
*/
public function testDeprecatedCacheControlHandlingThrowsWithUnknownDirectives()
{
/** @var Config */
Config::modify()->set(
HTTP::class,
'cache_control',
[
'no-store' => true,
'unknown' => true,
]
);
$response = new HTTPResponse('', 200);
$this->addCacheHeaders($response);
}
/**
* Tests {@link HTTP::getLinksIn()}
*/