CacheTest.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Component\HttpKernel\Cache;
  11. require_once __DIR__.'/CacheTestCase.php';
  12. class CacheTest extends CacheTestCase
  13. {
  14. public function testPassesOnNonGetHeadRequests()
  15. {
  16. $this->setNextResponse(200);
  17. $this->request('POST', '/');
  18. $this->assertHttpKernelIsCalled();
  19. $this->assertResponseOk();
  20. $this->assertTraceContains('pass');
  21. $this->assertFalse($this->response->headers->has('Age'));
  22. }
  23. public function testInvalidatesOnPostPutDeleteRequests()
  24. {
  25. foreach (array('post', 'put', 'delete') as $method) {
  26. $this->setNextResponse(200);
  27. $this->request($method, '/');
  28. $this->assertHttpKernelIsCalled();
  29. $this->assertResponseOk();
  30. $this->assertTraceContains('invalidate');
  31. $this->assertTraceContains('pass');
  32. }
  33. }
  34. public function testDoesNotCacheWithAuthorizationRequestHeaderAndNonPublicResponse()
  35. {
  36. $this->setNextResponse(200, array('ETag' => '"Foo"'));
  37. $this->request('GET', '/', array('HTTP_AUTHORIZATION' => 'basic foobarbaz'));
  38. $this->assertHttpKernelIsCalled();
  39. $this->assertResponseOk();
  40. $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
  41. $this->assertTraceContains('miss');
  42. $this->assertTraceNotContains('store');
  43. $this->assertFalse($this->response->headers->has('Age'));
  44. }
  45. public function testDoesCacheWithAuthorizationRequestHeaderAndPublicResponse()
  46. {
  47. $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '"Foo"'));
  48. $this->request('GET', '/', array('HTTP_AUTHORIZATION' => 'basic foobarbaz'));
  49. $this->assertHttpKernelIsCalled();
  50. $this->assertResponseOk();
  51. $this->assertTraceContains('miss');
  52. $this->assertTraceContains('store');
  53. $this->assertTrue($this->response->headers->has('Age'));
  54. $this->assertEquals('public', $this->response->headers->get('Cache-Control'));
  55. }
  56. public function testDoesNotCacheWithCookieHeaderAndNonPublicResponse()
  57. {
  58. $this->setNextResponse(200, array('ETag' => '"Foo"'));
  59. $this->request('GET', '/', array(), array('foo' => 'bar'));
  60. $this->assertHttpKernelIsCalled();
  61. $this->assertResponseOk();
  62. $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
  63. $this->assertTraceContains('miss');
  64. $this->assertTraceNotContains('store');
  65. $this->assertFalse($this->response->headers->has('Age'));
  66. }
  67. public function testDoesNotCacheRequestsWithACookieHeader()
  68. {
  69. $this->setNextResponse(200);
  70. $this->request('GET', '/', array(), array('foo' => 'bar'));
  71. $this->assertHttpKernelIsCalled();
  72. $this->assertResponseOk();
  73. $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
  74. $this->assertTraceContains('miss');
  75. $this->assertTraceNotContains('store');
  76. $this->assertFalse($this->response->headers->has('Age'));
  77. }
  78. public function testRespondsWith304WhenIfModifiedSinceMatchesLastModified()
  79. {
  80. $time = new \DateTime();
  81. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Last-Modified' => $time->format(DATE_RFC2822), 'Content-Type' => 'text/plain'), 'Hello World');
  82. $this->request('GET', '/', array('HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  83. $this->assertHttpKernelIsCalled();
  84. $this->assertEquals(304, $this->response->getStatusCode());
  85. $this->assertFalse($this->response->headers->has('Content-Length'));
  86. $this->assertFalse($this->response->headers->has('Content-Type'));
  87. $this->assertEmpty($this->response->getContent());
  88. $this->assertTraceContains('miss');
  89. $this->assertTraceContains('store');
  90. }
  91. public function testRespondsWith304WhenIfNoneMatchMatchesETag()
  92. {
  93. $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '12345', 'Content-Type' => 'text/plain'), 'Hello World');
  94. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345'));
  95. $this->assertHttpKernelIsCalled();
  96. $this->assertEquals(304, $this->response->getStatusCode());
  97. $this->assertFalse($this->response->headers->has('Content-Length'));
  98. $this->assertFalse($this->response->headers->has('Content-Type'));
  99. $this->assertTrue($this->response->headers->has('ETag'));
  100. $this->assertEmpty($this->response->getContent());
  101. $this->assertTraceContains('miss');
  102. $this->assertTraceContains('store');
  103. }
  104. public function testRespondsWith304OnlyIfIfNoneMatchAndIfModifiedSinceBothMatch()
  105. {
  106. $time = new \DateTime();
  107. $this->setNextResponse(200, array(), '', function ($request, $response) use ($time)
  108. {
  109. $response->setStatusCode(200);
  110. $response->headers->set('ETag', '12345');
  111. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  112. $response->headers->set('Content-Type', 'text/plain');
  113. $response->setContent('Hello World');
  114. });
  115. // only ETag matches
  116. $t = \DateTime::createFromFormat('U', time() - 3600);
  117. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => $t->format(DATE_RFC2822)));
  118. $this->assertHttpKernelIsCalled();
  119. $this->assertEquals(200, $this->response->getStatusCode());
  120. // only Last-Modified matches
  121. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '1234', 'HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  122. $this->assertHttpKernelIsCalled();
  123. $this->assertEquals(200, $this->response->getStatusCode());
  124. // Both matches
  125. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  126. $this->assertHttpKernelIsCalled();
  127. $this->assertEquals(304, $this->response->getStatusCode());
  128. }
  129. public function testValidatesPrivateResponsesCachedOnTheClient()
  130. {
  131. $this->setNextResponse(200, array(), '', function ($request, $response)
  132. {
  133. $etags = preg_split('/\s*,\s*/', $request->headers->get('IF_NONE_MATCH'));
  134. if ($request->cookies->has('authenticated')) {
  135. $response->headers->set('Cache-Control', 'private, no-store');
  136. $response->setETag('"private tag"');
  137. if (in_array('"private tag"', $etags)) {
  138. $response->setStatusCode(304);
  139. } else {
  140. $response->setStatusCode(200);
  141. $response->headers->set('Content-Type', 'text/plain');
  142. $response->setContent('private data');
  143. }
  144. } else {
  145. $response->setETag('"public tag"');
  146. if (in_array('"public tag"', $etags)) {
  147. $response->setStatusCode(304);
  148. } else {
  149. $response->setStatusCode(200);
  150. $response->headers->set('Content-Type', 'text/plain');
  151. $response->setContent('public data');
  152. }
  153. }
  154. });
  155. $this->request('GET', '/');
  156. $this->assertHttpKernelIsCalled();
  157. $this->assertEquals(200, $this->response->getStatusCode());
  158. $this->assertEquals('"public tag"', $this->response->headers->get('ETag'));
  159. $this->assertEquals('public data', $this->response->getContent());
  160. $this->assertTraceContains('miss');
  161. $this->assertTraceContains('store');
  162. $this->request('GET', '/', array(), array('authenticated' => ''));
  163. $this->assertHttpKernelIsCalled();
  164. $this->assertEquals(200, $this->response->getStatusCode());
  165. $this->assertEquals('"private tag"', $this->response->headers->get('ETag'));
  166. $this->assertEquals('private data', $this->response->getContent());
  167. $this->assertTraceContains('stale');
  168. $this->assertTraceContains('invalid');
  169. $this->assertTraceNotContains('store');
  170. }
  171. public function testStoresResponsesWhenNoCacheRequestDirectivePresent()
  172. {
  173. $time = \DateTime::createFromFormat('U', time() + 5);
  174. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  175. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  176. $this->assertHttpKernelIsCalled();
  177. $this->assertTraceContains('store');
  178. $this->assertTrue($this->response->headers->has('Age'));
  179. }
  180. public function testReloadsResponsesWhenCacheHitsButNoCacheRequestDirectivePresentWhenAllowReloadIsSetTrue()
  181. {
  182. $count = 0;
  183. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=10000'), '', function ($request, $response) use (&$count)
  184. {
  185. ++$count;
  186. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  187. });
  188. $this->request('GET', '/');
  189. $this->assertEquals(200, $this->response->getStatusCode());
  190. $this->assertEquals('Hello World', $this->response->getContent());
  191. $this->assertTraceContains('store');
  192. $this->request('GET', '/');
  193. $this->assertEquals(200, $this->response->getStatusCode());
  194. $this->assertEquals('Hello World', $this->response->getContent());
  195. $this->assertTraceContains('fresh');
  196. $this->cacheConfig['allow_reload'] = true;
  197. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  198. $this->assertEquals(200, $this->response->getStatusCode());
  199. $this->assertEquals('Goodbye World', $this->response->getContent());
  200. $this->assertTraceContains('reload');
  201. $this->assertTraceContains('store');
  202. }
  203. public function testDoesNotReloadResponsesWhenAllowReloadIsSetFalseDefault()
  204. {
  205. $count = 0;
  206. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=10000'), '', function ($request, $response) use (&$count)
  207. {
  208. ++$count;
  209. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  210. });
  211. $this->request('GET', '/');
  212. $this->assertEquals(200, $this->response->getStatusCode());
  213. $this->assertEquals('Hello World', $this->response->getContent());
  214. $this->assertTraceContains('store');
  215. $this->request('GET', '/');
  216. $this->assertEquals(200, $this->response->getStatusCode());
  217. $this->assertEquals('Hello World', $this->response->getContent());
  218. $this->assertTraceContains('fresh');
  219. $this->cacheConfig['allow_reload'] = false;
  220. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  221. $this->assertEquals(200, $this->response->getStatusCode());
  222. $this->assertEquals('Hello World', $this->response->getContent());
  223. $this->assertTraceNotContains('reload');
  224. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  225. $this->assertEquals(200, $this->response->getStatusCode());
  226. $this->assertEquals('Hello World', $this->response->getContent());
  227. $this->assertTraceNotContains('reload');
  228. }
  229. public function testRevalidatesFreshCacheEntryWhenMaxAgeRequestDirectiveIsExceededWhenAllowRevalidateOptionIsSetTrue()
  230. {
  231. $count = 0;
  232. $this->setNextResponse(200, array(), '', function ($request, $response) use (&$count)
  233. {
  234. ++$count;
  235. $response->headers->set('Cache-Control', 'public, max-age=10000');
  236. $response->setETag($count);
  237. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  238. });
  239. $this->request('GET', '/');
  240. $this->assertEquals(200, $this->response->getStatusCode());
  241. $this->assertEquals('Hello World', $this->response->getContent());
  242. $this->assertTraceContains('store');
  243. $this->request('GET', '/');
  244. $this->assertEquals(200, $this->response->getStatusCode());
  245. $this->assertEquals('Hello World', $this->response->getContent());
  246. $this->assertTraceContains('fresh');
  247. $this->cacheConfig['allow_revalidate'] = true;
  248. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  249. $this->assertEquals(200, $this->response->getStatusCode());
  250. $this->assertEquals('Goodbye World', $this->response->getContent());
  251. $this->assertTraceContains('stale');
  252. $this->assertTraceContains('invalid');
  253. $this->assertTraceContains('store');
  254. }
  255. public function testDoesNotRevalidateFreshCacheEntryWhenEnableRevalidateOptionIsSetFalseDefault()
  256. {
  257. $count = 0;
  258. $this->setNextResponse(200, array(), '', function ($request, $response) use (&$count)
  259. {
  260. ++$count;
  261. $response->headers->set('Cache-Control', 'public, max-age=10000');
  262. $response->setETag($count);
  263. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  264. });
  265. $this->request('GET', '/');
  266. $this->assertEquals(200, $this->response->getStatusCode());
  267. $this->assertEquals('Hello World', $this->response->getContent());
  268. $this->assertTraceContains('store');
  269. $this->request('GET', '/');
  270. $this->assertEquals(200, $this->response->getStatusCode());
  271. $this->assertEquals('Hello World', $this->response->getContent());
  272. $this->assertTraceContains('fresh');
  273. $this->cacheConfig['allow_revalidate'] = false;
  274. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  275. $this->assertEquals(200, $this->response->getStatusCode());
  276. $this->assertEquals('Hello World', $this->response->getContent());
  277. $this->assertTraceNotContains('stale');
  278. $this->assertTraceNotContains('invalid');
  279. $this->assertTraceContains('fresh');
  280. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  281. $this->assertEquals(200, $this->response->getStatusCode());
  282. $this->assertEquals('Hello World', $this->response->getContent());
  283. $this->assertTraceNotContains('stale');
  284. $this->assertTraceNotContains('invalid');
  285. $this->assertTraceContains('fresh');
  286. }
  287. public function testFetchesResponseFromBackendWhenCacheMisses()
  288. {
  289. $time = \DateTime::createFromFormat('U', time() + 5);
  290. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  291. $this->request('GET', '/');
  292. $this->assertEquals(200, $this->response->getStatusCode());
  293. $this->assertTraceContains('miss');
  294. $this->assertTrue($this->response->headers->has('Age'));
  295. }
  296. public function testDoesNotCacheSomeStatusCodeResponses()
  297. {
  298. 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) {
  299. $time = \DateTime::createFromFormat('U', time() + 5);
  300. $this->setNextResponse($code, array('Expires' => $time->format(DATE_RFC2822)));
  301. $this->request('GET', '/');
  302. $this->assertEquals($code, $this->response->getStatusCode());
  303. $this->assertTraceNotContains('store');
  304. $this->assertFalse($this->response->headers->has('Age'));
  305. }
  306. }
  307. public function testDoesNotCacheResponsesWithExplicitNoStoreDirective()
  308. {
  309. $time = \DateTime::createFromFormat('U', time() + 5);
  310. $this->setNextResponse(200, array('Expires' => $time->format(DATE_RFC2822), 'Cache-Control' => 'no-store'));
  311. $this->request('GET', '/');
  312. $this->assertTraceNotContains('store');
  313. $this->assertFalse($this->response->headers->has('Age'));
  314. }
  315. public function testDoesNotCacheResponsesWithoutFreshnessInformationOrAValidator()
  316. {
  317. $this->setNextResponse();
  318. $this->request('GET', '/');
  319. $this->assertEquals(200, $this->response->getStatusCode());
  320. $this->assertTraceNotContains('store');
  321. }
  322. public function testCachesResponesWithExplicitNoCacheDirective()
  323. {
  324. $time = \DateTime::createFromFormat('U', time() + 5);
  325. $this->setNextResponse(200, array('Expires' => $time->format(DATE_RFC2822), 'Cache-Control' => 'public, no-cache'));
  326. $this->request('GET', '/');
  327. $this->assertTraceContains('store');
  328. $this->assertTrue($this->response->headers->has('Age'));
  329. }
  330. public function testCachesResponsesWithAnExpirationHeader()
  331. {
  332. $time = \DateTime::createFromFormat('U', time() + 5);
  333. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  334. $this->request('GET', '/');
  335. $this->assertEquals(200, $this->response->getStatusCode());
  336. $this->assertEquals('Hello World', $this->response->getContent());
  337. $this->assertNotNull($this->response->headers->get('Date'));
  338. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  339. $this->assertTraceContains('miss');
  340. $this->assertTraceContains('store');
  341. $values = $this->getMetaStorageValues();
  342. $this->assertEquals(1, count($values));
  343. }
  344. public function testCachesResponsesWithAMaxAgeDirective()
  345. {
  346. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=5'));
  347. $this->request('GET', '/');
  348. $this->assertEquals(200, $this->response->getStatusCode());
  349. $this->assertEquals('Hello World', $this->response->getContent());
  350. $this->assertNotNull($this->response->headers->get('Date'));
  351. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  352. $this->assertTraceContains('miss');
  353. $this->assertTraceContains('store');
  354. $values = $this->getMetaStorageValues();
  355. $this->assertEquals(1, count($values));
  356. }
  357. public function testCachesResponsesWithASMaxAgeDirective()
  358. {
  359. $this->setNextResponse(200, array('Cache-Control' => 's-maxage=5'));
  360. $this->request('GET', '/');
  361. $this->assertEquals(200, $this->response->getStatusCode());
  362. $this->assertEquals('Hello World', $this->response->getContent());
  363. $this->assertNotNull($this->response->headers->get('Date'));
  364. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  365. $this->assertTraceContains('miss');
  366. $this->assertTraceContains('store');
  367. $values = $this->getMetaStorageValues();
  368. $this->assertEquals(1, count($values));
  369. }
  370. public function testCachesResponsesWithALastModifiedValidatorButNoFreshnessInformation()
  371. {
  372. $time = \DateTime::createFromFormat('U', time());
  373. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Last-Modified' => $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->assertTraceContains('miss');
  378. $this->assertTraceContains('store');
  379. }
  380. public function testCachesResponsesWithAnETagValidatorButNoFreshnessInformation()
  381. {
  382. $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '"123456"'));
  383. $this->request('GET', '/');
  384. $this->assertEquals(200, $this->response->getStatusCode());
  385. $this->assertEquals('Hello World', $this->response->getContent());
  386. $this->assertTraceContains('miss');
  387. $this->assertTraceContains('store');
  388. }
  389. public function testHitsCachedResponsesWithExpiresHeader()
  390. {
  391. $time1 = \DateTime::createFromFormat('U', time() - 5);
  392. $time2 = \DateTime::createFromFormat('U', time() + 5);
  393. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Date' => $time1->format(DATE_RFC2822), 'Expires' => $time2->format(DATE_RFC2822)));
  394. $this->request('GET', '/');
  395. $this->assertHttpKernelIsCalled();
  396. $this->assertEquals(200, $this->response->getStatusCode());
  397. $this->assertNotNull($this->response->headers->get('Date'));
  398. $this->assertTraceContains('miss');
  399. $this->assertTraceContains('store');
  400. $this->assertEquals('Hello World', $this->response->getContent());
  401. $this->request('GET', '/');
  402. $this->assertHttpKernelIsNotCalled();
  403. $this->assertEquals(200, $this->response->getStatusCode());
  404. $this->assertEquals($this->responses[0]->headers->get('Date'), $this->response->headers->get('Date'));
  405. $this->assertTrue($this->response->headers->get('Age') > 0);
  406. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  407. $this->assertTraceContains('fresh');
  408. $this->assertTraceNotContains('store');
  409. $this->assertEquals('Hello World', $this->response->getContent());
  410. }
  411. public function testHitsCachedResponseWithMaxAgeDirective()
  412. {
  413. $time = \DateTime::createFromFormat('U', time() - 5);
  414. $this->setNextResponse(200, array('Date' => $time->format(DATE_RFC2822), 'Cache-Control' => 'public, max-age=10'));
  415. $this->request('GET', '/');
  416. $this->assertHttpKernelIsCalled();
  417. $this->assertEquals(200, $this->response->getStatusCode());
  418. $this->assertNotNull($this->response->headers->get('Date'));
  419. $this->assertTraceContains('miss');
  420. $this->assertTraceContains('store');
  421. $this->assertEquals('Hello World', $this->response->getContent());
  422. $this->request('GET', '/');
  423. $this->assertHttpKernelIsNotCalled();
  424. $this->assertEquals(200, $this->response->getStatusCode());
  425. $this->assertEquals($this->responses[0]->headers->get('Date'), $this->response->headers->get('Date'));
  426. $this->assertTrue($this->response->headers->get('Age') > 0);
  427. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  428. $this->assertTraceContains('fresh');
  429. $this->assertTraceNotContains('store');
  430. $this->assertEquals('Hello World', $this->response->getContent());
  431. }
  432. public function testHitsCachedResponseWithSMaxAgeDirective()
  433. {
  434. $time = \DateTime::createFromFormat('U', time() - 5);
  435. $this->setNextResponse(200, array('Date' => $time->format(DATE_RFC2822), 'Cache-Control' => 's-maxage=10, max-age=0'));
  436. $this->request('GET', '/');
  437. $this->assertHttpKernelIsCalled();
  438. $this->assertEquals(200, $this->response->getStatusCode());
  439. $this->assertNotNull($this->response->headers->get('Date'));
  440. $this->assertTraceContains('miss');
  441. $this->assertTraceContains('store');
  442. $this->assertEquals('Hello World', $this->response->getContent());
  443. $this->request('GET', '/');
  444. $this->assertHttpKernelIsNotCalled();
  445. $this->assertEquals(200, $this->response->getStatusCode());
  446. $this->assertEquals($this->responses[0]->headers->get('Date'), $this->response->headers->get('Date'));
  447. $this->assertTrue($this->response->headers->get('Age') > 0);
  448. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  449. $this->assertTraceContains('fresh');
  450. $this->assertTraceNotContains('store');
  451. $this->assertEquals('Hello World', $this->response->getContent());
  452. }
  453. public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformation()
  454. {
  455. $this->setNextResponse();
  456. $this->cacheConfig['default_ttl'] = 10;
  457. $this->request('GET', '/');
  458. $this->assertHttpKernelIsCalled();
  459. $this->assertTraceContains('miss');
  460. $this->assertTraceContains('store');
  461. $this->assertEquals('Hello World', $this->response->getContent());
  462. $this->assertRegExp('/s-maxage=10/', $this->response->headers->get('Cache-Control'));
  463. $this->cacheConfig['default_ttl'] = 10;
  464. $this->request('GET', '/');
  465. $this->assertHttpKernelIsNotCalled();
  466. $this->assertEquals(200, $this->response->getStatusCode());
  467. $this->assertTraceContains('fresh');
  468. $this->assertTraceNotContains('store');
  469. $this->assertEquals('Hello World', $this->response->getContent());
  470. }
  471. public function testDoesNotAssignDefaultTtlWhenResponseHasMustRevalidateDirective()
  472. {
  473. $this->setNextResponse(200, array('Cache-Control' => 'must-revalidate'));
  474. $this->cacheConfig['default_ttl'] = 10;
  475. $this->request('GET', '/');
  476. $this->assertHttpKernelIsCalled();
  477. $this->assertEquals(200, $this->response->getStatusCode());
  478. $this->assertTraceContains('miss');
  479. $this->assertTraceNotContains('store');
  480. $this->assertNotRegExp('/s-maxage/', $this->response->headers->get('Cache-Control'));
  481. $this->assertEquals('Hello World', $this->response->getContent());
  482. }
  483. public function testFetchesFullResponseWhenCacheStaleAndNoValidatorsPresent()
  484. {
  485. $time = \DateTime::createFromFormat('U', time() + 5);
  486. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  487. // build initial request
  488. $this->request('GET', '/');
  489. $this->assertHttpKernelIsCalled();
  490. $this->assertEquals(200, $this->response->getStatusCode());
  491. $this->assertNotNull($this->response->headers->get('Date'));
  492. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  493. $this->assertNotNull($this->response->headers->get('Age'));
  494. $this->assertTraceContains('miss');
  495. $this->assertTraceContains('store');
  496. $this->assertEquals('Hello World', $this->response->getContent());
  497. # go in and play around with the cached metadata directly ...
  498. $values = $this->getMetaStorageValues();
  499. $this->assertEquals(1, count($values));
  500. $tmp = unserialize($values[0]);
  501. $time = \DateTime::createFromFormat('U', time());
  502. $tmp[0][1]['expires'] = $time->format(DATE_RFC2822);
  503. $this->store->save('md'.sha1('http://localhost/'), serialize($tmp));
  504. // build subsequent request; should be found but miss due to freshness
  505. $this->request('GET', '/');
  506. $this->assertHttpKernelIsCalled();
  507. $this->assertEquals(200, $this->response->getStatusCode());
  508. $this->assertEquals(0, $this->response->headers->get('Age'));
  509. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  510. $this->assertTraceContains('stale');
  511. $this->assertTraceNotContains('fresh');
  512. $this->assertTraceNotContains('miss');
  513. $this->assertTraceContains('store');
  514. $this->assertEquals('Hello World', $this->response->getContent());
  515. }
  516. public function testValidatesCachedResponsesWithLastModifiedAndNoFreshnessInformation()
  517. {
  518. $time = \DateTime::createFromFormat('U', time());
  519. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time)
  520. {
  521. $response->headers->set('Cache-Control', 'public');
  522. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  523. if ($time->format(DATE_RFC2822) == $request->headers->get('IF_MODIFIED_SINCE')) {
  524. $response->setStatusCode(304);
  525. $response->setContent('');
  526. }
  527. });
  528. // build initial request
  529. $this->request('GET', '/');
  530. $this->assertHttpKernelIsCalled();
  531. $this->assertEquals(200, $this->response->getStatusCode());
  532. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  533. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  534. $this->assertEquals('Hello World', $this->response->getContent());
  535. $this->assertTraceContains('miss');
  536. $this->assertTraceContains('store');
  537. $this->assertTraceNotContains('stale');
  538. // build subsequent request; should be found but miss due to freshness
  539. $this->request('GET', '/');
  540. $this->assertHttpKernelIsCalled();
  541. $this->assertEquals(200, $this->response->getStatusCode());
  542. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  543. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  544. $this->assertEquals(0, $this->response->headers->get('Age'));
  545. $this->assertEquals('Hello World', $this->response->getContent());
  546. $this->assertTraceContains('stale');
  547. $this->assertTraceContains('valid');
  548. $this->assertTraceContains('store');
  549. $this->assertTraceNotContains('miss');
  550. }
  551. public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
  552. {
  553. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response)
  554. {
  555. $response->headers->set('Cache-Control', 'public');
  556. $response->headers->set('ETag', '"12345"');
  557. if ($response->getETag() == $request->headers->get('IF_NONE_MATCH')) {
  558. $response->setStatusCode(304);
  559. $response->setContent('');
  560. }
  561. });
  562. // build initial request
  563. $this->request('GET', '/');
  564. $this->assertHttpKernelIsCalled();
  565. $this->assertEquals(200, $this->response->getStatusCode());
  566. $this->assertNotNull($this->response->headers->get('ETag'));
  567. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  568. $this->assertEquals('Hello World', $this->response->getContent());
  569. $this->assertTraceContains('miss');
  570. $this->assertTraceContains('store');
  571. // build subsequent request; should be found but miss due to freshness
  572. $this->request('GET', '/');
  573. $this->assertHttpKernelIsCalled();
  574. $this->assertEquals(200, $this->response->getStatusCode());
  575. $this->assertNotNull($this->response->headers->get('ETag'));
  576. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  577. $this->assertEquals(0, $this->response->headers->get('Age'));
  578. $this->assertEquals('Hello World', $this->response->getContent());
  579. $this->assertTraceContains('stale');
  580. $this->assertTraceContains('valid');
  581. $this->assertTraceContains('store');
  582. $this->assertTraceNotContains('miss');
  583. }
  584. public function testReplacesCachedResponsesWhenValidationResultsInNon304Response()
  585. {
  586. $time = \DateTime::createFromFormat('U', time());
  587. $count = 0;
  588. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time, &$count)
  589. {
  590. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  591. switch (++$count) {
  592. case 1:
  593. $response->setContent('first response');
  594. break;
  595. case 2:
  596. $response->setContent('second response');
  597. break;
  598. case 3:
  599. $response->setContent('');
  600. $response->setStatusCode(304);
  601. break;
  602. }
  603. });
  604. // first request should fetch from backend and store in cache
  605. $this->request('GET', '/');
  606. $this->assertEquals(200, $this->response->getStatusCode());
  607. $this->assertEquals('first response', $this->response->getContent());
  608. // second request is validated, is invalid, and replaces cached entry
  609. $this->request('GET', '/');
  610. $this->assertEquals(200, $this->response->getStatusCode());
  611. $this->assertEquals('second response', $this->response->getContent());
  612. // third response is validated, valid, and returns cached entry
  613. $this->request('GET', '/');
  614. $this->assertEquals(200, $this->response->getStatusCode());
  615. $this->assertEquals('second response', $this->response->getContent());
  616. $this->assertEquals(3, $count);
  617. }
  618. public function testPassesHeadRequestsThroughDirectlyOnPass()
  619. {
  620. $that = $this;
  621. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($that)
  622. {
  623. $response->setContent('');
  624. $response->setStatusCode(200);
  625. $that->assertEquals('HEAD', $request->getMethod());
  626. });
  627. $this->request('HEAD', '/', array('HTTP_EXPECT' => 'something ...'));
  628. $this->assertHttpKernelIsCalled();
  629. $this->assertEquals('', $this->response->getContent());
  630. }
  631. public function testUsesCacheToRespondToHeadRequestsWhenFresh()
  632. {
  633. $that = $this;
  634. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($that)
  635. {
  636. $response->headers->set('Cache-Control', 'public, max-age=10');
  637. $response->setContent('Hello World');
  638. $response->setStatusCode(200);
  639. $that->assertNotEquals('HEAD', $request->getMethod());
  640. });
  641. $this->request('GET', '/');
  642. $this->assertHttpKernelIsCalled();
  643. $this->assertEquals('Hello World', $this->response->getContent());
  644. $this->request('HEAD', '/');
  645. $this->assertHttpKernelIsNotCalled();
  646. $this->assertEquals(200, $this->response->getStatusCode());
  647. $this->assertEquals('', $this->response->getContent());
  648. $this->assertEquals(strlen('Hello World'), $this->response->headers->get('Content-Length'));
  649. }
  650. public function testInvalidatesCachedResponsesOnPost()
  651. {
  652. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response)
  653. {
  654. if ('GET' == $request->getMethod()) {
  655. $response->setStatusCode(200);
  656. $response->headers->set('Cache-Control', 'public, max-age=500');
  657. $response->setContent('Hello World');
  658. } elseif ('POST' == $request->getMethod()) {
  659. $response->setStatusCode(303);
  660. $response->headers->set('Location', '/');
  661. $response->headers->remove('Cache-Control');
  662. $response->setContent('');
  663. }
  664. });
  665. // build initial request to enter into the cache
  666. $this->request('GET', '/');
  667. $this->assertHttpKernelIsCalled();
  668. $this->assertEquals(200, $this->response->getStatusCode());
  669. $this->assertEquals('Hello World', $this->response->getContent());
  670. $this->assertTraceContains('miss');
  671. $this->assertTraceContains('store');
  672. // make sure it is valid
  673. $this->request('GET', '/');
  674. $this->assertHttpKernelIsNotCalled();
  675. $this->assertEquals(200, $this->response->getStatusCode());
  676. $this->assertEquals('Hello World', $this->response->getContent());
  677. $this->assertTraceContains('fresh');
  678. // now POST to same URL
  679. $this->request('POST', '/');
  680. $this->assertHttpKernelIsCalled();
  681. $this->assertEquals('/', $this->response->headers->get('Location'));
  682. $this->assertTraceContains('invalidate');
  683. $this->assertTraceContains('pass');
  684. $this->assertEquals('', $this->response->getContent());
  685. // now make sure it was actually invalidated
  686. $this->request('GET', '/');
  687. $this->assertHttpKernelIsCalled();
  688. $this->assertEquals(200, $this->response->getStatusCode());
  689. $this->assertEquals('Hello World', $this->response->getContent());
  690. $this->assertTraceContains('stale');
  691. $this->assertTraceContains('invalid');
  692. $this->assertTraceContains('store');
  693. }
  694. public function testServesFromCacheWhenHeadersMatch()
  695. {
  696. $count = 0;
  697. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count)
  698. {
  699. $response->headers->set('Vary', 'Accept User-Agent Foo');
  700. $response->headers->set('Cache-Control', 'public, max-age=10');
  701. $response->headers->set('X-Response-Count', ++$count);
  702. $response->setContent($request->headers->get('USER_AGENT'));
  703. });
  704. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  705. $this->assertEquals(200, $this->response->getStatusCode());
  706. $this->assertEquals('Bob/1.0', $this->response->getContent());
  707. $this->assertTraceContains('miss');
  708. $this->assertTraceContains('store');
  709. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  710. $this->assertEquals(200, $this->response->getStatusCode());
  711. $this->assertEquals('Bob/1.0', $this->response->getContent());
  712. $this->assertTraceContains('fresh');
  713. $this->assertTraceNotContains('store');
  714. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  715. }
  716. public function testStoresMultipleResponsesWhenHeadersDiffer()
  717. {
  718. $count = 0;
  719. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count)
  720. {
  721. $response->headers->set('Vary', 'Accept User-Agent Foo');
  722. $response->headers->set('Cache-Control', 'public, max-age=10');
  723. $response->headers->set('X-Response-Count', ++$count);
  724. $response->setContent($request->headers->get('USER_AGENT'));
  725. });
  726. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  727. $this->assertEquals(200, $this->response->getStatusCode());
  728. $this->assertEquals('Bob/1.0', $this->response->getContent());
  729. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  730. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  731. $this->assertEquals(200, $this->response->getStatusCode());
  732. $this->assertTraceContains('miss');
  733. $this->assertTraceContains('store');
  734. $this->assertEquals('Bob/2.0', $this->response->getContent());
  735. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  736. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  737. $this->assertTraceContains('fresh');
  738. $this->assertEquals('Bob/1.0', $this->response->getContent());
  739. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  740. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  741. $this->assertTraceContains('fresh');
  742. $this->assertEquals('Bob/2.0', $this->response->getContent());
  743. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  744. $this->request('GET', '/', array('HTTP_USER_AGENT' => 'Bob/2.0'));
  745. $this->assertTraceContains('miss');
  746. $this->assertEquals('Bob/2.0', $this->response->getContent());
  747. $this->assertEquals(3, $this->response->headers->get('X-Response-Count'));
  748. }
  749. }