ResponseCacheStrategyTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This code is partially based on the Rack-Cache library by Ryan Tomayko,
  8. * which is released under the MIT license.
  9. * (based on commit 02d2b48d75bcb63cf1c0c7149c077ad256542801)
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. namespace Symfony\Component\HttpKernel\Tests\HttpCache;
  15. use PHPUnit\Framework\TestCase;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\HttpCache\ResponseCacheStrategy;
  18. class ResponseCacheStrategyTest extends TestCase
  19. {
  20. public function testMinimumSharedMaxAgeWins()
  21. {
  22. $cacheStrategy = new ResponseCacheStrategy();
  23. $response1 = new Response();
  24. $response1->setSharedMaxAge(60);
  25. $cacheStrategy->add($response1);
  26. $response2 = new Response();
  27. $response2->setSharedMaxAge(3600);
  28. $cacheStrategy->add($response2);
  29. $response = new Response();
  30. $response->setSharedMaxAge(86400);
  31. $cacheStrategy->update($response);
  32. $this->assertSame('60', $response->headers->getCacheControlDirective('s-maxage'));
  33. }
  34. public function testSharedMaxAgeNotSetIfNotSetInAnyEmbeddedRequest()
  35. {
  36. $cacheStrategy = new ResponseCacheStrategy();
  37. $response1 = new Response();
  38. $response1->setSharedMaxAge(60);
  39. $cacheStrategy->add($response1);
  40. $response2 = new Response();
  41. $cacheStrategy->add($response2);
  42. $response = new Response();
  43. $response->setSharedMaxAge(86400);
  44. $cacheStrategy->update($response);
  45. $this->assertFalse($response->headers->hasCacheControlDirective('s-maxage'));
  46. }
  47. public function testSharedMaxAgeNotSetIfNotSetInMainRequest()
  48. {
  49. $cacheStrategy = new ResponseCacheStrategy();
  50. $response1 = new Response();
  51. $response1->setSharedMaxAge(60);
  52. $cacheStrategy->add($response1);
  53. $response2 = new Response();
  54. $response2->setSharedMaxAge(3600);
  55. $cacheStrategy->add($response2);
  56. $response = new Response();
  57. $cacheStrategy->update($response);
  58. $this->assertFalse($response->headers->hasCacheControlDirective('s-maxage'));
  59. }
  60. public function testMainResponseNotCacheableWhenEmbeddedResponseRequiresValidation()
  61. {
  62. $cacheStrategy = new ResponseCacheStrategy();
  63. $embeddedResponse = new Response();
  64. $embeddedResponse->setLastModified(new \DateTime());
  65. $cacheStrategy->add($embeddedResponse);
  66. $mainResponse = new Response();
  67. $mainResponse->setSharedMaxAge(3600);
  68. $cacheStrategy->update($mainResponse);
  69. $this->assertTrue($mainResponse->headers->hasCacheControlDirective('no-cache'));
  70. $this->assertTrue($mainResponse->headers->hasCacheControlDirective('must-revalidate'));
  71. $this->assertFalse($mainResponse->isFresh());
  72. }
  73. public function testValidationOnMainResponseIsNotPossibleWhenItContainsEmbeddedResponses()
  74. {
  75. $cacheStrategy = new ResponseCacheStrategy();
  76. // This main response uses the "validation" model
  77. $mainResponse = new Response();
  78. $mainResponse->setLastModified(new \DateTime());
  79. $mainResponse->setEtag('foo');
  80. // Embedded response uses "expiry" model
  81. $embeddedResponse = new Response();
  82. $mainResponse->setSharedMaxAge(3600);
  83. $cacheStrategy->add($embeddedResponse);
  84. $cacheStrategy->update($mainResponse);
  85. $this->assertFalse($mainResponse->isValidateable());
  86. $this->assertFalse($mainResponse->headers->has('Last-Modified'));
  87. $this->assertFalse($mainResponse->headers->has('ETag'));
  88. $this->assertTrue($mainResponse->headers->hasCacheControlDirective('no-cache'));
  89. $this->assertTrue($mainResponse->headers->hasCacheControlDirective('must-revalidate'));
  90. }
  91. public function testMainResponseWithValidationIsUnchangedWhenThereIsNoEmbeddedResponse()
  92. {
  93. $cacheStrategy = new ResponseCacheStrategy();
  94. $mainResponse = new Response();
  95. $mainResponse->setLastModified(new \DateTime());
  96. $cacheStrategy->update($mainResponse);
  97. $this->assertTrue($mainResponse->isValidateable());
  98. }
  99. public function testMainResponseWithExpirationIsUnchangedWhenThereIsNoEmbeddedResponse()
  100. {
  101. $cacheStrategy = new ResponseCacheStrategy();
  102. $mainResponse = new Response();
  103. $mainResponse->setSharedMaxAge(3600);
  104. $cacheStrategy->update($mainResponse);
  105. $this->assertTrue($mainResponse->isFresh());
  106. }
  107. public function testMainResponseIsNotCacheableWhenEmbeddedResponseIsNotCacheable()
  108. {
  109. $cacheStrategy = new ResponseCacheStrategy();
  110. $mainResponse = new Response();
  111. $mainResponse->setSharedMaxAge(3600); // Public, cacheable
  112. /* This response has no validation or expiration information.
  113. That makes it uncacheable, it is always stale.
  114. (It does *not* make this private, though.) */
  115. $embeddedResponse = new Response();
  116. $this->assertFalse($embeddedResponse->isFresh()); // not fresh, as no lifetime is provided
  117. $cacheStrategy->add($embeddedResponse);
  118. $cacheStrategy->update($mainResponse);
  119. $this->assertTrue($mainResponse->headers->hasCacheControlDirective('no-cache'));
  120. $this->assertTrue($mainResponse->headers->hasCacheControlDirective('must-revalidate'));
  121. $this->assertFalse($mainResponse->isFresh());
  122. }
  123. public function testEmbeddingPrivateResponseMakesMainResponsePrivate()
  124. {
  125. $cacheStrategy = new ResponseCacheStrategy();
  126. $mainResponse = new Response();
  127. $mainResponse->setSharedMaxAge(3600); // public, cacheable
  128. // The embedded response might for example contain per-user data that remains valid for 60 seconds
  129. $embeddedResponse = new Response();
  130. $embeddedResponse->setPrivate();
  131. $embeddedResponse->setMaxAge(60); // this would implicitly set "private" as well, but let's be explicit
  132. $cacheStrategy->add($embeddedResponse);
  133. $cacheStrategy->update($mainResponse);
  134. $this->assertTrue($mainResponse->headers->hasCacheControlDirective('private'));
  135. $this->assertFalse($mainResponse->headers->hasCacheControlDirective('public'));
  136. }
  137. public function testEmbeddingPublicResponseDoesNotMakeMainResponsePublic()
  138. {
  139. $cacheStrategy = new ResponseCacheStrategy();
  140. $mainResponse = new Response();
  141. $mainResponse->setPrivate(); // this is the default, but let's be explicit
  142. $mainResponse->setMaxAge(100);
  143. $embeddedResponse = new Response();
  144. $embeddedResponse->setPublic();
  145. $embeddedResponse->setSharedMaxAge(100);
  146. $cacheStrategy->add($embeddedResponse);
  147. $cacheStrategy->update($mainResponse);
  148. $this->assertTrue($mainResponse->headers->hasCacheControlDirective('private'));
  149. $this->assertFalse($mainResponse->headers->hasCacheControlDirective('public'));
  150. }
  151. public function testResponseIsExiprableWhenEmbeddedResponseCombinesExpiryAndValidation()
  152. {
  153. /* When "expiration wins over validation" (https://symfony.com/doc/current/http_cache/validation.html)
  154. * and both the main and embedded response provide s-maxage, then the more restricting value of both
  155. * should be fine, regardless of whether the embedded response can be validated later on or must be
  156. * completely regenerated.
  157. */
  158. $cacheStrategy = new ResponseCacheStrategy();
  159. $mainResponse = new Response();
  160. $mainResponse->setSharedMaxAge(3600);
  161. $embeddedResponse = new Response();
  162. $embeddedResponse->setSharedMaxAge(60);
  163. $embeddedResponse->setEtag('foo');
  164. $cacheStrategy->add($embeddedResponse);
  165. $cacheStrategy->update($mainResponse);
  166. $this->assertEqualsWithDelta(60, (int) $mainResponse->headers->getCacheControlDirective('s-maxage'), 1);
  167. }
  168. public function testResponseIsExpirableButNotValidateableWhenMainResponseCombinesExpirationAndValidation()
  169. {
  170. $cacheStrategy = new ResponseCacheStrategy();
  171. $mainResponse = new Response();
  172. $mainResponse->setSharedMaxAge(3600);
  173. $mainResponse->setEtag('foo');
  174. $mainResponse->setLastModified(new \DateTime());
  175. $embeddedResponse = new Response();
  176. $embeddedResponse->setSharedMaxAge(60);
  177. $cacheStrategy->add($embeddedResponse);
  178. $cacheStrategy->update($mainResponse);
  179. $this->assertSame('60', $mainResponse->headers->getCacheControlDirective('s-maxage'));
  180. $this->assertFalse($mainResponse->isValidateable());
  181. }
  182. /**
  183. * @group time-sensitive
  184. *
  185. * @dataProvider cacheControlMergingProvider
  186. */
  187. public function testCacheControlMerging(array $expects, array $master, array $surrogates)
  188. {
  189. $cacheStrategy = new ResponseCacheStrategy();
  190. $buildResponse = function ($config) {
  191. $response = new Response();
  192. foreach ($config as $key => $value) {
  193. switch ($key) {
  194. case 'age':
  195. $response->headers->set('Age', $value);
  196. break;
  197. case 'expires':
  198. $expires = clone $response->getDate();
  199. $expires = $expires->modify('+'.$value.' seconds');
  200. $response->setExpires($expires);
  201. break;
  202. case 'max-age':
  203. $response->setMaxAge($value);
  204. break;
  205. case 's-maxage':
  206. $response->setSharedMaxAge($value);
  207. break;
  208. case 'private':
  209. $response->setPrivate();
  210. break;
  211. case 'public':
  212. $response->setPublic();
  213. break;
  214. default:
  215. $response->headers->addCacheControlDirective($key, $value);
  216. }
  217. }
  218. return $response;
  219. };
  220. foreach ($surrogates as $config) {
  221. $cacheStrategy->add($buildResponse($config));
  222. }
  223. $response = $buildResponse($master);
  224. $cacheStrategy->update($response);
  225. foreach ($expects as $key => $value) {
  226. if ('expires' === $key) {
  227. $this->assertSame($value, $response->getExpires()->format('U') - $response->getDate()->format('U'));
  228. } elseif ('age' === $key) {
  229. $this->assertSame($value, $response->getAge());
  230. } elseif (true === $value) {
  231. $this->assertTrue($response->headers->hasCacheControlDirective($key), sprintf('Cache-Control header must have "%s" flag', $key));
  232. } elseif (false === $value) {
  233. $this->assertFalse(
  234. $response->headers->hasCacheControlDirective($key),
  235. sprintf('Cache-Control header must NOT have "%s" flag', $key)
  236. );
  237. } else {
  238. $this->assertSame($value, $response->headers->getCacheControlDirective($key), sprintf('Cache-Control flag "%s" should be "%s"', $key, $value));
  239. }
  240. }
  241. }
  242. public static function cacheControlMergingProvider()
  243. {
  244. yield 'result is public if all responses are public' => [
  245. ['private' => false, 'public' => true],
  246. ['public' => true],
  247. [
  248. ['public' => true],
  249. ],
  250. ];
  251. yield 'result is private by default' => [
  252. ['private' => true, 'public' => false],
  253. ['public' => true],
  254. [
  255. [],
  256. ],
  257. ];
  258. yield 'combines public and private responses' => [
  259. ['must-revalidate' => false, 'private' => true, 'public' => false],
  260. ['public' => true],
  261. [
  262. ['private' => true],
  263. ],
  264. ];
  265. yield 'inherits no-cache from surrogates' => [
  266. ['no-cache' => true, 'public' => false],
  267. ['public' => true],
  268. [
  269. ['no-cache' => true],
  270. ],
  271. ];
  272. yield 'inherits no-store from surrogate' => [
  273. ['no-store' => true, 'public' => false],
  274. ['public' => true],
  275. [
  276. ['no-store' => true],
  277. ],
  278. ];
  279. yield 'resolve to lowest possible max-age' => [
  280. ['public' => false, 'private' => true, 's-maxage' => false, 'max-age' => '60'],
  281. ['public' => true, 'max-age' => 3600],
  282. [
  283. ['private' => true, 'max-age' => 60],
  284. ],
  285. ];
  286. yield 'resolves multiple max-age' => [
  287. ['public' => false, 'private' => true, 's-maxage' => false, 'max-age' => '60'],
  288. ['private' => true, 'max-age' => 100],
  289. [
  290. ['private' => true, 'max-age' => 3600],
  291. ['public' => true, 'max-age' => 60, 's-maxage' => 60],
  292. ['private' => true, 'max-age' => 60],
  293. ],
  294. ];
  295. yield 'merge max-age and s-maxage' => [
  296. ['public' => true, 'max-age' => '60'],
  297. ['public' => true, 's-maxage' => 3600],
  298. [
  299. ['public' => true, 'max-age' => 60],
  300. ],
  301. ];
  302. yield 's-maxage may be set to 0' => [
  303. ['public' => true, 's-maxage' => '0', 'max-age' => null],
  304. ['public' => true, 's-maxage' => '0'],
  305. [
  306. ['public' => true, 's-maxage' => '60'],
  307. ],
  308. ];
  309. yield 's-maxage may be set to 0, and works independently from maxage' => [
  310. ['public' => true, 's-maxage' => '0', 'max-age' => '30'],
  311. ['public' => true, 's-maxage' => '0', 'max-age' => '30'],
  312. [
  313. ['public' => true, 'max-age' => '60'],
  314. ],
  315. ];
  316. yield 'public subresponse without lifetime does not remove lifetime for main response' => [
  317. ['public' => true, 's-maxage' => '30', 'max-age' => null],
  318. ['public' => true, 's-maxage' => '30'],
  319. [
  320. ['public' => true],
  321. ],
  322. ];
  323. yield 'lifetime for subresponse is kept when main response has no lifetime' => [
  324. ['public' => true, 'max-age' => '30'],
  325. ['public' => true],
  326. [
  327. ['public' => true, 'max-age' => '30'],
  328. ],
  329. ];
  330. yield 's-maxage on the subresponse implies public, so the result is public as well' => [
  331. ['public' => true, 'max-age' => '10', 's-maxage' => null],
  332. ['public' => true, 'max-age' => '10'],
  333. [
  334. ['max-age' => '30', 's-maxage' => '20'],
  335. ],
  336. ];
  337. yield 'result is private when combining private responses' => [
  338. ['no-cache' => false, 'must-revalidate' => false, 'private' => true],
  339. ['s-maxage' => 60, 'private' => true],
  340. [
  341. ['s-maxage' => 60, 'private' => true],
  342. ],
  343. ];
  344. yield 'result can have s-maxage and max-age' => [
  345. ['public' => true, 'private' => false, 's-maxage' => '60', 'max-age' => '30'],
  346. ['s-maxage' => 100, 'max-age' => 2000],
  347. [
  348. ['s-maxage' => 1000, 'max-age' => 30],
  349. ['s-maxage' => 500, 'max-age' => 500],
  350. ['s-maxage' => 60, 'max-age' => 1000],
  351. ],
  352. ];
  353. yield 'does not set headers without value' => [
  354. ['max-age' => null, 's-maxage' => null, 'public' => null],
  355. ['private' => true],
  356. [
  357. ['private' => true],
  358. ],
  359. ];
  360. yield 'max-age 0 is sent to the client' => [
  361. ['private' => true, 'max-age' => '0'],
  362. ['max-age' => 0, 'private' => true],
  363. [
  364. ['max-age' => 60, 'private' => true],
  365. ],
  366. ];
  367. yield 'max-age is relative to age' => [
  368. ['max-age' => '240', 'age' => 60],
  369. ['max-age' => 180],
  370. [
  371. ['max-age' => 600, 'age' => 60],
  372. ],
  373. ];
  374. yield 'retains lowest age of all responses' => [
  375. ['max-age' => '160', 'age' => 60],
  376. ['max-age' => 600, 'age' => 60],
  377. [
  378. ['max-age' => 120, 'age' => 20],
  379. ],
  380. ];
  381. yield 'max-age can be less than age, essentially expiring the response' => [
  382. ['age' => 120, 'max-age' => '90'],
  383. ['max-age' => 90, 'age' => 120],
  384. [
  385. ['max-age' => 120, 'age' => 60],
  386. ],
  387. ];
  388. yield 'max-age is 0 regardless of age' => [
  389. ['max-age' => '0'],
  390. ['max-age' => 60],
  391. [
  392. ['max-age' => 0, 'age' => 60],
  393. ],
  394. ];
  395. yield 'max-age is not negative' => [
  396. ['max-age' => '0'],
  397. ['max-age' => 0],
  398. [
  399. ['max-age' => 0, 'age' => 60],
  400. ],
  401. ];
  402. yield 'calculates lowest Expires header' => [
  403. ['expires' => 60],
  404. ['expires' => 60],
  405. [
  406. ['expires' => 120],
  407. ],
  408. ];
  409. yield 'calculates Expires header relative to age' => [
  410. ['expires' => 210, 'age' => 120],
  411. ['expires' => 90],
  412. [
  413. ['expires' => 600, 'age' => '120'],
  414. ],
  415. ];
  416. }
  417. }