HttpCacheTest.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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->headers->set('Cache-Control', 'public');
  146. $response->setETag('"public tag"');
  147. if (in_array('"public tag"', $etags)) {
  148. $response->setStatusCode(304);
  149. } else {
  150. $response->setStatusCode(200);
  151. $response->headers->set('Content-Type', 'text/plain');
  152. $response->setContent('public data');
  153. }
  154. }
  155. });
  156. $this->request('GET', '/');
  157. $this->assertHttpKernelIsCalled();
  158. $this->assertEquals(200, $this->response->getStatusCode());
  159. $this->assertEquals('"public tag"', $this->response->headers->get('ETag'));
  160. $this->assertEquals('public data', $this->response->getContent());
  161. $this->assertTraceContains('miss');
  162. $this->assertTraceContains('store');
  163. $this->request('GET', '/', array(), array('authenticated' => ''));
  164. $this->assertHttpKernelIsCalled();
  165. $this->assertEquals(200, $this->response->getStatusCode());
  166. $this->assertEquals('"private tag"', $this->response->headers->get('ETag'));
  167. $this->assertEquals('private data', $this->response->getContent());
  168. $this->assertTraceContains('stale');
  169. $this->assertTraceContains('invalid');
  170. $this->assertTraceNotContains('store');
  171. }
  172. public function testStoresResponsesWhenNoCacheRequestDirectivePresent()
  173. {
  174. $time = \DateTime::createFromFormat('U', time() + 5);
  175. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  176. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  177. $this->assertHttpKernelIsCalled();
  178. $this->assertTraceContains('store');
  179. $this->assertTrue($this->response->headers->has('Age'));
  180. }
  181. public function testReloadsResponsesWhenCacheHitsButNoCacheRequestDirectivePresentWhenAllowReloadIsSetTrue()
  182. {
  183. $count = 0;
  184. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=10000'), '', function ($request, $response) use (&$count)
  185. {
  186. ++$count;
  187. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  188. });
  189. $this->request('GET', '/');
  190. $this->assertEquals(200, $this->response->getStatusCode());
  191. $this->assertEquals('Hello World', $this->response->getContent());
  192. $this->assertTraceContains('store');
  193. $this->request('GET', '/');
  194. $this->assertEquals(200, $this->response->getStatusCode());
  195. $this->assertEquals('Hello World', $this->response->getContent());
  196. $this->assertTraceContains('fresh');
  197. $this->cacheConfig['allow_reload'] = true;
  198. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  199. $this->assertEquals(200, $this->response->getStatusCode());
  200. $this->assertEquals('Goodbye World', $this->response->getContent());
  201. $this->assertTraceContains('reload');
  202. $this->assertTraceContains('store');
  203. }
  204. public function testDoesNotReloadResponsesWhenAllowReloadIsSetFalseDefault()
  205. {
  206. $count = 0;
  207. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=10000'), '', function ($request, $response) use (&$count)
  208. {
  209. ++$count;
  210. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  211. });
  212. $this->request('GET', '/');
  213. $this->assertEquals(200, $this->response->getStatusCode());
  214. $this->assertEquals('Hello World', $this->response->getContent());
  215. $this->assertTraceContains('store');
  216. $this->request('GET', '/');
  217. $this->assertEquals(200, $this->response->getStatusCode());
  218. $this->assertEquals('Hello World', $this->response->getContent());
  219. $this->assertTraceContains('fresh');
  220. $this->cacheConfig['allow_reload'] = false;
  221. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  222. $this->assertEquals(200, $this->response->getStatusCode());
  223. $this->assertEquals('Hello World', $this->response->getContent());
  224. $this->assertTraceNotContains('reload');
  225. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  226. $this->assertEquals(200, $this->response->getStatusCode());
  227. $this->assertEquals('Hello World', $this->response->getContent());
  228. $this->assertTraceNotContains('reload');
  229. }
  230. public function testRevalidatesFreshCacheEntryWhenMaxAgeRequestDirectiveIsExceededWhenAllowRevalidateOptionIsSetTrue()
  231. {
  232. $count = 0;
  233. $this->setNextResponse(200, array(), '', function ($request, $response) use (&$count)
  234. {
  235. ++$count;
  236. $response->headers->set('Cache-Control', 'public, max-age=10000');
  237. $response->setETag($count);
  238. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  239. });
  240. $this->request('GET', '/');
  241. $this->assertEquals(200, $this->response->getStatusCode());
  242. $this->assertEquals('Hello World', $this->response->getContent());
  243. $this->assertTraceContains('store');
  244. $this->request('GET', '/');
  245. $this->assertEquals(200, $this->response->getStatusCode());
  246. $this->assertEquals('Hello World', $this->response->getContent());
  247. $this->assertTraceContains('fresh');
  248. $this->cacheConfig['allow_revalidate'] = true;
  249. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  250. $this->assertEquals(200, $this->response->getStatusCode());
  251. $this->assertEquals('Goodbye World', $this->response->getContent());
  252. $this->assertTraceContains('stale');
  253. $this->assertTraceContains('invalid');
  254. $this->assertTraceContains('store');
  255. }
  256. public function testDoesNotRevalidateFreshCacheEntryWhenEnableRevalidateOptionIsSetFalseDefault()
  257. {
  258. $count = 0;
  259. $this->setNextResponse(200, array(), '', function ($request, $response) use (&$count)
  260. {
  261. ++$count;
  262. $response->headers->set('Cache-Control', 'public, max-age=10000');
  263. $response->setETag($count);
  264. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  265. });
  266. $this->request('GET', '/');
  267. $this->assertEquals(200, $this->response->getStatusCode());
  268. $this->assertEquals('Hello World', $this->response->getContent());
  269. $this->assertTraceContains('store');
  270. $this->request('GET', '/');
  271. $this->assertEquals(200, $this->response->getStatusCode());
  272. $this->assertEquals('Hello World', $this->response->getContent());
  273. $this->assertTraceContains('fresh');
  274. $this->cacheConfig['allow_revalidate'] = false;
  275. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  276. $this->assertEquals(200, $this->response->getStatusCode());
  277. $this->assertEquals('Hello World', $this->response->getContent());
  278. $this->assertTraceNotContains('stale');
  279. $this->assertTraceNotContains('invalid');
  280. $this->assertTraceContains('fresh');
  281. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  282. $this->assertEquals(200, $this->response->getStatusCode());
  283. $this->assertEquals('Hello World', $this->response->getContent());
  284. $this->assertTraceNotContains('stale');
  285. $this->assertTraceNotContains('invalid');
  286. $this->assertTraceContains('fresh');
  287. }
  288. public function testFetchesResponseFromBackendWhenCacheMisses()
  289. {
  290. $time = \DateTime::createFromFormat('U', time() + 5);
  291. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  292. $this->request('GET', '/');
  293. $this->assertEquals(200, $this->response->getStatusCode());
  294. $this->assertTraceContains('miss');
  295. $this->assertTrue($this->response->headers->has('Age'));
  296. }
  297. public function testDoesNotCacheSomeStatusCodeResponses()
  298. {
  299. 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) {
  300. $time = \DateTime::createFromFormat('U', time() + 5);
  301. $this->setNextResponse($code, array('Expires' => $time->format(DATE_RFC2822)));
  302. $this->request('GET', '/');
  303. $this->assertEquals($code, $this->response->getStatusCode());
  304. $this->assertTraceNotContains('store');
  305. $this->assertFalse($this->response->headers->has('Age'));
  306. }
  307. }
  308. public function testDoesNotCacheResponsesWithExplicitNoStoreDirective()
  309. {
  310. $time = \DateTime::createFromFormat('U', time() + 5);
  311. $this->setNextResponse(200, array('Expires' => $time->format(DATE_RFC2822), 'Cache-Control' => 'no-store'));
  312. $this->request('GET', '/');
  313. $this->assertTraceNotContains('store');
  314. $this->assertFalse($this->response->headers->has('Age'));
  315. }
  316. public function testDoesNotCacheResponsesWithoutFreshnessInformationOrAValidator()
  317. {
  318. $this->setNextResponse();
  319. $this->request('GET', '/');
  320. $this->assertEquals(200, $this->response->getStatusCode());
  321. $this->assertTraceNotContains('store');
  322. }
  323. public function testCachesResponesWithExplicitNoCacheDirective()
  324. {
  325. $time = \DateTime::createFromFormat('U', time() + 5);
  326. $this->setNextResponse(200, array('Expires' => $time->format(DATE_RFC2822), 'Cache-Control' => 'public, no-cache'));
  327. $this->request('GET', '/');
  328. $this->assertTraceContains('store');
  329. $this->assertTrue($this->response->headers->has('Age'));
  330. }
  331. public function testCachesResponsesWithAnExpirationHeader()
  332. {
  333. $time = \DateTime::createFromFormat('U', time() + 5);
  334. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  335. $this->request('GET', '/');
  336. $this->assertEquals(200, $this->response->getStatusCode());
  337. $this->assertEquals('Hello World', $this->response->getContent());
  338. $this->assertNotNull($this->response->headers->get('Date'));
  339. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  340. $this->assertTraceContains('miss');
  341. $this->assertTraceContains('store');
  342. $values = $this->getMetaStorageValues();
  343. $this->assertEquals(1, count($values));
  344. }
  345. public function testCachesResponsesWithAMaxAgeDirective()
  346. {
  347. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=5'));
  348. $this->request('GET', '/');
  349. $this->assertEquals(200, $this->response->getStatusCode());
  350. $this->assertEquals('Hello World', $this->response->getContent());
  351. $this->assertNotNull($this->response->headers->get('Date'));
  352. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  353. $this->assertTraceContains('miss');
  354. $this->assertTraceContains('store');
  355. $values = $this->getMetaStorageValues();
  356. $this->assertEquals(1, count($values));
  357. }
  358. public function testCachesResponsesWithASMaxAgeDirective()
  359. {
  360. $this->setNextResponse(200, array('Cache-Control' => 's-maxage=5'));
  361. $this->request('GET', '/');
  362. $this->assertEquals(200, $this->response->getStatusCode());
  363. $this->assertEquals('Hello World', $this->response->getContent());
  364. $this->assertNotNull($this->response->headers->get('Date'));
  365. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  366. $this->assertTraceContains('miss');
  367. $this->assertTraceContains('store');
  368. $values = $this->getMetaStorageValues();
  369. $this->assertEquals(1, count($values));
  370. }
  371. public function testCachesResponsesWithALastModifiedValidatorButNoFreshnessInformation()
  372. {
  373. $time = \DateTime::createFromFormat('U', time());
  374. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Last-Modified' => $time->format(DATE_RFC2822)));
  375. $this->request('GET', '/');
  376. $this->assertEquals(200, $this->response->getStatusCode());
  377. $this->assertEquals('Hello World', $this->response->getContent());
  378. $this->assertTraceContains('miss');
  379. $this->assertTraceContains('store');
  380. }
  381. public function testCachesResponsesWithAnETagValidatorButNoFreshnessInformation()
  382. {
  383. $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '"123456"'));
  384. $this->request('GET', '/');
  385. $this->assertEquals(200, $this->response->getStatusCode());
  386. $this->assertEquals('Hello World', $this->response->getContent());
  387. $this->assertTraceContains('miss');
  388. $this->assertTraceContains('store');
  389. }
  390. public function testHitsCachedResponsesWithExpiresHeader()
  391. {
  392. $time1 = \DateTime::createFromFormat('U', time() - 5);
  393. $time2 = \DateTime::createFromFormat('U', time() + 5);
  394. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Date' => $time1->format(DATE_RFC2822), 'Expires' => $time2->format(DATE_RFC2822)));
  395. $this->request('GET', '/');
  396. $this->assertHttpKernelIsCalled();
  397. $this->assertEquals(200, $this->response->getStatusCode());
  398. $this->assertNotNull($this->response->headers->get('Date'));
  399. $this->assertTraceContains('miss');
  400. $this->assertTraceContains('store');
  401. $this->assertEquals('Hello World', $this->response->getContent());
  402. $this->request('GET', '/');
  403. $this->assertHttpKernelIsNotCalled();
  404. $this->assertEquals(200, $this->response->getStatusCode());
  405. $this->assertEquals($this->responses[0]->headers->get('Date'), $this->response->headers->get('Date'));
  406. $this->assertTrue($this->response->headers->get('Age') > 0);
  407. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  408. $this->assertTraceContains('fresh');
  409. $this->assertTraceNotContains('store');
  410. $this->assertEquals('Hello World', $this->response->getContent());
  411. }
  412. public function testHitsCachedResponseWithMaxAgeDirective()
  413. {
  414. $time = \DateTime::createFromFormat('U', time() - 5);
  415. $this->setNextResponse(200, array('Date' => $time->format(DATE_RFC2822), 'Cache-Control' => 'public, max-age=10'));
  416. $this->request('GET', '/');
  417. $this->assertHttpKernelIsCalled();
  418. $this->assertEquals(200, $this->response->getStatusCode());
  419. $this->assertNotNull($this->response->headers->get('Date'));
  420. $this->assertTraceContains('miss');
  421. $this->assertTraceContains('store');
  422. $this->assertEquals('Hello World', $this->response->getContent());
  423. $this->request('GET', '/');
  424. $this->assertHttpKernelIsNotCalled();
  425. $this->assertEquals(200, $this->response->getStatusCode());
  426. $this->assertEquals($this->responses[0]->headers->get('Date'), $this->response->headers->get('Date'));
  427. $this->assertTrue($this->response->headers->get('Age') > 0);
  428. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  429. $this->assertTraceContains('fresh');
  430. $this->assertTraceNotContains('store');
  431. $this->assertEquals('Hello World', $this->response->getContent());
  432. }
  433. public function testHitsCachedResponseWithSMaxAgeDirective()
  434. {
  435. $time = \DateTime::createFromFormat('U', time() - 5);
  436. $this->setNextResponse(200, array('Date' => $time->format(DATE_RFC2822), 'Cache-Control' => 's-maxage=10, max-age=0'));
  437. $this->request('GET', '/');
  438. $this->assertHttpKernelIsCalled();
  439. $this->assertEquals(200, $this->response->getStatusCode());
  440. $this->assertNotNull($this->response->headers->get('Date'));
  441. $this->assertTraceContains('miss');
  442. $this->assertTraceContains('store');
  443. $this->assertEquals('Hello World', $this->response->getContent());
  444. $this->request('GET', '/');
  445. $this->assertHttpKernelIsNotCalled();
  446. $this->assertEquals(200, $this->response->getStatusCode());
  447. $this->assertEquals($this->responses[0]->headers->get('Date'), $this->response->headers->get('Date'));
  448. $this->assertTrue($this->response->headers->get('Age') > 0);
  449. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  450. $this->assertTraceContains('fresh');
  451. $this->assertTraceNotContains('store');
  452. $this->assertEquals('Hello World', $this->response->getContent());
  453. }
  454. public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformation()
  455. {
  456. $this->setNextResponse();
  457. $this->cacheConfig['default_ttl'] = 10;
  458. $this->request('GET', '/');
  459. $this->assertHttpKernelIsCalled();
  460. $this->assertTraceContains('miss');
  461. $this->assertTraceContains('store');
  462. $this->assertEquals('Hello World', $this->response->getContent());
  463. $this->assertRegExp('/s-maxage=10/', $this->response->headers->get('Cache-Control'));
  464. $this->cacheConfig['default_ttl'] = 10;
  465. $this->request('GET', '/');
  466. $this->assertHttpKernelIsNotCalled();
  467. $this->assertEquals(200, $this->response->getStatusCode());
  468. $this->assertTraceContains('fresh');
  469. $this->assertTraceNotContains('store');
  470. $this->assertEquals('Hello World', $this->response->getContent());
  471. }
  472. public function testDoesNotAssignDefaultTtlWhenResponseHasMustRevalidateDirective()
  473. {
  474. $this->setNextResponse(200, array('Cache-Control' => 'must-revalidate'));
  475. $this->cacheConfig['default_ttl'] = 10;
  476. $this->request('GET', '/');
  477. $this->assertHttpKernelIsCalled();
  478. $this->assertEquals(200, $this->response->getStatusCode());
  479. $this->assertTraceContains('miss');
  480. $this->assertTraceNotContains('store');
  481. $this->assertNotRegExp('/s-maxage/', $this->response->headers->get('Cache-Control'));
  482. $this->assertEquals('Hello World', $this->response->getContent());
  483. }
  484. public function testFetchesFullResponseWhenCacheStaleAndNoValidatorsPresent()
  485. {
  486. $time = \DateTime::createFromFormat('U', time() + 5);
  487. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  488. // build initial request
  489. $this->request('GET', '/');
  490. $this->assertHttpKernelIsCalled();
  491. $this->assertEquals(200, $this->response->getStatusCode());
  492. $this->assertNotNull($this->response->headers->get('Date'));
  493. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  494. $this->assertNotNull($this->response->headers->get('Age'));
  495. $this->assertTraceContains('miss');
  496. $this->assertTraceContains('store');
  497. $this->assertEquals('Hello World', $this->response->getContent());
  498. # go in and play around with the cached metadata directly ...
  499. $values = $this->getMetaStorageValues();
  500. $this->assertEquals(1, count($values));
  501. $tmp = unserialize($values[0]);
  502. $time = \DateTime::createFromFormat('U', time());
  503. $tmp[0][1]['expires'] = $time->format(DATE_RFC2822);
  504. $r = new \ReflectionObject($this->store);
  505. $m = $r->getMethod('save');
  506. $m->setAccessible(true);
  507. $m->invoke($this->store, 'md'.sha1('http://localhost/'), serialize($tmp));
  508. // build subsequent request; should be found but miss due to freshness
  509. $this->request('GET', '/');
  510. $this->assertHttpKernelIsCalled();
  511. $this->assertEquals(200, $this->response->getStatusCode());
  512. $this->assertEquals(0, $this->response->headers->get('Age'));
  513. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  514. $this->assertTraceContains('stale');
  515. $this->assertTraceNotContains('fresh');
  516. $this->assertTraceNotContains('miss');
  517. $this->assertTraceContains('store');
  518. $this->assertEquals('Hello World', $this->response->getContent());
  519. }
  520. public function testValidatesCachedResponsesWithLastModifiedAndNoFreshnessInformation()
  521. {
  522. $time = \DateTime::createFromFormat('U', time());
  523. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time)
  524. {
  525. $response->headers->set('Cache-Control', 'public');
  526. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  527. if ($time->format(DATE_RFC2822) == $request->headers->get('IF_MODIFIED_SINCE')) {
  528. $response->setStatusCode(304);
  529. $response->setContent('');
  530. }
  531. });
  532. // build initial request
  533. $this->request('GET', '/');
  534. $this->assertHttpKernelIsCalled();
  535. $this->assertEquals(200, $this->response->getStatusCode());
  536. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  537. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  538. $this->assertEquals('Hello World', $this->response->getContent());
  539. $this->assertTraceContains('miss');
  540. $this->assertTraceContains('store');
  541. $this->assertTraceNotContains('stale');
  542. // build subsequent request; should be found but miss due to freshness
  543. $this->request('GET', '/');
  544. $this->assertHttpKernelIsCalled();
  545. $this->assertEquals(200, $this->response->getStatusCode());
  546. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  547. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  548. $this->assertEquals(0, $this->response->headers->get('Age'));
  549. $this->assertEquals('Hello World', $this->response->getContent());
  550. $this->assertTraceContains('stale');
  551. $this->assertTraceContains('valid');
  552. $this->assertTraceContains('store');
  553. $this->assertTraceNotContains('miss');
  554. }
  555. public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
  556. {
  557. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response)
  558. {
  559. $response->headers->set('Cache-Control', 'public');
  560. $response->headers->set('ETag', '"12345"');
  561. if ($response->getETag() == $request->headers->get('IF_NONE_MATCH')) {
  562. $response->setStatusCode(304);
  563. $response->setContent('');
  564. }
  565. });
  566. // build initial request
  567. $this->request('GET', '/');
  568. $this->assertHttpKernelIsCalled();
  569. $this->assertEquals(200, $this->response->getStatusCode());
  570. $this->assertNotNull($this->response->headers->get('ETag'));
  571. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  572. $this->assertEquals('Hello World', $this->response->getContent());
  573. $this->assertTraceContains('miss');
  574. $this->assertTraceContains('store');
  575. // build subsequent request; should be found but miss due to freshness
  576. $this->request('GET', '/');
  577. $this->assertHttpKernelIsCalled();
  578. $this->assertEquals(200, $this->response->getStatusCode());
  579. $this->assertNotNull($this->response->headers->get('ETag'));
  580. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  581. $this->assertEquals(0, $this->response->headers->get('Age'));
  582. $this->assertEquals('Hello World', $this->response->getContent());
  583. $this->assertTraceContains('stale');
  584. $this->assertTraceContains('valid');
  585. $this->assertTraceContains('store');
  586. $this->assertTraceNotContains('miss');
  587. }
  588. public function testReplacesCachedResponsesWhenValidationResultsInNon304Response()
  589. {
  590. $time = \DateTime::createFromFormat('U', time());
  591. $count = 0;
  592. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time, &$count)
  593. {
  594. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  595. $response->headers->set('Cache-Control', 'public');
  596. switch (++$count) {
  597. case 1:
  598. $response->setContent('first response');
  599. break;
  600. case 2:
  601. $response->setContent('second response');
  602. break;
  603. case 3:
  604. $response->setContent('');
  605. $response->setStatusCode(304);
  606. break;
  607. }
  608. });
  609. // first request should fetch from backend and store in cache
  610. $this->request('GET', '/');
  611. $this->assertEquals(200, $this->response->getStatusCode());
  612. $this->assertEquals('first response', $this->response->getContent());
  613. // second request is validated, is invalid, and replaces cached entry
  614. $this->request('GET', '/');
  615. $this->assertEquals(200, $this->response->getStatusCode());
  616. $this->assertEquals('second response', $this->response->getContent());
  617. // third response is validated, valid, and returns cached entry
  618. $this->request('GET', '/');
  619. $this->assertEquals(200, $this->response->getStatusCode());
  620. $this->assertEquals('second response', $this->response->getContent());
  621. $this->assertEquals(3, $count);
  622. }
  623. public function testPassesHeadRequestsThroughDirectlyOnPass()
  624. {
  625. $that = $this;
  626. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($that)
  627. {
  628. $response->setContent('');
  629. $response->setStatusCode(200);
  630. $that->assertEquals('HEAD', $request->getMethod());
  631. });
  632. $this->request('HEAD', '/', array('HTTP_EXPECT' => 'something ...'));
  633. $this->assertHttpKernelIsCalled();
  634. $this->assertEquals('', $this->response->getContent());
  635. }
  636. public function testUsesCacheToRespondToHeadRequestsWhenFresh()
  637. {
  638. $that = $this;
  639. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($that)
  640. {
  641. $response->headers->set('Cache-Control', 'public, max-age=10');
  642. $response->setContent('Hello World');
  643. $response->setStatusCode(200);
  644. $that->assertNotEquals('HEAD', $request->getMethod());
  645. });
  646. $this->request('GET', '/');
  647. $this->assertHttpKernelIsCalled();
  648. $this->assertEquals('Hello World', $this->response->getContent());
  649. $this->request('HEAD', '/');
  650. $this->assertHttpKernelIsNotCalled();
  651. $this->assertEquals(200, $this->response->getStatusCode());
  652. $this->assertEquals('', $this->response->getContent());
  653. $this->assertEquals(strlen('Hello World'), $this->response->headers->get('Content-Length'));
  654. }
  655. public function testInvalidatesCachedResponsesOnPost()
  656. {
  657. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response)
  658. {
  659. if ('GET' == $request->getMethod()) {
  660. $response->setStatusCode(200);
  661. $response->headers->set('Cache-Control', 'public, max-age=500');
  662. $response->setContent('Hello World');
  663. } elseif ('POST' == $request->getMethod()) {
  664. $response->setStatusCode(303);
  665. $response->headers->set('Location', '/');
  666. $response->headers->remove('Cache-Control');
  667. $response->setContent('');
  668. }
  669. });
  670. // build initial request to enter into the cache
  671. $this->request('GET', '/');
  672. $this->assertHttpKernelIsCalled();
  673. $this->assertEquals(200, $this->response->getStatusCode());
  674. $this->assertEquals('Hello World', $this->response->getContent());
  675. $this->assertTraceContains('miss');
  676. $this->assertTraceContains('store');
  677. // make sure it is valid
  678. $this->request('GET', '/');
  679. $this->assertHttpKernelIsNotCalled();
  680. $this->assertEquals(200, $this->response->getStatusCode());
  681. $this->assertEquals('Hello World', $this->response->getContent());
  682. $this->assertTraceContains('fresh');
  683. // now POST to same URL
  684. $this->request('POST', '/');
  685. $this->assertHttpKernelIsCalled();
  686. $this->assertEquals('/', $this->response->headers->get('Location'));
  687. $this->assertTraceContains('invalidate');
  688. $this->assertTraceContains('pass');
  689. $this->assertEquals('', $this->response->getContent());
  690. // now make sure it was actually invalidated
  691. $this->request('GET', '/');
  692. $this->assertHttpKernelIsCalled();
  693. $this->assertEquals(200, $this->response->getStatusCode());
  694. $this->assertEquals('Hello World', $this->response->getContent());
  695. $this->assertTraceContains('stale');
  696. $this->assertTraceContains('invalid');
  697. $this->assertTraceContains('store');
  698. }
  699. public function testServesFromCacheWhenHeadersMatch()
  700. {
  701. $count = 0;
  702. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count)
  703. {
  704. $response->headers->set('Vary', 'Accept User-Agent Foo');
  705. $response->headers->set('Cache-Control', 'public, max-age=10');
  706. $response->headers->set('X-Response-Count', ++$count);
  707. $response->setContent($request->headers->get('USER_AGENT'));
  708. });
  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('miss');
  713. $this->assertTraceContains('store');
  714. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  715. $this->assertEquals(200, $this->response->getStatusCode());
  716. $this->assertEquals('Bob/1.0', $this->response->getContent());
  717. $this->assertTraceContains('fresh');
  718. $this->assertTraceNotContains('store');
  719. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  720. }
  721. public function testStoresMultipleResponsesWhenHeadersDiffer()
  722. {
  723. $count = 0;
  724. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count)
  725. {
  726. $response->headers->set('Vary', 'Accept User-Agent Foo');
  727. $response->headers->set('Cache-Control', 'public, max-age=10');
  728. $response->headers->set('X-Response-Count', ++$count);
  729. $response->setContent($request->headers->get('USER_AGENT'));
  730. });
  731. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  732. $this->assertEquals(200, $this->response->getStatusCode());
  733. $this->assertEquals('Bob/1.0', $this->response->getContent());
  734. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  735. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  736. $this->assertEquals(200, $this->response->getStatusCode());
  737. $this->assertTraceContains('miss');
  738. $this->assertTraceContains('store');
  739. $this->assertEquals('Bob/2.0', $this->response->getContent());
  740. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  741. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  742. $this->assertTraceContains('fresh');
  743. $this->assertEquals('Bob/1.0', $this->response->getContent());
  744. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  745. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  746. $this->assertTraceContains('fresh');
  747. $this->assertEquals('Bob/2.0', $this->response->getContent());
  748. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  749. $this->request('GET', '/', array('HTTP_USER_AGENT' => 'Bob/2.0'));
  750. $this->assertTraceContains('miss');
  751. $this->assertEquals('Bob/2.0', $this->response->getContent());
  752. $this->assertEquals(3, $this->response->headers->get('X-Response-Count'));
  753. }
  754. public function testShouldCatchExceptions()
  755. {
  756. $this->catchExceptions();
  757. $this->setNextResponse();
  758. $this->request('GET', '/');
  759. $this->assertExceptionsAreCaught();
  760. }
  761. public function testShouldNotCatchExceptions()
  762. {
  763. $this->catchExceptions(false);
  764. $this->setNextResponse();
  765. $this->request('GET', '/');
  766. $this->assertExceptionsAreNotCaught();
  767. }
  768. public function testEsiCacheSendsTheLowestTtl()
  769. {
  770. $responses = array(
  771. array(
  772. 'status' => 200,
  773. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  774. 'headers' => array(
  775. 'Cache-Control' => 's-maxage=300',
  776. 'Surrogate-Control' => 'content="ESI/1.0"',
  777. ),
  778. ),
  779. array(
  780. 'status' => 200,
  781. 'body' => 'Hello World!',
  782. 'headers' => array('Cache-Control' => 's-maxage=300'),
  783. ),
  784. array(
  785. 'status' => 200,
  786. 'body' => 'My name is Bobby.',
  787. 'headers' => array('Cache-Control' => 's-maxage=100'),
  788. ),
  789. );
  790. $this->setNextResponses($responses);
  791. $this->request('GET', '/', array(), array(), true);
  792. $this->assertEquals("Hello World! My name is Bobby.", $this->response->getContent());
  793. $this->assertEquals(100, $this->response->getTtl());
  794. }
  795. public function testEsiCacheForceValidation()
  796. {
  797. $responses = array(
  798. array(
  799. 'status' => 200,
  800. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  801. 'headers' => array(
  802. 'Cache-Control' => 's-maxage=300',
  803. 'Surrogate-Control' => 'content="ESI/1.0"',
  804. ),
  805. ),
  806. array(
  807. 'status' => 200,
  808. 'body' => 'Hello World!',
  809. 'headers' => array('ETag' => 'foobar'),
  810. ),
  811. array(
  812. 'status' => 200,
  813. 'body' => 'My name is Bobby.',
  814. 'headers' => array('Cache-Control' => 's-maxage=100'),
  815. ),
  816. );
  817. $this->setNextResponses($responses);
  818. $this->request('GET', '/', array(), array(), true);
  819. $this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
  820. $this->assertEquals(null, $this->response->getTtl());
  821. $this->assertTrue($this->response->mustRevalidate());
  822. $this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
  823. $this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
  824. }
  825. }