HttpCacheTest.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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\HttpCache;
  11. require_once __DIR__.'/HttpCacheTestCase.php';
  12. class HttpCacheTest extends HttpCacheTestCase
  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. $r = new \ReflectionObject($this->store);
  504. $m = $r->getMethod('save');
  505. $m->setAccessible(true);
  506. $m->invoke($this->store, 'md'.sha1('http://localhost/'), serialize($tmp));
  507. // build subsequent request; should be found but miss due to freshness
  508. $this->request('GET', '/');
  509. $this->assertHttpKernelIsCalled();
  510. $this->assertEquals(200, $this->response->getStatusCode());
  511. $this->assertEquals(0, $this->response->headers->get('Age'));
  512. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  513. $this->assertTraceContains('stale');
  514. $this->assertTraceNotContains('fresh');
  515. $this->assertTraceNotContains('miss');
  516. $this->assertTraceContains('store');
  517. $this->assertEquals('Hello World', $this->response->getContent());
  518. }
  519. public function testValidatesCachedResponsesWithLastModifiedAndNoFreshnessInformation()
  520. {
  521. $time = \DateTime::createFromFormat('U', time());
  522. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time)
  523. {
  524. $response->headers->set('Cache-Control', 'public');
  525. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  526. if ($time->format(DATE_RFC2822) == $request->headers->get('IF_MODIFIED_SINCE')) {
  527. $response->setStatusCode(304);
  528. $response->setContent('');
  529. }
  530. });
  531. // build initial request
  532. $this->request('GET', '/');
  533. $this->assertHttpKernelIsCalled();
  534. $this->assertEquals(200, $this->response->getStatusCode());
  535. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  536. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  537. $this->assertEquals('Hello World', $this->response->getContent());
  538. $this->assertTraceContains('miss');
  539. $this->assertTraceContains('store');
  540. $this->assertTraceNotContains('stale');
  541. // build subsequent request; should be found but miss due to freshness
  542. $this->request('GET', '/');
  543. $this->assertHttpKernelIsCalled();
  544. $this->assertEquals(200, $this->response->getStatusCode());
  545. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  546. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  547. $this->assertEquals(0, $this->response->headers->get('Age'));
  548. $this->assertEquals('Hello World', $this->response->getContent());
  549. $this->assertTraceContains('stale');
  550. $this->assertTraceContains('valid');
  551. $this->assertTraceContains('store');
  552. $this->assertTraceNotContains('miss');
  553. }
  554. public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
  555. {
  556. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response)
  557. {
  558. $response->headers->set('Cache-Control', 'public');
  559. $response->headers->set('ETag', '"12345"');
  560. if ($response->getETag() == $request->headers->get('IF_NONE_MATCH')) {
  561. $response->setStatusCode(304);
  562. $response->setContent('');
  563. }
  564. });
  565. // build initial request
  566. $this->request('GET', '/');
  567. $this->assertHttpKernelIsCalled();
  568. $this->assertEquals(200, $this->response->getStatusCode());
  569. $this->assertNotNull($this->response->headers->get('ETag'));
  570. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  571. $this->assertEquals('Hello World', $this->response->getContent());
  572. $this->assertTraceContains('miss');
  573. $this->assertTraceContains('store');
  574. // build subsequent request; should be found but miss due to freshness
  575. $this->request('GET', '/');
  576. $this->assertHttpKernelIsCalled();
  577. $this->assertEquals(200, $this->response->getStatusCode());
  578. $this->assertNotNull($this->response->headers->get('ETag'));
  579. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  580. $this->assertEquals(0, $this->response->headers->get('Age'));
  581. $this->assertEquals('Hello World', $this->response->getContent());
  582. $this->assertTraceContains('stale');
  583. $this->assertTraceContains('valid');
  584. $this->assertTraceContains('store');
  585. $this->assertTraceNotContains('miss');
  586. }
  587. public function testReplacesCachedResponsesWhenValidationResultsInNon304Response()
  588. {
  589. $time = \DateTime::createFromFormat('U', time());
  590. $count = 0;
  591. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time, &$count)
  592. {
  593. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  594. switch (++$count) {
  595. case 1:
  596. $response->setContent('first response');
  597. break;
  598. case 2:
  599. $response->setContent('second response');
  600. break;
  601. case 3:
  602. $response->setContent('');
  603. $response->setStatusCode(304);
  604. break;
  605. }
  606. });
  607. // first request should fetch from backend and store in cache
  608. $this->request('GET', '/');
  609. $this->assertEquals(200, $this->response->getStatusCode());
  610. $this->assertEquals('first response', $this->response->getContent());
  611. // second request is validated, is invalid, and replaces cached entry
  612. $this->request('GET', '/');
  613. $this->assertEquals(200, $this->response->getStatusCode());
  614. $this->assertEquals('second response', $this->response->getContent());
  615. // third response is validated, valid, and returns cached entry
  616. $this->request('GET', '/');
  617. $this->assertEquals(200, $this->response->getStatusCode());
  618. $this->assertEquals('second response', $this->response->getContent());
  619. $this->assertEquals(3, $count);
  620. }
  621. public function testPassesHeadRequestsThroughDirectlyOnPass()
  622. {
  623. $that = $this;
  624. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($that)
  625. {
  626. $response->setContent('');
  627. $response->setStatusCode(200);
  628. $that->assertEquals('HEAD', $request->getMethod());
  629. });
  630. $this->request('HEAD', '/', array('HTTP_EXPECT' => 'something ...'));
  631. $this->assertHttpKernelIsCalled();
  632. $this->assertEquals('', $this->response->getContent());
  633. }
  634. public function testUsesCacheToRespondToHeadRequestsWhenFresh()
  635. {
  636. $that = $this;
  637. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($that)
  638. {
  639. $response->headers->set('Cache-Control', 'public, max-age=10');
  640. $response->setContent('Hello World');
  641. $response->setStatusCode(200);
  642. $that->assertNotEquals('HEAD', $request->getMethod());
  643. });
  644. $this->request('GET', '/');
  645. $this->assertHttpKernelIsCalled();
  646. $this->assertEquals('Hello World', $this->response->getContent());
  647. $this->request('HEAD', '/');
  648. $this->assertHttpKernelIsNotCalled();
  649. $this->assertEquals(200, $this->response->getStatusCode());
  650. $this->assertEquals('', $this->response->getContent());
  651. $this->assertEquals(strlen('Hello World'), $this->response->headers->get('Content-Length'));
  652. }
  653. public function testInvalidatesCachedResponsesOnPost()
  654. {
  655. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response)
  656. {
  657. if ('GET' == $request->getMethod()) {
  658. $response->setStatusCode(200);
  659. $response->headers->set('Cache-Control', 'public, max-age=500');
  660. $response->setContent('Hello World');
  661. } elseif ('POST' == $request->getMethod()) {
  662. $response->setStatusCode(303);
  663. $response->headers->set('Location', '/');
  664. $response->headers->remove('Cache-Control');
  665. $response->setContent('');
  666. }
  667. });
  668. // build initial request to enter into the cache
  669. $this->request('GET', '/');
  670. $this->assertHttpKernelIsCalled();
  671. $this->assertEquals(200, $this->response->getStatusCode());
  672. $this->assertEquals('Hello World', $this->response->getContent());
  673. $this->assertTraceContains('miss');
  674. $this->assertTraceContains('store');
  675. // make sure it is valid
  676. $this->request('GET', '/');
  677. $this->assertHttpKernelIsNotCalled();
  678. $this->assertEquals(200, $this->response->getStatusCode());
  679. $this->assertEquals('Hello World', $this->response->getContent());
  680. $this->assertTraceContains('fresh');
  681. // now POST to same URL
  682. $this->request('POST', '/');
  683. $this->assertHttpKernelIsCalled();
  684. $this->assertEquals('/', $this->response->headers->get('Location'));
  685. $this->assertTraceContains('invalidate');
  686. $this->assertTraceContains('pass');
  687. $this->assertEquals('', $this->response->getContent());
  688. // now make sure it was actually invalidated
  689. $this->request('GET', '/');
  690. $this->assertHttpKernelIsCalled();
  691. $this->assertEquals(200, $this->response->getStatusCode());
  692. $this->assertEquals('Hello World', $this->response->getContent());
  693. $this->assertTraceContains('stale');
  694. $this->assertTraceContains('invalid');
  695. $this->assertTraceContains('store');
  696. }
  697. public function testServesFromCacheWhenHeadersMatch()
  698. {
  699. $count = 0;
  700. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count)
  701. {
  702. $response->headers->set('Vary', 'Accept User-Agent Foo');
  703. $response->headers->set('Cache-Control', 'public, max-age=10');
  704. $response->headers->set('X-Response-Count', ++$count);
  705. $response->setContent($request->headers->get('USER_AGENT'));
  706. });
  707. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  708. $this->assertEquals(200, $this->response->getStatusCode());
  709. $this->assertEquals('Bob/1.0', $this->response->getContent());
  710. $this->assertTraceContains('miss');
  711. $this->assertTraceContains('store');
  712. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  713. $this->assertEquals(200, $this->response->getStatusCode());
  714. $this->assertEquals('Bob/1.0', $this->response->getContent());
  715. $this->assertTraceContains('fresh');
  716. $this->assertTraceNotContains('store');
  717. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  718. }
  719. public function testStoresMultipleResponsesWhenHeadersDiffer()
  720. {
  721. $count = 0;
  722. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count)
  723. {
  724. $response->headers->set('Vary', 'Accept User-Agent Foo');
  725. $response->headers->set('Cache-Control', 'public, max-age=10');
  726. $response->headers->set('X-Response-Count', ++$count);
  727. $response->setContent($request->headers->get('USER_AGENT'));
  728. });
  729. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  730. $this->assertEquals(200, $this->response->getStatusCode());
  731. $this->assertEquals('Bob/1.0', $this->response->getContent());
  732. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  733. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  734. $this->assertEquals(200, $this->response->getStatusCode());
  735. $this->assertTraceContains('miss');
  736. $this->assertTraceContains('store');
  737. $this->assertEquals('Bob/2.0', $this->response->getContent());
  738. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  739. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  740. $this->assertTraceContains('fresh');
  741. $this->assertEquals('Bob/1.0', $this->response->getContent());
  742. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  743. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  744. $this->assertTraceContains('fresh');
  745. $this->assertEquals('Bob/2.0', $this->response->getContent());
  746. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  747. $this->request('GET', '/', array('HTTP_USER_AGENT' => 'Bob/2.0'));
  748. $this->assertTraceContains('miss');
  749. $this->assertEquals('Bob/2.0', $this->response->getContent());
  750. $this->assertEquals(3, $this->response->headers->get('X-Response-Count'));
  751. }
  752. public function testShouldCatchExceptions()
  753. {
  754. $this->catchExceptions();
  755. $this->setNextResponse();
  756. $this->request('GET', '/');
  757. $this->assertExceptionsAreCaught();
  758. }
  759. public function testShouldNotCatchExceptions()
  760. {
  761. $this->catchExceptions(false);
  762. $this->setNextResponse();
  763. $this->request('GET', '/');
  764. $this->assertExceptionsAreNotCaught();
  765. }
  766. public function testEsiCacheSendsTheLowestTtl()
  767. {
  768. $responses = array(
  769. array(
  770. 'status' => 200,
  771. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  772. 'headers' => array(
  773. 'Cache-Control' => 's-maxage=300',
  774. 'Surrogate-Control' => 'content="ESI/1.0"',
  775. ),
  776. ),
  777. array(
  778. 'status' => 200,
  779. 'body' => 'Hello World!',
  780. 'headers' => array('Cache-Control' => 's-maxage=300'),
  781. ),
  782. array(
  783. 'status' => 200,
  784. 'body' => 'My name is Bobby.',
  785. 'headers' => array('Cache-Control' => 's-maxage=100'),
  786. ),
  787. );
  788. $this->setNextResponses($responses);
  789. $this->request('GET', '/', array(), array(), true);
  790. $this->assertEquals("Hello World! My name is Bobby.", $this->response->getContent());
  791. $this->assertEquals(100, $this->response->getTtl());
  792. }
  793. public function testEsiCacheForceValidation()
  794. {
  795. $responses = array(
  796. array(
  797. 'status' => 200,
  798. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  799. 'headers' => array(
  800. 'Cache-Control' => 's-maxage=300',
  801. 'Surrogate-Control' => 'content="ESI/1.0"',
  802. ),
  803. ),
  804. array(
  805. 'status' => 200,
  806. 'body' => 'Hello World!',
  807. 'headers' => array('ETag' => 'foobar'),
  808. ),
  809. array(
  810. 'status' => 200,
  811. 'body' => 'My name is Bobby.',
  812. 'headers' => array('Cache-Control' => 's-maxage=100'),
  813. ),
  814. );
  815. $this->setNextResponses($responses);
  816. $this->request('GET', '/', array(), array(), true);
  817. $this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
  818. $this->assertEquals(null, $this->response->getTtl());
  819. $this->assertTrue($this->response->mustRevalidate());
  820. $this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
  821. $this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
  822. }
  823. }