HttpCacheTest.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests\HttpCache;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\HttpCache\Esi;
  14. use Symfony\Component\HttpKernel\HttpCache\HttpCache;
  15. use Symfony\Component\HttpKernel\HttpCache\Store;
  16. use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
  17. use Symfony\Component\HttpKernel\HttpKernelInterface;
  18. use Symfony\Component\HttpKernel\Kernel;
  19. /**
  20. * @group time-sensitive
  21. */
  22. class HttpCacheTest extends HttpCacheTestCase
  23. {
  24. public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
  25. {
  26. $storeMock = $this->getMockBuilder(StoreInterface::class)
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. // does not implement TerminableInterface
  30. $kernel = new TestKernel();
  31. $httpCache = new HttpCache($kernel, $storeMock);
  32. $httpCache->terminate(Request::create('/'), new Response());
  33. $this->assertFalse($kernel->terminateCalled, 'terminate() is never called if the kernel class does not implement TerminableInterface');
  34. // implements TerminableInterface
  35. $kernelMock = $this->getMockBuilder(Kernel::class)
  36. ->disableOriginalConstructor()
  37. ->onlyMethods(['terminate', 'registerBundles', 'registerContainerConfiguration'])
  38. ->getMock();
  39. $kernelMock->expects($this->once())
  40. ->method('terminate');
  41. $kernel = new HttpCache($kernelMock, $storeMock);
  42. $kernel->terminate(Request::create('/'), new Response());
  43. }
  44. public function testPassesOnNonGetHeadRequests()
  45. {
  46. $this->setNextResponse(200);
  47. $this->request('POST', '/');
  48. $this->assertHttpKernelIsCalled();
  49. $this->assertResponseOk();
  50. $this->assertTraceContains('pass');
  51. $this->assertFalse($this->response->headers->has('Age'));
  52. }
  53. public function testInvalidatesOnPostPutDeleteRequests()
  54. {
  55. foreach (['post', 'put', 'delete'] as $method) {
  56. $this->setNextResponse(200);
  57. $this->request($method, '/');
  58. $this->assertHttpKernelIsCalled();
  59. $this->assertResponseOk();
  60. $this->assertTraceContains('invalidate');
  61. $this->assertTraceContains('pass');
  62. }
  63. }
  64. public function testDoesNotCacheWithAuthorizationRequestHeaderAndNonPublicResponse()
  65. {
  66. $this->setNextResponse(200, ['ETag' => '"Foo"']);
  67. $this->request('GET', '/', ['HTTP_AUTHORIZATION' => 'basic foobarbaz']);
  68. $this->assertHttpKernelIsCalled();
  69. $this->assertResponseOk();
  70. $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
  71. $this->assertTraceContains('miss');
  72. $this->assertTraceNotContains('store');
  73. $this->assertFalse($this->response->headers->has('Age'));
  74. }
  75. public function testDoesCacheWithAuthorizationRequestHeaderAndPublicResponse()
  76. {
  77. $this->setNextResponse(200, ['Cache-Control' => 'public', 'ETag' => '"Foo"']);
  78. $this->request('GET', '/', ['HTTP_AUTHORIZATION' => 'basic foobarbaz']);
  79. $this->assertHttpKernelIsCalled();
  80. $this->assertResponseOk();
  81. $this->assertTraceContains('miss');
  82. $this->assertTraceContains('store');
  83. $this->assertTrue($this->response->headers->has('Age'));
  84. $this->assertEquals('public', $this->response->headers->get('Cache-Control'));
  85. }
  86. public function testDoesNotCacheWithCookieHeaderAndNonPublicResponse()
  87. {
  88. $this->setNextResponse(200, ['ETag' => '"Foo"']);
  89. $this->request('GET', '/', [], ['foo' => 'bar']);
  90. $this->assertHttpKernelIsCalled();
  91. $this->assertResponseOk();
  92. $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
  93. $this->assertTraceContains('miss');
  94. $this->assertTraceNotContains('store');
  95. $this->assertFalse($this->response->headers->has('Age'));
  96. }
  97. public function testDoesNotCacheRequestsWithACookieHeader()
  98. {
  99. $this->setNextResponse(200);
  100. $this->request('GET', '/', [], ['foo' => 'bar']);
  101. $this->assertHttpKernelIsCalled();
  102. $this->assertResponseOk();
  103. $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
  104. $this->assertTraceContains('miss');
  105. $this->assertTraceNotContains('store');
  106. $this->assertFalse($this->response->headers->has('Age'));
  107. }
  108. public function testRespondsWith304WhenIfModifiedSinceMatchesLastModified()
  109. {
  110. $time = \DateTime::createFromFormat('U', time());
  111. $this->setNextResponse(200, ['Cache-Control' => 'public', 'Last-Modified' => $time->format(\DATE_RFC2822), 'Content-Type' => 'text/plain'], 'Hello World');
  112. $this->request('GET', '/', ['HTTP_IF_MODIFIED_SINCE' => $time->format(\DATE_RFC2822)]);
  113. $this->assertHttpKernelIsCalled();
  114. $this->assertEquals(304, $this->response->getStatusCode());
  115. $this->assertEquals('', $this->response->headers->get('Content-Type'));
  116. $this->assertEmpty($this->response->getContent());
  117. $this->assertTraceContains('miss');
  118. $this->assertTraceContains('store');
  119. }
  120. public function testRespondsWith304WhenIfNoneMatchMatchesETag()
  121. {
  122. $this->setNextResponse(200, ['Cache-Control' => 'public', 'ETag' => '12345', 'Content-Type' => 'text/plain'], 'Hello World');
  123. $this->request('GET', '/', ['HTTP_IF_NONE_MATCH' => '12345']);
  124. $this->assertHttpKernelIsCalled();
  125. $this->assertEquals(304, $this->response->getStatusCode());
  126. $this->assertEquals('', $this->response->headers->get('Content-Type'));
  127. $this->assertTrue($this->response->headers->has('ETag'));
  128. $this->assertEmpty($this->response->getContent());
  129. $this->assertTraceContains('miss');
  130. $this->assertTraceContains('store');
  131. }
  132. public function testRespondsWith304WhenIfNoneMatchAndIfModifiedSinceBothMatch()
  133. {
  134. $time = \DateTime::createFromFormat('U', time());
  135. $this->setNextResponse(200, [], '', function ($request, $response) use ($time) {
  136. $response->setStatusCode(200);
  137. $response->headers->set('ETag', '12345');
  138. $response->headers->set('Last-Modified', $time->format(\DATE_RFC2822));
  139. $response->headers->set('Content-Type', 'text/plain');
  140. $response->setContent('Hello World');
  141. });
  142. // only ETag matches
  143. $t = \DateTime::createFromFormat('U', time() - 3600);
  144. $this->request('GET', '/', ['HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => $t->format(\DATE_RFC2822)]);
  145. $this->assertHttpKernelIsCalled();
  146. $this->assertEquals(304, $this->response->getStatusCode());
  147. // only Last-Modified matches
  148. $this->request('GET', '/', ['HTTP_IF_NONE_MATCH' => '1234', 'HTTP_IF_MODIFIED_SINCE' => $time->format(\DATE_RFC2822)]);
  149. $this->assertHttpKernelIsCalled();
  150. $this->assertEquals(200, $this->response->getStatusCode());
  151. // Both matches
  152. $this->request('GET', '/', ['HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => $time->format(\DATE_RFC2822)]);
  153. $this->assertHttpKernelIsCalled();
  154. $this->assertEquals(304, $this->response->getStatusCode());
  155. }
  156. public function testIncrementsMaxAgeWhenNoDateIsSpecifiedEventWhenUsingETag()
  157. {
  158. $this->setNextResponse(
  159. 200,
  160. [
  161. 'ETag' => '1234',
  162. 'Cache-Control' => 'public, s-maxage=60',
  163. ]
  164. );
  165. $this->request('GET', '/');
  166. $this->assertHttpKernelIsCalled();
  167. $this->assertEquals(200, $this->response->getStatusCode());
  168. $this->assertTraceContains('miss');
  169. $this->assertTraceContains('store');
  170. sleep(2);
  171. $this->request('GET', '/');
  172. $this->assertHttpKernelIsNotCalled();
  173. $this->assertEquals(200, $this->response->getStatusCode());
  174. $this->assertTraceContains('fresh');
  175. $this->assertEquals(2, $this->response->headers->get('Age'));
  176. }
  177. public function testValidatesPrivateResponsesCachedOnTheClient()
  178. {
  179. $this->setNextResponse(200, [], '', function (Request $request, $response) {
  180. $etags = preg_split('/\s*,\s*/', $request->headers->get('IF_NONE_MATCH', ''));
  181. if ($request->cookies->has('authenticated')) {
  182. $response->headers->set('Cache-Control', 'private, no-store');
  183. $response->setETag('"private tag"');
  184. if (\in_array('"private tag"', $etags)) {
  185. $response->setStatusCode(304);
  186. } else {
  187. $response->setStatusCode(200);
  188. $response->headers->set('Content-Type', 'text/plain');
  189. $response->setContent('private data');
  190. }
  191. } else {
  192. $response->headers->set('Cache-Control', 'public');
  193. $response->setETag('"public tag"');
  194. if (\in_array('"public tag"', $etags)) {
  195. $response->setStatusCode(304);
  196. } else {
  197. $response->setStatusCode(200);
  198. $response->headers->set('Content-Type', 'text/plain');
  199. $response->setContent('public data');
  200. }
  201. }
  202. });
  203. $this->request('GET', '/');
  204. $this->assertHttpKernelIsCalled();
  205. $this->assertEquals(200, $this->response->getStatusCode());
  206. $this->assertEquals('"public tag"', $this->response->headers->get('ETag'));
  207. $this->assertEquals('public data', $this->response->getContent());
  208. $this->assertTraceContains('miss');
  209. $this->assertTraceContains('store');
  210. $this->request('GET', '/', [], ['authenticated' => '']);
  211. $this->assertHttpKernelIsCalled();
  212. $this->assertEquals(200, $this->response->getStatusCode());
  213. $this->assertEquals('"private tag"', $this->response->headers->get('ETag'));
  214. $this->assertEquals('private data', $this->response->getContent());
  215. $this->assertTraceContains('stale');
  216. $this->assertTraceContains('invalid');
  217. $this->assertTraceNotContains('store');
  218. }
  219. public function testStoresResponsesWhenNoCacheRequestDirectivePresent()
  220. {
  221. $time = \DateTime::createFromFormat('U', time() + 5);
  222. $this->setNextResponse(200, ['Cache-Control' => 'public', 'Expires' => $time->format(\DATE_RFC2822)]);
  223. $this->request('GET', '/', ['HTTP_CACHE_CONTROL' => 'no-cache']);
  224. $this->assertHttpKernelIsCalled();
  225. $this->assertTraceContains('store');
  226. $this->assertTrue($this->response->headers->has('Age'));
  227. }
  228. public function testReloadsResponsesWhenCacheHitsButNoCacheRequestDirectivePresentWhenAllowReloadIsSetTrue()
  229. {
  230. $count = 0;
  231. $this->setNextResponse(200, ['Cache-Control' => 'public, max-age=10000'], '', function ($request, $response) use (&$count) {
  232. ++$count;
  233. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  234. });
  235. $this->request('GET', '/');
  236. $this->assertEquals(200, $this->response->getStatusCode());
  237. $this->assertEquals('Hello World', $this->response->getContent());
  238. $this->assertTraceContains('store');
  239. $this->request('GET', '/');
  240. $this->assertEquals(200, $this->response->getStatusCode());
  241. $this->assertEquals('Hello World', $this->response->getContent());
  242. $this->assertTraceContains('fresh');
  243. $this->cacheConfig['allow_reload'] = true;
  244. $this->request('GET', '/', ['HTTP_CACHE_CONTROL' => 'no-cache']);
  245. $this->assertEquals(200, $this->response->getStatusCode());
  246. $this->assertEquals('Goodbye World', $this->response->getContent());
  247. $this->assertTraceContains('reload');
  248. $this->assertTraceContains('store');
  249. }
  250. public function testDoesNotReloadResponsesWhenAllowReloadIsSetFalseDefault()
  251. {
  252. $count = 0;
  253. $this->setNextResponse(200, ['Cache-Control' => 'public, max-age=10000'], '', function ($request, $response) use (&$count) {
  254. ++$count;
  255. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  256. });
  257. $this->request('GET', '/');
  258. $this->assertEquals(200, $this->response->getStatusCode());
  259. $this->assertEquals('Hello World', $this->response->getContent());
  260. $this->assertTraceContains('store');
  261. $this->request('GET', '/');
  262. $this->assertEquals(200, $this->response->getStatusCode());
  263. $this->assertEquals('Hello World', $this->response->getContent());
  264. $this->assertTraceContains('fresh');
  265. $this->cacheConfig['allow_reload'] = false;
  266. $this->request('GET', '/', ['HTTP_CACHE_CONTROL' => 'no-cache']);
  267. $this->assertEquals(200, $this->response->getStatusCode());
  268. $this->assertEquals('Hello World', $this->response->getContent());
  269. $this->assertTraceNotContains('reload');
  270. $this->request('GET', '/', ['HTTP_CACHE_CONTROL' => 'no-cache']);
  271. $this->assertEquals(200, $this->response->getStatusCode());
  272. $this->assertEquals('Hello World', $this->response->getContent());
  273. $this->assertTraceNotContains('reload');
  274. }
  275. public function testRevalidatesFreshCacheEntryWhenMaxAgeRequestDirectiveIsExceededWhenAllowRevalidateOptionIsSetTrue()
  276. {
  277. $count = 0;
  278. $this->setNextResponse(200, [], '', function ($request, $response) use (&$count) {
  279. ++$count;
  280. $response->headers->set('Cache-Control', 'public, max-age=10000');
  281. $response->setETag($count);
  282. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  283. });
  284. $this->request('GET', '/');
  285. $this->assertEquals(200, $this->response->getStatusCode());
  286. $this->assertEquals('Hello World', $this->response->getContent());
  287. $this->assertTraceContains('store');
  288. $this->request('GET', '/');
  289. $this->assertEquals(200, $this->response->getStatusCode());
  290. $this->assertEquals('Hello World', $this->response->getContent());
  291. $this->assertTraceContains('fresh');
  292. $this->cacheConfig['allow_revalidate'] = true;
  293. $this->request('GET', '/', ['HTTP_CACHE_CONTROL' => 'max-age=0']);
  294. $this->assertEquals(200, $this->response->getStatusCode());
  295. $this->assertEquals('Goodbye World', $this->response->getContent());
  296. $this->assertTraceContains('stale');
  297. $this->assertTraceContains('invalid');
  298. $this->assertTraceContains('store');
  299. }
  300. public function testDoesNotRevalidateFreshCacheEntryWhenEnableRevalidateOptionIsSetFalseDefault()
  301. {
  302. $count = 0;
  303. $this->setNextResponse(200, [], '', function ($request, $response) use (&$count) {
  304. ++$count;
  305. $response->headers->set('Cache-Control', 'public, max-age=10000');
  306. $response->setETag($count);
  307. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  308. });
  309. $this->request('GET', '/');
  310. $this->assertEquals(200, $this->response->getStatusCode());
  311. $this->assertEquals('Hello World', $this->response->getContent());
  312. $this->assertTraceContains('store');
  313. $this->request('GET', '/');
  314. $this->assertEquals(200, $this->response->getStatusCode());
  315. $this->assertEquals('Hello World', $this->response->getContent());
  316. $this->assertTraceContains('fresh');
  317. $this->cacheConfig['allow_revalidate'] = false;
  318. $this->request('GET', '/', ['HTTP_CACHE_CONTROL' => 'max-age=0']);
  319. $this->assertEquals(200, $this->response->getStatusCode());
  320. $this->assertEquals('Hello World', $this->response->getContent());
  321. $this->assertTraceNotContains('stale');
  322. $this->assertTraceNotContains('invalid');
  323. $this->assertTraceContains('fresh');
  324. $this->request('GET', '/', ['HTTP_CACHE_CONTROL' => 'max-age=0']);
  325. $this->assertEquals(200, $this->response->getStatusCode());
  326. $this->assertEquals('Hello World', $this->response->getContent());
  327. $this->assertTraceNotContains('stale');
  328. $this->assertTraceNotContains('invalid');
  329. $this->assertTraceContains('fresh');
  330. }
  331. public function testFetchesResponseFromBackendWhenCacheMisses()
  332. {
  333. $time = \DateTime::createFromFormat('U', time() + 5);
  334. $this->setNextResponse(200, ['Cache-Control' => 'public', 'Expires' => $time->format(\DATE_RFC2822)]);
  335. $this->request('GET', '/');
  336. $this->assertEquals(200, $this->response->getStatusCode());
  337. $this->assertTraceContains('miss');
  338. $this->assertTrue($this->response->headers->has('Age'));
  339. }
  340. public function testDoesNotCacheSomeStatusCodeResponses()
  341. {
  342. 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) {
  343. $time = \DateTime::createFromFormat('U', time() + 5);
  344. $this->setNextResponse($code, ['Expires' => $time->format(\DATE_RFC2822)]);
  345. $this->request('GET', '/');
  346. $this->assertEquals($code, $this->response->getStatusCode());
  347. $this->assertTraceNotContains('store');
  348. $this->assertFalse($this->response->headers->has('Age'));
  349. }
  350. }
  351. public function testDoesNotCacheResponsesWithExplicitNoStoreDirective()
  352. {
  353. $time = \DateTime::createFromFormat('U', time() + 5);
  354. $this->setNextResponse(200, ['Expires' => $time->format(\DATE_RFC2822), 'Cache-Control' => 'no-store']);
  355. $this->request('GET', '/');
  356. $this->assertTraceNotContains('store');
  357. $this->assertFalse($this->response->headers->has('Age'));
  358. }
  359. public function testDoesNotCacheResponsesWithoutFreshnessInformationOrAValidator()
  360. {
  361. $this->setNextResponse();
  362. $this->request('GET', '/');
  363. $this->assertEquals(200, $this->response->getStatusCode());
  364. $this->assertTraceNotContains('store');
  365. }
  366. public function testCachesResponsesWithExplicitNoCacheDirective()
  367. {
  368. $time = \DateTime::createFromFormat('U', time() + 5);
  369. $this->setNextResponse(200, ['Expires' => $time->format(\DATE_RFC2822), 'Cache-Control' => 'public, no-cache']);
  370. $this->request('GET', '/');
  371. $this->assertTraceContains('store');
  372. $this->assertTrue($this->response->headers->has('Age'));
  373. }
  374. public function testRevalidatesResponsesWithNoCacheDirectiveEvenIfFresh()
  375. {
  376. $this->setNextResponse(200, ['Cache-Control' => 'public, no-cache, max-age=10', 'ETag' => 'some-etag'], 'OK');
  377. $this->request('GET', '/'); // warm the cache
  378. sleep(5);
  379. $this->setNextResponse(304, ['Cache-Control' => 'public, no-cache, max-age=10', 'ETag' => 'some-etag']);
  380. $this->request('GET', '/');
  381. $this->assertHttpKernelIsCalled(); // no-cache -> MUST have revalidated at origin
  382. $this->assertTraceContains('valid');
  383. $this->assertEquals('OK', $this->response->getContent());
  384. $this->assertEquals(0, $this->response->getAge());
  385. }
  386. public function testCachesResponsesWithAnExpirationHeader()
  387. {
  388. $time = \DateTime::createFromFormat('U', time() + 5);
  389. $this->setNextResponse(200, ['Cache-Control' => 'public', 'Expires' => $time->format(\DATE_RFC2822)]);
  390. $this->request('GET', '/');
  391. $this->assertEquals(200, $this->response->getStatusCode());
  392. $this->assertEquals('Hello World', $this->response->getContent());
  393. $this->assertNotNull($this->response->headers->get('Date'));
  394. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  395. $this->assertTraceContains('miss');
  396. $this->assertTraceContains('store');
  397. $values = $this->getMetaStorageValues();
  398. $this->assertCount(1, $values);
  399. }
  400. public function testCachesResponsesWithAMaxAgeDirective()
  401. {
  402. $this->setNextResponse(200, ['Cache-Control' => 'public, max-age=5']);
  403. $this->request('GET', '/');
  404. $this->assertEquals(200, $this->response->getStatusCode());
  405. $this->assertEquals('Hello World', $this->response->getContent());
  406. $this->assertNotNull($this->response->headers->get('Date'));
  407. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  408. $this->assertTraceContains('miss');
  409. $this->assertTraceContains('store');
  410. $values = $this->getMetaStorageValues();
  411. $this->assertCount(1, $values);
  412. }
  413. public function testCachesResponsesWithASMaxAgeDirective()
  414. {
  415. $this->setNextResponse(200, ['Cache-Control' => 's-maxage=5']);
  416. $this->request('GET', '/');
  417. $this->assertEquals(200, $this->response->getStatusCode());
  418. $this->assertEquals('Hello World', $this->response->getContent());
  419. $this->assertNotNull($this->response->headers->get('Date'));
  420. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  421. $this->assertTraceContains('miss');
  422. $this->assertTraceContains('store');
  423. $values = $this->getMetaStorageValues();
  424. $this->assertCount(1, $values);
  425. }
  426. public function testCachesResponsesWithALastModifiedValidatorButNoFreshnessInformation()
  427. {
  428. $time = \DateTime::createFromFormat('U', time());
  429. $this->setNextResponse(200, ['Cache-Control' => 'public', 'Last-Modified' => $time->format(\DATE_RFC2822)]);
  430. $this->request('GET', '/');
  431. $this->assertEquals(200, $this->response->getStatusCode());
  432. $this->assertEquals('Hello World', $this->response->getContent());
  433. $this->assertTraceContains('miss');
  434. $this->assertTraceContains('store');
  435. }
  436. public function testCachesResponsesWithAnETagValidatorButNoFreshnessInformation()
  437. {
  438. $this->setNextResponse(200, ['Cache-Control' => 'public', 'ETag' => '"123456"']);
  439. $this->request('GET', '/');
  440. $this->assertEquals(200, $this->response->getStatusCode());
  441. $this->assertEquals('Hello World', $this->response->getContent());
  442. $this->assertTraceContains('miss');
  443. $this->assertTraceContains('store');
  444. }
  445. public function testHitsCachedResponsesWithExpiresHeader()
  446. {
  447. $time1 = \DateTime::createFromFormat('U', time() - 5);
  448. $time2 = \DateTime::createFromFormat('U', time() + 5);
  449. $this->setNextResponse(200, ['Cache-Control' => 'public', 'Date' => $time1->format(\DATE_RFC2822), 'Expires' => $time2->format(\DATE_RFC2822)]);
  450. $this->request('GET', '/');
  451. $this->assertHttpKernelIsCalled();
  452. $this->assertEquals(200, $this->response->getStatusCode());
  453. $this->assertNotNull($this->response->headers->get('Date'));
  454. $this->assertTraceContains('miss');
  455. $this->assertTraceContains('store');
  456. $this->assertEquals('Hello World', $this->response->getContent());
  457. $this->request('GET', '/');
  458. $this->assertHttpKernelIsNotCalled();
  459. $this->assertEquals(200, $this->response->getStatusCode());
  460. $this->assertLessThan(2, strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')));
  461. $this->assertGreaterThan(0, $this->response->headers->get('Age'));
  462. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  463. $this->assertTraceContains('fresh');
  464. $this->assertTraceNotContains('store');
  465. $this->assertEquals('Hello World', $this->response->getContent());
  466. }
  467. public function testHitsCachedResponseWithMaxAgeDirective()
  468. {
  469. $time = \DateTime::createFromFormat('U', time() - 5);
  470. $this->setNextResponse(200, ['Date' => $time->format(\DATE_RFC2822), 'Cache-Control' => 'public, max-age=10']);
  471. $this->request('GET', '/');
  472. $this->assertHttpKernelIsCalled();
  473. $this->assertEquals(200, $this->response->getStatusCode());
  474. $this->assertNotNull($this->response->headers->get('Date'));
  475. $this->assertTraceContains('miss');
  476. $this->assertTraceContains('store');
  477. $this->assertEquals('Hello World', $this->response->getContent());
  478. $this->request('GET', '/');
  479. $this->assertHttpKernelIsNotCalled();
  480. $this->assertEquals(200, $this->response->getStatusCode());
  481. $this->assertLessThan(2, strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')));
  482. $this->assertGreaterThan(0, $this->response->headers->get('Age'));
  483. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  484. $this->assertTraceContains('fresh');
  485. $this->assertTraceNotContains('store');
  486. $this->assertEquals('Hello World', $this->response->getContent());
  487. }
  488. public function testDegradationWhenCacheLocked()
  489. {
  490. if ('\\' === \DIRECTORY_SEPARATOR) {
  491. $this->markTestSkipped('Skips on windows to avoid permissions issues.');
  492. }
  493. $this->cacheConfig['stale_while_revalidate'] = 10;
  494. // The presence of Last-Modified makes this cacheable (because Response::isValidateable() then).
  495. $this->setNextResponse(200, ['Cache-Control' => 'public, s-maxage=5', 'Last-Modified' => 'some while ago'], 'Old response');
  496. $this->request('GET', '/'); // warm the cache
  497. // Now, lock the cache
  498. $concurrentRequest = Request::create('/', 'GET');
  499. $this->store->lock($concurrentRequest);
  500. /*
  501. * After 10s, the cached response has become stale. Yet, we're still within the "stale_while_revalidate"
  502. * timeout so we may serve the stale response.
  503. */
  504. sleep(10);
  505. $this->request('GET', '/');
  506. $this->assertHttpKernelIsNotCalled();
  507. $this->assertEquals(200, $this->response->getStatusCode());
  508. $this->assertTraceContains('stale-while-revalidate');
  509. $this->assertEquals('Old response', $this->response->getContent());
  510. /*
  511. * Another 10s later, stale_while_revalidate is over. Resort to serving the old response, but
  512. * do so with a "server unavailable" message.
  513. */
  514. sleep(10);
  515. $this->request('GET', '/');
  516. $this->assertHttpKernelIsNotCalled();
  517. $this->assertEquals(503, $this->response->getStatusCode());
  518. $this->assertEquals('Old response', $this->response->getContent());
  519. }
  520. public function testHitsCachedResponseWithSMaxAgeDirective()
  521. {
  522. $time = \DateTime::createFromFormat('U', time() - 5);
  523. $this->setNextResponse(200, ['Date' => $time->format(\DATE_RFC2822), 'Cache-Control' => 's-maxage=10, max-age=0']);
  524. $this->request('GET', '/');
  525. $this->assertHttpKernelIsCalled();
  526. $this->assertEquals(200, $this->response->getStatusCode());
  527. $this->assertNotNull($this->response->headers->get('Date'));
  528. $this->assertTraceContains('miss');
  529. $this->assertTraceContains('store');
  530. $this->assertEquals('Hello World', $this->response->getContent());
  531. $this->request('GET', '/');
  532. $this->assertHttpKernelIsNotCalled();
  533. $this->assertEquals(200, $this->response->getStatusCode());
  534. $this->assertLessThan(2, strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')));
  535. $this->assertGreaterThan(0, $this->response->headers->get('Age'));
  536. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  537. $this->assertTraceContains('fresh');
  538. $this->assertTraceNotContains('store');
  539. $this->assertEquals('Hello World', $this->response->getContent());
  540. }
  541. public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformation()
  542. {
  543. $this->setNextResponse();
  544. $this->cacheConfig['default_ttl'] = 10;
  545. $this->request('GET', '/');
  546. $this->assertHttpKernelIsCalled();
  547. $this->assertTraceContains('miss');
  548. $this->assertTraceContains('store');
  549. $this->assertEquals('Hello World', $this->response->getContent());
  550. $this->assertMatchesRegularExpression('/s-maxage=10/', $this->response->headers->get('Cache-Control'));
  551. $this->cacheConfig['default_ttl'] = 10;
  552. $this->request('GET', '/');
  553. $this->assertHttpKernelIsNotCalled();
  554. $this->assertEquals(200, $this->response->getStatusCode());
  555. $this->assertTraceContains('fresh');
  556. $this->assertTraceNotContains('store');
  557. $this->assertEquals('Hello World', $this->response->getContent());
  558. $this->assertMatchesRegularExpression('/s-maxage=10/', $this->response->headers->get('Cache-Control'));
  559. }
  560. public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAfterTtlWasExpired()
  561. {
  562. $this->setNextResponse();
  563. $this->cacheConfig['default_ttl'] = 2;
  564. $this->request('GET', '/');
  565. $this->assertHttpKernelIsCalled();
  566. $this->assertTraceContains('miss');
  567. $this->assertTraceContains('store');
  568. $this->assertEquals('Hello World', $this->response->getContent());
  569. $this->assertMatchesRegularExpression('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));
  570. $this->request('GET', '/');
  571. $this->assertHttpKernelIsNotCalled();
  572. $this->assertEquals(200, $this->response->getStatusCode());
  573. $this->assertTraceContains('fresh');
  574. $this->assertTraceNotContains('store');
  575. $this->assertEquals('Hello World', $this->response->getContent());
  576. $this->assertMatchesRegularExpression('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));
  577. // expires the cache
  578. $values = $this->getMetaStorageValues();
  579. $this->assertCount(1, $values);
  580. $tmp = unserialize($values[0]);
  581. $time = \DateTime::createFromFormat('U', time() - 5);
  582. $tmp[0][1]['date'] = $time->format(\DATE_RFC2822);
  583. $r = new \ReflectionObject($this->store);
  584. $m = $r->getMethod('save');
  585. $m->setAccessible(true);
  586. $m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));
  587. $this->request('GET', '/');
  588. $this->assertHttpKernelIsCalled();
  589. $this->assertEquals(200, $this->response->getStatusCode());
  590. $this->assertTraceContains('stale');
  591. $this->assertTraceContains('invalid');
  592. $this->assertTraceContains('store');
  593. $this->assertEquals('Hello World', $this->response->getContent());
  594. $this->assertMatchesRegularExpression('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));
  595. $this->setNextResponse();
  596. $this->request('GET', '/');
  597. $this->assertHttpKernelIsNotCalled();
  598. $this->assertEquals(200, $this->response->getStatusCode());
  599. $this->assertTraceContains('fresh');
  600. $this->assertTraceNotContains('store');
  601. $this->assertEquals('Hello World', $this->response->getContent());
  602. $this->assertMatchesRegularExpression('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));
  603. }
  604. public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAfterTtlWasExpiredWithStatus304()
  605. {
  606. $this->setNextResponse();
  607. $this->cacheConfig['default_ttl'] = 2;
  608. $this->request('GET', '/');
  609. $this->assertHttpKernelIsCalled();
  610. $this->assertTraceContains('miss');
  611. $this->assertTraceContains('store');
  612. $this->assertEquals('Hello World', $this->response->getContent());
  613. $this->assertMatchesRegularExpression('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));
  614. $this->request('GET', '/');
  615. $this->assertHttpKernelIsNotCalled();
  616. $this->assertEquals(200, $this->response->getStatusCode());
  617. $this->assertTraceContains('fresh');
  618. $this->assertTraceNotContains('store');
  619. $this->assertEquals('Hello World', $this->response->getContent());
  620. // expires the cache
  621. $values = $this->getMetaStorageValues();
  622. $this->assertCount(1, $values);
  623. $tmp = unserialize($values[0]);
  624. $time = \DateTime::createFromFormat('U', time() - 5);
  625. $tmp[0][1]['date'] = $time->format(\DATE_RFC2822);
  626. $r = new \ReflectionObject($this->store);
  627. $m = $r->getMethod('save');
  628. $m->setAccessible(true);
  629. $m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));
  630. $this->request('GET', '/');
  631. $this->assertHttpKernelIsCalled();
  632. $this->assertEquals(200, $this->response->getStatusCode());
  633. $this->assertTraceContains('stale');
  634. $this->assertTraceContains('valid');
  635. $this->assertTraceContains('store');
  636. $this->assertTraceNotContains('miss');
  637. $this->assertEquals('Hello World', $this->response->getContent());
  638. $this->assertMatchesRegularExpression('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));
  639. $this->request('GET', '/');
  640. $this->assertHttpKernelIsNotCalled();
  641. $this->assertEquals(200, $this->response->getStatusCode());
  642. $this->assertTraceContains('fresh');
  643. $this->assertTraceNotContains('store');
  644. $this->assertEquals('Hello World', $this->response->getContent());
  645. $this->assertMatchesRegularExpression('/s-maxage=(2|3)/', $this->response->headers->get('Cache-Control'));
  646. }
  647. public function testDoesNotAssignDefaultTtlWhenResponseHasMustRevalidateDirective()
  648. {
  649. $this->setNextResponse(200, ['Cache-Control' => 'must-revalidate']);
  650. $this->cacheConfig['default_ttl'] = 10;
  651. $this->request('GET', '/');
  652. $this->assertHttpKernelIsCalled();
  653. $this->assertEquals(200, $this->response->getStatusCode());
  654. $this->assertTraceContains('miss');
  655. $this->assertTraceNotContains('store');
  656. $this->assertDoesNotMatchRegularExpression('/s-maxage/', $this->response->headers->get('Cache-Control'));
  657. $this->assertEquals('Hello World', $this->response->getContent());
  658. }
  659. public function testFetchesFullResponseWhenCacheStaleAndNoValidatorsPresent()
  660. {
  661. $time = \DateTime::createFromFormat('U', time() + 5);
  662. $this->setNextResponse(200, ['Cache-Control' => 'public', 'Expires' => $time->format(\DATE_RFC2822)]);
  663. // build initial request
  664. $this->request('GET', '/');
  665. $this->assertHttpKernelIsCalled();
  666. $this->assertEquals(200, $this->response->getStatusCode());
  667. $this->assertNotNull($this->response->headers->get('Date'));
  668. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  669. $this->assertNotNull($this->response->headers->get('Age'));
  670. $this->assertTraceContains('miss');
  671. $this->assertTraceContains('store');
  672. $this->assertEquals('Hello World', $this->response->getContent());
  673. // go in and play around with the cached metadata directly ...
  674. $values = $this->getMetaStorageValues();
  675. $this->assertCount(1, $values);
  676. $tmp = unserialize($values[0]);
  677. $time = \DateTime::createFromFormat('U', time());
  678. $tmp[0][1]['expires'] = $time->format(\DATE_RFC2822);
  679. $r = new \ReflectionObject($this->store);
  680. $m = $r->getMethod('save');
  681. $m->setAccessible(true);
  682. $m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));
  683. // build subsequent request; should be found but miss due to freshness
  684. $this->request('GET', '/');
  685. $this->assertHttpKernelIsCalled();
  686. $this->assertEquals(200, $this->response->getStatusCode());
  687. $this->assertLessThanOrEqual(1, $this->response->headers->get('Age'));
  688. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  689. $this->assertTraceContains('stale');
  690. $this->assertTraceNotContains('fresh');
  691. $this->assertTraceNotContains('miss');
  692. $this->assertTraceContains('store');
  693. $this->assertEquals('Hello World', $this->response->getContent());
  694. }
  695. public function testValidatesCachedResponsesWithLastModifiedAndNoFreshnessInformation()
  696. {
  697. $time = \DateTime::createFromFormat('U', time());
  698. $this->setNextResponse(200, [], 'Hello World', function ($request, $response) use ($time) {
  699. $response->headers->set('Cache-Control', 'public');
  700. $response->headers->set('Last-Modified', $time->format(\DATE_RFC2822));
  701. if ($time->format(\DATE_RFC2822) == $request->headers->get('IF_MODIFIED_SINCE')) {
  702. $response->setStatusCode(304);
  703. $response->setContent('');
  704. }
  705. });
  706. // build initial request
  707. $this->request('GET', '/');
  708. $this->assertHttpKernelIsCalled();
  709. $this->assertEquals(200, $this->response->getStatusCode());
  710. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  711. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  712. $this->assertEquals('Hello World', $this->response->getContent());
  713. $this->assertTraceContains('miss');
  714. $this->assertTraceContains('store');
  715. $this->assertTraceNotContains('stale');
  716. // build subsequent request; should be found but miss due to freshness
  717. $this->request('GET', '/');
  718. $this->assertHttpKernelIsCalled();
  719. $this->assertEquals(200, $this->response->getStatusCode());
  720. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  721. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  722. $this->assertLessThanOrEqual(1, $this->response->headers->get('Age'));
  723. $this->assertEquals('Hello World', $this->response->getContent());
  724. $this->assertTraceContains('stale');
  725. $this->assertTraceContains('valid');
  726. $this->assertTraceContains('store');
  727. $this->assertTraceNotContains('miss');
  728. }
  729. public function testValidatesCachedResponsesUseSameHttpMethod()
  730. {
  731. $this->setNextResponse(200, [], 'Hello World', function ($request, $response) {
  732. $this->assertSame('OPTIONS', $request->getMethod());
  733. });
  734. // build initial request
  735. $this->request('OPTIONS', '/');
  736. // build subsequent request
  737. $this->request('OPTIONS', '/');
  738. }
  739. public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
  740. {
  741. $this->setNextResponse(200, [], 'Hello World', function ($request, $response) {
  742. $this->assertFalse($request->headers->has('If-Modified-Since'));
  743. $response->headers->set('Cache-Control', 'public');
  744. $response->headers->set('ETag', '"12345"');
  745. if ($response->getETag() == $request->headers->get('IF_NONE_MATCH')) {
  746. $response->setStatusCode(304);
  747. $response->setContent('');
  748. }
  749. });
  750. // build initial request
  751. $this->request('GET', '/');
  752. $this->assertHttpKernelIsCalled();
  753. $this->assertEquals(200, $this->response->getStatusCode());
  754. $this->assertNotNull($this->response->headers->get('ETag'));
  755. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  756. $this->assertEquals('Hello World', $this->response->getContent());
  757. $this->assertTraceContains('miss');
  758. $this->assertTraceContains('store');
  759. // build subsequent request; should be found but miss due to freshness
  760. $this->request('GET', '/');
  761. $this->assertHttpKernelIsCalled();
  762. $this->assertEquals(200, $this->response->getStatusCode());
  763. $this->assertNotNull($this->response->headers->get('ETag'));
  764. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  765. $this->assertLessThanOrEqual(1, $this->response->headers->get('Age'));
  766. $this->assertEquals('Hello World', $this->response->getContent());
  767. $this->assertTraceContains('stale');
  768. $this->assertTraceContains('valid');
  769. $this->assertTraceContains('store');
  770. $this->assertTraceNotContains('miss');
  771. }
  772. public function testServesResponseWhileFreshAndRevalidatesWithLastModifiedInformation()
  773. {
  774. $time = \DateTime::createFromFormat('U', time());
  775. $this->setNextResponse(200, [], 'Hello World', function (Request $request, Response $response) use ($time) {
  776. $response->setSharedMaxAge(10);
  777. $response->headers->set('Last-Modified', $time->format(\DATE_RFC2822));
  778. });
  779. // prime the cache
  780. $this->request('GET', '/');
  781. // next request before s-maxage has expired: Serve from cache
  782. // without hitting the backend
  783. $this->request('GET', '/');
  784. $this->assertHttpKernelIsNotCalled();
  785. $this->assertEquals(200, $this->response->getStatusCode());
  786. $this->assertEquals('Hello World', $this->response->getContent());
  787. $this->assertTraceContains('fresh');
  788. sleep(15); // expire the cache
  789. $this->setNextResponse(304, [], '', function (Request $request, Response $response) use ($time) {
  790. $this->assertEquals($time->format(\DATE_RFC2822), $request->headers->get('IF_MODIFIED_SINCE'));
  791. });
  792. $this->request('GET', '/');
  793. $this->assertHttpKernelIsCalled();
  794. $this->assertEquals(200, $this->response->getStatusCode());
  795. $this->assertEquals('Hello World', $this->response->getContent());
  796. $this->assertTraceContains('stale');
  797. $this->assertTraceContains('valid');
  798. }
  799. public function testReplacesCachedResponsesWhenValidationResultsInNon304Response()
  800. {
  801. $time = \DateTime::createFromFormat('U', time());
  802. $count = 0;
  803. $this->setNextResponse(200, [], 'Hello World', function ($request, $response) use ($time, &$count) {
  804. $response->headers->set('Last-Modified', $time->format(\DATE_RFC2822));
  805. $response->headers->set('Cache-Control', 'public');
  806. switch (++$count) {
  807. case 1:
  808. $response->setContent('first response');
  809. break;
  810. case 2:
  811. $response->setContent('second response');
  812. break;
  813. case 3:
  814. $response->setContent('');
  815. $response->setStatusCode(304);
  816. break;
  817. }
  818. });
  819. // first request should fetch from backend and store in cache
  820. $this->request('GET', '/');
  821. $this->assertEquals(200, $this->response->getStatusCode());
  822. $this->assertEquals('first response', $this->response->getContent());
  823. // second request is validated, is invalid, and replaces cached entry
  824. $this->request('GET', '/');
  825. $this->assertEquals(200, $this->response->getStatusCode());
  826. $this->assertEquals('second response', $this->response->getContent());
  827. // third response is validated, valid, and returns cached entry
  828. $this->request('GET', '/');
  829. $this->assertEquals(200, $this->response->getStatusCode());
  830. $this->assertEquals('second response', $this->response->getContent());
  831. $this->assertEquals(3, $count);
  832. }
  833. public function testPassesHeadRequestsThroughDirectlyOnPass()
  834. {
  835. $this->setNextResponse(200, [], 'Hello World', function ($request, $response) {
  836. $response->setContent('');
  837. $response->setStatusCode(200);
  838. $this->assertEquals('HEAD', $request->getMethod());
  839. });
  840. $this->request('HEAD', '/', ['HTTP_EXPECT' => 'something ...']);
  841. $this->assertHttpKernelIsCalled();
  842. $this->assertEquals('', $this->response->getContent());
  843. }
  844. public function testUsesCacheToRespondToHeadRequestsWhenFresh()
  845. {
  846. $this->setNextResponse(200, [], 'Hello World', function ($request, $response) {
  847. $response->headers->set('Cache-Control', 'public, max-age=10');
  848. $response->setContent('Hello World');
  849. $response->setStatusCode(200);
  850. $this->assertNotEquals('HEAD', $request->getMethod());
  851. });
  852. $this->request('GET', '/');
  853. $this->assertHttpKernelIsCalled();
  854. $this->assertEquals('Hello World', $this->response->getContent());
  855. $this->request('HEAD', '/');
  856. $this->assertHttpKernelIsNotCalled();
  857. $this->assertEquals(200, $this->response->getStatusCode());
  858. $this->assertEquals('', $this->response->getContent());
  859. $this->assertEquals(\strlen('Hello World'), $this->response->headers->get('Content-Length'));
  860. }
  861. public function testSendsNoContentWhenFresh()
  862. {
  863. $time = \DateTime::createFromFormat('U', time());
  864. $this->setNextResponse(200, [], 'Hello World', function ($request, $response) use ($time) {
  865. $response->headers->set('Cache-Control', 'public, max-age=10');
  866. $response->headers->set('Last-Modified', $time->format(\DATE_RFC2822));
  867. });
  868. $this->request('GET', '/');
  869. $this->assertHttpKernelIsCalled();
  870. $this->assertEquals('Hello World', $this->response->getContent());
  871. $this->request('GET', '/', ['HTTP_IF_MODIFIED_SINCE' => $time->format(\DATE_RFC2822)]);
  872. $this->assertHttpKernelIsNotCalled();
  873. $this->assertEquals(304, $this->response->getStatusCode());
  874. $this->assertEquals('', $this->response->getContent());
  875. }
  876. public function testInvalidatesCachedResponsesOnPost()
  877. {
  878. $this->setNextResponse(200, [], 'Hello World', function ($request, $response) {
  879. if ('GET' == $request->getMethod()) {
  880. $response->setStatusCode(200);
  881. $response->headers->set('Cache-Control', 'public, max-age=500');
  882. $response->setContent('Hello World');
  883. } elseif ('POST' == $request->getMethod()) {
  884. $response->setStatusCode(303);
  885. $response->headers->set('Location', '/');
  886. $response->headers->remove('Cache-Control');
  887. $response->setContent('');
  888. }
  889. });
  890. // build initial request to enter into the cache
  891. $this->request('GET', '/');
  892. $this->assertHttpKernelIsCalled();
  893. $this->assertEquals(200, $this->response->getStatusCode());
  894. $this->assertEquals('Hello World', $this->response->getContent());
  895. $this->assertTraceContains('miss');
  896. $this->assertTraceContains('store');
  897. // make sure it is valid
  898. $this->request('GET', '/');
  899. $this->assertHttpKernelIsNotCalled();
  900. $this->assertEquals(200, $this->response->getStatusCode());
  901. $this->assertEquals('Hello World', $this->response->getContent());
  902. $this->assertTraceContains('fresh');
  903. // now POST to same URL
  904. $this->request('POST', '/helloworld');
  905. $this->assertHttpKernelIsCalled();
  906. $this->assertEquals('/', $this->response->headers->get('Location'));
  907. $this->assertTraceContains('invalidate');
  908. $this->assertTraceContains('pass');
  909. $this->assertEquals('', $this->response->getContent());
  910. // now make sure it was actually invalidated
  911. $this->request('GET', '/');
  912. $this->assertHttpKernelIsCalled();
  913. $this->assertEquals(200, $this->response->getStatusCode());
  914. $this->assertEquals('Hello World', $this->response->getContent());
  915. $this->assertTraceContains('stale');
  916. $this->assertTraceContains('invalid');
  917. $this->assertTraceContains('store');
  918. }
  919. public function testServesFromCacheWhenHeadersMatch()
  920. {
  921. $count = 0;
  922. $this->setNextResponse(200, ['Cache-Control' => 'max-age=10000'], '', function ($request, $response) use (&$count) {
  923. $response->headers->set('Vary', 'Accept User-Agent Foo');
  924. $response->headers->set('Cache-Control', 'public, max-age=10');
  925. $response->headers->set('X-Response-Count', ++$count);
  926. $response->setContent($request->headers->get('USER_AGENT'));
  927. });
  928. $this->request('GET', '/', ['HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0']);
  929. $this->assertEquals(200, $this->response->getStatusCode());
  930. $this->assertEquals('Bob/1.0', $this->response->getContent());
  931. $this->assertTraceContains('miss');
  932. $this->assertTraceContains('store');
  933. $this->request('GET', '/', ['HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0']);
  934. $this->assertEquals(200, $this->response->getStatusCode());
  935. $this->assertEquals('Bob/1.0', $this->response->getContent());
  936. $this->assertTraceContains('fresh');
  937. $this->assertTraceNotContains('store');
  938. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  939. }
  940. public function testStoresMultipleResponsesWhenHeadersDiffer()
  941. {
  942. $count = 0;
  943. $this->setNextResponse(200, ['Cache-Control' => 'max-age=10000'], '', function ($request, $response) use (&$count) {
  944. $response->headers->set('Vary', 'Accept User-Agent Foo');
  945. $response->headers->set('Cache-Control', 'public, max-age=10');
  946. $response->headers->set('X-Response-Count', ++$count);
  947. $response->setContent($request->headers->get('USER_AGENT'));
  948. });
  949. $this->request('GET', '/', ['HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0']);
  950. $this->assertEquals(200, $this->response->getStatusCode());
  951. $this->assertEquals('Bob/1.0', $this->response->getContent());
  952. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  953. $this->request('GET', '/', ['HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0']);
  954. $this->assertEquals(200, $this->response->getStatusCode());
  955. $this->assertTraceContains('miss');
  956. $this->assertTraceContains('store');
  957. $this->assertEquals('Bob/2.0', $this->response->getContent());
  958. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  959. $this->request('GET', '/', ['HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0']);
  960. $this->assertTraceContains('fresh');
  961. $this->assertEquals('Bob/1.0', $this->response->getContent());
  962. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  963. $this->request('GET', '/', ['HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0']);
  964. $this->assertTraceContains('fresh');
  965. $this->assertEquals('Bob/2.0', $this->response->getContent());
  966. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  967. $this->request('GET', '/', ['HTTP_USER_AGENT' => 'Bob/2.0']);
  968. $this->assertTraceContains('miss');
  969. $this->assertEquals('Bob/2.0', $this->response->getContent());
  970. $this->assertEquals(3, $this->response->headers->get('X-Response-Count'));
  971. }
  972. public function testShouldCatchExceptions()
  973. {
  974. $this->catchExceptions();
  975. $this->setNextResponse();
  976. $this->request('GET', '/');
  977. $this->assertExceptionsAreCaught();
  978. }
  979. public function testShouldCatchExceptionsWhenReloadingAndNoCacheRequest()
  980. {
  981. $this->catchExceptions();
  982. $this->setNextResponse();
  983. $this->cacheConfig['allow_reload'] = true;
  984. $this->request('GET', '/', [], [], false, ['Pragma' => 'no-cache']);
  985. $this->assertExceptionsAreCaught();
  986. }
  987. public function testShouldNotCatchExceptions()
  988. {
  989. $this->catchExceptions(false);
  990. $this->setNextResponse();
  991. $this->request('GET', '/');
  992. $this->assertExceptionsAreNotCaught();
  993. }
  994. public function testEsiCacheSendsTheLowestTtl()
  995. {
  996. $responses = [
  997. [
  998. 'status' => 200,
  999. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  1000. 'headers' => [
  1001. 'Cache-Control' => 's-maxage=300',
  1002. 'Surrogate-Control' => 'content="ESI/1.0"',
  1003. ],
  1004. ],
  1005. [
  1006. 'status' => 200,
  1007. 'body' => 'Hello World!',
  1008. 'headers' => ['Cache-Control' => 's-maxage=200'],
  1009. ],
  1010. [
  1011. 'status' => 200,
  1012. 'body' => 'My name is Bobby.',
  1013. 'headers' => ['Cache-Control' => 's-maxage=100'],
  1014. ],
  1015. ];
  1016. $this->setNextResponses($responses);
  1017. $this->request('GET', '/', [], [], true);
  1018. $this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
  1019. $this->assertEquals(100, $this->response->getTtl());
  1020. }
  1021. public function testEsiCacheSendsTheLowestTtlForHeadRequests()
  1022. {
  1023. $responses = [
  1024. [
  1025. 'status' => 200,
  1026. 'body' => 'I am a long-lived main response, but I embed a short-lived resource: <esi:include src="/foo" />',
  1027. 'headers' => [
  1028. 'Cache-Control' => 's-maxage=300',
  1029. 'Surrogate-Control' => 'content="ESI/1.0"',
  1030. ],
  1031. ],
  1032. [
  1033. 'status' => 200,
  1034. 'body' => 'I am a short-lived resource',
  1035. 'headers' => ['Cache-Control' => 's-maxage=100'],
  1036. ],
  1037. ];
  1038. $this->setNextResponses($responses);
  1039. $this->request('HEAD', '/', [], [], true);
  1040. $this->assertEmpty($this->response->getContent());
  1041. $this->assertEquals(100, $this->response->getTtl());
  1042. }
  1043. public function testEsiCacheForceValidation()
  1044. {
  1045. $responses = [
  1046. [
  1047. 'status' => 200,
  1048. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  1049. 'headers' => [
  1050. 'Cache-Control' => 's-maxage=300',
  1051. 'Surrogate-Control' => 'content="ESI/1.0"',
  1052. ],
  1053. ],
  1054. [
  1055. 'status' => 200,
  1056. 'body' => 'Hello World!',
  1057. 'headers' => ['ETag' => 'foobar'],
  1058. ],
  1059. [
  1060. 'status' => 200,
  1061. 'body' => 'My name is Bobby.',
  1062. 'headers' => ['Cache-Control' => 's-maxage=100'],
  1063. ],
  1064. ];
  1065. $this->setNextResponses($responses);
  1066. $this->request('GET', '/', [], [], true);
  1067. $this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
  1068. $this->assertNull($this->response->getTtl());
  1069. $this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
  1070. $this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
  1071. }
  1072. public function testEsiCacheForceValidationForHeadRequests()
  1073. {
  1074. $responses = [
  1075. [
  1076. 'status' => 200,
  1077. 'body' => 'I am the main response and use expiration caching, but I embed another resource: <esi:include src="/foo" />',
  1078. 'headers' => [
  1079. 'Cache-Control' => 's-maxage=300',
  1080. 'Surrogate-Control' => 'content="ESI/1.0"',
  1081. ],
  1082. ],
  1083. [
  1084. 'status' => 200,
  1085. 'body' => 'I am the embedded resource and use validation caching',
  1086. 'headers' => ['ETag' => 'foobar'],
  1087. ],
  1088. ];
  1089. $this->setNextResponses($responses);
  1090. $this->request('HEAD', '/', [], [], true);
  1091. // The response has been assembled from expiration and validation based resources
  1092. // This can neither be cached nor revalidated, so it should be private/no cache
  1093. $this->assertEmpty($this->response->getContent());
  1094. $this->assertNull($this->response->getTtl());
  1095. $this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
  1096. $this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
  1097. }
  1098. public function testEsiRecalculateContentLengthHeader()
  1099. {
  1100. $responses = [
  1101. [
  1102. 'status' => 200,
  1103. 'body' => '<esi:include src="/foo" />',
  1104. 'headers' => [
  1105. 'Content-Length' => 26,
  1106. 'Surrogate-Control' => 'content="ESI/1.0"',
  1107. ],
  1108. ],
  1109. [
  1110. 'status' => 200,
  1111. 'body' => 'Hello World!',
  1112. 'headers' => [],
  1113. ],
  1114. ];
  1115. $this->setNextResponses($responses);
  1116. $this->request('GET', '/', [], [], true);
  1117. $this->assertEquals('Hello World!', $this->response->getContent());
  1118. $this->assertEquals(12, $this->response->headers->get('Content-Length'));
  1119. }
  1120. public function testEsiRecalculateContentLengthHeaderForHeadRequest()
  1121. {
  1122. $responses = [
  1123. [
  1124. 'status' => 200,
  1125. 'body' => '<esi:include src="/foo" />',
  1126. 'headers' => [
  1127. 'Content-Length' => 26,
  1128. 'Surrogate-Control' => 'content="ESI/1.0"',
  1129. ],
  1130. ],
  1131. [
  1132. 'status' => 200,
  1133. 'body' => 'Hello World!',
  1134. 'headers' => [],
  1135. ],
  1136. ];
  1137. $this->setNextResponses($responses);
  1138. $this->request('HEAD', '/', [], [], true);
  1139. // https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13
  1140. // "The Content-Length entity-header field indicates the size of the entity-body,
  1141. // in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD
  1142. // method, the size of the entity-body that would have been sent had the request
  1143. // been a GET."
  1144. $this->assertEmpty($this->response->getContent());
  1145. $this->assertEquals(12, $this->response->headers->get('Content-Length'));
  1146. }
  1147. public function testClientIpIsAlwaysLocalhostForForwardedRequests()
  1148. {
  1149. $this->setNextResponse();
  1150. $this->request('GET', '/', ['REMOTE_ADDR' => '10.0.0.1']);
  1151. $this->kernel->assert(function ($backendRequest) {
  1152. $this->assertSame('127.0.0.1', $backendRequest->server->get('REMOTE_ADDR'));
  1153. });
  1154. }
  1155. /**
  1156. * @dataProvider getTrustedProxyData
  1157. */
  1158. public function testHttpCacheIsSetAsATrustedProxy(array $existing)
  1159. {
  1160. Request::setTrustedProxies($existing, Request::HEADER_X_FORWARDED_FOR);
  1161. $this->setNextResponse();
  1162. $this->request('GET', '/', ['REMOTE_ADDR' => '10.0.0.1']);
  1163. $this->assertSame($existing, Request::getTrustedProxies());
  1164. $existing = array_unique(array_merge($existing, ['127.0.0.1']));
  1165. $this->kernel->assert(function ($backendRequest) use ($existing) {
  1166. $this->assertSame($existing, Request::getTrustedProxies());
  1167. $this->assertsame('10.0.0.1', $backendRequest->getClientIp());
  1168. });
  1169. Request::setTrustedProxies([], -1);
  1170. }
  1171. public static function getTrustedProxyData()
  1172. {
  1173. return [
  1174. [[]],
  1175. [['10.0.0.2']],
  1176. [['10.0.0.2', '127.0.0.1']],
  1177. ];
  1178. }
  1179. /**
  1180. * @dataProvider getForwardedData
  1181. */
  1182. public function testForwarderHeaderForForwardedRequests($forwarded, $expected)
  1183. {
  1184. $this->setNextResponse();
  1185. $server = ['REMOTE_ADDR' => '10.0.0.1'];
  1186. if (null !== $forwarded) {
  1187. Request::setTrustedProxies($server, -1);
  1188. $server['HTTP_FORWARDED'] = $forwarded;
  1189. }
  1190. $this->request('GET', '/', $server);
  1191. $this->kernel->assert(function ($backendRequest) use ($expected) {
  1192. $this->assertSame($expected, $backendRequest->headers->get('Forwarded'));
  1193. });
  1194. Request::setTrustedProxies([], -1);
  1195. }
  1196. public static function getForwardedData()
  1197. {
  1198. return [
  1199. [null, 'for="10.0.0.1";host="localhost";proto=http'],
  1200. ['for=10.0.0.2', 'for="10.0.0.2";host="localhost";proto=http, for="10.0.0.1"'],
  1201. ['for=10.0.0.2, for=10.0.0.3', 'for="10.0.0.2";host="localhost";proto=http, for="10.0.0.3", for="10.0.0.1"'],
  1202. ];
  1203. }
  1204. public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponses()
  1205. {
  1206. $time = \DateTime::createFromFormat('U', time());
  1207. $responses = [
  1208. [
  1209. 'status' => 200,
  1210. 'body' => '<esi:include src="/hey" />',
  1211. 'headers' => [
  1212. 'Surrogate-Control' => 'content="ESI/1.0"',
  1213. 'ETag' => 'hey',
  1214. 'Last-Modified' => $time->format(\DATE_RFC2822),
  1215. ],
  1216. ],
  1217. [
  1218. 'status' => 200,
  1219. 'body' => 'Hey!',
  1220. 'headers' => [],
  1221. ],
  1222. ];
  1223. $this->setNextResponses($responses);
  1224. $this->request('GET', '/', [], [], true);
  1225. $this->assertNull($this->response->getETag());
  1226. $this->assertNull($this->response->getLastModified());
  1227. }
  1228. public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponsesAndHeadRequest()
  1229. {
  1230. $time = \DateTime::createFromFormat('U', time());
  1231. $responses = [
  1232. [
  1233. 'status' => 200,
  1234. 'body' => '<esi:include src="/hey" />',
  1235. 'headers' => [
  1236. 'Surrogate-Control' => 'content="ESI/1.0"',
  1237. 'ETag' => 'hey',
  1238. 'Last-Modified' => $time->format(\DATE_RFC2822),
  1239. ],
  1240. ],
  1241. [
  1242. 'status' => 200,
  1243. 'body' => 'Hey!',
  1244. 'headers' => [],
  1245. ],
  1246. ];
  1247. $this->setNextResponses($responses);
  1248. $this->request('HEAD', '/', [], [], true);
  1249. $this->assertEmpty($this->response->getContent());
  1250. $this->assertNull($this->response->getETag());
  1251. $this->assertNull($this->response->getLastModified());
  1252. }
  1253. public function testDoesNotCacheOptionsRequest()
  1254. {
  1255. $this->setNextResponse(200, ['Cache-Control' => 'public, s-maxage=60'], 'get');
  1256. $this->request('GET', '/');
  1257. $this->assertHttpKernelIsCalled();
  1258. $this->setNextResponse(200, ['Cache-Control' => 'public, s-maxage=60'], 'options');
  1259. $this->request('OPTIONS', '/');
  1260. $this->assertHttpKernelIsCalled();
  1261. $this->request('GET', '/');
  1262. $this->assertHttpKernelIsNotCalled();
  1263. $this->assertSame('get', $this->response->getContent());
  1264. }
  1265. public function testUsesOriginalRequestForSurrogate()
  1266. {
  1267. $kernel = $this->createMock(HttpKernelInterface::class);
  1268. $store = $this->createMock(StoreInterface::class);
  1269. $kernel
  1270. ->expects($this->exactly(2))
  1271. ->method('handle')
  1272. ->willReturnCallback(function (Request $request) {
  1273. $this->assertSame('127.0.0.1', $request->server->get('REMOTE_ADDR'));
  1274. return new Response();
  1275. });
  1276. $cache = new HttpCache($kernel,
  1277. $store,
  1278. new Esi()
  1279. );
  1280. $request = Request::create('/');
  1281. $request->server->set('REMOTE_ADDR', '10.0.0.1');
  1282. // Main request
  1283. $cache->handle($request, HttpKernelInterface::MAIN_REQUEST);
  1284. // Main request was now modified by HttpCache
  1285. // The surrogate will ask for the request using $this->cache->getRequest()
  1286. // which MUST return the original request so the surrogate
  1287. // can actually behave like a reverse proxy like e.g. Varnish would.
  1288. $this->assertSame('10.0.0.1', $cache->getRequest()->getClientIp());
  1289. $this->assertSame('10.0.0.1', $cache->getRequest()->server->get('REMOTE_ADDR'));
  1290. // Surrogate request
  1291. $cache->handle($request, HttpKernelInterface::SUB_REQUEST);
  1292. }
  1293. public function testStaleIfErrorMustNotResetLifetime()
  1294. {
  1295. // Make sure we don't accidentally treat the response as fresh (revalidated) again
  1296. // when stale-if-error handling kicks in.
  1297. $responses = [
  1298. [
  1299. 'status' => 200,
  1300. 'body' => 'OK',
  1301. // This is cacheable and can be used in stale-if-error cases:
  1302. 'headers' => ['Cache-Control' => 'public, max-age=10', 'ETag' => 'some-etag'],
  1303. ],
  1304. [
  1305. 'status' => 500,
  1306. 'body' => 'FAIL',
  1307. 'headers' => [],
  1308. ],
  1309. [
  1310. 'status' => 500,
  1311. 'body' => 'FAIL',
  1312. 'headers' => [],
  1313. ],
  1314. ];
  1315. $this->setNextResponses($responses);
  1316. $this->cacheConfig['stale_if_error'] = 10;
  1317. $this->request('GET', '/'); // warm cache
  1318. sleep(15); // now the entry is stale, but still within the grace period (10s max-age + 10s stale-if-error)
  1319. $this->request('GET', '/'); // hit backend error
  1320. $this->assertEquals(200, $this->response->getStatusCode()); // stale-if-error saved the day
  1321. $this->assertEquals(15, $this->response->getAge());
  1322. sleep(10); // now we're outside the grace period
  1323. $this->request('GET', '/'); // hit backend error
  1324. $this->assertEquals(500, $this->response->getStatusCode()); // fail
  1325. }
  1326. /**
  1327. * @dataProvider getResponseDataThatMayBeServedStaleIfError
  1328. */
  1329. public function testResponsesThatMayBeUsedStaleIfError($responseHeaders, $sleepBetweenRequests = null)
  1330. {
  1331. $responses = [
  1332. [
  1333. 'status' => 200,
  1334. 'body' => 'OK',
  1335. 'headers' => $responseHeaders,
  1336. ],
  1337. [
  1338. 'status' => 500,
  1339. 'body' => 'FAIL',
  1340. 'headers' => [],
  1341. ],
  1342. ];
  1343. $this->setNextResponses($responses);
  1344. $this->cacheConfig['stale_if_error'] = 10; // after stale, may be served for 10s
  1345. $this->request('GET', '/'); // warm cache
  1346. if ($sleepBetweenRequests) {
  1347. sleep($sleepBetweenRequests);
  1348. }
  1349. $this->request('GET', '/'); // hit backend error
  1350. $this->assertEquals(200, $this->response->getStatusCode());
  1351. $this->assertEquals('OK', $this->response->getContent());
  1352. $this->assertTraceContains('stale-if-error');
  1353. }
  1354. public static function getResponseDataThatMayBeServedStaleIfError()
  1355. {
  1356. // All data sets assume that a 10s stale-if-error grace period has been configured
  1357. yield 'public, max-age expired' => [['Cache-Control' => 'public, max-age=60'], 65];
  1358. yield 'public, validateable with ETag, no TTL' => [['Cache-Control' => 'public', 'ETag' => 'some-etag'], 5];
  1359. yield 'public, validateable with Last-Modified, no TTL' => [['Cache-Control' => 'public', 'Last-Modified' => 'yesterday'], 5];
  1360. yield 'public, s-maxage will be served stale-if-error, even if the RFC mandates otherwise' => [['Cache-Control' => 'public, s-maxage=20'], 25];
  1361. }
  1362. /**
  1363. * @dataProvider getResponseDataThatMustNotBeServedStaleIfError
  1364. */
  1365. public function testResponsesThatMustNotBeUsedStaleIfError($responseHeaders, $sleepBetweenRequests = null)
  1366. {
  1367. $responses = [
  1368. [
  1369. 'status' => 200,
  1370. 'body' => 'OK',
  1371. 'headers' => $responseHeaders,
  1372. ],
  1373. [
  1374. 'status' => 500,
  1375. 'body' => 'FAIL',
  1376. 'headers' => [],
  1377. ],
  1378. ];
  1379. $this->setNextResponses($responses);
  1380. $this->cacheConfig['stale_if_error'] = 10; // after stale, may be served for 10s
  1381. $this->cacheConfig['strict_smaxage'] = true; // full RFC compliance for this feature
  1382. $this->request('GET', '/'); // warm cache
  1383. if ($sleepBetweenRequests) {
  1384. sleep($sleepBetweenRequests);
  1385. }
  1386. $this->request('GET', '/'); // hit backend error
  1387. $this->assertEquals(500, $this->response->getStatusCode());
  1388. }
  1389. public static function getResponseDataThatMustNotBeServedStaleIfError()
  1390. {
  1391. // All data sets assume that a 10s stale-if-error grace period has been configured
  1392. yield 'public, no TTL but beyond grace period' => [['Cache-Control' => 'public'], 15];
  1393. yield 'public, validateable with ETag, no TTL but beyond grace period' => [['Cache-Control' => 'public', 'ETag' => 'some-etag'], 15];
  1394. yield 'public, validateable with Last-Modified, no TTL but beyond grace period' => [['Cache-Control' => 'public', 'Last-Modified' => 'yesterday'], 15];
  1395. yield 'public, stale beyond grace period' => [['Cache-Control' => 'public, max-age=10'], 30];
  1396. // Cache-control values that prohibit serving stale responses or responses without positive validation -
  1397. // see https://tools.ietf.org/html/rfc7234#section-4.2.4 and
  1398. // https://tools.ietf.org/html/rfc7234#section-5.2.2
  1399. yield 'no-cache requires positive validation' => [['Cache-Control' => 'public, no-cache', 'ETag' => 'some-etag']];
  1400. yield 'no-cache requires positive validation, even if fresh' => [['Cache-Control' => 'public, no-cache, max-age=10']];
  1401. yield 'must-revalidate requires positive validation once stale' => [['Cache-Control' => 'public, max-age=10, must-revalidate'], 15];
  1402. yield 'proxy-revalidate requires positive validation once stale' => [['Cache-Control' => 'public, max-age=10, proxy-revalidate'], 15];
  1403. }
  1404. public function testStaleIfErrorWhenStrictSmaxageDisabled()
  1405. {
  1406. $responses = [
  1407. [
  1408. 'status' => 200,
  1409. 'body' => 'OK',
  1410. 'headers' => ['Cache-Control' => 'public, s-maxage=20'],
  1411. ],
  1412. [
  1413. 'status' => 500,
  1414. 'body' => 'FAIL',
  1415. 'headers' => [],
  1416. ],
  1417. ];
  1418. $this->setNextResponses($responses);
  1419. $this->cacheConfig['stale_if_error'] = 10;
  1420. $this->cacheConfig['strict_smaxage'] = false;
  1421. $this->request('GET', '/'); // warm cache
  1422. sleep(25);
  1423. $this->request('GET', '/'); // hit backend error
  1424. $this->assertEquals(200, $this->response->getStatusCode());
  1425. $this->assertEquals('OK', $this->response->getContent());
  1426. $this->assertTraceContains('stale-if-error');
  1427. }
  1428. public function testTraceHeaderNameCanBeChanged()
  1429. {
  1430. $this->cacheConfig['trace_header'] = 'X-My-Header';
  1431. $this->setNextResponse();
  1432. $this->request('GET', '/');
  1433. $this->assertTrue($this->response->headers->has('X-My-Header'));
  1434. }
  1435. public function testTraceLevelDefaultsToFullIfDebug()
  1436. {
  1437. $this->setNextResponse();
  1438. $this->request('GET', '/');
  1439. $this->assertTrue($this->response->headers->has('X-Symfony-Cache'));
  1440. $this->assertEquals('GET /: miss', $this->response->headers->get('X-Symfony-Cache'));
  1441. }
  1442. public function testTraceLevelDefaultsToNoneIfNotDebug()
  1443. {
  1444. $this->cacheConfig['debug'] = false;
  1445. $this->setNextResponse();
  1446. $this->request('GET', '/');
  1447. $this->assertFalse($this->response->headers->has('X-Symfony-Cache'));
  1448. }
  1449. public function testTraceLevelShort()
  1450. {
  1451. $this->cacheConfig['trace_level'] = 'short';
  1452. $this->setNextResponse();
  1453. $this->request('GET', '/');
  1454. $this->assertTrue($this->response->headers->has('X-Symfony-Cache'));
  1455. $this->assertEquals('miss', $this->response->headers->get('X-Symfony-Cache'));
  1456. }
  1457. }
  1458. class TestKernel implements HttpKernelInterface
  1459. {
  1460. public $terminateCalled = false;
  1461. public function terminate(Request $request, Response $response)
  1462. {
  1463. $this->terminateCalled = true;
  1464. }
  1465. public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = true): Response
  1466. {
  1467. }
  1468. }