silverstripe-framework/tests/php/Control/HTTPCacheControlIntegrationTest.php

112 lines
4.8 KiB
PHP
Raw Normal View History

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,
];
protected function setUp(): void
2018-06-12 15:26:19 +02:00
{
parent::setUp();
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());
$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());
$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());
$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());
$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());
$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());
$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());
$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
}
}