2018-06-12 15:26:19 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Control\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Control\HTTP;
|
|
|
|
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
|
|
|
|
use SilverStripe\Control\Tests\HTTPCacheControlIntegrationTest\RuleController;
|
|
|
|
use SilverStripe\Control\Tests\HTTPCacheControlIntegrationTest\SessionController;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
|
|
|
|
class HTTPCacheControlIntegrationTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
protected static $extra_controllers = [
|
|
|
|
SessionController::class,
|
|
|
|
RuleController::class,
|
|
|
|
];
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2018-06-12 15:26:19 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2018-06-13 07:56:47 +02:00
|
|
|
HTTPCacheControlMiddleware::config()
|
|
|
|
->set('defaultState', 'disabled')
|
|
|
|
->set('defaultForcingLevel', 0);
|
2018-06-12 15:26:19 +02:00
|
|
|
HTTPCacheControlMiddleware::reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFormCSRF()
|
|
|
|
{
|
|
|
|
// CSRF sets caching to disabled
|
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_SessionController/showform');
|
|
|
|
$header = $response->getHeader('Cache-Control');
|
|
|
|
$this->assertFalse($response->isError());
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('public', $header);
|
|
|
|
$this->assertStringNotContainsString('private', $header);
|
|
|
|
$this->assertStringContainsString('no-cache', $header);
|
|
|
|
$this->assertStringContainsString('no-store', $header);
|
|
|
|
$this->assertStringContainsString('must-revalidate', $header);
|
2018-06-12 15:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPublicForm()
|
|
|
|
{
|
|
|
|
// Public forms (http get) allow public caching
|
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_SessionController/showpublicform');
|
|
|
|
$header = $response->getHeader('Cache-Control');
|
|
|
|
$this->assertFalse($response->isError());
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('public', $header);
|
|
|
|
$this->assertStringContainsString('must-revalidate', $header);
|
|
|
|
$this->assertStringNotContainsString('no-cache', $response->getHeader('Cache-Control'));
|
|
|
|
$this->assertStringNotContainsString('no-store', $response->getHeader('Cache-Control'));
|
2018-06-12 15:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrivateActionsError()
|
|
|
|
{
|
|
|
|
// disallowed private actions don't cache
|
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_SessionController/privateaction');
|
|
|
|
$header = $response->getHeader('Cache-Control');
|
|
|
|
$this->assertTrue($response->isError());
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('no-cache', $header);
|
|
|
|
$this->assertStringContainsString('no-store', $header);
|
|
|
|
$this->assertStringContainsString('must-revalidate', $header);
|
2018-06-12 15:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrivateActionsAuthenticated()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
// Authenticated actions are private cache
|
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_SessionController/privateaction');
|
|
|
|
$header = $response->getHeader('Cache-Control');
|
|
|
|
$this->assertFalse($response->isError());
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('private', $header);
|
|
|
|
$this->assertStringContainsString('must-revalidate', $header);
|
|
|
|
$this->assertStringNotContainsString('no-cache', $header);
|
|
|
|
$this->assertStringNotContainsString('no-store', $header);
|
2018-06-12 15:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrivateCache()
|
|
|
|
{
|
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_RuleController/privateaction');
|
|
|
|
$header = $response->getHeader('Cache-Control');
|
|
|
|
$this->assertFalse($response->isError());
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('private', $header);
|
|
|
|
$this->assertStringContainsString('must-revalidate', $header);
|
|
|
|
$this->assertStringNotContainsString('no-cache', $header);
|
|
|
|
$this->assertStringNotContainsString('no-store', $header);
|
2018-06-12 15:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPublicCache()
|
|
|
|
{
|
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_RuleController/publicaction');
|
|
|
|
$header = $response->getHeader('Cache-Control');
|
|
|
|
$this->assertFalse($response->isError());
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('public', $header);
|
|
|
|
$this->assertStringContainsString('must-revalidate', $header);
|
|
|
|
$this->assertStringNotContainsString('no-cache', $header);
|
|
|
|
$this->assertStringNotContainsString('no-store', $header);
|
|
|
|
$this->assertStringContainsString('max-age=9000', $header);
|
2018-06-12 15:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisabledCache()
|
|
|
|
{
|
|
|
|
$response = $this->get('HTTPCacheControlIntegrationTest_RuleController/disabledaction');
|
|
|
|
$header = $response->getHeader('Cache-Control');
|
|
|
|
$this->assertFalse($response->isError());
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('public', $header);
|
|
|
|
$this->assertStringNotContainsString('private', $header);
|
|
|
|
$this->assertStringContainsString('no-cache', $header);
|
|
|
|
$this->assertStringContainsString('no-store', $header);
|
|
|
|
$this->assertStringContainsString('must-revalidate', $header);
|
2018-06-12 15:26:19 +02:00
|
|
|
}
|
|
|
|
}
|