No Description

HttpCacheTest.php 52KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests\HttpCache;
  11. use Symfony\Component\HttpKernel\HttpCache\HttpCache;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class HttpCacheTest extends HttpCacheTestCase
  15. {
  16. public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
  17. {
  18. $storeMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\HttpCache\\StoreInterface')
  19. ->disableOriginalConstructor()
  20. ->getMock();
  21. // does not implement TerminableInterface
  22. $kernelMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\HttpKernelInterface')
  23. ->disableOriginalConstructor()
  24. ->getMock();
  25. $kernelMock->expects($this->never())
  26. ->method('terminate');
  27. $kernel = new HttpCache($kernelMock, $storeMock);
  28. $kernel->terminate(Request::create('/'), new Response());
  29. // implements TerminableInterface
  30. $kernelMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Kernel')
  31. ->disableOriginalConstructor()
  32. ->setMethods(array('terminate', 'registerBundles', 'registerContainerConfiguration'))
  33. ->getMock();
  34. $kernelMock->expects($this->once())
  35. ->method('terminate');
  36. $kernel = new HttpCache($kernelMock, $storeMock);
  37. $kernel->terminate(Request::create('/'), new Response());
  38. }
  39. public function testPassesOnNonGetHeadRequests()
  40. {
  41. $this->setNextResponse(200);
  42. $this->request('POST', '/');
  43. $this->assertHttpKernelIsCalled();
  44. $this->assertResponseOk();
  45. $this->assertTraceContains('pass');
  46. $this->assertFalse($this->response->headers->has('Age'));
  47. }
  48. public function testInvalidatesOnPostPutDeleteRequests()
  49. {
  50. foreach (array('post', 'put', 'delete') as $method) {
  51. $this->setNextResponse(200);
  52. $this->request($method, '/');
  53. $this->assertHttpKernelIsCalled();
  54. $this->assertResponseOk();
  55. $this->assertTraceContains('invalidate');
  56. $this->assertTraceContains('pass');
  57. }
  58. }
  59. public function testDoesNotCacheWithAuthorizationRequestHeaderAndNonPublicResponse()
  60. {
  61. $this->setNextResponse(200, array('ETag' => '"Foo"'));
  62. $this->request('GET', '/', array('HTTP_AUTHORIZATION' => 'basic foobarbaz'));
  63. $this->assertHttpKernelIsCalled();
  64. $this->assertResponseOk();
  65. $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
  66. $this->assertTraceContains('miss');
  67. $this->assertTraceNotContains('store');
  68. $this->assertFalse($this->response->headers->has('Age'));
  69. }
  70. public function testDoesCacheWithAuthorizationRequestHeaderAndPublicResponse()
  71. {
  72. $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '"Foo"'));
  73. $this->request('GET', '/', array('HTTP_AUTHORIZATION' => 'basic foobarbaz'));
  74. $this->assertHttpKernelIsCalled();
  75. $this->assertResponseOk();
  76. $this->assertTraceContains('miss');
  77. $this->assertTraceContains('store');
  78. $this->assertTrue($this->response->headers->has('Age'));
  79. $this->assertEquals('public', $this->response->headers->get('Cache-Control'));
  80. }
  81. public function testDoesNotCacheWithCookieHeaderAndNonPublicResponse()
  82. {
  83. $this->setNextResponse(200, array('ETag' => '"Foo"'));
  84. $this->request('GET', '/', array(), array('foo' => 'bar'));
  85. $this->assertHttpKernelIsCalled();
  86. $this->assertResponseOk();
  87. $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
  88. $this->assertTraceContains('miss');
  89. $this->assertTraceNotContains('store');
  90. $this->assertFalse($this->response->headers->has('Age'));
  91. }
  92. public function testDoesNotCacheRequestsWithACookieHeader()
  93. {
  94. $this->setNextResponse(200);
  95. $this->request('GET', '/', array(), array('foo' => 'bar'));
  96. $this->assertHttpKernelIsCalled();
  97. $this->assertResponseOk();
  98. $this->assertEquals('private', $this->response->headers->get('Cache-Control'));
  99. $this->assertTraceContains('miss');
  100. $this->assertTraceNotContains('store');
  101. $this->assertFalse($this->response->headers->has('Age'));
  102. }
  103. public function testRespondsWith304WhenIfModifiedSinceMatchesLastModified()
  104. {
  105. $time = new \DateTime();
  106. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Last-Modified' => $time->format(DATE_RFC2822), 'Content-Type' => 'text/plain'), 'Hello World');
  107. $this->request('GET', '/', array('HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  108. $this->assertHttpKernelIsCalled();
  109. $this->assertEquals(304, $this->response->getStatusCode());
  110. $this->assertEquals('', $this->response->headers->get('Content-Type'));
  111. $this->assertEmpty($this->response->getContent());
  112. $this->assertTraceContains('miss');
  113. $this->assertTraceContains('store');
  114. }
  115. public function testRespondsWith304WhenIfNoneMatchMatchesETag()
  116. {
  117. $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '12345', 'Content-Type' => 'text/plain'), 'Hello World');
  118. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345'));
  119. $this->assertHttpKernelIsCalled();
  120. $this->assertEquals(304, $this->response->getStatusCode());
  121. $this->assertEquals('', $this->response->headers->get('Content-Type'));
  122. $this->assertTrue($this->response->headers->has('ETag'));
  123. $this->assertEmpty($this->response->getContent());
  124. $this->assertTraceContains('miss');
  125. $this->assertTraceContains('store');
  126. }
  127. public function testRespondsWith304OnlyIfIfNoneMatchAndIfModifiedSinceBothMatch()
  128. {
  129. $time = new \DateTime();
  130. $this->setNextResponse(200, array(), '', function ($request, $response) use ($time) {
  131. $response->setStatusCode(200);
  132. $response->headers->set('ETag', '12345');
  133. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  134. $response->headers->set('Content-Type', 'text/plain');
  135. $response->setContent('Hello World');
  136. });
  137. // only ETag matches
  138. $t = \DateTime::createFromFormat('U', time() - 3600);
  139. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => $t->format(DATE_RFC2822)));
  140. $this->assertHttpKernelIsCalled();
  141. $this->assertEquals(200, $this->response->getStatusCode());
  142. // only Last-Modified matches
  143. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '1234', 'HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  144. $this->assertHttpKernelIsCalled();
  145. $this->assertEquals(200, $this->response->getStatusCode());
  146. // Both matches
  147. $this->request('GET', '/', array('HTTP_IF_NONE_MATCH' => '12345', 'HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  148. $this->assertHttpKernelIsCalled();
  149. $this->assertEquals(304, $this->response->getStatusCode());
  150. }
  151. public function testValidatesPrivateResponsesCachedOnTheClient()
  152. {
  153. $this->setNextResponse(200, array(), '', function ($request, $response) {
  154. $etags = preg_split('/\s*,\s*/', $request->headers->get('IF_NONE_MATCH'));
  155. if ($request->cookies->has('authenticated')) {
  156. $response->headers->set('Cache-Control', 'private, no-store');
  157. $response->setETag('"private tag"');
  158. if (in_array('"private tag"', $etags)) {
  159. $response->setStatusCode(304);
  160. } else {
  161. $response->setStatusCode(200);
  162. $response->headers->set('Content-Type', 'text/plain');
  163. $response->setContent('private data');
  164. }
  165. } else {
  166. $response->headers->set('Cache-Control', 'public');
  167. $response->setETag('"public tag"');
  168. if (in_array('"public tag"', $etags)) {
  169. $response->setStatusCode(304);
  170. } else {
  171. $response->setStatusCode(200);
  172. $response->headers->set('Content-Type', 'text/plain');
  173. $response->setContent('public data');
  174. }
  175. }
  176. });
  177. $this->request('GET', '/');
  178. $this->assertHttpKernelIsCalled();
  179. $this->assertEquals(200, $this->response->getStatusCode());
  180. $this->assertEquals('"public tag"', $this->response->headers->get('ETag'));
  181. $this->assertEquals('public data', $this->response->getContent());
  182. $this->assertTraceContains('miss');
  183. $this->assertTraceContains('store');
  184. $this->request('GET', '/', array(), array('authenticated' => ''));
  185. $this->assertHttpKernelIsCalled();
  186. $this->assertEquals(200, $this->response->getStatusCode());
  187. $this->assertEquals('"private tag"', $this->response->headers->get('ETag'));
  188. $this->assertEquals('private data', $this->response->getContent());
  189. $this->assertTraceContains('stale');
  190. $this->assertTraceContains('invalid');
  191. $this->assertTraceNotContains('store');
  192. }
  193. public function testStoresResponsesWhenNoCacheRequestDirectivePresent()
  194. {
  195. $time = \DateTime::createFromFormat('U', time() + 5);
  196. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  197. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  198. $this->assertHttpKernelIsCalled();
  199. $this->assertTraceContains('store');
  200. $this->assertTrue($this->response->headers->has('Age'));
  201. }
  202. public function testReloadsResponsesWhenCacheHitsButNoCacheRequestDirectivePresentWhenAllowReloadIsSetTrue()
  203. {
  204. $count = 0;
  205. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=10000'), '', function ($request, $response) use (&$count) {
  206. ++$count;
  207. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  208. });
  209. $this->request('GET', '/');
  210. $this->assertEquals(200, $this->response->getStatusCode());
  211. $this->assertEquals('Hello World', $this->response->getContent());
  212. $this->assertTraceContains('store');
  213. $this->request('GET', '/');
  214. $this->assertEquals(200, $this->response->getStatusCode());
  215. $this->assertEquals('Hello World', $this->response->getContent());
  216. $this->assertTraceContains('fresh');
  217. $this->cacheConfig['allow_reload'] = true;
  218. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  219. $this->assertEquals(200, $this->response->getStatusCode());
  220. $this->assertEquals('Goodbye World', $this->response->getContent());
  221. $this->assertTraceContains('reload');
  222. $this->assertTraceContains('store');
  223. }
  224. public function testDoesNotReloadResponsesWhenAllowReloadIsSetFalseDefault()
  225. {
  226. $count = 0;
  227. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=10000'), '', function ($request, $response) use (&$count) {
  228. ++$count;
  229. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  230. });
  231. $this->request('GET', '/');
  232. $this->assertEquals(200, $this->response->getStatusCode());
  233. $this->assertEquals('Hello World', $this->response->getContent());
  234. $this->assertTraceContains('store');
  235. $this->request('GET', '/');
  236. $this->assertEquals(200, $this->response->getStatusCode());
  237. $this->assertEquals('Hello World', $this->response->getContent());
  238. $this->assertTraceContains('fresh');
  239. $this->cacheConfig['allow_reload'] = false;
  240. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  241. $this->assertEquals(200, $this->response->getStatusCode());
  242. $this->assertEquals('Hello World', $this->response->getContent());
  243. $this->assertTraceNotContains('reload');
  244. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'no-cache'));
  245. $this->assertEquals(200, $this->response->getStatusCode());
  246. $this->assertEquals('Hello World', $this->response->getContent());
  247. $this->assertTraceNotContains('reload');
  248. }
  249. public function testRevalidatesFreshCacheEntryWhenMaxAgeRequestDirectiveIsExceededWhenAllowRevalidateOptionIsSetTrue()
  250. {
  251. $count = 0;
  252. $this->setNextResponse(200, array(), '', function ($request, $response) use (&$count) {
  253. ++$count;
  254. $response->headers->set('Cache-Control', 'public, max-age=10000');
  255. $response->setETag($count);
  256. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  257. });
  258. $this->request('GET', '/');
  259. $this->assertEquals(200, $this->response->getStatusCode());
  260. $this->assertEquals('Hello World', $this->response->getContent());
  261. $this->assertTraceContains('store');
  262. $this->request('GET', '/');
  263. $this->assertEquals(200, $this->response->getStatusCode());
  264. $this->assertEquals('Hello World', $this->response->getContent());
  265. $this->assertTraceContains('fresh');
  266. $this->cacheConfig['allow_revalidate'] = true;
  267. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  268. $this->assertEquals(200, $this->response->getStatusCode());
  269. $this->assertEquals('Goodbye World', $this->response->getContent());
  270. $this->assertTraceContains('stale');
  271. $this->assertTraceContains('invalid');
  272. $this->assertTraceContains('store');
  273. }
  274. public function testDoesNotRevalidateFreshCacheEntryWhenEnableRevalidateOptionIsSetFalseDefault()
  275. {
  276. $count = 0;
  277. $this->setNextResponse(200, array(), '', function ($request, $response) use (&$count) {
  278. ++$count;
  279. $response->headers->set('Cache-Control', 'public, max-age=10000');
  280. $response->setETag($count);
  281. $response->setContent(1 == $count ? 'Hello World' : 'Goodbye World');
  282. });
  283. $this->request('GET', '/');
  284. $this->assertEquals(200, $this->response->getStatusCode());
  285. $this->assertEquals('Hello World', $this->response->getContent());
  286. $this->assertTraceContains('store');
  287. $this->request('GET', '/');
  288. $this->assertEquals(200, $this->response->getStatusCode());
  289. $this->assertEquals('Hello World', $this->response->getContent());
  290. $this->assertTraceContains('fresh');
  291. $this->cacheConfig['allow_revalidate'] = false;
  292. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  293. $this->assertEquals(200, $this->response->getStatusCode());
  294. $this->assertEquals('Hello World', $this->response->getContent());
  295. $this->assertTraceNotContains('stale');
  296. $this->assertTraceNotContains('invalid');
  297. $this->assertTraceContains('fresh');
  298. $this->request('GET', '/', array('HTTP_CACHE_CONTROL' => 'max-age=0'));
  299. $this->assertEquals(200, $this->response->getStatusCode());
  300. $this->assertEquals('Hello World', $this->response->getContent());
  301. $this->assertTraceNotContains('stale');
  302. $this->assertTraceNotContains('invalid');
  303. $this->assertTraceContains('fresh');
  304. }
  305. public function testFetchesResponseFromBackendWhenCacheMisses()
  306. {
  307. $time = \DateTime::createFromFormat('U', time() + 5);
  308. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  309. $this->request('GET', '/');
  310. $this->assertEquals(200, $this->response->getStatusCode());
  311. $this->assertTraceContains('miss');
  312. $this->assertTrue($this->response->headers->has('Age'));
  313. }
  314. public function testDoesNotCacheSomeStatusCodeResponses()
  315. {
  316. 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) {
  317. $time = \DateTime::createFromFormat('U', time() + 5);
  318. $this->setNextResponse($code, array('Expires' => $time->format(DATE_RFC2822)));
  319. $this->request('GET', '/');
  320. $this->assertEquals($code, $this->response->getStatusCode());
  321. $this->assertTraceNotContains('store');
  322. $this->assertFalse($this->response->headers->has('Age'));
  323. }
  324. }
  325. public function testDoesNotCacheResponsesWithExplicitNoStoreDirective()
  326. {
  327. $time = \DateTime::createFromFormat('U', time() + 5);
  328. $this->setNextResponse(200, array('Expires' => $time->format(DATE_RFC2822), 'Cache-Control' => 'no-store'));
  329. $this->request('GET', '/');
  330. $this->assertTraceNotContains('store');
  331. $this->assertFalse($this->response->headers->has('Age'));
  332. }
  333. public function testDoesNotCacheResponsesWithoutFreshnessInformationOrAValidator()
  334. {
  335. $this->setNextResponse();
  336. $this->request('GET', '/');
  337. $this->assertEquals(200, $this->response->getStatusCode());
  338. $this->assertTraceNotContains('store');
  339. }
  340. public function testCachesResponsesWithExplicitNoCacheDirective()
  341. {
  342. $time = \DateTime::createFromFormat('U', time() + 5);
  343. $this->setNextResponse(200, array('Expires' => $time->format(DATE_RFC2822), 'Cache-Control' => 'public, no-cache'));
  344. $this->request('GET', '/');
  345. $this->assertTraceContains('store');
  346. $this->assertTrue($this->response->headers->has('Age'));
  347. }
  348. public function testCachesResponsesWithAnExpirationHeader()
  349. {
  350. $time = \DateTime::createFromFormat('U', time() + 5);
  351. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  352. $this->request('GET', '/');
  353. $this->assertEquals(200, $this->response->getStatusCode());
  354. $this->assertEquals('Hello World', $this->response->getContent());
  355. $this->assertNotNull($this->response->headers->get('Date'));
  356. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  357. $this->assertTraceContains('miss');
  358. $this->assertTraceContains('store');
  359. $values = $this->getMetaStorageValues();
  360. $this->assertCount(1, $values);
  361. }
  362. public function testCachesResponsesWithAMaxAgeDirective()
  363. {
  364. $this->setNextResponse(200, array('Cache-Control' => 'public, max-age=5'));
  365. $this->request('GET', '/');
  366. $this->assertEquals(200, $this->response->getStatusCode());
  367. $this->assertEquals('Hello World', $this->response->getContent());
  368. $this->assertNotNull($this->response->headers->get('Date'));
  369. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  370. $this->assertTraceContains('miss');
  371. $this->assertTraceContains('store');
  372. $values = $this->getMetaStorageValues();
  373. $this->assertCount(1, $values);
  374. }
  375. public function testCachesResponsesWithASMaxAgeDirective()
  376. {
  377. $this->setNextResponse(200, array('Cache-Control' => 's-maxage=5'));
  378. $this->request('GET', '/');
  379. $this->assertEquals(200, $this->response->getStatusCode());
  380. $this->assertEquals('Hello World', $this->response->getContent());
  381. $this->assertNotNull($this->response->headers->get('Date'));
  382. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  383. $this->assertTraceContains('miss');
  384. $this->assertTraceContains('store');
  385. $values = $this->getMetaStorageValues();
  386. $this->assertCount(1, $values);
  387. }
  388. public function testCachesResponsesWithALastModifiedValidatorButNoFreshnessInformation()
  389. {
  390. $time = \DateTime::createFromFormat('U', time());
  391. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Last-Modified' => $time->format(DATE_RFC2822)));
  392. $this->request('GET', '/');
  393. $this->assertEquals(200, $this->response->getStatusCode());
  394. $this->assertEquals('Hello World', $this->response->getContent());
  395. $this->assertTraceContains('miss');
  396. $this->assertTraceContains('store');
  397. }
  398. public function testCachesResponsesWithAnETagValidatorButNoFreshnessInformation()
  399. {
  400. $this->setNextResponse(200, array('Cache-Control' => 'public', 'ETag' => '"123456"'));
  401. $this->request('GET', '/');
  402. $this->assertEquals(200, $this->response->getStatusCode());
  403. $this->assertEquals('Hello World', $this->response->getContent());
  404. $this->assertTraceContains('miss');
  405. $this->assertTraceContains('store');
  406. }
  407. public function testHitsCachedResponsesWithExpiresHeader()
  408. {
  409. $time1 = \DateTime::createFromFormat('U', time() - 5);
  410. $time2 = \DateTime::createFromFormat('U', time() + 5);
  411. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Date' => $time1->format(DATE_RFC2822), 'Expires' => $time2->format(DATE_RFC2822)));
  412. $this->request('GET', '/');
  413. $this->assertHttpKernelIsCalled();
  414. $this->assertEquals(200, $this->response->getStatusCode());
  415. $this->assertNotNull($this->response->headers->get('Date'));
  416. $this->assertTraceContains('miss');
  417. $this->assertTraceContains('store');
  418. $this->assertEquals('Hello World', $this->response->getContent());
  419. $this->request('GET', '/');
  420. $this->assertHttpKernelIsNotCalled();
  421. $this->assertEquals(200, $this->response->getStatusCode());
  422. $this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
  423. $this->assertTrue($this->response->headers->get('Age') > 0);
  424. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  425. $this->assertTraceContains('fresh');
  426. $this->assertTraceNotContains('store');
  427. $this->assertEquals('Hello World', $this->response->getContent());
  428. }
  429. public function testHitsCachedResponseWithMaxAgeDirective()
  430. {
  431. $time = \DateTime::createFromFormat('U', time() - 5);
  432. $this->setNextResponse(200, array('Date' => $time->format(DATE_RFC2822), 'Cache-Control' => 'public, max-age=10'));
  433. $this->request('GET', '/');
  434. $this->assertHttpKernelIsCalled();
  435. $this->assertEquals(200, $this->response->getStatusCode());
  436. $this->assertNotNull($this->response->headers->get('Date'));
  437. $this->assertTraceContains('miss');
  438. $this->assertTraceContains('store');
  439. $this->assertEquals('Hello World', $this->response->getContent());
  440. $this->request('GET', '/');
  441. $this->assertHttpKernelIsNotCalled();
  442. $this->assertEquals(200, $this->response->getStatusCode());
  443. $this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
  444. $this->assertTrue($this->response->headers->get('Age') > 0);
  445. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  446. $this->assertTraceContains('fresh');
  447. $this->assertTraceNotContains('store');
  448. $this->assertEquals('Hello World', $this->response->getContent());
  449. }
  450. public function testHitsCachedResponseWithSMaxAgeDirective()
  451. {
  452. $time = \DateTime::createFromFormat('U', time() - 5);
  453. $this->setNextResponse(200, array('Date' => $time->format(DATE_RFC2822), 'Cache-Control' => 's-maxage=10, max-age=0'));
  454. $this->request('GET', '/');
  455. $this->assertHttpKernelIsCalled();
  456. $this->assertEquals(200, $this->response->getStatusCode());
  457. $this->assertNotNull($this->response->headers->get('Date'));
  458. $this->assertTraceContains('miss');
  459. $this->assertTraceContains('store');
  460. $this->assertEquals('Hello World', $this->response->getContent());
  461. $this->request('GET', '/');
  462. $this->assertHttpKernelIsNotCalled();
  463. $this->assertEquals(200, $this->response->getStatusCode());
  464. $this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
  465. $this->assertTrue($this->response->headers->get('Age') > 0);
  466. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  467. $this->assertTraceContains('fresh');
  468. $this->assertTraceNotContains('store');
  469. $this->assertEquals('Hello World', $this->response->getContent());
  470. }
  471. public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformation()
  472. {
  473. $this->setNextResponse();
  474. $this->cacheConfig['default_ttl'] = 10;
  475. $this->request('GET', '/');
  476. $this->assertHttpKernelIsCalled();
  477. $this->assertTraceContains('miss');
  478. $this->assertTraceContains('store');
  479. $this->assertEquals('Hello World', $this->response->getContent());
  480. $this->assertRegExp('/s-maxage=10/', $this->response->headers->get('Cache-Control'));
  481. $this->cacheConfig['default_ttl'] = 10;
  482. $this->request('GET', '/');
  483. $this->assertHttpKernelIsNotCalled();
  484. $this->assertEquals(200, $this->response->getStatusCode());
  485. $this->assertTraceContains('fresh');
  486. $this->assertTraceNotContains('store');
  487. $this->assertEquals('Hello World', $this->response->getContent());
  488. $this->assertRegExp('/s-maxage=10/', $this->response->headers->get('Cache-Control'));
  489. }
  490. public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAfterTtlWasExpired()
  491. {
  492. $this->setNextResponse();
  493. $this->cacheConfig['default_ttl'] = 2;
  494. $this->request('GET', '/');
  495. $this->assertHttpKernelIsCalled();
  496. $this->assertTraceContains('miss');
  497. $this->assertTraceContains('store');
  498. $this->assertEquals('Hello World', $this->response->getContent());
  499. $this->assertRegExp('/s-maxage=(?:2|3)/', $this->response->headers->get('Cache-Control'));
  500. $this->request('GET', '/');
  501. $this->assertHttpKernelIsNotCalled();
  502. $this->assertEquals(200, $this->response->getStatusCode());
  503. $this->assertTraceContains('fresh');
  504. $this->assertTraceNotContains('store');
  505. $this->assertEquals('Hello World', $this->response->getContent());
  506. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  507. // expires the cache
  508. $values = $this->getMetaStorageValues();
  509. $this->assertCount(1, $values);
  510. $tmp = unserialize($values[0]);
  511. $tmp[0][1]['date'] = \DateTime::createFromFormat('U', time() - 5)->format(DATE_RFC2822);
  512. $r = new \ReflectionObject($this->store);
  513. $m = $r->getMethod('save');
  514. $m->setAccessible(true);
  515. $m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));
  516. $this->request('GET', '/');
  517. $this->assertHttpKernelIsCalled();
  518. $this->assertEquals(200, $this->response->getStatusCode());
  519. $this->assertTraceContains('stale');
  520. $this->assertTraceContains('invalid');
  521. $this->assertTraceContains('store');
  522. $this->assertEquals('Hello World', $this->response->getContent());
  523. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  524. $this->setNextResponse();
  525. $this->request('GET', '/');
  526. $this->assertHttpKernelIsNotCalled();
  527. $this->assertEquals(200, $this->response->getStatusCode());
  528. $this->assertTraceContains('fresh');
  529. $this->assertTraceNotContains('store');
  530. $this->assertEquals('Hello World', $this->response->getContent());
  531. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  532. }
  533. public function testAssignsDefaultTtlWhenResponseHasNoFreshnessInformationAndAfterTtlWasExpiredWithStatus304()
  534. {
  535. $this->setNextResponse();
  536. $this->cacheConfig['default_ttl'] = 2;
  537. $this->request('GET', '/');
  538. $this->assertHttpKernelIsCalled();
  539. $this->assertTraceContains('miss');
  540. $this->assertTraceContains('store');
  541. $this->assertEquals('Hello World', $this->response->getContent());
  542. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  543. $this->request('GET', '/');
  544. $this->assertHttpKernelIsNotCalled();
  545. $this->assertEquals(200, $this->response->getStatusCode());
  546. $this->assertTraceContains('fresh');
  547. $this->assertTraceNotContains('store');
  548. $this->assertEquals('Hello World', $this->response->getContent());
  549. // expires the cache
  550. $values = $this->getMetaStorageValues();
  551. $this->assertCount(1, $values);
  552. $tmp = unserialize($values[0]);
  553. $tmp[0][1]['date'] = \DateTime::createFromFormat('U', time() - 5)->format(DATE_RFC2822);
  554. $r = new \ReflectionObject($this->store);
  555. $m = $r->getMethod('save');
  556. $m->setAccessible(true);
  557. $m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));
  558. $this->request('GET', '/');
  559. $this->assertHttpKernelIsCalled();
  560. $this->assertEquals(200, $this->response->getStatusCode());
  561. $this->assertTraceContains('stale');
  562. $this->assertTraceContains('valid');
  563. $this->assertTraceContains('store');
  564. $this->assertTraceNotContains('miss');
  565. $this->assertEquals('Hello World', $this->response->getContent());
  566. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  567. $this->request('GET', '/');
  568. $this->assertHttpKernelIsNotCalled();
  569. $this->assertEquals(200, $this->response->getStatusCode());
  570. $this->assertTraceContains('fresh');
  571. $this->assertTraceNotContains('store');
  572. $this->assertEquals('Hello World', $this->response->getContent());
  573. $this->assertRegExp('/s-maxage=2/', $this->response->headers->get('Cache-Control'));
  574. }
  575. public function testDoesNotAssignDefaultTtlWhenResponseHasMustRevalidateDirective()
  576. {
  577. $this->setNextResponse(200, array('Cache-Control' => 'must-revalidate'));
  578. $this->cacheConfig['default_ttl'] = 10;
  579. $this->request('GET', '/');
  580. $this->assertHttpKernelIsCalled();
  581. $this->assertEquals(200, $this->response->getStatusCode());
  582. $this->assertTraceContains('miss');
  583. $this->assertTraceNotContains('store');
  584. $this->assertNotRegExp('/s-maxage/', $this->response->headers->get('Cache-Control'));
  585. $this->assertEquals('Hello World', $this->response->getContent());
  586. }
  587. public function testFetchesFullResponseWhenCacheStaleAndNoValidatorsPresent()
  588. {
  589. $time = \DateTime::createFromFormat('U', time() + 5);
  590. $this->setNextResponse(200, array('Cache-Control' => 'public', 'Expires' => $time->format(DATE_RFC2822)));
  591. // build initial request
  592. $this->request('GET', '/');
  593. $this->assertHttpKernelIsCalled();
  594. $this->assertEquals(200, $this->response->getStatusCode());
  595. $this->assertNotNull($this->response->headers->get('Date'));
  596. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  597. $this->assertNotNull($this->response->headers->get('Age'));
  598. $this->assertTraceContains('miss');
  599. $this->assertTraceContains('store');
  600. $this->assertEquals('Hello World', $this->response->getContent());
  601. # go in and play around with the cached metadata directly ...
  602. $values = $this->getMetaStorageValues();
  603. $this->assertCount(1, $values);
  604. $tmp = unserialize($values[0]);
  605. $time = \DateTime::createFromFormat('U', time());
  606. $tmp[0][1]['expires'] = $time->format(DATE_RFC2822);
  607. $r = new \ReflectionObject($this->store);
  608. $m = $r->getMethod('save');
  609. $m->setAccessible(true);
  610. $m->invoke($this->store, 'md'.hash('sha256', 'http://localhost/'), serialize($tmp));
  611. // build subsequent request; should be found but miss due to freshness
  612. $this->request('GET', '/');
  613. $this->assertHttpKernelIsCalled();
  614. $this->assertEquals(200, $this->response->getStatusCode());
  615. $this->assertTrue($this->response->headers->get('Age') <= 1);
  616. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  617. $this->assertTraceContains('stale');
  618. $this->assertTraceNotContains('fresh');
  619. $this->assertTraceNotContains('miss');
  620. $this->assertTraceContains('store');
  621. $this->assertEquals('Hello World', $this->response->getContent());
  622. }
  623. public function testValidatesCachedResponsesWithLastModifiedAndNoFreshnessInformation()
  624. {
  625. $time = \DateTime::createFromFormat('U', time());
  626. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time) {
  627. $response->headers->set('Cache-Control', 'public');
  628. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  629. if ($time->format(DATE_RFC2822) == $request->headers->get('IF_MODIFIED_SINCE')) {
  630. $response->setStatusCode(304);
  631. $response->setContent('');
  632. }
  633. });
  634. // build initial request
  635. $this->request('GET', '/');
  636. $this->assertHttpKernelIsCalled();
  637. $this->assertEquals(200, $this->response->getStatusCode());
  638. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  639. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  640. $this->assertEquals('Hello World', $this->response->getContent());
  641. $this->assertTraceContains('miss');
  642. $this->assertTraceContains('store');
  643. $this->assertTraceNotContains('stale');
  644. // build subsequent request; should be found but miss due to freshness
  645. $this->request('GET', '/');
  646. $this->assertHttpKernelIsCalled();
  647. $this->assertEquals(200, $this->response->getStatusCode());
  648. $this->assertNotNull($this->response->headers->get('Last-Modified'));
  649. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  650. $this->assertTrue($this->response->headers->get('Age') <= 1);
  651. $this->assertEquals('Hello World', $this->response->getContent());
  652. $this->assertTraceContains('stale');
  653. $this->assertTraceContains('valid');
  654. $this->assertTraceContains('store');
  655. $this->assertTraceNotContains('miss');
  656. }
  657. public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
  658. {
  659. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
  660. $response->headers->set('Cache-Control', 'public');
  661. $response->headers->set('ETag', '"12345"');
  662. if ($response->getETag() == $request->headers->get('IF_NONE_MATCH')) {
  663. $response->setStatusCode(304);
  664. $response->setContent('');
  665. }
  666. });
  667. // build initial request
  668. $this->request('GET', '/');
  669. $this->assertHttpKernelIsCalled();
  670. $this->assertEquals(200, $this->response->getStatusCode());
  671. $this->assertNotNull($this->response->headers->get('ETag'));
  672. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  673. $this->assertEquals('Hello World', $this->response->getContent());
  674. $this->assertTraceContains('miss');
  675. $this->assertTraceContains('store');
  676. // build subsequent request; should be found but miss due to freshness
  677. $this->request('GET', '/');
  678. $this->assertHttpKernelIsCalled();
  679. $this->assertEquals(200, $this->response->getStatusCode());
  680. $this->assertNotNull($this->response->headers->get('ETag'));
  681. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  682. $this->assertTrue($this->response->headers->get('Age') <= 1);
  683. $this->assertEquals('Hello World', $this->response->getContent());
  684. $this->assertTraceContains('stale');
  685. $this->assertTraceContains('valid');
  686. $this->assertTraceContains('store');
  687. $this->assertTraceNotContains('miss');
  688. }
  689. public function testReplacesCachedResponsesWhenValidationResultsInNon304Response()
  690. {
  691. $time = \DateTime::createFromFormat('U', time());
  692. $count = 0;
  693. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($time, &$count) {
  694. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  695. $response->headers->set('Cache-Control', 'public');
  696. switch (++$count) {
  697. case 1:
  698. $response->setContent('first response');
  699. break;
  700. case 2:
  701. $response->setContent('second response');
  702. break;
  703. case 3:
  704. $response->setContent('');
  705. $response->setStatusCode(304);
  706. break;
  707. }
  708. });
  709. // first request should fetch from backend and store in cache
  710. $this->request('GET', '/');
  711. $this->assertEquals(200, $this->response->getStatusCode());
  712. $this->assertEquals('first response', $this->response->getContent());
  713. // second request is validated, is invalid, and replaces cached entry
  714. $this->request('GET', '/');
  715. $this->assertEquals(200, $this->response->getStatusCode());
  716. $this->assertEquals('second response', $this->response->getContent());
  717. // third response is validated, valid, and returns cached entry
  718. $this->request('GET', '/');
  719. $this->assertEquals(200, $this->response->getStatusCode());
  720. $this->assertEquals('second response', $this->response->getContent());
  721. $this->assertEquals(3, $count);
  722. }
  723. public function testPassesHeadRequestsThroughDirectlyOnPass()
  724. {
  725. $that = $this;
  726. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($that) {
  727. $response->setContent('');
  728. $response->setStatusCode(200);
  729. $that->assertEquals('HEAD', $request->getMethod());
  730. });
  731. $this->request('HEAD', '/', array('HTTP_EXPECT' => 'something ...'));
  732. $this->assertHttpKernelIsCalled();
  733. $this->assertEquals('', $this->response->getContent());
  734. }
  735. public function testUsesCacheToRespondToHeadRequestsWhenFresh()
  736. {
  737. $that = $this;
  738. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($that) {
  739. $response->headers->set('Cache-Control', 'public, max-age=10');
  740. $response->setContent('Hello World');
  741. $response->setStatusCode(200);
  742. $that->assertNotEquals('HEAD', $request->getMethod());
  743. });
  744. $this->request('GET', '/');
  745. $this->assertHttpKernelIsCalled();
  746. $this->assertEquals('Hello World', $this->response->getContent());
  747. $this->request('HEAD', '/');
  748. $this->assertHttpKernelIsNotCalled();
  749. $this->assertEquals(200, $this->response->getStatusCode());
  750. $this->assertEquals('', $this->response->getContent());
  751. $this->assertEquals(strlen('Hello World'), $this->response->headers->get('Content-Length'));
  752. }
  753. public function testSendsNoContentWhenFresh()
  754. {
  755. $time = \DateTime::createFromFormat('U', time());
  756. $that = $this;
  757. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($that, $time) {
  758. $response->headers->set('Cache-Control', 'public, max-age=10');
  759. $response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
  760. });
  761. $this->request('GET', '/');
  762. $this->assertHttpKernelIsCalled();
  763. $this->assertEquals('Hello World', $this->response->getContent());
  764. $this->request('GET', '/', array('HTTP_IF_MODIFIED_SINCE' => $time->format(DATE_RFC2822)));
  765. $this->assertHttpKernelIsNotCalled();
  766. $this->assertEquals(304, $this->response->getStatusCode());
  767. $this->assertEquals('', $this->response->getContent());
  768. }
  769. public function testInvalidatesCachedResponsesOnPost()
  770. {
  771. $this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
  772. if ('GET' == $request->getMethod()) {
  773. $response->setStatusCode(200);
  774. $response->headers->set('Cache-Control', 'public, max-age=500');
  775. $response->setContent('Hello World');
  776. } elseif ('POST' == $request->getMethod()) {
  777. $response->setStatusCode(303);
  778. $response->headers->set('Location', '/');
  779. $response->headers->remove('Cache-Control');
  780. $response->setContent('');
  781. }
  782. });
  783. // build initial request to enter into the cache
  784. $this->request('GET', '/');
  785. $this->assertHttpKernelIsCalled();
  786. $this->assertEquals(200, $this->response->getStatusCode());
  787. $this->assertEquals('Hello World', $this->response->getContent());
  788. $this->assertTraceContains('miss');
  789. $this->assertTraceContains('store');
  790. // make sure it is valid
  791. $this->request('GET', '/');
  792. $this->assertHttpKernelIsNotCalled();
  793. $this->assertEquals(200, $this->response->getStatusCode());
  794. $this->assertEquals('Hello World', $this->response->getContent());
  795. $this->assertTraceContains('fresh');
  796. // now POST to same URL
  797. $this->request('POST', '/helloworld');
  798. $this->assertHttpKernelIsCalled();
  799. $this->assertEquals('/', $this->response->headers->get('Location'));
  800. $this->assertTraceContains('invalidate');
  801. $this->assertTraceContains('pass');
  802. $this->assertEquals('', $this->response->getContent());
  803. // now make sure it was actually invalidated
  804. $this->request('GET', '/');
  805. $this->assertHttpKernelIsCalled();
  806. $this->assertEquals(200, $this->response->getStatusCode());
  807. $this->assertEquals('Hello World', $this->response->getContent());
  808. $this->assertTraceContains('stale');
  809. $this->assertTraceContains('invalid');
  810. $this->assertTraceContains('store');
  811. }
  812. public function testServesFromCacheWhenHeadersMatch()
  813. {
  814. $count = 0;
  815. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count) {
  816. $response->headers->set('Vary', 'Accept User-Agent Foo');
  817. $response->headers->set('Cache-Control', 'public, max-age=10');
  818. $response->headers->set('X-Response-Count', ++$count);
  819. $response->setContent($request->headers->get('USER_AGENT'));
  820. });
  821. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  822. $this->assertEquals(200, $this->response->getStatusCode());
  823. $this->assertEquals('Bob/1.0', $this->response->getContent());
  824. $this->assertTraceContains('miss');
  825. $this->assertTraceContains('store');
  826. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  827. $this->assertEquals(200, $this->response->getStatusCode());
  828. $this->assertEquals('Bob/1.0', $this->response->getContent());
  829. $this->assertTraceContains('fresh');
  830. $this->assertTraceNotContains('store');
  831. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  832. }
  833. public function testStoresMultipleResponsesWhenHeadersDiffer()
  834. {
  835. $count = 0;
  836. $this->setNextResponse(200, array('Cache-Control' => 'max-age=10000'), '', function ($request, $response) use (&$count) {
  837. $response->headers->set('Vary', 'Accept User-Agent Foo');
  838. $response->headers->set('Cache-Control', 'public, max-age=10');
  839. $response->headers->set('X-Response-Count', ++$count);
  840. $response->setContent($request->headers->get('USER_AGENT'));
  841. });
  842. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  843. $this->assertEquals(200, $this->response->getStatusCode());
  844. $this->assertEquals('Bob/1.0', $this->response->getContent());
  845. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  846. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  847. $this->assertEquals(200, $this->response->getStatusCode());
  848. $this->assertTraceContains('miss');
  849. $this->assertTraceContains('store');
  850. $this->assertEquals('Bob/2.0', $this->response->getContent());
  851. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  852. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/1.0'));
  853. $this->assertTraceContains('fresh');
  854. $this->assertEquals('Bob/1.0', $this->response->getContent());
  855. $this->assertEquals(1, $this->response->headers->get('X-Response-Count'));
  856. $this->request('GET', '/', array('HTTP_ACCEPT' => 'text/html', 'HTTP_USER_AGENT' => 'Bob/2.0'));
  857. $this->assertTraceContains('fresh');
  858. $this->assertEquals('Bob/2.0', $this->response->getContent());
  859. $this->assertEquals(2, $this->response->headers->get('X-Response-Count'));
  860. $this->request('GET', '/', array('HTTP_USER_AGENT' => 'Bob/2.0'));
  861. $this->assertTraceContains('miss');
  862. $this->assertEquals('Bob/2.0', $this->response->getContent());
  863. $this->assertEquals(3, $this->response->headers->get('X-Response-Count'));
  864. }
  865. public function testShouldCatchExceptions()
  866. {
  867. $this->catchExceptions();
  868. $this->setNextResponse();
  869. $this->request('GET', '/');
  870. $this->assertExceptionsAreCaught();
  871. }
  872. public function testShouldCatchExceptionsWhenReloadingAndNoCacheRequest()
  873. {
  874. $this->catchExceptions();
  875. $this->setNextResponse();
  876. $this->cacheConfig['allow_reload'] = true;
  877. $this->request('GET', '/', array(), array(), false, array('Pragma' => 'no-cache'));
  878. $this->assertExceptionsAreCaught();
  879. }
  880. public function testShouldNotCatchExceptions()
  881. {
  882. $this->catchExceptions(false);
  883. $this->setNextResponse();
  884. $this->request('GET', '/');
  885. $this->assertExceptionsAreNotCaught();
  886. }
  887. public function testEsiCacheSendsTheLowestTtl()
  888. {
  889. $responses = array(
  890. array(
  891. 'status' => 200,
  892. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  893. 'headers' => array(
  894. 'Cache-Control' => 's-maxage=300',
  895. 'Surrogate-Control' => 'content="ESI/1.0"',
  896. ),
  897. ),
  898. array(
  899. 'status' => 200,
  900. 'body' => 'Hello World!',
  901. 'headers' => array('Cache-Control' => 's-maxage=300'),
  902. ),
  903. array(
  904. 'status' => 200,
  905. 'body' => 'My name is Bobby.',
  906. 'headers' => array('Cache-Control' => 's-maxage=100'),
  907. ),
  908. );
  909. $this->setNextResponses($responses);
  910. $this->request('GET', '/', array(), array(), true);
  911. $this->assertEquals("Hello World! My name is Bobby.", $this->response->getContent());
  912. // check for 100 or 99 as the test can be executed after a second change
  913. $this->assertTrue(in_array($this->response->getTtl(), array(99, 100)));
  914. }
  915. public function testEsiCacheForceValidation()
  916. {
  917. $responses = array(
  918. array(
  919. 'status' => 200,
  920. 'body' => '<esi:include src="/foo" /> <esi:include src="/bar" />',
  921. 'headers' => array(
  922. 'Cache-Control' => 's-maxage=300',
  923. 'Surrogate-Control' => 'content="ESI/1.0"',
  924. ),
  925. ),
  926. array(
  927. 'status' => 200,
  928. 'body' => 'Hello World!',
  929. 'headers' => array('ETag' => 'foobar'),
  930. ),
  931. array(
  932. 'status' => 200,
  933. 'body' => 'My name is Bobby.',
  934. 'headers' => array('Cache-Control' => 's-maxage=100'),
  935. ),
  936. );
  937. $this->setNextResponses($responses);
  938. $this->request('GET', '/', array(), array(), true);
  939. $this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
  940. $this->assertNull($this->response->getTtl());
  941. $this->assertTrue($this->response->mustRevalidate());
  942. $this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
  943. $this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
  944. }
  945. public function testEsiRecalculateContentLengthHeader()
  946. {
  947. $responses = array(
  948. array(
  949. 'status' => 200,
  950. 'body' => '<esi:include src="/foo" />',
  951. 'headers' => array(
  952. 'Content-Length' => 26,
  953. 'Cache-Control' => 's-maxage=300',
  954. 'Surrogate-Control' => 'content="ESI/1.0"',
  955. ),
  956. ),
  957. array(
  958. 'status' => 200,
  959. 'body' => 'Hello World!',
  960. 'headers' => array(),
  961. ),
  962. );
  963. $this->setNextResponses($responses);
  964. $this->request('GET', '/', array(), array(), true);
  965. $this->assertEquals('Hello World!', $this->response->getContent());
  966. $this->assertEquals(12, $this->response->headers->get('Content-Length'));
  967. }
  968. public function testClientIpIsAlwaysLocalhostForForwardedRequests()
  969. {
  970. $this->setNextResponse();
  971. $this->request('GET', '/', array('REMOTE_ADDR' => '10.0.0.1'));
  972. $this->assertEquals('127.0.0.1', $this->kernel->getBackendRequest()->server->get('REMOTE_ADDR'));
  973. }
  974. /**
  975. * @dataProvider getTrustedProxyData
  976. */
  977. public function testHttpCacheIsSetAsATrustedProxy(array $existing, array $expected)
  978. {
  979. Request::setTrustedProxies($existing);
  980. $this->setNextResponse();
  981. $this->request('GET', '/', array('REMOTE_ADDR' => '10.0.0.1'));
  982. $this->assertEquals($expected, Request::getTrustedProxies());
  983. }
  984. public function getTrustedProxyData()
  985. {
  986. return array(
  987. array(array(), array('127.0.0.1')),
  988. array(array('10.0.0.2'), array('10.0.0.2', '127.0.0.1')),
  989. array(array('10.0.0.2', '127.0.0.1'), array('10.0.0.2', '127.0.0.1')),
  990. );
  991. }
  992. /**
  993. * @dataProvider getXForwardedForData
  994. */
  995. public function testXForwarderForHeaderForForwardedRequests($xForwardedFor, $expected)
  996. {
  997. $this->setNextResponse();
  998. $server = array('REMOTE_ADDR' => '10.0.0.1');
  999. if (false !== $xForwardedFor) {
  1000. $server['HTTP_X_FORWARDED_FOR'] = $xForwardedFor;
  1001. }
  1002. $this->request('GET', '/', $server);
  1003. $this->assertEquals($expected, $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For'));
  1004. }
  1005. public function getXForwardedForData()
  1006. {
  1007. return array(
  1008. array(false, '10.0.0.1'),
  1009. array('10.0.0.2', '10.0.0.2, 10.0.0.1'),
  1010. array('10.0.0.2, 10.0.0.3', '10.0.0.2, 10.0.0.3, 10.0.0.1'),
  1011. );
  1012. }
  1013. public function testXForwarderForHeaderForPassRequests()
  1014. {
  1015. $this->setNextResponse();
  1016. $server = array('REMOTE_ADDR' => '10.0.0.1');
  1017. $this->request('POST', '/', $server);
  1018. $this->assertEquals('10.0.0.1', $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For'));
  1019. }
  1020. public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponses()
  1021. {
  1022. $time = new \DateTime();
  1023. $responses = array(
  1024. array(
  1025. 'status' => 200,
  1026. 'body' => '<esi:include src="/hey" />',
  1027. 'headers' => array(
  1028. 'Surrogate-Control' => 'content="ESI/1.0"',
  1029. 'ETag' => 'hey',
  1030. 'Last-Modified' => $time->format(DATE_RFC2822),
  1031. ),
  1032. ),
  1033. array(
  1034. 'status' => 200,
  1035. 'body' => 'Hey!',
  1036. 'headers' => array(),
  1037. ),
  1038. );
  1039. $this->setNextResponses($responses);
  1040. $this->request('GET', '/', array(), array(), true);
  1041. $this->assertNull($this->response->getETag());
  1042. $this->assertNull($this->response->getLastModified());
  1043. }
  1044. }