diff options
author | jenkins-bot <jenkins-bot@gerrit.wikimedia.org> | 2022-01-10 01:54:17 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@wikimedia.org> | 2022-01-10 01:54:17 +0000 |
commit | 99d0848db797fcae01a4712b635c0d3409cd842c (patch) | |
tree | c8c4b74cdeab26762c3f865af4502e3707eb8d11 | |
parent | fab9b871a1aad3b8720cfc0beb7ad6cb88133c20 (diff) | |
parent | 2f6bfb126d5de65ac84b9bbdca6c59b8183c9537 (diff) |
Merge "Add tests for ApiMain::sendCacheHeaders"
-rw-r--r-- | tests/phpunit/includes/api/ApiMainTest.php | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/phpunit/includes/api/ApiMainTest.php b/tests/phpunit/includes/api/ApiMainTest.php index 0fdb3c5900b1..0c9d09f544b2 100644 --- a/tests/phpunit/includes/api/ApiMainTest.php +++ b/tests/phpunit/includes/api/ApiMainTest.php @@ -1172,4 +1172,51 @@ class ApiMainTest extends ApiTestCase { $this->assertFalse( $api->matchRequestedHeaders( 'Accept,Foo', $allowedHeaders ) ); $this->assertFalse( $api->matchRequestedHeaders( 'Accept, fOO', $allowedHeaders ) ); } + + /** + * @param string $cacheMode + * @param string|null $expectedVary + * @param string $expectedCacheControl + * @param array $requestData + * @param Config|null $config + * @dataProvider provideCacheHeaders + */ + public function testCacheHeaders( + string $cacheMode, + ?string $expectedVary, + string $expectedCacheControl, + array $requestData = [], + Config $config = null + ) { + $req = new FauxRequest( $requestData ); + $ctx = new RequestContext(); + $ctx->setRequest( $req ); + if ( $config ) { + $ctx->setConfig( $config ); + } + /** @var ApiMain|TestingAccessWrapper $api */ + $api = TestingAccessWrapper::newFromObject( new ApiMain( $ctx ) ); + + $api->setCacheMode( $cacheMode ); + $this->assertSame( $cacheMode, $api->mCacheMode, 'Cache mode precondition' ); + $api->sendCacheHeaders( false ); + + $this->assertSame( $expectedVary, $req->response()->getHeader( 'Vary' ), 'Vary' ); + $this->assertSame( $expectedCacheControl, $req->response()->getHeader( 'Cache-Control' ), 'Cache-Control' ); + } + + public function provideCacheHeaders(): Generator { + yield 'Private' => [ 'private', null, 'private, must-revalidate, max-age=0' ]; + yield 'Public' => [ + 'public', + 'Accept-Encoding, Treat-as-Untrusted, Cookie', + 'private, must-revalidate, max-age=0', + [ 'uselang' => 'en' ] + ]; + yield 'Anon public, user private' => [ + 'anon-public-user-private', + 'Accept-Encoding, Treat-as-Untrusted, Cookie', + 'private, must-revalidate, max-age=0' + ]; + } } |