123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\HttpKernel\Tests\HttpCache;
- use Symfony\Component\HttpKernel\HttpCache\HttpCache;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\HttpKernelInterface;
- /**
- * @group time-sensitive
- */
- class HttpCacheTest extends HttpCacheTestCase
- {
- public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
- {
- $storeMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\HttpCache\\StoreInterface')
- ->disableOriginalConstructor()
- ->getMock();
- // does not implement TerminableInterface
- $kernel = new TestKernel();
- $httpCache = new HttpCache($kernel, $storeMock);
- $httpCache->terminate(Request::create('/'), new Response());
- $this->assertFalse($kernel->terminateCalled, 'terminate() is never called if the kernel class does not implement TerminableInterface');
- // implements TerminableInterface
- $kernelMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Kernel')
- ->disableOriginalConstructor()
- ->setMethods(array('terminate', 'registerBundles', 'registerContainerConfiguration'))
- ->getMock();
- $kernelMock->expects($this->once())
- ->method('terminate');
- $kernel = new HttpCache($kernelMock, $storeMock);
- $kernel->terminate(Request::create('/'), new Response());
- }
- public function testPassesOnNonGetHeadRequests()
- {
- $this->setNextResponse(200);
- $this->request('POST', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertResponseOk();
- $this->assertTraceContains('pass');
- $this->assertFalse($this->response->headers->has('Age'));
- }
- public function testInvalidatesOnPostPutDeleteRequests()
- {
- foreach (array('post', 'put', 'delete') as $method) {
- $this->setNextResponse(200);
- $this->request($method, '/');
- $this->assertHttpKernelIsCalled();
- $this->assertResponseOk();
- $this->assertTraceContains('invalidate');
- $this->assertTraceContains('pass');
- }
- }
- public function testDoesNotCacheWithAuthorizationRequestHeaderAndNonPublicResponse()
- {
- $this->setNextResponse(200, array('ETag' => '"Foo"'));
- $this->request('GET', '/', array('HTTP_AUTHORIZATION' => 'basic foobarbaz'));
- $this->assertHttpKernelIsCalled();
- $this->assertResponseOk();
- $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
- $this->assertTraceContains('miss');
- $this->assertTraceNotContains('store');
- $this->assertFalse($this->response->headers->has('Age'));
- }
- public function testDoesCacheWithAuthorizationRequestHeaderAndPublicResponse()
- {
- $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '"Foo"'));
- $this->request('GET', '/', array('HTTP_AUTHORIZATION' => 'basic foobarbaz'));
- $this->assertHttpKernelIsCalled();
- $this->assertResponseOk();
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $this->assertTrue($this->response->headers->has('Age'));
- $this->assertEquals('public', $this->response->headers->get('Cache-Control'));
- }
- public function testDoesNotCacheWithCookieHeaderAndNonPublicResponse()
- {
- $this->setNextResponse(200, array('ETag' => '"Foo"'));
- $this->request('GET', '/', array(), array('foo' => 'bar'));
- $this->assertHttpKernelIsCalled();
- $this->assertResponseOk();
- $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
- $this->assertTraceContains('miss');
- $this->assertTraceNotContains('store');
- $this->assertFalse($this->response->headers->has('Age'));
- }
- public function testDoesNotCacheRequestsWithACookieHeader()
- {
- $this->setNextResponse(200);
- $this->request('GET', '/', array(), array('foo' => 'bar'));
- $this->assertHttpKernelIsCalled();
- $this->assertResponseOk();
- $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
- $this->assertTraceContains('miss');
- $this->assertTraceNotContains('store');
- $this->assertFalse($this->response->headers->has('Age'));
- }
- public function testRespondsWith304WhenIfModifiedSinceMatchesLastModified()
- {
- $time = \DateTime::createFromFormat('U', time());
- $this->setNextResponse(200, array('Cache-Control' => 'public', 'Last-Modified' => $time->format(DATE_RFC2822), 'Content-Type' => 'text/plain'), 'Hello World');
- $this->request('GET', '/', array('HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(304, $this->response->getStatusCode());
- $this->assertEquals('', $this->response->headers->get('Content-Type'));
- $this->assertEmpty($this->response->getContent());
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- }
- public function testRespondsWith304WhenIfNoneMatchMatchesETag()
- {
- $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '12345', 'Content-Type' => 'text/plain'), 'Hello World');
- $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345'));
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(304, $this->response->getStatusCode());
- $this->assertEquals('', $this->response->headers->get('Content-Type'));
- $this->assertTrue($this->response->headers->has('ETag'));
- $this->assertEmpty($this->response->getContent());
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- }
- public function testRespondsWith304OnlyIfIfNoneMatchAndIfModifiedSinceBothMatch()
- {
- $time = \DateTime::createFromFormat('U', time());
- $this->setNextResponse(200, array(), '', function ($request, $response) use ($time) {
- $response->setStatusCode(200);
- $response->headers->set('ETag', '12345');
- $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
- $response->headers->set('Content-Type', 'text/plain');
- $response->setContent('Hello World');
- });
- // only ETag matches
- $t = \DateTime::createFromFormat('U', time() - 3600);
- $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => $t->format(DATE_RFC2822)));
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- // only Last-Modified matches
- $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '1234', 'HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- // Both matches
- $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(304, $this->response->getStatusCode());
- }
- public function testIncrementsMaxAgeWhenNoDateIsSpecifiedEventWhenUsingETag()
- {
- $this->setNextResponse(
- 200,
- array(
- 'ETag' => '1234',
- 'Cache-Control' => 'public, s-maxage=60',
- )
- );
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- sleep(2);
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('fresh');
- $this->assertEquals(2, $this->response->headers->get('Age'));
- }
- public function testValidatesPrivateResponsesCachedOnTheClient()
- {
- $this->setNextResponse(200, array(), '', function ($request, $response) {
- $etags = preg_split('/\s*,\s*/', $request->headers->get('IF_NONE_MATCH'));
- if ($request->cookies->has('authenticated')) {
- $response->headers->set('Cache-Control', 'private, no-store');
- $response->setETag('"private tag"');
- if (in_array('"private tag"', $etags)) {
- $response->setStatusCode(304);
- } else {
- $response->setStatusCode(200);
- $response->headers->set('Content-Type', 'text/plain');
- $response->setContent('private data');
- }
- } else {
- $response->headers->set('Cache-Control', 'public');
- $response->setETag('"public tag"');
- if (in_array('"public tag"', $etags)) {
- $response->setStatusCode(304);
- } else {
- $response->setStatusCode(200);
- $response->headers->set('Content-Type', 'text/plain');
- $response->setContent('public data');
- }
- }
- });
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('"public tag"', $this->response->headers->get('ETag'));
- $this->assertEquals('public data', $this->response->getContent());
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $this->request('GET', '/', array(), array('authenticated' => ''));
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('"private tag"', $this->response->headers->get('ETag'));
- $this->assertEquals('private data', $this->response->getContent());
- $this->assertTraceContains('stale');
- $this->assertTraceContains('invalid');
- $this->assertTraceNotContains('store');
- }
- public function testStoresResponsesWhenNoCacheRequestDirectivePresent()
- {
- $time = \DateTime::createFromFormat('U', time() + 5);
- $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
- $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
- $this->assertHttpKernelIsCalled();
- $this->assertTraceContains('store');
- $this->assertTrue($this->response->headers->has('Age'));
- }
- public function testReloadsResponsesWhenCacheHitsButNoCacheRequestDirectivePresentWhenAllowReloadIsSetTrue()
- {
- $count = 0;
- $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=10000'), '', function ($request, $response) use (&$count) {
- ++$count;
- $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
- });
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('store');
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('fresh');
- $this->cacheConfig['allow_reload'] = true;
- $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Goodbye World', $this->response->getContent());
- $this->assertTraceContains('reload');
- $this->assertTraceContains('store');
- }
- public function testDoesNotReloadResponsesWhenAllowReloadIsSetFalseDefault()
- {
- $count = 0;
- $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=10000'), '', function ($request, $response) use (&$count) {
- ++$count;
- $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
- });
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('store');
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('fresh');
- $this->cacheConfig['allow_reload'] = false;
- $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceNotContains('reload');
- $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceNotContains('reload');
- }
- public function testRevalidatesFreshCacheEntryWhenMaxAgeRequestDirectiveIsExceededWhenAllowRevalidateOptionIsSetTrue()
- {
- $count = 0;
- $this->setNextResponse(200, array(), '', function ($request, $response) use (&$count) {
- ++$count;
- $response->headers->set('Cache-Control', 'public, max-age=10000');
- $response->setETag($count);
- $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
- });
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('store');
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('fresh');
- $this->cacheConfig['allow_revalidate'] = true;
- $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Goodbye World', $this->response->getContent());
- $this->assertTraceContains('stale');
- $this->assertTraceContains('invalid');
- $this->assertTraceContains('store');
- }
- public function testDoesNotRevalidateFreshCacheEntryWhenEnableRevalidateOptionIsSetFalseDefault()
- {
- $count = 0;
- $this->setNextResponse(200, array(), '', function ($request, $response) use (&$count) {
- ++$count;
- $response->headers->set('Cache-Control', 'public, max-age=10000');
- $response->setETag($count);
- $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
- });
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('store');
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('fresh');
- $this->cacheConfig['allow_revalidate'] = false;
- $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceNotContains('stale');
- $this->assertTraceNotContains('invalid');
- $this->assertTraceContains('fresh');
- $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceNotContains('stale');
- $this->assertTraceNotContains('invalid');
- $this->assertTraceContains('fresh');
- }
- public function testFetchesResponseFromBackendWhenCacheMisses()
- {
- $time = \DateTime::createFromFormat('U', time() + 5);
- $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('miss');
- $this->assertTrue($this->response->headers->has('Age'));
- }
- public function testDoesNotCacheSomeStatusCodeResponses()
- {
- foreach (array_merge(range(201, 202), range(204, 206), range(303, 305), range(400, 403), range(405, 409), range(411, 417), range(500, 505)) as $code) {
- $time = \DateTime::createFromFormat('U', time() + 5);
- $this->setNextResponse($code, array('Expires' => $time->format(DATE_RFC2822)));
- $this->request('GET', '/');
- $this->assertEquals($code, $this->response->getStatusCode());
- $this->assertTraceNotContains('store');
- $this->assertFalse($this->response->headers->has('Age'));
- }
- }
- public function testDoesNotCacheResponsesWithExplicitNoStoreDirective()
- {
- $time = \DateTime::createFromFormat('U', time() + 5);
- $this->setNextResponse(200, array('Expires' => $time->format(DATE_RFC2822), 'Cache-Control' => 'no-store'));
- $this->request('GET', '/');
- $this->assertTraceNotContains('store');
- $this->assertFalse($this->response->headers->has('Age'));
- }
- public function testDoesNotCacheResponsesWithoutFreshnessInformationOrAValidator()
- {
- $this->setNextResponse();
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceNotContains('store');
- }
- public function testCachesResponsesWithExplicitNoCacheDirective()
- {
- $time = \DateTime::createFromFormat('U', time() + 5);
- $this->setNextResponse(200, array('Expires' => $time->format(DATE_RFC2822), 'Cache-Control' => 'public, no-cache'));
- $this->request('GET', '/');
- $this->assertTraceContains('store');
- $this->assertTrue($this->response->headers->has('Age'));
- }
- public function testCachesResponsesWithAnExpirationHeader()
- {
- $time = \DateTime::createFromFormat('U', time() + 5);
- $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertNotNull($this->response->headers->get('Date'));
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $values = $this->getMetaStorageValues();
- $this->assertCount(1, $values);
- }
- public function testCachesResponsesWithAMaxAgeDirective()
- {
- $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=5'));
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertNotNull($this->response->headers->get('Date'));
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $values = $this->getMetaStorageValues();
- $this->assertCount(1, $values);
- }
- public function testCachesResponsesWithASMaxAgeDirective()
- {
- $this->setNextResponse(200, array('Cache-Control' => 's-maxage=5'));
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertNotNull($this->response->headers->get('Date'));
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $values = $this->getMetaStorageValues();
- $this->assertCount(1, $values);
- }
- public function testCachesResponsesWithALastModifiedValidatorButNoFreshnessInformation()
- {
- $time = \DateTime::createFromFormat('U', time());
- $this->setNextResponse(200, array('Cache-Control' => 'public', 'Last-Modified' => $time->format(DATE_RFC2822)));
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- }
- public function testCachesResponsesWithAnETagValidatorButNoFreshnessInformation()
- {
- $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '"123456"'));
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- }
- public function testHitsCachedResponsesWithExpiresHeader()
- {
- $time1 = \DateTime::createFromFormat('U', time() - 5);
- $time2 = \DateTime::createFromFormat('U', time() + 5);
- $this->setNextResponse(200, array('Cache-Control' => 'public', 'Date' => $time1->format(DATE_RFC2822), 'Expires' => $time2->format(DATE_RFC2822)));
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertNotNull($this->response->headers->get('Date'));
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
- $this->assertTrue($this->response->headers->get('Age') > 0);
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- $this->assertTraceContains('fresh');
- $this->assertTraceNotContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- }
- public function testHitsCachedResponseWithMaxAgeDirective()
- {
- $time = \DateTime::createFromFormat('U', time() - 5);
- $this->setNextResponse(200, array('Date' => $time->format(DATE_RFC2822), 'Cache-Control' => 'public, max-age=10'));
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertNotNull($this->response->headers->get('Date'));
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
- $this->assertTrue($this->response->headers->get('Age') > 0);
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- $this->assertTraceContains('fresh');
- $this->assertTraceNotContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- }
- public function testDegradationWhenCacheLocked()
- {
- if ('\\' === DIRECTORY_SEPARATOR) {
- $this->markTestSkipped('Skips on windows to avoid permissions issues.');
- }
- $this->cacheConfig['stale_while_revalidate'] = 10;
- // The prescence of Last-Modified makes this cacheable (because Response::isValidateable() then).
- $this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=5', 'Last-Modified' => 'some while ago'), 'Old response');
- $this->request('GET', '/'); // warm the cache
- // Now, lock the cache
- $concurrentRequest = Request::create('/', 'GET');
- $this->store->lock($concurrentRequest);
- /*
- * After 10s, the cached response has become stale. Yet, we're still within the "stale_while_revalidate"
- * timeout so we may serve the stale response.
- */
- sleep(10);
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('stale-while-revalidate');
- $this->assertEquals('Old response', $this->response->getContent());
- /*
- * Another 10s later, stale_while_revalidate is over. Resort to serving the old response, but
- * do so with a "server unavailable" message.
- */
- sleep(10);
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(503, $this->response->getStatusCode());
- $this->assertEquals('Old response', $this->response->getContent());
- }
- public function testHitsCachedResponseWithSMaxAgeDirective()
- {
- $time = \DateTime::createFromFormat('U', time() - 5);
- $this->setNextResponse(200, array('Date' => $time->format(DATE_RFC2822), 'Cache-Control' => 's-maxage=10, max-age=0'));
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertNotNull($this->response->headers->get('Date'));
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
- $this->assertTrue($this->response->headers->get('Age') > 0);
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- $this->assertTraceContains('fresh');
- $this->assertTraceNotContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- }
- public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformation()
- {
- $this->setNextResponse();
- $this->cacheConfig['default_ttl'] = 10;
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertRegExp('/s-maxage=10/', $this->response->headers->get('Cache-Control'));
- $this->cacheConfig['default_ttl'] = 10;
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('fresh');
- $this->assertTraceNotContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertRegExp('/s-maxage=10/', $this->response->headers->get('Cache-Control'));
- }
- public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAfterTtlWasExpired()
- {
- $this->setNextResponse();
- $this->cacheConfig['default_ttl'] = 2;
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('fresh');
- $this->assertTraceNotContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
- // expires the cache
- $values = $this->getMetaStorageValues();
- $this->assertCount(1, $values);
- $tmp = unserialize($values[0]);
- $time = \DateTime::createFromFormat('U', time() - 5);
- $tmp[0][1]['date'] = $time->format(DATE_RFC2822);
- $r = new \ReflectionObject($this->store);
- $m = $r->getMethod('save');
- $m->setAccessible(true);
- $m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('stale');
- $this->assertTraceContains('invalid');
- $this->assertTraceContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
- $this->setNextResponse();
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('fresh');
- $this->assertTraceNotContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
- }
- public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAfterTtlWasExpiredWithStatus304()
- {
- $this->setNextResponse();
- $this->cacheConfig['default_ttl'] = 2;
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('fresh');
- $this->assertTraceNotContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- // expires the cache
- $values = $this->getMetaStorageValues();
- $this->assertCount(1, $values);
- $tmp = unserialize($values[0]);
- $time = \DateTime::createFromFormat('U', time() - 5);
- $tmp[0][1]['date'] = $time->format(DATE_RFC2822);
- $r = new \ReflectionObject($this->store);
- $m = $r->getMethod('save');
- $m->setAccessible(true);
- $m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('stale');
- $this->assertTraceContains('valid');
- $this->assertTraceContains('store');
- $this->assertTraceNotContains('miss');
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('fresh');
- $this->assertTraceNotContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
- }
- public function testDoesNotAssignDefaultTtlWhenResponseHasMustRevalidateDirective()
- {
- $this->setNextResponse(200, array('Cache-Control' => 'must-revalidate'));
- $this->cacheConfig['default_ttl'] = 10;
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('miss');
- $this->assertTraceNotContains('store');
- $this->assertNotRegExp('/s-maxage/', $this->response->headers->get('Cache-Control'));
- $this->assertEquals('Hello World', $this->response->getContent());
- }
- public function testFetchesFullResponseWhenCacheStaleAndNoValidatorsPresent()
- {
- $time = \DateTime::createFromFormat('U', time() + 5);
- $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
- // build initial request
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertNotNull($this->response->headers->get('Date'));
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- $this->assertNotNull($this->response->headers->get('Age'));
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- // go in and play around with the cached metadata directly ...
- $values = $this->getMetaStorageValues();
- $this->assertCount(1, $values);
- $tmp = unserialize($values[0]);
- $time = \DateTime::createFromFormat('U', time());
- $tmp[0][1]['expires'] = $time->format(DATE_RFC2822);
- $r = new \ReflectionObject($this->store);
- $m = $r->getMethod('save');
- $m->setAccessible(true);
- $m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));
- // build subsequent request; should be found but miss due to freshness
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTrue($this->response->headers->get('Age') <= 1);
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- $this->assertTraceContains('stale');
- $this->assertTraceNotContains('fresh');
- $this->assertTraceNotContains('miss');
- $this->assertTraceContains('store');
- $this->assertEquals('Hello World', $this->response->getContent());
- }
- public function testValidatesCachedResponsesWithLastModifiedAndNoFreshnessInformation()
- {
- $time = \DateTime::createFromFormat('U', time());
- $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time) {
- $response->headers->set('Cache-Control', 'public');
- $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
- if ($time->format(DATE_RFC2822) == $request->headers->get('IF_MODIFIED_SINCE')) {
- $response->setStatusCode(304);
- $response->setContent('');
- }
- });
- // build initial request
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertNotNull($this->response->headers->get('Last-Modified'));
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $this->assertTraceNotContains('stale');
- // build subsequent request; should be found but miss due to freshness
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertNotNull($this->response->headers->get('Last-Modified'));
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- $this->assertTrue($this->response->headers->get('Age') <= 1);
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('stale');
- $this->assertTraceContains('valid');
- $this->assertTraceContains('store');
- $this->assertTraceNotContains('miss');
- }
- public function testValidatesCachedResponsesUseSameHttpMethod()
- {
- $test = $this;
- $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($test) {
- $test->assertSame('OPTIONS', $request->getMethod());
- });
- // build initial request
- $this->request('OPTIONS', '/');
- // build subsequent request
- $this->request('OPTIONS', '/');
- }
- public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
- {
- $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
- $response->headers->set('Cache-Control', 'public');
- $response->headers->set('ETag', '"12345"');
- if ($response->getETag() == $request->headers->get('IF_NONE_MATCH')) {
- $response->setStatusCode(304);
- $response->setContent('');
- }
- });
- // build initial request
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertNotNull($this->response->headers->get('ETag'));
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- // build subsequent request; should be found but miss due to freshness
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertNotNull($this->response->headers->get('ETag'));
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- $this->assertTrue($this->response->headers->get('Age') <= 1);
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('stale');
- $this->assertTraceContains('valid');
- $this->assertTraceContains('store');
- $this->assertTraceNotContains('miss');
- }
- public function testServesResponseWhileFreshAndRevalidatesWithLastModifiedInformation()
- {
- $time = \DateTime::createFromFormat('U', time());
- $this->setNextResponse(200, array(), 'Hello World', function (Request $request, Response $response) use ($time) {
- $response->setSharedMaxAge(10);
- $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
- });
- // prime the cache
- $this->request('GET', '/');
- // next request before s-maxage has expired: Serve from cache
- // without hitting the backend
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('fresh');
- sleep(15); // expire the cache
- $this->setNextResponse(304, array(), '', function (Request $request, Response $response) use ($time) {
- $this->assertEquals($time->format(DATE_RFC2822), $request->headers->get('IF_MODIFIED_SINCE'));
- });
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('stale');
- $this->assertTraceContains('valid');
- }
- public function testReplacesCachedResponsesWhenValidationResultsInNon304Response()
- {
- $time = \DateTime::createFromFormat('U', time());
- $count = 0;
- $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time, &$count) {
- $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
- $response->headers->set('Cache-Control', 'public');
- switch (++$count) {
- case 1:
- $response->setContent('first response');
- break;
- case 2:
- $response->setContent('second response');
- break;
- case 3:
- $response->setContent('');
- $response->setStatusCode(304);
- break;
- }
- });
- // first request should fetch from backend and store in cache
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('first response', $this->response->getContent());
- // second request is validated, is invalid, and replaces cached entry
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('second response', $this->response->getContent());
- // third response is validated, valid, and returns cached entry
- $this->request('GET', '/');
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('second response', $this->response->getContent());
- $this->assertEquals(3, $count);
- }
- public function testPassesHeadRequestsThroughDirectlyOnPass()
- {
- $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
- $response->setContent('');
- $response->setStatusCode(200);
- $this->assertEquals('HEAD', $request->getMethod());
- });
- $this->request('HEAD', '/', array('HTTP_EXPECT' => 'something ...'));
- $this->assertHttpKernelIsCalled();
- $this->assertEquals('', $this->response->getContent());
- }
- public function testUsesCacheToRespondToHeadRequestsWhenFresh()
- {
- $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
- $response->headers->set('Cache-Control', 'public, max-age=10');
- $response->setContent('Hello World');
- $response->setStatusCode(200);
- $this->assertNotEquals('HEAD', $request->getMethod());
- });
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->request('HEAD', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('', $this->response->getContent());
- $this->assertEquals(strlen('Hello World'), $this->response->headers->get('Content-Length'));
- }
- public function testSendsNoContentWhenFresh()
- {
- $time = \DateTime::createFromFormat('U', time());
- $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time) {
- $response->headers->set('Cache-Control', 'public, max-age=10');
- $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
- });
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->request('GET', '/', array('HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(304, $this->response->getStatusCode());
- $this->assertEquals('', $this->response->getContent());
- }
- public function testInvalidatesCachedResponsesOnPost()
- {
- $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
- if ('GET' == $request->getMethod()) {
- $response->setStatusCode(200);
- $response->headers->set('Cache-Control', 'public, max-age=500');
- $response->setContent('Hello World');
- } elseif ('POST' == $request->getMethod()) {
- $response->setStatusCode(303);
- $response->headers->set('Location', '/');
- $response->headers->remove('Cache-Control');
- $response->setContent('');
- }
- });
- // build initial request to enter into the cache
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- // make sure it is valid
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('fresh');
- // now POST to same URL
- $this->request('POST', '/helloworld');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals('/', $this->response->headers->get('Location'));
- $this->assertTraceContains('invalidate');
- $this->assertTraceContains('pass');
- $this->assertEquals('', $this->response->getContent());
- // now make sure it was actually invalidated
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Hello World', $this->response->getContent());
- $this->assertTraceContains('stale');
- $this->assertTraceContains('invalid');
- $this->assertTraceContains('store');
- }
- public function testServesFromCacheWhenHeadersMatch()
- {
- $count = 0;
- $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count) {
- $response->headers->set('Vary', 'Accept User-Agent Foo');
- $response->headers->set('Cache-Control', 'public, max-age=10');
- $response->headers->set('X-Response-Count', ++$count);
- $response->setContent($request->headers->get('USER_AGENT'));
- });
- $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Bob/1.0', $this->response->getContent());
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Bob/1.0', $this->response->getContent());
- $this->assertTraceContains('fresh');
- $this->assertTraceNotContains('store');
- $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
- }
- public function testStoresMultipleResponsesWhenHeadersDiffer()
- {
- $count = 0;
- $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count) {
- $response->headers->set('Vary', 'Accept User-Agent Foo');
- $response->headers->set('Cache-Control', 'public, max-age=10');
- $response->headers->set('X-Response-Count', ++$count);
- $response->setContent($request->headers->get('USER_AGENT'));
- });
- $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertEquals('Bob/1.0', $this->response->getContent());
- $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
- $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
- $this->assertEquals(200, $this->response->getStatusCode());
- $this->assertTraceContains('miss');
- $this->assertTraceContains('store');
- $this->assertEquals('Bob/2.0', $this->response->getContent());
- $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
- $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
- $this->assertTraceContains('fresh');
- $this->assertEquals('Bob/1.0', $this->response->getContent());
- $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
- $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
- $this->assertTraceContains('fresh');
- $this->assertEquals('Bob/2.0', $this->response->getContent());
- $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
- $this->request('GET', '/', array('HTTP_USER_AGENT' => 'Bob/2.0'));
- $this->assertTraceContains('miss');
- $this->assertEquals('Bob/2.0', $this->response->getContent());
- $this->assertEquals(3, $this->response->headers->get('X-Response-Count'));
- }
- public function testShouldCatchExceptions()
- {
- $this->catchExceptions();
- $this->setNextResponse();
- $this->request('GET', '/');
- $this->assertExceptionsAreCaught();
- }
- public function testShouldCatchExceptionsWhenReloadingAndNoCacheRequest()
- {
- $this->catchExceptions();
- $this->setNextResponse();
- $this->cacheConfig['allow_reload'] = true;
- $this->request('GET', '/', array(), array(), false, array('Pragma' => 'no-cache'));
- $this->assertExceptionsAreCaught();
- }
- public function testShouldNotCatchExceptions()
- {
- $this->catchExceptions(false);
- $this->setNextResponse();
- $this->request('GET', '/');
- $this->assertExceptionsAreNotCaught();
- }
- public function testEsiCacheSendsTheLowestTtl()
- {
- $responses = array(
- array(
- 'status' => 200,
- 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
- 'headers' => array(
- 'Cache-Control' => 's-maxage=300',
- 'Surrogate-Control' => 'content="ESI/1.0"',
- ),
- ),
- array(
- 'status' => 200,
- 'body' => 'Hello World!',
- 'headers' => array('Cache-Control' => 's-maxage=300'),
- ),
- array(
- 'status' => 200,
- 'body' => 'My name is Bobby.',
- 'headers' => array('Cache-Control' => 's-maxage=100'),
- ),
- );
- $this->setNextResponses($responses);
- $this->request('GET', '/', array(), array(), true);
- $this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
- // check for 100 or 99 as the test can be executed after a second change
- $this->assertTrue(in_array($this->response->getTtl(), array(99, 100)));
- }
- public function testEsiCacheForceValidation()
- {
- $responses = array(
- array(
- 'status' => 200,
- 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
- 'headers' => array(
- 'Cache-Control' => 's-maxage=300',
- 'Surrogate-Control' => 'content="ESI/1.0"',
- ),
- ),
- array(
- 'status' => 200,
- 'body' => 'Hello World!',
- 'headers' => array('ETag' => 'foobar'),
- ),
- array(
- 'status' => 200,
- 'body' => 'My name is Bobby.',
- 'headers' => array('Cache-Control' => 's-maxage=100'),
- ),
- );
- $this->setNextResponses($responses);
- $this->request('GET', '/', array(), array(), true);
- $this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
- $this->assertNull($this->response->getTtl());
- $this->assertTrue($this->response->mustRevalidate());
- $this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
- $this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
- }
- public function testEsiRecalculateContentLengthHeader()
- {
- $responses = array(
- array(
- 'status' => 200,
- 'body' => '<esi:include src="/foo" />',
- 'headers' => array(
- 'Content-Length' => 26,
- 'Cache-Control' => 's-maxage=300',
- 'Surrogate-Control' => 'content="ESI/1.0"',
- ),
- ),
- array(
- 'status' => 200,
- 'body' => 'Hello World!',
- 'headers' => array(),
- ),
- );
- $this->setNextResponses($responses);
- $this->request('GET', '/', array(), array(), true);
- $this->assertEquals('Hello World!', $this->response->getContent());
- $this->assertEquals(12, $this->response->headers->get('Content-Length'));
- }
- public function testClientIpIsAlwaysLocalhostForForwardedRequests()
- {
- $this->setNextResponse();
- $this->request('GET', '/', array('REMOTE_ADDR' => '10.0.0.1'));
- $this->assertEquals('127.0.0.1', $this->kernel->getBackendRequest()->server->get('REMOTE_ADDR'));
- }
- /**
- * @dataProvider getTrustedProxyData
- */
- public function testHttpCacheIsSetAsATrustedProxy(array $existing, array $expected)
- {
- Request::setTrustedProxies($existing, Request::HEADER_X_FORWARDED_ALL);
- $this->setNextResponse();
- $this->request('GET', '/', array('REMOTE_ADDR' => '10.0.0.1'));
- $this->assertEquals($expected, Request::getTrustedProxies());
- }
- public function getTrustedProxyData()
- {
- return array(
- array(array(), array('127.0.0.1')),
- array(array('10.0.0.2'), array('10.0.0.2', '127.0.0.1')),
- array(array('10.0.0.2', '127.0.0.1'), array('10.0.0.2', '127.0.0.1')),
- );
- }
- /**
- * @dataProvider getXForwardedForData
- */
- public function testXForwarderForHeaderForForwardedRequests($xForwardedFor, $expected)
- {
- $this->setNextResponse();
- $server = array('REMOTE_ADDR' => '10.0.0.1');
- if (false !== $xForwardedFor) {
- $server['HTTP_X_FORWARDED_FOR'] = $xForwardedFor;
- }
- $this->request('GET', '/', $server);
- $this->assertEquals($expected, $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For'));
- }
- public function getXForwardedForData()
- {
- return array(
- array(false, '10.0.0.1'),
- array('10.0.0.2', '10.0.0.2, 10.0.0.1'),
- array('10.0.0.2, 10.0.0.3', '10.0.0.2, 10.0.0.3, 10.0.0.1'),
- );
- }
- public function testXForwarderForHeaderForPassRequests()
- {
- $this->setNextResponse();
- $server = array('REMOTE_ADDR' => '10.0.0.1');
- $this->request('POST', '/', $server);
- $this->assertEquals('10.0.0.1', $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For'));
- }
- public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponses()
- {
- $time = \DateTime::createFromFormat('U', time());
- $responses = array(
- array(
- 'status' => 200,
- 'body' => '<esi:include src="/hey" />',
- 'headers' => array(
- 'Surrogate-Control' => 'content="ESI/1.0"',
- 'ETag' => 'hey',
- 'Last-Modified' => $time->format(DATE_RFC2822),
- ),
- ),
- array(
- 'status' => 200,
- 'body' => 'Hey!',
- 'headers' => array(),
- ),
- );
- $this->setNextResponses($responses);
- $this->request('GET', '/', array(), array(), true);
- $this->assertNull($this->response->getETag());
- $this->assertNull($this->response->getLastModified());
- }
- public function testDoesNotCacheOptionsRequest()
- {
- $this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=60'), 'get');
- $this->request('GET', '/');
- $this->assertHttpKernelIsCalled();
- $this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=60'), 'options');
- $this->request('OPTIONS', '/');
- $this->assertHttpKernelIsCalled();
- $this->request('GET', '/');
- $this->assertHttpKernelIsNotCalled();
- $this->assertSame('get', $this->response->getContent());
- }
- }
- class TestKernel implements HttpKernelInterface
- {
- public $terminateCalled = false;
- public function terminate(Request $request, Response $response)
- {
- $this->terminateCalled = true;
- }
- public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
- {
- }
- }
|