mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Properly deprecate HTTP.cache_control
This commit is contained in:
parent
0f5420b6a5
commit
d12c2fe631
@ -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');
|
||||
|
@ -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()}
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user