HttpCacheTest.php 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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. }
  1282. class TestKernel implements HttpKernelInterface
  1283. {
  1284. public $terminateCalled = false;
  1285. public function terminate(Request $request, Response $response)
  1286. {
  1287. $this->terminateCalled = true;
  1288. }
  1289. public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
  1290. {
  1291. }
  1292. }