Нет описания

HttpCacheTest.php 54KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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\HttpKernel\HttpCache\HttpCache;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\HttpKernelInterface;
  15. /**
  16. * @group time-sensitive
  17. */
  18. class HttpCacheTest extends HttpCacheTestCase
  19. {
  20. public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
  21. {
  22. $storeMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\HttpCache\\StoreInterface')
  23. ->disableOriginalConstructor()
  24. ->getMock();
  25. // does not implement TerminableInterface
  26. $kernel = new TestKernel();
  27. $httpCache = new HttpCache($kernel, $storeMock);
  28. $httpCache->terminate(Request::create('/'), new Response());
  29. $this->assertFalse($kernel->terminateCalled, 'terminate() is never called if the kernel class does not implement TerminableInterface');
  30. // implements TerminableInterface
  31. $kernelMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Kernel')
  32. ->disableOriginalConstructor()
  33. ->setMethods(array('terminate', 'registerBundles', 'registerContainerConfiguration'))
  34. ->getMock();
  35. $kernelMock->expects($this->once())
  36. ->method('terminate');
  37. $kernel = new HttpCache($kernelMock, $storeMock);
  38. $kernel->terminate(Request::create('/'), new Response());
  39. }
  40. public function testPassesOnNonGetHeadRequests()
  41. {
  42. $this->setNextResponse(200);
  43. $this->request('POST', '/');
  44. $this->assertHttpKernelIsCalled();
  45. $this->assertResponseOk();
  46. $this->assertTraceContains('pass');
  47. $this->assertFalse($this->response->headers->has('Age'));
  48. }
  49. public function testInvalidatesOnPostPutDeleteRequests()
  50. {
  51. foreach (array('post', 'put', 'delete') as $method) {
  52. $this->setNextResponse(200);
  53. $this->request($method, '/');
  54. $this->assertHttpKernelIsCalled();
  55. $this->assertResponseOk();
  56. $this->assertTraceContains('invalidate');
  57. $this->assertTraceContains('pass');
  58. }
  59. }
  60. public function testDoesNotCacheWithAuthorizationRequestHeaderAndNonPublicResponse()
  61. {
  62. $this->setNextResponse(200, array('ETag' => '"Foo"'));
  63. $this->request('GET', '/', array('HTTP_AUTHORIZATION' => 'basic foobarbaz'));
  64. $this->assertHttpKernelIsCalled();
  65. $this->assertResponseOk();
  66. $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
  67. $this->assertTraceContains('miss');
  68. $this->assertTraceNotContains('store');
  69. $this->assertFalse($this->response->headers->has('Age'));
  70. }
  71. public function testDoesCacheWithAuthorizationRequestHeaderAndPublicResponse()
  72. {
  73. $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '"Foo"'));
  74. $this->request('GET', '/', array('HTTP_AUTHORIZATION' => 'basic foobarbaz'));
  75. $this->assertHttpKernelIsCalled();
  76. $this->assertResponseOk();
  77. $this->assertTraceContains('miss');
  78. $this->assertTraceContains('store');
  79. $this->assertTrue($this->response->headers->has('Age'));
  80. $this->assertEquals('public', $this->response->headers->get('Cache-Control'));
  81. }
  82. public function testDoesNotCacheWithCookieHeaderAndNonPublicResponse()
  83. {
  84. $this->setNextResponse(200, array('ETag' => '"Foo"'));
  85. $this->request('GET', '/', array(), array('foo' => 'bar'));
  86. $this->assertHttpKernelIsCalled();
  87. $this->assertResponseOk();
  88. $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
  89. $this->assertTraceContains('miss');
  90. $this->assertTraceNotContains('store');
  91. $this->assertFalse($this->response->headers->has('Age'));
  92. }
  93. public function testDoesNotCacheRequestsWithACookieHeader()
  94. {
  95. $this->setNextResponse(200);
  96. $this->request('GET', '/', array(), array('foo' => 'bar'));
  97. $this->assertHttpKernelIsCalled();
  98. $this->assertResponseOk();
  99. $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
  100. $this->assertTraceContains('miss');
  101. $this->assertTraceNotContains('store');
  102. $this->assertFalse($this->response->headers->has('Age'));
  103. }
  104. public function testRespondsWith304WhenIfModifiedSinceMatchesLastModified()
  105. {
  106. $time = \DateTime::createFromFormat('U', time());
  107. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Last-Modified' => $time->format(DATE_RFC2822), 'Content-Type' => 'text/plain'), 'Hello World');
  108. $this->request('GET', '/', array('HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  109. $this->assertHttpKernelIsCalled();
  110. $this->assertEquals(304, $this->response->getStatusCode());
  111. $this->assertEquals('', $this->response->headers->get('Content-Type'));
  112. $this->assertEmpty($this->response->getContent());
  113. $this->assertTraceContains('miss');
  114. $this->assertTraceContains('store');
  115. }
  116. public function testRespondsWith304WhenIfNoneMatchMatchesETag()
  117. {
  118. $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '12345', 'Content-Type' => 'text/plain'), 'Hello World');
  119. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345'));
  120. $this->assertHttpKernelIsCalled();
  121. $this->assertEquals(304, $this->response->getStatusCode());
  122. $this->assertEquals('', $this->response->headers->get('Content-Type'));
  123. $this->assertTrue($this->response->headers->has('ETag'));
  124. $this->assertEmpty($this->response->getContent());
  125. $this->assertTraceContains('miss');
  126. $this->assertTraceContains('store');
  127. }
  128. public function testRespondsWith304OnlyIfIfNoneMatchAndIfModifiedSinceBothMatch()
  129. {
  130. $time = \DateTime::createFromFormat('U', time());
  131. $this->setNextResponse(200, array(), '', function ($request, $response) use ($time) {
  132. $response->setStatusCode(200);
  133. $response->headers->set('ETag', '12345');
  134. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  135. $response->headers->set('Content-Type', 'text/plain');
  136. $response->setContent('Hello World');
  137. });
  138. // only ETag matches
  139. $t = \DateTime::createFromFormat('U', time() - 3600);
  140. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => $t->format(DATE_RFC2822)));
  141. $this->assertHttpKernelIsCalled();
  142. $this->assertEquals(200, $this->response->getStatusCode());
  143. // only Last-Modified matches
  144. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '1234', 'HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  145. $this->assertHttpKernelIsCalled();
  146. $this->assertEquals(200, $this->response->getStatusCode());
  147. // Both matches
  148. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  149. $this->assertHttpKernelIsCalled();
  150. $this->assertEquals(304, $this->response->getStatusCode());
  151. }
  152. public function testIncrementsMaxAgeWhenNoDateIsSpecifiedEventWhenUsingETag()
  153. {
  154. $this->setNextResponse(
  155. 200,
  156. array(
  157. 'ETag' => '1234',
  158. 'Cache-Control' => 'public, s-maxage=60',
  159. )
  160. );
  161. $this->request('GET', '/');
  162. $this->assertHttpKernelIsCalled();
  163. $this->assertEquals(200, $this->response->getStatusCode());
  164. $this->assertTraceContains('miss');
  165. $this->assertTraceContains('store');
  166. sleep(2);
  167. $this->request('GET', '/');
  168. $this->assertHttpKernelIsNotCalled();
  169. $this->assertEquals(200, $this->response->getStatusCode());
  170. $this->assertTraceContains('fresh');
  171. $this->assertEquals(2, $this->response->headers->get('Age'));
  172. }
  173. public function testValidatesPrivateResponsesCachedOnTheClient()
  174. {
  175. $this->setNextResponse(200, array(), '', function ($request, $response) {
  176. $etags = preg_split('/\s*,\s*/', $request->headers->get('IF_NONE_MATCH'));
  177. if ($request->cookies->has('authenticated')) {
  178. $response->headers->set('Cache-Control', 'private, no-store');
  179. $response->setETag('"private tag"');
  180. if (in_array('"private tag"', $etags)) {
  181. $response->setStatusCode(304);
  182. } else {
  183. $response->setStatusCode(200);
  184. $response->headers->set('Content-Type', 'text/plain');
  185. $response->setContent('private data');
  186. }
  187. } else {
  188. $response->headers->set('Cache-Control', 'public');
  189. $response->setETag('"public tag"');
  190. if (in_array('"public tag"', $etags)) {
  191. $response->setStatusCode(304);
  192. } else {
  193. $response->setStatusCode(200);
  194. $response->headers->set('Content-Type', 'text/plain');
  195. $response->setContent('public data');
  196. }
  197. }
  198. });
  199. $this->request('GET', '/');
  200. $this->assertHttpKernelIsCalled();
  201. $this->assertEquals(200, $this->response->getStatusCode());
  202. $this->assertEquals('"public tag"', $this->response->headers->get('ETag'));
  203. $this->assertEquals('public data', $this->response->getContent());
  204. $this->assertTraceContains('miss');
  205. $this->assertTraceContains('store');
  206. $this->request('GET', '/', array(), array('authenticated' => ''));
  207. $this->assertHttpKernelIsCalled();
  208. $this->assertEquals(200, $this->response->getStatusCode());
  209. $this->assertEquals('"private tag"', $this->response->headers->get('ETag'));
  210. $this->assertEquals('private data', $this->response->getContent());
  211. $this->assertTraceContains('stale');
  212. $this->assertTraceContains('invalid');
  213. $this->assertTraceNotContains('store');
  214. }
  215. public function testStoresResponsesWhenNoCacheRequestDirectivePresent()
  216. {
  217. $time = \DateTime::createFromFormat('U', time() + 5);
  218. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  219. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  220. $this->assertHttpKernelIsCalled();
  221. $this->assertTraceContains('store');
  222. $this->assertTrue($this->response->headers->has('Age'));
  223. }
  224. public function testReloadsResponsesWhenCacheHitsButNoCacheRequestDirectivePresentWhenAllowReloadIsSetTrue()
  225. {
  226. $count = 0;
  227. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=10000'), '', function ($request, $response) use (&$count) {
  228. ++$count;
  229. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  230. });
  231. $this->request('GET', '/');
  232. $this->assertEquals(200, $this->response->getStatusCode());
  233. $this->assertEquals('Hello World', $this->response->getContent());
  234. $this->assertTraceContains('store');
  235. $this->request('GET', '/');
  236. $this->assertEquals(200, $this->response->getStatusCode());
  237. $this->assertEquals('Hello World', $this->response->getContent());
  238. $this->assertTraceContains('fresh');
  239. $this->cacheConfig['allow_reload'] = true;
  240. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  241. $this->assertEquals(200, $this->response->getStatusCode());
  242. $this->assertEquals('Goodbye World', $this->response->getContent());
  243. $this->assertTraceContains('reload');
  244. $this->assertTraceContains('store');
  245. }
  246. public function testDoesNotReloadResponsesWhenAllowReloadIsSetFalseDefault()
  247. {
  248. $count = 0;
  249. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=10000'), '', function ($request, $response) use (&$count) {
  250. ++$count;
  251. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  252. });
  253. $this->request('GET', '/');
  254. $this->assertEquals(200, $this->response->getStatusCode());
  255. $this->assertEquals('Hello World', $this->response->getContent());
  256. $this->assertTraceContains('store');
  257. $this->request('GET', '/');
  258. $this->assertEquals(200, $this->response->getStatusCode());
  259. $this->assertEquals('Hello World', $this->response->getContent());
  260. $this->assertTraceContains('fresh');
  261. $this->cacheConfig['allow_reload'] = false;
  262. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  263. $this->assertEquals(200, $this->response->getStatusCode());
  264. $this->assertEquals('Hello World', $this->response->getContent());
  265. $this->assertTraceNotContains('reload');
  266. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  267. $this->assertEquals(200, $this->response->getStatusCode());
  268. $this->assertEquals('Hello World', $this->response->getContent());
  269. $this->assertTraceNotContains('reload');
  270. }
  271. public function testRevalidatesFreshCacheEntryWhenMaxAgeRequestDirectiveIsExceededWhenAllowRevalidateOptionIsSetTrue()
  272. {
  273. $count = 0;
  274. $this->setNextResponse(200, array(), '', function ($request, $response) use (&$count) {
  275. ++$count;
  276. $response->headers->set('Cache-Control', 'public, max-age=10000');
  277. $response->setETag($count);
  278. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  279. });
  280. $this->request('GET', '/');
  281. $this->assertEquals(200, $this->response->getStatusCode());
  282. $this->assertEquals('Hello World', $this->response->getContent());
  283. $this->assertTraceContains('store');
  284. $this->request('GET', '/');
  285. $this->assertEquals(200, $this->response->getStatusCode());
  286. $this->assertEquals('Hello World', $this->response->getContent());
  287. $this->assertTraceContains('fresh');
  288. $this->cacheConfig['allow_revalidate'] = true;
  289. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  290. $this->assertEquals(200, $this->response->getStatusCode());
  291. $this->assertEquals('Goodbye World', $this->response->getContent());
  292. $this->assertTraceContains('stale');
  293. $this->assertTraceContains('invalid');
  294. $this->assertTraceContains('store');
  295. }
  296. public function testDoesNotRevalidateFreshCacheEntryWhenEnableRevalidateOptionIsSetFalseDefault()
  297. {
  298. $count = 0;
  299. $this->setNextResponse(200, array(), '', function ($request, $response) use (&$count) {
  300. ++$count;
  301. $response->headers->set('Cache-Control', 'public, max-age=10000');
  302. $response->setETag($count);
  303. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  304. });
  305. $this->request('GET', '/');
  306. $this->assertEquals(200, $this->response->getStatusCode());
  307. $this->assertEquals('Hello World', $this->response->getContent());
  308. $this->assertTraceContains('store');
  309. $this->request('GET', '/');
  310. $this->assertEquals(200, $this->response->getStatusCode());
  311. $this->assertEquals('Hello World', $this->response->getContent());
  312. $this->assertTraceContains('fresh');
  313. $this->cacheConfig['allow_revalidate'] = false;
  314. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  315. $this->assertEquals(200, $this->response->getStatusCode());
  316. $this->assertEquals('Hello World', $this->response->getContent());
  317. $this->assertTraceNotContains('stale');
  318. $this->assertTraceNotContains('invalid');
  319. $this->assertTraceContains('fresh');
  320. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  321. $this->assertEquals(200, $this->response->getStatusCode());
  322. $this->assertEquals('Hello World', $this->response->getContent());
  323. $this->assertTraceNotContains('stale');
  324. $this->assertTraceNotContains('invalid');
  325. $this->assertTraceContains('fresh');
  326. }
  327. public function testFetchesResponseFromBackendWhenCacheMisses()
  328. {
  329. $time = \DateTime::createFromFormat('U', time() + 5);
  330. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  331. $this->request('GET', '/');
  332. $this->assertEquals(200, $this->response->getStatusCode());
  333. $this->assertTraceContains('miss');
  334. $this->assertTrue($this->response->headers->has('Age'));
  335. }
  336. public function testDoesNotCacheSomeStatusCodeResponses()
  337. {
  338. 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) {
  339. $time = \DateTime::createFromFormat('U', time() + 5);
  340. $this->setNextResponse($code, array('Expires' => $time->format(DATE_RFC2822)));
  341. $this->request('GET', '/');
  342. $this->assertEquals($code, $this->response->getStatusCode());
  343. $this->assertTraceNotContains('store');
  344. $this->assertFalse($this->response->headers->has('Age'));
  345. }
  346. }
  347. public function testDoesNotCacheResponsesWithExplicitNoStoreDirective()
  348. {
  349. $time = \DateTime::createFromFormat('U', time() + 5);
  350. $this->setNextResponse(200, array('Expires' => $time->format(DATE_RFC2822), 'Cache-Control' => 'no-store'));
  351. $this->request('GET', '/');
  352. $this->assertTraceNotContains('store');
  353. $this->assertFalse($this->response->headers->has('Age'));
  354. }
  355. public function testDoesNotCacheResponsesWithoutFreshnessInformationOrAValidator()
  356. {
  357. $this->setNextResponse();
  358. $this->request('GET', '/');
  359. $this->assertEquals(200, $this->response->getStatusCode());
  360. $this->assertTraceNotContains('store');
  361. }
  362. public function testCachesResponsesWithExplicitNoCacheDirective()
  363. {
  364. $time = \DateTime::createFromFormat('U', time() + 5);
  365. $this->setNextResponse(200, array('Expires' => $time->format(DATE_RFC2822), 'Cache-Control' => 'public, no-cache'));
  366. $this->request('GET', '/');
  367. $this->assertTraceContains('store');
  368. $this->assertTrue($this->response->headers->has('Age'));
  369. }
  370. public function testCachesResponsesWithAnExpirationHeader()
  371. {
  372. $time = \DateTime::createFromFormat('U', time() + 5);
  373. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  374. $this->request('GET', '/');
  375. $this->assertEquals(200, $this->response->getStatusCode());
  376. $this->assertEquals('Hello World', $this->response->getContent());
  377. $this->assertNotNull($this->response->headers->get('Date'));
  378. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  379. $this->assertTraceContains('miss');
  380. $this->assertTraceContains('store');
  381. $values = $this->getMetaStorageValues();
  382. $this->assertCount(1, $values);
  383. }
  384. public function testCachesResponsesWithAMaxAgeDirective()
  385. {
  386. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=5'));
  387. $this->request('GET', '/');
  388. $this->assertEquals(200, $this->response->getStatusCode());
  389. $this->assertEquals('Hello World', $this->response->getContent());
  390. $this->assertNotNull($this->response->headers->get('Date'));
  391. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  392. $this->assertTraceContains('miss');
  393. $this->assertTraceContains('store');
  394. $values = $this->getMetaStorageValues();
  395. $this->assertCount(1, $values);
  396. }
  397. public function testCachesResponsesWithASMaxAgeDirective()
  398. {
  399. $this->setNextResponse(200, array('Cache-Control' => 's-maxage=5'));
  400. $this->request('GET', '/');
  401. $this->assertEquals(200, $this->response->getStatusCode());
  402. $this->assertEquals('Hello World', $this->response->getContent());
  403. $this->assertNotNull($this->response->headers->get('Date'));
  404. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  405. $this->assertTraceContains('miss');
  406. $this->assertTraceContains('store');
  407. $values = $this->getMetaStorageValues();
  408. $this->assertCount(1, $values);
  409. }
  410. public function testCachesResponsesWithALastModifiedValidatorButNoFreshnessInformation()
  411. {
  412. $time = \DateTime::createFromFormat('U', time());
  413. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Last-Modified' => $time->format(DATE_RFC2822)));
  414. $this->request('GET', '/');
  415. $this->assertEquals(200, $this->response->getStatusCode());
  416. $this->assertEquals('Hello World', $this->response->getContent());
  417. $this->assertTraceContains('miss');
  418. $this->assertTraceContains('store');
  419. }
  420. public function testCachesResponsesWithAnETagValidatorButNoFreshnessInformation()
  421. {
  422. $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '"123456"'));
  423. $this->request('GET', '/');
  424. $this->assertEquals(200, $this->response->getStatusCode());
  425. $this->assertEquals('Hello World', $this->response->getContent());
  426. $this->assertTraceContains('miss');
  427. $this->assertTraceContains('store');
  428. }
  429. public function testHitsCachedResponsesWithExpiresHeader()
  430. {
  431. $time1 = \DateTime::createFromFormat('U', time() - 5);
  432. $time2 = \DateTime::createFromFormat('U', time() + 5);
  433. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Date' => $time1->format(DATE_RFC2822), 'Expires' => $time2->format(DATE_RFC2822)));
  434. $this->request('GET', '/');
  435. $this->assertHttpKernelIsCalled();
  436. $this->assertEquals(200, $this->response->getStatusCode());
  437. $this->assertNotNull($this->response->headers->get('Date'));
  438. $this->assertTraceContains('miss');
  439. $this->assertTraceContains('store');
  440. $this->assertEquals('Hello World', $this->response->getContent());
  441. $this->request('GET', '/');
  442. $this->assertHttpKernelIsNotCalled();
  443. $this->assertEquals(200, $this->response->getStatusCode());
  444. $this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
  445. $this->assertTrue($this->response->headers->get('Age') > 0);
  446. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  447. $this->assertTraceContains('fresh');
  448. $this->assertTraceNotContains('store');
  449. $this->assertEquals('Hello World', $this->response->getContent());
  450. }
  451. public function testHitsCachedResponseWithMaxAgeDirective()
  452. {
  453. $time = \DateTime::createFromFormat('U', time() - 5);
  454. $this->setNextResponse(200, array('Date' => $time->format(DATE_RFC2822), 'Cache-Control' => 'public, max-age=10'));
  455. $this->request('GET', '/');
  456. $this->assertHttpKernelIsCalled();
  457. $this->assertEquals(200, $this->response->getStatusCode());
  458. $this->assertNotNull($this->response->headers->get('Date'));
  459. $this->assertTraceContains('miss');
  460. $this->assertTraceContains('store');
  461. $this->assertEquals('Hello World', $this->response->getContent());
  462. $this->request('GET', '/');
  463. $this->assertHttpKernelIsNotCalled();
  464. $this->assertEquals(200, $this->response->getStatusCode());
  465. $this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
  466. $this->assertTrue($this->response->headers->get('Age') > 0);
  467. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  468. $this->assertTraceContains('fresh');
  469. $this->assertTraceNotContains('store');
  470. $this->assertEquals('Hello World', $this->response->getContent());
  471. }
  472. public function testHitsCachedResponseWithSMaxAgeDirective()
  473. {
  474. $time = \DateTime::createFromFormat('U', time() - 5);
  475. $this->setNextResponse(200, array('Date' => $time->format(DATE_RFC2822), 'Cache-Control' => 's-maxage=10, max-age=0'));
  476. $this->request('GET', '/');
  477. $this->assertHttpKernelIsCalled();
  478. $this->assertEquals(200, $this->response->getStatusCode());
  479. $this->assertNotNull($this->response->headers->get('Date'));
  480. $this->assertTraceContains('miss');
  481. $this->assertTraceContains('store');
  482. $this->assertEquals('Hello World', $this->response->getContent());
  483. $this->request('GET', '/');
  484. $this->assertHttpKernelIsNotCalled();
  485. $this->assertEquals(200, $this->response->getStatusCode());
  486. $this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
  487. $this->assertTrue($this->response->headers->get('Age') > 0);
  488. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  489. $this->assertTraceContains('fresh');
  490. $this->assertTraceNotContains('store');
  491. $this->assertEquals('Hello World', $this->response->getContent());
  492. }
  493. public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformation()
  494. {
  495. $this->setNextResponse();
  496. $this->cacheConfig['default_ttl'] = 10;
  497. $this->request('GET', '/');
  498. $this->assertHttpKernelIsCalled();
  499. $this->assertTraceContains('miss');
  500. $this->assertTraceContains('store');
  501. $this->assertEquals('Hello World', $this->response->getContent());
  502. $this->assertRegExp('/s-maxage=10/', $this->response->headers->get('Cache-Control'));
  503. $this->cacheConfig['default_ttl'] = 10;
  504. $this->request('GET', '/');
  505. $this->assertHttpKernelIsNotCalled();
  506. $this->assertEquals(200, $this->response->getStatusCode());
  507. $this->assertTraceContains('fresh');
  508. $this->assertTraceNotContains('store');
  509. $this->assertEquals('Hello World', $this->response->getContent());
  510. $this->assertRegExp('/s-maxage=10/', $this->response->headers->get('Cache-Control'));
  511. }
  512. public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAfterTtlWasExpired()
  513. {
  514. $this->setNextResponse();
  515. $this->cacheConfig['default_ttl'] = 2;
  516. $this->request('GET', '/');
  517. $this->assertHttpKernelIsCalled();
  518. $this->assertTraceContains('miss');
  519. $this->assertTraceContains('store');
  520. $this->assertEquals('Hello World', $this->response->getContent());
  521. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  522. $this->request('GET', '/');
  523. $this->assertHttpKernelIsNotCalled();
  524. $this->assertEquals(200, $this->response->getStatusCode());
  525. $this->assertTraceContains('fresh');
  526. $this->assertTraceNotContains('store');
  527. $this->assertEquals('Hello World', $this->response->getContent());
  528. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  529. // expires the cache
  530. $values = $this->getMetaStorageValues();
  531. $this->assertCount(1, $values);
  532. $tmp = unserialize($values[0]);
  533. $time = \DateTime::createFromFormat('U', time() - 5);
  534. $tmp[0][1]['date'] = $time->format(DATE_RFC2822);
  535. $r = new \ReflectionObject($this->store);
  536. $m = $r->getMethod('save');
  537. $m->setAccessible(true);
  538. $m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));
  539. $this->request('GET', '/');
  540. $this->assertHttpKernelIsCalled();
  541. $this->assertEquals(200, $this->response->getStatusCode());
  542. $this->assertTraceContains('stale');
  543. $this->assertTraceContains('invalid');
  544. $this->assertTraceContains('store');
  545. $this->assertEquals('Hello World', $this->response->getContent());
  546. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  547. $this->setNextResponse();
  548. $this->request('GET', '/');
  549. $this->assertHttpKernelIsNotCalled();
  550. $this->assertEquals(200, $this->response->getStatusCode());
  551. $this->assertTraceContains('fresh');
  552. $this->assertTraceNotContains('store');
  553. $this->assertEquals('Hello World', $this->response->getContent());
  554. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  555. }
  556. public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAfterTtlWasExpiredWithStatus304()
  557. {
  558. $this->setNextResponse();
  559. $this->cacheConfig['default_ttl'] = 2;
  560. $this->request('GET', '/');
  561. $this->assertHttpKernelIsCalled();
  562. $this->assertTraceContains('miss');
  563. $this->assertTraceContains('store');
  564. $this->assertEquals('Hello World', $this->response->getContent());
  565. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  566. $this->request('GET', '/');
  567. $this->assertHttpKernelIsNotCalled();
  568. $this->assertEquals(200, $this->response->getStatusCode());
  569. $this->assertTraceContains('fresh');
  570. $this->assertTraceNotContains('store');
  571. $this->assertEquals('Hello World', $this->response->getContent());
  572. // expires the cache
  573. $values = $this->getMetaStorageValues();
  574. $this->assertCount(1, $values);
  575. $tmp = unserialize($values[0]);
  576. $time = \DateTime::createFromFormat('U', time() - 5);
  577. $tmp[0][1]['date'] = $time->format(DATE_RFC2822);
  578. $r = new \ReflectionObject($this->store);
  579. $m = $r->getMethod('save');
  580. $m->setAccessible(true);
  581. $m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));
  582. $this->request('GET', '/');
  583. $this->assertHttpKernelIsCalled();
  584. $this->assertEquals(200, $this->response->getStatusCode());
  585. $this->assertTraceContains('stale');
  586. $this->assertTraceContains('valid');
  587. $this->assertTraceContains('store');
  588. $this->assertTraceNotContains('miss');
  589. $this->assertEquals('Hello World', $this->response->getContent());
  590. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  591. $this->request('GET', '/');
  592. $this->assertHttpKernelIsNotCalled();
  593. $this->assertEquals(200, $this->response->getStatusCode());
  594. $this->assertTraceContains('fresh');
  595. $this->assertTraceNotContains('store');
  596. $this->assertEquals('Hello World', $this->response->getContent());
  597. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  598. }
  599. public function testDoesNotAssignDefaultTtlWhenResponseHasMustRevalidateDirective()
  600. {
  601. $this->setNextResponse(200, array('Cache-Control' => 'must-revalidate'));
  602. $this->cacheConfig['default_ttl'] = 10;
  603. $this->request('GET', '/');
  604. $this->assertHttpKernelIsCalled();
  605. $this->assertEquals(200, $this->response->getStatusCode());
  606. $this->assertTraceContains('miss');
  607. $this->assertTraceNotContains('store');
  608. $this->assertNotRegExp('/s-maxage/', $this->response->headers->get('Cache-Control'));
  609. $this->assertEquals('Hello World', $this->response->getContent());
  610. }
  611. public function testFetchesFullResponseWhenCacheStaleAndNoValidatorsPresent()
  612. {
  613. $time = \DateTime::createFromFormat('U', time() + 5);
  614. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  615. // build initial request
  616. $this->request('GET', '/');
  617. $this->assertHttpKernelIsCalled();
  618. $this->assertEquals(200, $this->response->getStatusCode());
  619. $this->assertNotNull($this->response->headers->get('Date'));
  620. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  621. $this->assertNotNull($this->response->headers->get('Age'));
  622. $this->assertTraceContains('miss');
  623. $this->assertTraceContains('store');
  624. $this->assertEquals('Hello World', $this->response->getContent());
  625. // go in and play around with the cached metadata directly ...
  626. $values = $this->getMetaStorageValues();
  627. $this->assertCount(1, $values);
  628. $tmp = unserialize($values[0]);
  629. $time = \DateTime::createFromFormat('U', time());
  630. $tmp[0][1]['expires'] = $time->format(DATE_RFC2822);
  631. $r = new \ReflectionObject($this->store);
  632. $m = $r->getMethod('save');
  633. $m->setAccessible(true);
  634. $m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));
  635. // build subsequent request; should be found but miss due to freshness
  636. $this->request('GET', '/');
  637. $this->assertHttpKernelIsCalled();
  638. $this->assertEquals(200, $this->response->getStatusCode());
  639. $this->assertTrue($this->response->headers->get('Age') <= 1);
  640. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  641. $this->assertTraceContains('stale');
  642. $this->assertTraceNotContains('fresh');
  643. $this->assertTraceNotContains('miss');
  644. $this->assertTraceContains('store');
  645. $this->assertEquals('Hello World', $this->response->getContent());
  646. }
  647. public function testValidatesCachedResponsesWithLastModifiedAndNoFreshnessInformation()
  648. {
  649. $time = \DateTime::createFromFormat('U', time());
  650. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time) {
  651. $response->headers->set('Cache-Control', 'public');
  652. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  653. if ($time->format(DATE_RFC2822) == $request->headers->get('IF_MODIFIED_SINCE')) {
  654. $response->setStatusCode(304);
  655. $response->setContent('');
  656. }
  657. });
  658. // build initial request
  659. $this->request('GET', '/');
  660. $this->assertHttpKernelIsCalled();
  661. $this->assertEquals(200, $this->response->getStatusCode());
  662. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  663. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  664. $this->assertEquals('Hello World', $this->response->getContent());
  665. $this->assertTraceContains('miss');
  666. $this->assertTraceContains('store');
  667. $this->assertTraceNotContains('stale');
  668. // build subsequent request; should be found but miss due to freshness
  669. $this->request('GET', '/');
  670. $this->assertHttpKernelIsCalled();
  671. $this->assertEquals(200, $this->response->getStatusCode());
  672. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  673. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  674. $this->assertTrue($this->response->headers->get('Age') <= 1);
  675. $this->assertEquals('Hello World', $this->response->getContent());
  676. $this->assertTraceContains('stale');
  677. $this->assertTraceContains('valid');
  678. $this->assertTraceContains('store');
  679. $this->assertTraceNotContains('miss');
  680. }
  681. public function testValidatesCachedResponsesUseSameHttpMethod()
  682. {
  683. $test = $this;
  684. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($test) {
  685. $test->assertSame('OPTIONS', $request->getMethod());
  686. });
  687. // build initial request
  688. $this->request('OPTIONS', '/');
  689. // build subsequent request
  690. $this->request('OPTIONS', '/');
  691. }
  692. public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
  693. {
  694. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
  695. $response->headers->set('Cache-Control', 'public');
  696. $response->headers->set('ETag', '"12345"');
  697. if ($response->getETag() == $request->headers->get('IF_NONE_MATCH')) {
  698. $response->setStatusCode(304);
  699. $response->setContent('');
  700. }
  701. });
  702. // build initial request
  703. $this->request('GET', '/');
  704. $this->assertHttpKernelIsCalled();
  705. $this->assertEquals(200, $this->response->getStatusCode());
  706. $this->assertNotNull($this->response->headers->get('ETag'));
  707. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  708. $this->assertEquals('Hello World', $this->response->getContent());
  709. $this->assertTraceContains('miss');
  710. $this->assertTraceContains('store');
  711. // build subsequent request; should be found but miss due to freshness
  712. $this->request('GET', '/');
  713. $this->assertHttpKernelIsCalled();
  714. $this->assertEquals(200, $this->response->getStatusCode());
  715. $this->assertNotNull($this->response->headers->get('ETag'));
  716. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  717. $this->assertTrue($this->response->headers->get('Age') <= 1);
  718. $this->assertEquals('Hello World', $this->response->getContent());
  719. $this->assertTraceContains('stale');
  720. $this->assertTraceContains('valid');
  721. $this->assertTraceContains('store');
  722. $this->assertTraceNotContains('miss');
  723. }
  724. public function testReplacesCachedResponsesWhenValidationResultsInNon304Response()
  725. {
  726. $time = \DateTime::createFromFormat('U', time());
  727. $count = 0;
  728. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time, &$count) {
  729. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  730. $response->headers->set('Cache-Control', 'public');
  731. switch (++$count) {
  732. case 1:
  733. $response->setContent('first response');
  734. break;
  735. case 2:
  736. $response->setContent('second response');
  737. break;
  738. case 3:
  739. $response->setContent('');
  740. $response->setStatusCode(304);
  741. break;
  742. }
  743. });
  744. // first request should fetch from backend and store in cache
  745. $this->request('GET', '/');
  746. $this->assertEquals(200, $this->response->getStatusCode());
  747. $this->assertEquals('first response', $this->response->getContent());
  748. // second request is validated, is invalid, and replaces cached entry
  749. $this->request('GET', '/');
  750. $this->assertEquals(200, $this->response->getStatusCode());
  751. $this->assertEquals('second response', $this->response->getContent());
  752. // third response is validated, valid, and returns cached entry
  753. $this->request('GET', '/');
  754. $this->assertEquals(200, $this->response->getStatusCode());
  755. $this->assertEquals('second response', $this->response->getContent());
  756. $this->assertEquals(3, $count);
  757. }
  758. public function testPassesHeadRequestsThroughDirectlyOnPass()
  759. {
  760. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
  761. $response->setContent('');
  762. $response->setStatusCode(200);
  763. $this->assertEquals('HEAD', $request->getMethod());
  764. });
  765. $this->request('HEAD', '/', array('HTTP_EXPECT' => 'something ...'));
  766. $this->assertHttpKernelIsCalled();
  767. $this->assertEquals('', $this->response->getContent());
  768. }
  769. public function testUsesCacheToRespondToHeadRequestsWhenFresh()
  770. {
  771. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
  772. $response->headers->set('Cache-Control', 'public, max-age=10');
  773. $response->setContent('Hello World');
  774. $response->setStatusCode(200);
  775. $this->assertNotEquals('HEAD', $request->getMethod());
  776. });
  777. $this->request('GET', '/');
  778. $this->assertHttpKernelIsCalled();
  779. $this->assertEquals('Hello World', $this->response->getContent());
  780. $this->request('HEAD', '/');
  781. $this->assertHttpKernelIsNotCalled();
  782. $this->assertEquals(200, $this->response->getStatusCode());
  783. $this->assertEquals('', $this->response->getContent());
  784. $this->assertEquals(strlen('Hello World'), $this->response->headers->get('Content-Length'));
  785. }
  786. public function testSendsNoContentWhenFresh()
  787. {
  788. $time = \DateTime::createFromFormat('U', time());
  789. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time) {
  790. $response->headers->set('Cache-Control', 'public, max-age=10');
  791. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  792. });
  793. $this->request('GET', '/');
  794. $this->assertHttpKernelIsCalled();
  795. $this->assertEquals('Hello World', $this->response->getContent());
  796. $this->request('GET', '/', array('HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  797. $this->assertHttpKernelIsNotCalled();
  798. $this->assertEquals(304, $this->response->getStatusCode());
  799. $this->assertEquals('', $this->response->getContent());
  800. }
  801. public function testInvalidatesCachedResponsesOnPost()
  802. {
  803. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
  804. if ('GET' == $request->getMethod()) {
  805. $response->setStatusCode(200);
  806. $response->headers->set('Cache-Control', 'public, max-age=500');
  807. $response->setContent('Hello World');
  808. } elseif ('POST' == $request->getMethod()) {
  809. $response->setStatusCode(303);
  810. $response->headers->set('Location', '/');
  811. $response->headers->remove('Cache-Control');
  812. $response->setContent('');
  813. }
  814. });
  815. // build initial request to enter into the cache
  816. $this->request('GET', '/');
  817. $this->assertHttpKernelIsCalled();
  818. $this->assertEquals(200, $this->response->getStatusCode());
  819. $this->assertEquals('Hello World', $this->response->getContent());
  820. $this->assertTraceContains('miss');
  821. $this->assertTraceContains('store');
  822. // make sure it is valid
  823. $this->request('GET', '/');
  824. $this->assertHttpKernelIsNotCalled();
  825. $this->assertEquals(200, $this->response->getStatusCode());
  826. $this->assertEquals('Hello World', $this->response->getContent());
  827. $this->assertTraceContains('fresh');
  828. // now POST to same URL
  829. $this->request('POST', '/helloworld');
  830. $this->assertHttpKernelIsCalled();
  831. $this->assertEquals('/', $this->response->headers->get('Location'));
  832. $this->assertTraceContains('invalidate');
  833. $this->assertTraceContains('pass');
  834. $this->assertEquals('', $this->response->getContent());
  835. // now make sure it was actually invalidated
  836. $this->request('GET', '/');
  837. $this->assertHttpKernelIsCalled();
  838. $this->assertEquals(200, $this->response->getStatusCode());
  839. $this->assertEquals('Hello World', $this->response->getContent());
  840. $this->assertTraceContains('stale');
  841. $this->assertTraceContains('invalid');
  842. $this->assertTraceContains('store');
  843. }
  844. public function testServesFromCacheWhenHeadersMatch()
  845. {
  846. $count = 0;
  847. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count) {
  848. $response->headers->set('Vary', 'Accept User-Agent Foo');
  849. $response->headers->set('Cache-Control', 'public, max-age=10');
  850. $response->headers->set('X-Response-Count', ++$count);
  851. $response->setContent($request->headers->get('USER_AGENT'));
  852. });
  853. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  854. $this->assertEquals(200, $this->response->getStatusCode());
  855. $this->assertEquals('Bob/1.0', $this->response->getContent());
  856. $this->assertTraceContains('miss');
  857. $this->assertTraceContains('store');
  858. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  859. $this->assertEquals(200, $this->response->getStatusCode());
  860. $this->assertEquals('Bob/1.0', $this->response->getContent());
  861. $this->assertTraceContains('fresh');
  862. $this->assertTraceNotContains('store');
  863. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  864. }
  865. public function testStoresMultipleResponsesWhenHeadersDiffer()
  866. {
  867. $count = 0;
  868. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count) {
  869. $response->headers->set('Vary', 'Accept User-Agent Foo');
  870. $response->headers->set('Cache-Control', 'public, max-age=10');
  871. $response->headers->set('X-Response-Count', ++$count);
  872. $response->setContent($request->headers->get('USER_AGENT'));
  873. });
  874. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  875. $this->assertEquals(200, $this->response->getStatusCode());
  876. $this->assertEquals('Bob/1.0', $this->response->getContent());
  877. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  878. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  879. $this->assertEquals(200, $this->response->getStatusCode());
  880. $this->assertTraceContains('miss');
  881. $this->assertTraceContains('store');
  882. $this->assertEquals('Bob/2.0', $this->response->getContent());
  883. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  884. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  885. $this->assertTraceContains('fresh');
  886. $this->assertEquals('Bob/1.0', $this->response->getContent());
  887. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  888. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  889. $this->assertTraceContains('fresh');
  890. $this->assertEquals('Bob/2.0', $this->response->getContent());
  891. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  892. $this->request('GET', '/', array('HTTP_USER_AGENT' => 'Bob/2.0'));
  893. $this->assertTraceContains('miss');
  894. $this->assertEquals('Bob/2.0', $this->response->getContent());
  895. $this->assertEquals(3, $this->response->headers->get('X-Response-Count'));
  896. }
  897. public function testShouldCatchExceptions()
  898. {
  899. $this->catchExceptions();
  900. $this->setNextResponse();
  901. $this->request('GET', '/');
  902. $this->assertExceptionsAreCaught();
  903. }
  904. public function testShouldCatchExceptionsWhenReloadingAndNoCacheRequest()
  905. {
  906. $this->catchExceptions();
  907. $this->setNextResponse();
  908. $this->cacheConfig['allow_reload'] = true;
  909. $this->request('GET', '/', array(), array(), false, array('Pragma' => 'no-cache'));
  910. $this->assertExceptionsAreCaught();
  911. }
  912. public function testShouldNotCatchExceptions()
  913. {
  914. $this->catchExceptions(false);
  915. $this->setNextResponse();
  916. $this->request('GET', '/');
  917. $this->assertExceptionsAreNotCaught();
  918. }
  919. public function testEsiCacheSendsTheLowestTtl()
  920. {
  921. $responses = array(
  922. array(
  923. 'status' => 200,
  924. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  925. 'headers' => array(
  926. 'Cache-Control' => 's-maxage=300',
  927. 'Surrogate-Control' => 'content="ESI/1.0"',
  928. ),
  929. ),
  930. array(
  931. 'status' => 200,
  932. 'body' => 'Hello World!',
  933. 'headers' => array('Cache-Control' => 's-maxage=300'),
  934. ),
  935. array(
  936. 'status' => 200,
  937. 'body' => 'My name is Bobby.',
  938. 'headers' => array('Cache-Control' => 's-maxage=100'),
  939. ),
  940. );
  941. $this->setNextResponses($responses);
  942. $this->request('GET', '/', array(), array(), true);
  943. $this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
  944. // check for 100 or 99 as the test can be executed after a second change
  945. $this->assertTrue(in_array($this->response->getTtl(), array(99, 100)));
  946. }
  947. public function testEsiCacheForceValidation()
  948. {
  949. $responses = array(
  950. array(
  951. 'status' => 200,
  952. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  953. 'headers' => array(
  954. 'Cache-Control' => 's-maxage=300',
  955. 'Surrogate-Control' => 'content="ESI/1.0"',
  956. ),
  957. ),
  958. array(
  959. 'status' => 200,
  960. 'body' => 'Hello World!',
  961. 'headers' => array('ETag' => 'foobar'),
  962. ),
  963. array(
  964. 'status' => 200,
  965. 'body' => 'My name is Bobby.',
  966. 'headers' => array('Cache-Control' => 's-maxage=100'),
  967. ),
  968. );
  969. $this->setNextResponses($responses);
  970. $this->request('GET', '/', array(), array(), true);
  971. $this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
  972. $this->assertNull($this->response->getTtl());
  973. $this->assertTrue($this->response->mustRevalidate());
  974. $this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
  975. $this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
  976. }
  977. public function testEsiRecalculateContentLengthHeader()
  978. {
  979. $responses = array(
  980. array(
  981. 'status' => 200,
  982. 'body' => '<esi:include src="/foo" />',
  983. 'headers' => array(
  984. 'Content-Length' => 26,
  985. 'Cache-Control' => 's-maxage=300',
  986. 'Surrogate-Control' => 'content="ESI/1.0"',
  987. ),
  988. ),
  989. array(
  990. 'status' => 200,
  991. 'body' => 'Hello World!',
  992. 'headers' => array(),
  993. ),
  994. );
  995. $this->setNextResponses($responses);
  996. $this->request('GET', '/', array(), array(), true);
  997. $this->assertEquals('Hello World!', $this->response->getContent());
  998. $this->assertEquals(12, $this->response->headers->get('Content-Length'));
  999. }
  1000. public function testClientIpIsAlwaysLocalhostForForwardedRequests()
  1001. {
  1002. $this->setNextResponse();
  1003. $this->request('GET', '/', array('REMOTE_ADDR' => '10.0.0.1'));
  1004. $this->assertEquals('127.0.0.1', $this->kernel->getBackendRequest()->server->get('REMOTE_ADDR'));
  1005. }
  1006. /**
  1007. * @dataProvider getTrustedProxyData
  1008. */
  1009. public function testHttpCacheIsSetAsATrustedProxy(array $existing, array $expected)
  1010. {
  1011. Request::setTrustedProxies($existing);
  1012. $this->setNextResponse();
  1013. $this->request('GET', '/', array('REMOTE_ADDR' => '10.0.0.1'));
  1014. $this->assertEquals($expected, Request::getTrustedProxies());
  1015. }
  1016. public function getTrustedProxyData()
  1017. {
  1018. return array(
  1019. array(array(), array('127.0.0.1')),
  1020. array(array('10.0.0.2'), array('10.0.0.2', '127.0.0.1')),
  1021. array(array('10.0.0.2', '127.0.0.1'), array('10.0.0.2', '127.0.0.1')),
  1022. );
  1023. }
  1024. /**
  1025. * @dataProvider getXForwardedForData
  1026. */
  1027. public function testXForwarderForHeaderForForwardedRequests($xForwardedFor, $expected)
  1028. {
  1029. $this->setNextResponse();
  1030. $server = array('REMOTE_ADDR' => '10.0.0.1');
  1031. if (false !== $xForwardedFor) {
  1032. $server['HTTP_X_FORWARDED_FOR'] = $xForwardedFor;
  1033. }
  1034. $this->request('GET', '/', $server);
  1035. $this->assertEquals($expected, $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For'));
  1036. }
  1037. public function getXForwardedForData()
  1038. {
  1039. return array(
  1040. array(false, '10.0.0.1'),
  1041. array('10.0.0.2', '10.0.0.2, 10.0.0.1'),
  1042. array('10.0.0.2, 10.0.0.3', '10.0.0.2, 10.0.0.3, 10.0.0.1'),
  1043. );
  1044. }
  1045. public function testXForwarderForHeaderForPassRequests()
  1046. {
  1047. $this->setNextResponse();
  1048. $server = array('REMOTE_ADDR' => '10.0.0.1');
  1049. $this->request('POST', '/', $server);
  1050. $this->assertEquals('10.0.0.1', $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For'));
  1051. }
  1052. public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponses()
  1053. {
  1054. $time = \DateTime::createFromFormat('U', time());
  1055. $responses = array(
  1056. array(
  1057. 'status' => 200,
  1058. 'body' => '<esi:include src="/hey" />',
  1059. 'headers' => array(
  1060. 'Surrogate-Control' => 'content="ESI/1.0"',
  1061. 'ETag' => 'hey',
  1062. 'Last-Modified' => $time->format(DATE_RFC2822),
  1063. ),
  1064. ),
  1065. array(
  1066. 'status' => 200,
  1067. 'body' => 'Hey!',
  1068. 'headers' => array(),
  1069. ),
  1070. );
  1071. $this->setNextResponses($responses);
  1072. $this->request('GET', '/', array(), array(), true);
  1073. $this->assertNull($this->response->getETag());
  1074. $this->assertNull($this->response->getLastModified());
  1075. }
  1076. public function testDoesNotCacheOptionsRequest()
  1077. {
  1078. $this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=60'), 'get');
  1079. $this->request('GET', '/');
  1080. $this->assertHttpKernelIsCalled();
  1081. $this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=60'), 'options');
  1082. $this->request('OPTIONS', '/');
  1083. $this->assertHttpKernelIsCalled();
  1084. $this->request('GET', '/');
  1085. $this->assertHttpKernelIsNotCalled();
  1086. $this->assertSame('get', $this->response->getContent());
  1087. }
  1088. }
  1089. class TestKernel implements HttpKernelInterface
  1090. {
  1091. public $terminateCalled = false;
  1092. public function terminate(Request $request, Response $response)
  1093. {
  1094. $this->terminateCalled = true;
  1095. }
  1096. public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
  1097. {
  1098. }
  1099. }