HttpCacheTest.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\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->assertEquals('text/html; charset=UTF-8', $this->response->headers->get('Content-Type'));
  86. $this->assertEmpty($this->response->getContent());
  87. $this->assertTraceContains('miss');
  88. $this->assertTraceContains('store');
  89. }
  90. public function testRespondsWith304WhenIfNoneMatchMatchesETag()
  91. {
  92. $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '12345', 'Content-Type' => 'text/plain'), 'Hello World');
  93. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345'));
  94. $this->assertHttpKernelIsCalled();
  95. $this->assertEquals(304, $this->response->getStatusCode());
  96. $this->assertEquals('text/html; charset=UTF-8', $this->response->headers->get('Content-Type'));
  97. $this->assertTrue($this->response->headers->has('ETag'));
  98. $this->assertEmpty($this->response->getContent());
  99. $this->assertTraceContains('miss');
  100. $this->assertTraceContains('store');
  101. }
  102. public function testRespondsWith304OnlyIfIfNoneMatchAndIfModifiedSinceBothMatch()
  103. {
  104. $time = new \DateTime();
  105. $this->setNextResponse(200, array(), '', function ($request, $response) use ($time)
  106. {
  107. $response->setStatusCode(200);
  108. $response->headers->set('ETag', '12345');
  109. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  110. $response->headers->set('Content-Type', 'text/plain');
  111. $response->setContent('Hello World');
  112. });
  113. // only ETag matches
  114. $t = \DateTime::createFromFormat('U', time() - 3600);
  115. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => $t->format(DATE_RFC2822)));
  116. $this->assertHttpKernelIsCalled();
  117. $this->assertEquals(200, $this->response->getStatusCode());
  118. // only Last-Modified matches
  119. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '1234', 'HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  120. $this->assertHttpKernelIsCalled();
  121. $this->assertEquals(200, $this->response->getStatusCode());
  122. // Both matches
  123. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  124. $this->assertHttpKernelIsCalled();
  125. $this->assertEquals(304, $this->response->getStatusCode());
  126. }
  127. public function testValidatesPrivateResponsesCachedOnTheClient()
  128. {
  129. $this->setNextResponse(200, array(), '', function ($request, $response)
  130. {
  131. $etags = preg_split('/\s*,\s*/', $request->headers->get('IF_NONE_MATCH'));
  132. if ($request->cookies->has('authenticated')) {
  133. $response->headers->set('Cache-Control', 'private, no-store');
  134. $response->setETag('"private tag"');
  135. if (in_array('"private tag"', $etags)) {
  136. $response->setStatusCode(304);
  137. } else {
  138. $response->setStatusCode(200);
  139. $response->headers->set('Content-Type', 'text/plain');
  140. $response->setContent('private data');
  141. }
  142. } else {
  143. $response->headers->set('Cache-Control', 'public');
  144. $response->setETag('"public tag"');
  145. if (in_array('"public tag"', $etags)) {
  146. $response->setStatusCode(304);
  147. } else {
  148. $response->setStatusCode(200);
  149. $response->headers->set('Content-Type', 'text/plain');
  150. $response->setContent('public data');
  151. }
  152. }
  153. });
  154. $this->request('GET', '/');
  155. $this->assertHttpKernelIsCalled();
  156. $this->assertEquals(200, $this->response->getStatusCode());
  157. $this->assertEquals('"public tag"', $this->response->headers->get('ETag'));
  158. $this->assertEquals('public data', $this->response->getContent());
  159. $this->assertTraceContains('miss');
  160. $this->assertTraceContains('store');
  161. $this->request('GET', '/', array(), array('authenticated' => ''));
  162. $this->assertHttpKernelIsCalled();
  163. $this->assertEquals(200, $this->response->getStatusCode());
  164. $this->assertEquals('"private tag"', $this->response->headers->get('ETag'));
  165. $this->assertEquals('private data', $this->response->getContent());
  166. $this->assertTraceContains('stale');
  167. $this->assertTraceContains('invalid');
  168. $this->assertTraceNotContains('store');
  169. }
  170. public function testStoresResponsesWhenNoCacheRequestDirectivePresent()
  171. {
  172. $time = \DateTime::createFromFormat('U', time() + 5);
  173. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  174. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  175. $this->assertHttpKernelIsCalled();
  176. $this->assertTraceContains('store');
  177. $this->assertTrue($this->response->headers->has('Age'));
  178. }
  179. public function testReloadsResponsesWhenCacheHitsButNoCacheRequestDirectivePresentWhenAllowReloadIsSetTrue()
  180. {
  181. $count = 0;
  182. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=10000'), '', function ($request, $response) use (&$count)
  183. {
  184. ++$count;
  185. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  186. });
  187. $this->request('GET', '/');
  188. $this->assertEquals(200, $this->response->getStatusCode());
  189. $this->assertEquals('Hello World', $this->response->getContent());
  190. $this->assertTraceContains('store');
  191. $this->request('GET', '/');
  192. $this->assertEquals(200, $this->response->getStatusCode());
  193. $this->assertEquals('Hello World', $this->response->getContent());
  194. $this->assertTraceContains('fresh');
  195. $this->cacheConfig['allow_reload'] = true;
  196. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  197. $this->assertEquals(200, $this->response->getStatusCode());
  198. $this->assertEquals('Goodbye World', $this->response->getContent());
  199. $this->assertTraceContains('reload');
  200. $this->assertTraceContains('store');
  201. }
  202. public function testDoesNotReloadResponsesWhenAllowReloadIsSetFalseDefault()
  203. {
  204. $count = 0;
  205. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=10000'), '', function ($request, $response) use (&$count)
  206. {
  207. ++$count;
  208. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  209. });
  210. $this->request('GET', '/');
  211. $this->assertEquals(200, $this->response->getStatusCode());
  212. $this->assertEquals('Hello World', $this->response->getContent());
  213. $this->assertTraceContains('store');
  214. $this->request('GET', '/');
  215. $this->assertEquals(200, $this->response->getStatusCode());
  216. $this->assertEquals('Hello World', $this->response->getContent());
  217. $this->assertTraceContains('fresh');
  218. $this->cacheConfig['allow_reload'] = false;
  219. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  220. $this->assertEquals(200, $this->response->getStatusCode());
  221. $this->assertEquals('Hello World', $this->response->getContent());
  222. $this->assertTraceNotContains('reload');
  223. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  224. $this->assertEquals(200, $this->response->getStatusCode());
  225. $this->assertEquals('Hello World', $this->response->getContent());
  226. $this->assertTraceNotContains('reload');
  227. }
  228. public function testRevalidatesFreshCacheEntryWhenMaxAgeRequestDirectiveIsExceededWhenAllowRevalidateOptionIsSetTrue()
  229. {
  230. $count = 0;
  231. $this->setNextResponse(200, array(), '', function ($request, $response) use (&$count)
  232. {
  233. ++$count;
  234. $response->headers->set('Cache-Control', 'public, max-age=10000');
  235. $response->setETag($count);
  236. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  237. });
  238. $this->request('GET', '/');
  239. $this->assertEquals(200, $this->response->getStatusCode());
  240. $this->assertEquals('Hello World', $this->response->getContent());
  241. $this->assertTraceContains('store');
  242. $this->request('GET', '/');
  243. $this->assertEquals(200, $this->response->getStatusCode());
  244. $this->assertEquals('Hello World', $this->response->getContent());
  245. $this->assertTraceContains('fresh');
  246. $this->cacheConfig['allow_revalidate'] = true;
  247. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  248. $this->assertEquals(200, $this->response->getStatusCode());
  249. $this->assertEquals('Goodbye World', $this->response->getContent());
  250. $this->assertTraceContains('stale');
  251. $this->assertTraceContains('invalid');
  252. $this->assertTraceContains('store');
  253. }
  254. public function testDoesNotRevalidateFreshCacheEntryWhenEnableRevalidateOptionIsSetFalseDefault()
  255. {
  256. $count = 0;
  257. $this->setNextResponse(200, array(), '', function ($request, $response) use (&$count)
  258. {
  259. ++$count;
  260. $response->headers->set('Cache-Control', 'public, max-age=10000');
  261. $response->setETag($count);
  262. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  263. });
  264. $this->request('GET', '/');
  265. $this->assertEquals(200, $this->response->getStatusCode());
  266. $this->assertEquals('Hello World', $this->response->getContent());
  267. $this->assertTraceContains('store');
  268. $this->request('GET', '/');
  269. $this->assertEquals(200, $this->response->getStatusCode());
  270. $this->assertEquals('Hello World', $this->response->getContent());
  271. $this->assertTraceContains('fresh');
  272. $this->cacheConfig['allow_revalidate'] = false;
  273. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  274. $this->assertEquals(200, $this->response->getStatusCode());
  275. $this->assertEquals('Hello World', $this->response->getContent());
  276. $this->assertTraceNotContains('stale');
  277. $this->assertTraceNotContains('invalid');
  278. $this->assertTraceContains('fresh');
  279. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  280. $this->assertEquals(200, $this->response->getStatusCode());
  281. $this->assertEquals('Hello World', $this->response->getContent());
  282. $this->assertTraceNotContains('stale');
  283. $this->assertTraceNotContains('invalid');
  284. $this->assertTraceContains('fresh');
  285. }
  286. public function testFetchesResponseFromBackendWhenCacheMisses()
  287. {
  288. $time = \DateTime::createFromFormat('U', time() + 5);
  289. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  290. $this->request('GET', '/');
  291. $this->assertEquals(200, $this->response->getStatusCode());
  292. $this->assertTraceContains('miss');
  293. $this->assertTrue($this->response->headers->has('Age'));
  294. }
  295. public function testDoesNotCacheSomeStatusCodeResponses()
  296. {
  297. 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) {
  298. $time = \DateTime::createFromFormat('U', time() + 5);
  299. $this->setNextResponse($code, array('Expires' => $time->format(DATE_RFC2822)));
  300. $this->request('GET', '/');
  301. $this->assertEquals($code, $this->response->getStatusCode());
  302. $this->assertTraceNotContains('store');
  303. $this->assertFalse($this->response->headers->has('Age'));
  304. }
  305. }
  306. public function testDoesNotCacheResponsesWithExplicitNoStoreDirective()
  307. {
  308. $time = \DateTime::createFromFormat('U', time() + 5);
  309. $this->setNextResponse(200, array('Expires' => $time->format(DATE_RFC2822), 'Cache-Control' => 'no-store'));
  310. $this->request('GET', '/');
  311. $this->assertTraceNotContains('store');
  312. $this->assertFalse($this->response->headers->has('Age'));
  313. }
  314. public function testDoesNotCacheResponsesWithoutFreshnessInformationOrAValidator()
  315. {
  316. $this->setNextResponse();
  317. $this->request('GET', '/');
  318. $this->assertEquals(200, $this->response->getStatusCode());
  319. $this->assertTraceNotContains('store');
  320. }
  321. public function testCachesResponsesWithExplicitNoCacheDirective()
  322. {
  323. $time = \DateTime::createFromFormat('U', time() + 5);
  324. $this->setNextResponse(200, array('Expires' => $time->format(DATE_RFC2822), 'Cache-Control' => 'public, no-cache'));
  325. $this->request('GET', '/');
  326. $this->assertTraceContains('store');
  327. $this->assertTrue($this->response->headers->has('Age'));
  328. }
  329. public function testCachesResponsesWithAnExpirationHeader()
  330. {
  331. $time = \DateTime::createFromFormat('U', time() + 5);
  332. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  333. $this->request('GET', '/');
  334. $this->assertEquals(200, $this->response->getStatusCode());
  335. $this->assertEquals('Hello World', $this->response->getContent());
  336. $this->assertNotNull($this->response->headers->get('Date'));
  337. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  338. $this->assertTraceContains('miss');
  339. $this->assertTraceContains('store');
  340. $values = $this->getMetaStorageValues();
  341. $this->assertEquals(1, count($values));
  342. }
  343. public function testCachesResponsesWithAMaxAgeDirective()
  344. {
  345. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=5'));
  346. $this->request('GET', '/');
  347. $this->assertEquals(200, $this->response->getStatusCode());
  348. $this->assertEquals('Hello World', $this->response->getContent());
  349. $this->assertNotNull($this->response->headers->get('Date'));
  350. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  351. $this->assertTraceContains('miss');
  352. $this->assertTraceContains('store');
  353. $values = $this->getMetaStorageValues();
  354. $this->assertEquals(1, count($values));
  355. }
  356. public function testCachesResponsesWithASMaxAgeDirective()
  357. {
  358. $this->setNextResponse(200, array('Cache-Control' => 's-maxage=5'));
  359. $this->request('GET', '/');
  360. $this->assertEquals(200, $this->response->getStatusCode());
  361. $this->assertEquals('Hello World', $this->response->getContent());
  362. $this->assertNotNull($this->response->headers->get('Date'));
  363. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  364. $this->assertTraceContains('miss');
  365. $this->assertTraceContains('store');
  366. $values = $this->getMetaStorageValues();
  367. $this->assertEquals(1, count($values));
  368. }
  369. public function testCachesResponsesWithALastModifiedValidatorButNoFreshnessInformation()
  370. {
  371. $time = \DateTime::createFromFormat('U', time());
  372. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Last-Modified' => $time->format(DATE_RFC2822)));
  373. $this->request('GET', '/');
  374. $this->assertEquals(200, $this->response->getStatusCode());
  375. $this->assertEquals('Hello World', $this->response->getContent());
  376. $this->assertTraceContains('miss');
  377. $this->assertTraceContains('store');
  378. }
  379. public function testCachesResponsesWithAnETagValidatorButNoFreshnessInformation()
  380. {
  381. $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '"123456"'));
  382. $this->request('GET', '/');
  383. $this->assertEquals(200, $this->response->getStatusCode());
  384. $this->assertEquals('Hello World', $this->response->getContent());
  385. $this->assertTraceContains('miss');
  386. $this->assertTraceContains('store');
  387. }
  388. public function testHitsCachedResponsesWithExpiresHeader()
  389. {
  390. $time1 = \DateTime::createFromFormat('U', time() - 5);
  391. $time2 = \DateTime::createFromFormat('U', time() + 5);
  392. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Date' => $time1->format(DATE_RFC2822), 'Expires' => $time2->format(DATE_RFC2822)));
  393. $this->request('GET', '/');
  394. $this->assertHttpKernelIsCalled();
  395. $this->assertEquals(200, $this->response->getStatusCode());
  396. $this->assertNotNull($this->response->headers->get('Date'));
  397. $this->assertTraceContains('miss');
  398. $this->assertTraceContains('store');
  399. $this->assertEquals('Hello World', $this->response->getContent());
  400. $this->request('GET', '/');
  401. $this->assertHttpKernelIsNotCalled();
  402. $this->assertEquals(200, $this->response->getStatusCode());
  403. $this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
  404. $this->assertTrue($this->response->headers->get('Age') > 0);
  405. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  406. $this->assertTraceContains('fresh');
  407. $this->assertTraceNotContains('store');
  408. $this->assertEquals('Hello World', $this->response->getContent());
  409. }
  410. public function testHitsCachedResponseWithMaxAgeDirective()
  411. {
  412. $time = \DateTime::createFromFormat('U', time() - 5);
  413. $this->setNextResponse(200, array('Date' => $time->format(DATE_RFC2822), 'Cache-Control' => 'public, max-age=10'));
  414. $this->request('GET', '/');
  415. $this->assertHttpKernelIsCalled();
  416. $this->assertEquals(200, $this->response->getStatusCode());
  417. $this->assertNotNull($this->response->headers->get('Date'));
  418. $this->assertTraceContains('miss');
  419. $this->assertTraceContains('store');
  420. $this->assertEquals('Hello World', $this->response->getContent());
  421. $this->request('GET', '/');
  422. $this->assertHttpKernelIsNotCalled();
  423. $this->assertEquals(200, $this->response->getStatusCode());
  424. $this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
  425. $this->assertTrue($this->response->headers->get('Age') > 0);
  426. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  427. $this->assertTraceContains('fresh');
  428. $this->assertTraceNotContains('store');
  429. $this->assertEquals('Hello World', $this->response->getContent());
  430. }
  431. public function testHitsCachedResponseWithSMaxAgeDirective()
  432. {
  433. $time = \DateTime::createFromFormat('U', time() - 5);
  434. $this->setNextResponse(200, array('Date' => $time->format(DATE_RFC2822), 'Cache-Control' => 's-maxage=10, max-age=0'));
  435. $this->request('GET', '/');
  436. $this->assertHttpKernelIsCalled();
  437. $this->assertEquals(200, $this->response->getStatusCode());
  438. $this->assertNotNull($this->response->headers->get('Date'));
  439. $this->assertTraceContains('miss');
  440. $this->assertTraceContains('store');
  441. $this->assertEquals('Hello World', $this->response->getContent());
  442. $this->request('GET', '/');
  443. $this->assertHttpKernelIsNotCalled();
  444. $this->assertEquals(200, $this->response->getStatusCode());
  445. $this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
  446. $this->assertTrue($this->response->headers->get('Age') > 0);
  447. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  448. $this->assertTraceContains('fresh');
  449. $this->assertTraceNotContains('store');
  450. $this->assertEquals('Hello World', $this->response->getContent());
  451. }
  452. public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformation()
  453. {
  454. $this->setNextResponse();
  455. $this->cacheConfig['default_ttl'] = 10;
  456. $this->request('GET', '/');
  457. $this->assertHttpKernelIsCalled();
  458. $this->assertTraceContains('miss');
  459. $this->assertTraceContains('store');
  460. $this->assertEquals('Hello World', $this->response->getContent());
  461. $this->assertRegExp('/s-maxage=10/', $this->response->headers->get('Cache-Control'));
  462. $this->cacheConfig['default_ttl'] = 10;
  463. $this->request('GET', '/');
  464. $this->assertHttpKernelIsNotCalled();
  465. $this->assertEquals(200, $this->response->getStatusCode());
  466. $this->assertTraceContains('fresh');
  467. $this->assertTraceNotContains('store');
  468. $this->assertEquals('Hello World', $this->response->getContent());
  469. }
  470. public function testDoesNotAssignDefaultTtlWhenResponseHasMustRevalidateDirective()
  471. {
  472. $this->setNextResponse(200, array('Cache-Control' => 'must-revalidate'));
  473. $this->cacheConfig['default_ttl'] = 10;
  474. $this->request('GET', '/');
  475. $this->assertHttpKernelIsCalled();
  476. $this->assertEquals(200, $this->response->getStatusCode());
  477. $this->assertTraceContains('miss');
  478. $this->assertTraceNotContains('store');
  479. $this->assertNotRegExp('/s-maxage/', $this->response->headers->get('Cache-Control'));
  480. $this->assertEquals('Hello World', $this->response->getContent());
  481. }
  482. public function testFetchesFullResponseWhenCacheStaleAndNoValidatorsPresent()
  483. {
  484. $time = \DateTime::createFromFormat('U', time() + 5);
  485. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  486. // build initial request
  487. $this->request('GET', '/');
  488. $this->assertHttpKernelIsCalled();
  489. $this->assertEquals(200, $this->response->getStatusCode());
  490. $this->assertNotNull($this->response->headers->get('Date'));
  491. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  492. $this->assertNotNull($this->response->headers->get('Age'));
  493. $this->assertTraceContains('miss');
  494. $this->assertTraceContains('store');
  495. $this->assertEquals('Hello World', $this->response->getContent());
  496. # go in and play around with the cached metadata directly ...
  497. $values = $this->getMetaStorageValues();
  498. $this->assertEquals(1, count($values));
  499. $tmp = unserialize($values[0]);
  500. $time = \DateTime::createFromFormat('U', time());
  501. $tmp[0][1]['expires'] = $time->format(DATE_RFC2822);
  502. $r = new \ReflectionObject($this->store);
  503. $m = $r->getMethod('save');
  504. $m->setAccessible(true);
  505. $m->invoke($this->store, 'md'.sha1('http://localhost/'), serialize($tmp));
  506. // build subsequent request; should be found but miss due to freshness
  507. $this->request('GET', '/');
  508. $this->assertHttpKernelIsCalled();
  509. $this->assertEquals(200, $this->response->getStatusCode());
  510. $this->assertTrue($this->response->headers->get('Age') <= 1);
  511. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  512. $this->assertTraceContains('stale');
  513. $this->assertTraceNotContains('fresh');
  514. $this->assertTraceNotContains('miss');
  515. $this->assertTraceContains('store');
  516. $this->assertEquals('Hello World', $this->response->getContent());
  517. }
  518. public function testValidatesCachedResponsesWithLastModifiedAndNoFreshnessInformation()
  519. {
  520. $time = \DateTime::createFromFormat('U', time());
  521. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time)
  522. {
  523. $response->headers->set('Cache-Control', 'public');
  524. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  525. if ($time->format(DATE_RFC2822) == $request->headers->get('IF_MODIFIED_SINCE')) {
  526. $response->setStatusCode(304);
  527. $response->setContent('');
  528. }
  529. });
  530. // build initial request
  531. $this->request('GET', '/');
  532. $this->assertHttpKernelIsCalled();
  533. $this->assertEquals(200, $this->response->getStatusCode());
  534. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  535. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  536. $this->assertEquals('Hello World', $this->response->getContent());
  537. $this->assertTraceContains('miss');
  538. $this->assertTraceContains('store');
  539. $this->assertTraceNotContains('stale');
  540. // build subsequent request; should be found but miss due to freshness
  541. $this->request('GET', '/');
  542. $this->assertHttpKernelIsCalled();
  543. $this->assertEquals(200, $this->response->getStatusCode());
  544. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  545. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  546. $this->assertTrue($this->response->headers->get('Age') <= 1);
  547. $this->assertEquals('Hello World', $this->response->getContent());
  548. $this->assertTraceContains('stale');
  549. $this->assertTraceContains('valid');
  550. $this->assertTraceContains('store');
  551. $this->assertTraceNotContains('miss');
  552. }
  553. public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
  554. {
  555. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response)
  556. {
  557. $response->headers->set('Cache-Control', 'public');
  558. $response->headers->set('ETag', '"12345"');
  559. if ($response->getETag() == $request->headers->get('IF_NONE_MATCH')) {
  560. $response->setStatusCode(304);
  561. $response->setContent('');
  562. }
  563. });
  564. // build initial request
  565. $this->request('GET', '/');
  566. $this->assertHttpKernelIsCalled();
  567. $this->assertEquals(200, $this->response->getStatusCode());
  568. $this->assertNotNull($this->response->headers->get('ETag'));
  569. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  570. $this->assertEquals('Hello World', $this->response->getContent());
  571. $this->assertTraceContains('miss');
  572. $this->assertTraceContains('store');
  573. // build subsequent request; should be found but miss due to freshness
  574. $this->request('GET', '/');
  575. $this->assertHttpKernelIsCalled();
  576. $this->assertEquals(200, $this->response->getStatusCode());
  577. $this->assertNotNull($this->response->headers->get('ETag'));
  578. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  579. $this->assertTrue($this->response->headers->get('Age') <= 1);
  580. $this->assertEquals('Hello World', $this->response->getContent());
  581. $this->assertTraceContains('stale');
  582. $this->assertTraceContains('valid');
  583. $this->assertTraceContains('store');
  584. $this->assertTraceNotContains('miss');
  585. }
  586. public function testReplacesCachedResponsesWhenValidationResultsInNon304Response()
  587. {
  588. $time = \DateTime::createFromFormat('U', time());
  589. $count = 0;
  590. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time, &$count)
  591. {
  592. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  593. $response->headers->set('Cache-Control', 'public');
  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 testSendsNoContentWhenFresh()
  654. {
  655. $time = \DateTime::createFromFormat('U', time());
  656. $that = $this;
  657. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($that, $time)
  658. {
  659. $response->headers->set('Cache-Control', 'public, max-age=10');
  660. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  661. });
  662. $this->request('GET', '/');
  663. $this->assertHttpKernelIsCalled();
  664. $this->assertEquals('Hello World', $this->response->getContent());
  665. $this->request('GET', '/', array('HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  666. $this->assertHttpKernelIsNotCalled();
  667. $this->assertEquals(304, $this->response->getStatusCode());
  668. $this->assertEquals('', $this->response->getContent());
  669. }
  670. public function testInvalidatesCachedResponsesOnPost()
  671. {
  672. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response)
  673. {
  674. if ('GET' == $request->getMethod()) {
  675. $response->setStatusCode(200);
  676. $response->headers->set('Cache-Control', 'public, max-age=500');
  677. $response->setContent('Hello World');
  678. } elseif ('POST' == $request->getMethod()) {
  679. $response->setStatusCode(303);
  680. $response->headers->set('Location', '/');
  681. $response->headers->remove('Cache-Control');
  682. $response->setContent('');
  683. }
  684. });
  685. // build initial request to enter into the cache
  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('miss');
  691. $this->assertTraceContains('store');
  692. // make sure it is valid
  693. $this->request('GET', '/');
  694. $this->assertHttpKernelIsNotCalled();
  695. $this->assertEquals(200, $this->response->getStatusCode());
  696. $this->assertEquals('Hello World', $this->response->getContent());
  697. $this->assertTraceContains('fresh');
  698. // now POST to same URL
  699. $this->request('POST', '/');
  700. $this->assertHttpKernelIsCalled();
  701. $this->assertEquals('/', $this->response->headers->get('Location'));
  702. $this->assertTraceContains('invalidate');
  703. $this->assertTraceContains('pass');
  704. $this->assertEquals('', $this->response->getContent());
  705. // now make sure it was actually invalidated
  706. $this->request('GET', '/');
  707. $this->assertHttpKernelIsCalled();
  708. $this->assertEquals(200, $this->response->getStatusCode());
  709. $this->assertEquals('Hello World', $this->response->getContent());
  710. $this->assertTraceContains('stale');
  711. $this->assertTraceContains('invalid');
  712. $this->assertTraceContains('store');
  713. }
  714. public function testServesFromCacheWhenHeadersMatch()
  715. {
  716. $count = 0;
  717. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count)
  718. {
  719. $response->headers->set('Vary', 'Accept User-Agent Foo');
  720. $response->headers->set('Cache-Control', 'public, max-age=10');
  721. $response->headers->set('X-Response-Count', ++$count);
  722. $response->setContent($request->headers->get('USER_AGENT'));
  723. });
  724. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  725. $this->assertEquals(200, $this->response->getStatusCode());
  726. $this->assertEquals('Bob/1.0', $this->response->getContent());
  727. $this->assertTraceContains('miss');
  728. $this->assertTraceContains('store');
  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->assertTraceContains('fresh');
  733. $this->assertTraceNotContains('store');
  734. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  735. }
  736. public function testStoresMultipleResponsesWhenHeadersDiffer()
  737. {
  738. $count = 0;
  739. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count)
  740. {
  741. $response->headers->set('Vary', 'Accept User-Agent Foo');
  742. $response->headers->set('Cache-Control', 'public, max-age=10');
  743. $response->headers->set('X-Response-Count', ++$count);
  744. $response->setContent($request->headers->get('USER_AGENT'));
  745. });
  746. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  747. $this->assertEquals(200, $this->response->getStatusCode());
  748. $this->assertEquals('Bob/1.0', $this->response->getContent());
  749. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  750. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  751. $this->assertEquals(200, $this->response->getStatusCode());
  752. $this->assertTraceContains('miss');
  753. $this->assertTraceContains('store');
  754. $this->assertEquals('Bob/2.0', $this->response->getContent());
  755. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  756. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  757. $this->assertTraceContains('fresh');
  758. $this->assertEquals('Bob/1.0', $this->response->getContent());
  759. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  760. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  761. $this->assertTraceContains('fresh');
  762. $this->assertEquals('Bob/2.0', $this->response->getContent());
  763. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  764. $this->request('GET', '/', array('HTTP_USER_AGENT' => 'Bob/2.0'));
  765. $this->assertTraceContains('miss');
  766. $this->assertEquals('Bob/2.0', $this->response->getContent());
  767. $this->assertEquals(3, $this->response->headers->get('X-Response-Count'));
  768. }
  769. public function testShouldCatchExceptions()
  770. {
  771. $this->catchExceptions();
  772. $this->setNextResponse();
  773. $this->request('GET', '/');
  774. $this->assertExceptionsAreCaught();
  775. }
  776. public function testShouldNotCatchExceptions()
  777. {
  778. $this->catchExceptions(false);
  779. $this->setNextResponse();
  780. $this->request('GET', '/');
  781. $this->assertExceptionsAreNotCaught();
  782. }
  783. public function testEsiCacheSendsTheLowestTtl()
  784. {
  785. $responses = array(
  786. array(
  787. 'status' => 200,
  788. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  789. 'headers' => array(
  790. 'Cache-Control' => 's-maxage=300',
  791. 'Surrogate-Control' => 'content="ESI/1.0"',
  792. ),
  793. ),
  794. array(
  795. 'status' => 200,
  796. 'body' => 'Hello World!',
  797. 'headers' => array('Cache-Control' => 's-maxage=300'),
  798. ),
  799. array(
  800. 'status' => 200,
  801. 'body' => 'My name is Bobby.',
  802. 'headers' => array('Cache-Control' => 's-maxage=100'),
  803. ),
  804. );
  805. $this->setNextResponses($responses);
  806. $this->request('GET', '/', array(), array(), true);
  807. $this->assertEquals("Hello World! My name is Bobby.", $this->response->getContent());
  808. // check for 100 or 99 as the test can be executed after a second change
  809. $this->assertTrue(in_array($this->response->getTtl(), array(99, 100)));
  810. }
  811. public function testEsiCacheForceValidation()
  812. {
  813. $responses = array(
  814. array(
  815. 'status' => 200,
  816. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  817. 'headers' => array(
  818. 'Cache-Control' => 's-maxage=300',
  819. 'Surrogate-Control' => 'content="ESI/1.0"',
  820. ),
  821. ),
  822. array(
  823. 'status' => 200,
  824. 'body' => 'Hello World!',
  825. 'headers' => array('ETag' => 'foobar'),
  826. ),
  827. array(
  828. 'status' => 200,
  829. 'body' => 'My name is Bobby.',
  830. 'headers' => array('Cache-Control' => 's-maxage=100'),
  831. ),
  832. );
  833. $this->setNextResponses($responses);
  834. $this->request('GET', '/', array(), array(), true);
  835. $this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
  836. $this->assertEquals(null, $this->response->getTtl());
  837. $this->assertTrue($this->response->mustRevalidate());
  838. $this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
  839. $this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
  840. }
  841. public function testEsiRecalculateContentLengthHeader()
  842. {
  843. $responses = array(
  844. array(
  845. 'status' => 200,
  846. 'body' => '<esi:include src="/foo" />',
  847. 'headers' => array(
  848. 'Content-Length' => 26,
  849. 'Cache-Control' => 's-maxage=300',
  850. 'Surrogate-Control' => 'content="ESI/1.0"',
  851. ),
  852. ),
  853. array(
  854. 'status' => 200,
  855. 'body' => 'Hello World!',
  856. 'headers' => array(),
  857. ),
  858. );
  859. $this->setNextResponses($responses);
  860. $this->request('GET', '/', array(), array(), true);
  861. $this->assertEquals('Hello World!', $this->response->getContent());
  862. $this->assertEquals(12, $this->response->headers->get('Content-Length'));
  863. }
  864. }