HttpCacheTest.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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 testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
  682. {
  683. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
  684. $response->headers->set('Cache-Control', 'public');
  685. $response->headers->set('ETag', '"12345"');
  686. if ($response->getETag() == $request->headers->get('IF_NONE_MATCH')) {
  687. $response->setStatusCode(304);
  688. $response->setContent('');
  689. }
  690. });
  691. // build initial request
  692. $this->request('GET', '/');
  693. $this->assertHttpKernelIsCalled();
  694. $this->assertEquals(200, $this->response->getStatusCode());
  695. $this->assertNotNull($this->response->headers->get('ETag'));
  696. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  697. $this->assertEquals('Hello World', $this->response->getContent());
  698. $this->assertTraceContains('miss');
  699. $this->assertTraceContains('store');
  700. // build subsequent request; should be found but miss due to freshness
  701. $this->request('GET', '/');
  702. $this->assertHttpKernelIsCalled();
  703. $this->assertEquals(200, $this->response->getStatusCode());
  704. $this->assertNotNull($this->response->headers->get('ETag'));
  705. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  706. $this->assertTrue($this->response->headers->get('Age') <= 1);
  707. $this->assertEquals('Hello World', $this->response->getContent());
  708. $this->assertTraceContains('stale');
  709. $this->assertTraceContains('valid');
  710. $this->assertTraceContains('store');
  711. $this->assertTraceNotContains('miss');
  712. }
  713. public function testReplacesCachedResponsesWhenValidationResultsInNon304Response()
  714. {
  715. $time = \DateTime::createFromFormat('U', time());
  716. $count = 0;
  717. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time, &$count) {
  718. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  719. $response->headers->set('Cache-Control', 'public');
  720. switch (++$count) {
  721. case 1:
  722. $response->setContent('first response');
  723. break;
  724. case 2:
  725. $response->setContent('second response');
  726. break;
  727. case 3:
  728. $response->setContent('');
  729. $response->setStatusCode(304);
  730. break;
  731. }
  732. });
  733. // first request should fetch from backend and store in cache
  734. $this->request('GET', '/');
  735. $this->assertEquals(200, $this->response->getStatusCode());
  736. $this->assertEquals('first response', $this->response->getContent());
  737. // second request is validated, is invalid, and replaces cached entry
  738. $this->request('GET', '/');
  739. $this->assertEquals(200, $this->response->getStatusCode());
  740. $this->assertEquals('second response', $this->response->getContent());
  741. // third response is validated, valid, and returns cached entry
  742. $this->request('GET', '/');
  743. $this->assertEquals(200, $this->response->getStatusCode());
  744. $this->assertEquals('second response', $this->response->getContent());
  745. $this->assertEquals(3, $count);
  746. }
  747. public function testPassesHeadRequestsThroughDirectlyOnPass()
  748. {
  749. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
  750. $response->setContent('');
  751. $response->setStatusCode(200);
  752. $this->assertEquals('HEAD', $request->getMethod());
  753. });
  754. $this->request('HEAD', '/', array('HTTP_EXPECT' => 'something ...'));
  755. $this->assertHttpKernelIsCalled();
  756. $this->assertEquals('', $this->response->getContent());
  757. }
  758. public function testUsesCacheToRespondToHeadRequestsWhenFresh()
  759. {
  760. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
  761. $response->headers->set('Cache-Control', 'public, max-age=10');
  762. $response->setContent('Hello World');
  763. $response->setStatusCode(200);
  764. $this->assertNotEquals('HEAD', $request->getMethod());
  765. });
  766. $this->request('GET', '/');
  767. $this->assertHttpKernelIsCalled();
  768. $this->assertEquals('Hello World', $this->response->getContent());
  769. $this->request('HEAD', '/');
  770. $this->assertHttpKernelIsNotCalled();
  771. $this->assertEquals(200, $this->response->getStatusCode());
  772. $this->assertEquals('', $this->response->getContent());
  773. $this->assertEquals(strlen('Hello World'), $this->response->headers->get('Content-Length'));
  774. }
  775. public function testSendsNoContentWhenFresh()
  776. {
  777. $time = \DateTime::createFromFormat('U', time());
  778. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time) {
  779. $response->headers->set('Cache-Control', 'public, max-age=10');
  780. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  781. });
  782. $this->request('GET', '/');
  783. $this->assertHttpKernelIsCalled();
  784. $this->assertEquals('Hello World', $this->response->getContent());
  785. $this->request('GET', '/', array('HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  786. $this->assertHttpKernelIsNotCalled();
  787. $this->assertEquals(304, $this->response->getStatusCode());
  788. $this->assertEquals('', $this->response->getContent());
  789. }
  790. public function testInvalidatesCachedResponsesOnPost()
  791. {
  792. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
  793. if ('GET' == $request->getMethod()) {
  794. $response->setStatusCode(200);
  795. $response->headers->set('Cache-Control', 'public, max-age=500');
  796. $response->setContent('Hello World');
  797. } elseif ('POST' == $request->getMethod()) {
  798. $response->setStatusCode(303);
  799. $response->headers->set('Location', '/');
  800. $response->headers->remove('Cache-Control');
  801. $response->setContent('');
  802. }
  803. });
  804. // build initial request to enter into the cache
  805. $this->request('GET', '/');
  806. $this->assertHttpKernelIsCalled();
  807. $this->assertEquals(200, $this->response->getStatusCode());
  808. $this->assertEquals('Hello World', $this->response->getContent());
  809. $this->assertTraceContains('miss');
  810. $this->assertTraceContains('store');
  811. // make sure it is valid
  812. $this->request('GET', '/');
  813. $this->assertHttpKernelIsNotCalled();
  814. $this->assertEquals(200, $this->response->getStatusCode());
  815. $this->assertEquals('Hello World', $this->response->getContent());
  816. $this->assertTraceContains('fresh');
  817. // now POST to same URL
  818. $this->request('POST', '/helloworld');
  819. $this->assertHttpKernelIsCalled();
  820. $this->assertEquals('/', $this->response->headers->get('Location'));
  821. $this->assertTraceContains('invalidate');
  822. $this->assertTraceContains('pass');
  823. $this->assertEquals('', $this->response->getContent());
  824. // now make sure it was actually invalidated
  825. $this->request('GET', '/');
  826. $this->assertHttpKernelIsCalled();
  827. $this->assertEquals(200, $this->response->getStatusCode());
  828. $this->assertEquals('Hello World', $this->response->getContent());
  829. $this->assertTraceContains('stale');
  830. $this->assertTraceContains('invalid');
  831. $this->assertTraceContains('store');
  832. }
  833. public function testServesFromCacheWhenHeadersMatch()
  834. {
  835. $count = 0;
  836. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count) {
  837. $response->headers->set('Vary', 'Accept User-Agent Foo');
  838. $response->headers->set('Cache-Control', 'public, max-age=10');
  839. $response->headers->set('X-Response-Count', ++$count);
  840. $response->setContent($request->headers->get('USER_AGENT'));
  841. });
  842. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  843. $this->assertEquals(200, $this->response->getStatusCode());
  844. $this->assertEquals('Bob/1.0', $this->response->getContent());
  845. $this->assertTraceContains('miss');
  846. $this->assertTraceContains('store');
  847. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  848. $this->assertEquals(200, $this->response->getStatusCode());
  849. $this->assertEquals('Bob/1.0', $this->response->getContent());
  850. $this->assertTraceContains('fresh');
  851. $this->assertTraceNotContains('store');
  852. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  853. }
  854. public function testStoresMultipleResponsesWhenHeadersDiffer()
  855. {
  856. $count = 0;
  857. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count) {
  858. $response->headers->set('Vary', 'Accept User-Agent Foo');
  859. $response->headers->set('Cache-Control', 'public, max-age=10');
  860. $response->headers->set('X-Response-Count', ++$count);
  861. $response->setContent($request->headers->get('USER_AGENT'));
  862. });
  863. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  864. $this->assertEquals(200, $this->response->getStatusCode());
  865. $this->assertEquals('Bob/1.0', $this->response->getContent());
  866. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  867. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  868. $this->assertEquals(200, $this->response->getStatusCode());
  869. $this->assertTraceContains('miss');
  870. $this->assertTraceContains('store');
  871. $this->assertEquals('Bob/2.0', $this->response->getContent());
  872. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  873. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  874. $this->assertTraceContains('fresh');
  875. $this->assertEquals('Bob/1.0', $this->response->getContent());
  876. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  877. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  878. $this->assertTraceContains('fresh');
  879. $this->assertEquals('Bob/2.0', $this->response->getContent());
  880. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  881. $this->request('GET', '/', array('HTTP_USER_AGENT' => 'Bob/2.0'));
  882. $this->assertTraceContains('miss');
  883. $this->assertEquals('Bob/2.0', $this->response->getContent());
  884. $this->assertEquals(3, $this->response->headers->get('X-Response-Count'));
  885. }
  886. public function testShouldCatchExceptions()
  887. {
  888. $this->catchExceptions();
  889. $this->setNextResponse();
  890. $this->request('GET', '/');
  891. $this->assertExceptionsAreCaught();
  892. }
  893. public function testShouldCatchExceptionsWhenReloadingAndNoCacheRequest()
  894. {
  895. $this->catchExceptions();
  896. $this->setNextResponse();
  897. $this->cacheConfig['allow_reload'] = true;
  898. $this->request('GET', '/', array(), array(), false, array('Pragma' => 'no-cache'));
  899. $this->assertExceptionsAreCaught();
  900. }
  901. public function testShouldNotCatchExceptions()
  902. {
  903. $this->catchExceptions(false);
  904. $this->setNextResponse();
  905. $this->request('GET', '/');
  906. $this->assertExceptionsAreNotCaught();
  907. }
  908. public function testEsiCacheSendsTheLowestTtl()
  909. {
  910. $responses = array(
  911. array(
  912. 'status' => 200,
  913. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  914. 'headers' => array(
  915. 'Cache-Control' => 's-maxage=300',
  916. 'Surrogate-Control' => 'content="ESI/1.0"',
  917. ),
  918. ),
  919. array(
  920. 'status' => 200,
  921. 'body' => 'Hello World!',
  922. 'headers' => array('Cache-Control' => 's-maxage=300'),
  923. ),
  924. array(
  925. 'status' => 200,
  926. 'body' => 'My name is Bobby.',
  927. 'headers' => array('Cache-Control' => 's-maxage=100'),
  928. ),
  929. );
  930. $this->setNextResponses($responses);
  931. $this->request('GET', '/', array(), array(), true);
  932. $this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
  933. // check for 100 or 99 as the test can be executed after a second change
  934. $this->assertTrue(in_array($this->response->getTtl(), array(99, 100)));
  935. }
  936. public function testEsiCacheForceValidation()
  937. {
  938. $responses = array(
  939. array(
  940. 'status' => 200,
  941. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  942. 'headers' => array(
  943. 'Cache-Control' => 's-maxage=300',
  944. 'Surrogate-Control' => 'content="ESI/1.0"',
  945. ),
  946. ),
  947. array(
  948. 'status' => 200,
  949. 'body' => 'Hello World!',
  950. 'headers' => array('ETag' => 'foobar'),
  951. ),
  952. array(
  953. 'status' => 200,
  954. 'body' => 'My name is Bobby.',
  955. 'headers' => array('Cache-Control' => 's-maxage=100'),
  956. ),
  957. );
  958. $this->setNextResponses($responses);
  959. $this->request('GET', '/', array(), array(), true);
  960. $this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
  961. $this->assertNull($this->response->getTtl());
  962. $this->assertTrue($this->response->mustRevalidate());
  963. $this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
  964. $this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
  965. }
  966. public function testEsiRecalculateContentLengthHeader()
  967. {
  968. $responses = array(
  969. array(
  970. 'status' => 200,
  971. 'body' => '<esi:include src="/foo" />',
  972. 'headers' => array(
  973. 'Content-Length' => 26,
  974. 'Cache-Control' => 's-maxage=300',
  975. 'Surrogate-Control' => 'content="ESI/1.0"',
  976. ),
  977. ),
  978. array(
  979. 'status' => 200,
  980. 'body' => 'Hello World!',
  981. 'headers' => array(),
  982. ),
  983. );
  984. $this->setNextResponses($responses);
  985. $this->request('GET', '/', array(), array(), true);
  986. $this->assertEquals('Hello World!', $this->response->getContent());
  987. $this->assertEquals(12, $this->response->headers->get('Content-Length'));
  988. }
  989. public function testClientIpIsAlwaysLocalhostForForwardedRequests()
  990. {
  991. $this->setNextResponse();
  992. $this->request('GET', '/', array('REMOTE_ADDR' => '10.0.0.1'));
  993. $this->assertEquals('127.0.0.1', $this->kernel->getBackendRequest()->server->get('REMOTE_ADDR'));
  994. }
  995. /**
  996. * @dataProvider getTrustedProxyData
  997. */
  998. public function testHttpCacheIsSetAsATrustedProxy(array $existing, array $expected)
  999. {
  1000. Request::setTrustedProxies($existing);
  1001. $this->setNextResponse();
  1002. $this->request('GET', '/', array('REMOTE_ADDR' => '10.0.0.1'));
  1003. $this->assertEquals($expected, Request::getTrustedProxies());
  1004. }
  1005. public function getTrustedProxyData()
  1006. {
  1007. return array(
  1008. array(array(), array('127.0.0.1')),
  1009. array(array('10.0.0.2'), array('10.0.0.2', '127.0.0.1')),
  1010. array(array('10.0.0.2', '127.0.0.1'), array('10.0.0.2', '127.0.0.1')),
  1011. );
  1012. }
  1013. /**
  1014. * @dataProvider getXForwardedForData
  1015. */
  1016. public function testXForwarderForHeaderForForwardedRequests($xForwardedFor, $expected)
  1017. {
  1018. $this->setNextResponse();
  1019. $server = array('REMOTE_ADDR' => '10.0.0.1');
  1020. if (false !== $xForwardedFor) {
  1021. $server['HTTP_X_FORWARDED_FOR'] = $xForwardedFor;
  1022. }
  1023. $this->request('GET', '/', $server);
  1024. $this->assertEquals($expected, $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For'));
  1025. }
  1026. public function getXForwardedForData()
  1027. {
  1028. return array(
  1029. array(false, '10.0.0.1'),
  1030. array('10.0.0.2', '10.0.0.2, 10.0.0.1'),
  1031. array('10.0.0.2, 10.0.0.3', '10.0.0.2, 10.0.0.3, 10.0.0.1'),
  1032. );
  1033. }
  1034. public function testXForwarderForHeaderForPassRequests()
  1035. {
  1036. $this->setNextResponse();
  1037. $server = array('REMOTE_ADDR' => '10.0.0.1');
  1038. $this->request('POST', '/', $server);
  1039. $this->assertEquals('10.0.0.1', $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For'));
  1040. }
  1041. public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponses()
  1042. {
  1043. $time = \DateTime::createFromFormat('U', time());
  1044. $responses = array(
  1045. array(
  1046. 'status' => 200,
  1047. 'body' => '<esi:include src="/hey" />',
  1048. 'headers' => array(
  1049. 'Surrogate-Control' => 'content="ESI/1.0"',
  1050. 'ETag' => 'hey',
  1051. 'Last-Modified' => $time->format(DATE_RFC2822),
  1052. ),
  1053. ),
  1054. array(
  1055. 'status' => 200,
  1056. 'body' => 'Hey!',
  1057. 'headers' => array(),
  1058. ),
  1059. );
  1060. $this->setNextResponses($responses);
  1061. $this->request('GET', '/', array(), array(), true);
  1062. $this->assertNull($this->response->getETag());
  1063. $this->assertNull($this->response->getLastModified());
  1064. }
  1065. }
  1066. class TestKernel implements HttpKernelInterface
  1067. {
  1068. public $terminateCalled = false;
  1069. public function terminate(Request $request, Response $response)
  1070. {
  1071. $this->terminateCalled = true;
  1072. }
  1073. public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
  1074. {
  1075. }
  1076. }