public function PageCacheTest::testCacheableResponseResponses in Drupal 9
Same name and namespace in other branches
- 8 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testCacheableResponseResponses()
- 10 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testCacheableResponseResponses()
Tests cacheability of a CacheableResponse.
Tests the difference between having a controller return a plain Symfony Response object versus returning a Response object that implements the CacheableResponseInterface.
File
- core/
modules/ page_cache/ tests/ src/ Functional/ PageCacheTest.php, line 504
Class
- PageCacheTest
- Enables the page cache and tests it with various HTTP requests.
Namespace
Drupal\Tests\page_cache\FunctionalCode
public function testCacheableResponseResponses() {
$config = $this
->config('system.performance');
$config
->set('cache.page.max_age', 300);
$config
->save();
// GET a URL, which would be marked as a cache miss if it were cacheable.
$this
->drupalGet('/system-test/respond-response');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache');
$this
->assertSession()
->responseHeaderEquals('Cache-Control', 'must-revalidate, no-cache, private');
// GET it again, verify it's still not cached.
$this
->drupalGet('/system-test/respond-response');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache');
$this
->assertSession()
->responseHeaderEquals('Cache-Control', 'must-revalidate, no-cache, private');
// GET a URL, which would be marked as a cache miss if it were cacheable.
$this
->drupalGet('/system-test/respond-public-response');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache');
$this
->assertSession()
->responseHeaderEquals('Cache-Control', 'max-age=60, public');
// GET it again, verify it's still not cached.
$this
->drupalGet('/system-test/respond-public-response');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache');
$this
->assertSession()
->responseHeaderEquals('Cache-Control', 'max-age=60, public');
// GET a URL, which should be marked as a cache miss.
$this
->drupalGet('/system-test/respond-cacheable-response');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
$this
->assertSession()
->responseHeaderEquals('Cache-Control', 'max-age=300, public');
// GET it again, it should now be a cache hit.
$this
->drupalGet('/system-test/respond-cacheable-response');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
$this
->assertSession()
->responseHeaderEquals('Cache-Control', 'max-age=300, public');
// Uninstall page cache. This should flush all caches so the next call to a
// previously cached page should be a miss now.
$this->container
->get('module_installer')
->uninstall([
'page_cache',
]);
// GET a URL that was cached by Page Cache before, it should not be now.
$this
->drupalGet('/respond-cacheable-response');
$this
->assertSession()
->responseHeaderDoesNotExist('X-Drupal-Cache');
}