Add tests to OutputPage::sendCacheControl()

Before making improvements to OutputPage::sendCacheControl() the test coverage
should be increased.

Bug: T236862
Change-Id: I4a76b0894e2d574688e1cbd30f32ee7504801082
This commit is contained in:
David Barratt 2019-10-29 21:13:03 -04:00
parent 2a971f97d6
commit 441ff2003e
No known key found for this signature in database
GPG key ID: 8C82E156FE80A93E
2 changed files with 106 additions and 0 deletions

View file

@ -147,4 +147,11 @@ class FauxResponse extends WebResponse {
public function getCookies() {
return $this->cookies;
}
/**
* {@inheritdoc}
*/
public function hasCookies() {
return count( $this->cookies ) > 0;
}
}

View file

@ -3073,6 +3073,105 @@ class OutputPageTest extends MediaWikiTestCase {
];
}
/**
* @param array $options
* @param array $expectations
* @covers OutputPage::sendCacheControl
* @dataProvider provideSendCacheControl
*/
public function testSendCacheControl( array $options = [], array $expecations = [] ) {
$output = $this->newInstance( [
'LoggedOutMaxAge' => $options['loggedOutMaxAge'] ?? 0,
'UseCdn' => $options['useCdn'] ?? false,
] );
$output->enableClientCache( $options['enableClientCache'] ?? true );
$output->setCdnMaxAge( $options['cdnMaxAge'] ?? 0 );
if ( isset( $options['lastModified'] ) ) {
$output->setLastModified( $options['lastModified'] );
}
$response = $output->getRequest()->response();
if ( isset( $options['cookie'] ) ) {
$response->setCookie( 'test', 1234 );
}
$output->sendCacheControl();
$headers = [
'Vary' => 'Accept-Encoding, Cookie',
'Cache-Control' => 'private, must-revalidate, max-age=0',
'Pragma' => false,
'Expires' => true,
'Last-Modified' => false,
];
foreach ( $headers as $header => $default ) {
$value = $expecations[$header] ?? $default;
if ( $value === true ) {
$this->assertNotEmpty( $response->getHeader( $header ) );
} elseif ( $value === false ) {
$this->assertNull( $response->getHeader( $header ) );
} else {
$this->assertEquals( $value, $response->getHeader( $header ) );
}
}
}
public function provideSendCacheControl() {
return [
'Default' => [],
'Logged out max-age' => [
[
'loggedOutMaxAge' => 300,
],
[
'Cache-Control' => 'private, must-revalidate, max-age=300',
],
],
'Cookies' => [
[
'cookie' => true,
],
],
'Cookies with logged out max-age' => [
[
'loggedOutMaxAge' => 300,
'cookie' => true,
],
],
'Disable client cache' => [
[
'enableClientCache' => false,
],
[
'Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate',
'Pragma' => 'no-cache'
],
],
'Set last modified' => [
[
// 0 is the current time, so we'll use 1 instead.
'lastModified' => 1,
],
[
'Last-Modified' => 'Thu, 01 Jan 1970 00:00:01 GMT',
]
],
'Public' => [
[
'useCdn' => true,
'cdnMaxAge' => 300,
],
[
'Cache-Control' => 's-maxage=300, must-revalidate, max-age=0',
'Expires' => false,
],
],
];
}
private function newInstance(
array $config = [],
WebRequest $request = null,