HttpCacheTest.php 47 KB

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