Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HttpCacheTest.php 64KB

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