ResponseCacheStrategyTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 testSharedMaxAgeNotSetIfNotSetInMasterRequest()
  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 testMasterResponseNotCacheableWhenEmbeddedResponseRequiresValidation()
  61. {
  62. $cacheStrategy = new ResponseCacheStrategy();
  63. $embeddedResponse = new Response();
  64. $embeddedResponse->setLastModified(new \DateTime());
  65. $cacheStrategy->add($embeddedResponse);
  66. $masterResponse = new Response();
  67. $masterResponse->setSharedMaxAge(3600);
  68. $cacheStrategy->update($masterResponse);
  69. $this->assertTrue($masterResponse->headers->hasCacheControlDirective('no-cache'));
  70. $this->assertTrue($masterResponse->headers->hasCacheControlDirective('must-revalidate'));
  71. $this->assertFalse($masterResponse->isFresh());
  72. }
  73. public function testValidationOnMasterResponseIsNotPossibleWhenItContainsEmbeddedResponses()
  74. {
  75. $cacheStrategy = new ResponseCacheStrategy();
  76. // This master response uses the "validation" model
  77. $masterResponse = new Response();
  78. $masterResponse->setLastModified(new \DateTime());
  79. $masterResponse->setEtag('foo');
  80. // Embedded response uses "expiry" model
  81. $embeddedResponse = new Response();
  82. $masterResponse->setSharedMaxAge(3600);
  83. $cacheStrategy->add($embeddedResponse);
  84. $cacheStrategy->update($masterResponse);
  85. $this->assertFalse($masterResponse->isValidateable());
  86. $this->assertFalse($masterResponse->headers->has('Last-Modified'));
  87. $this->assertFalse($masterResponse->headers->has('ETag'));
  88. $this->assertTrue($masterResponse->headers->hasCacheControlDirective('no-cache'));
  89. $this->assertTrue($masterResponse->headers->hasCacheControlDirective('must-revalidate'));
  90. }
  91. public function testMasterResponseWithValidationIsUnchangedWhenThereIsNoEmbeddedResponse()
  92. {
  93. $cacheStrategy = new ResponseCacheStrategy();
  94. $masterResponse = new Response();
  95. $masterResponse->setLastModified(new \DateTime());
  96. $cacheStrategy->update($masterResponse);
  97. $this->assertTrue($masterResponse->isValidateable());
  98. }
  99. public function testMasterResponseWithExpirationIsUnchangedWhenThereIsNoEmbeddedResponse()
  100. {
  101. $cacheStrategy = new ResponseCacheStrategy();
  102. $masterResponse = new Response();
  103. $masterResponse->setSharedMaxAge(3600);
  104. $cacheStrategy->update($masterResponse);
  105. $this->assertTrue($masterResponse->isFresh());
  106. }
  107. public function testMasterResponseIsNotCacheableWhenEmbeddedResponseIsNotCacheable()
  108. {
  109. $cacheStrategy = new ResponseCacheStrategy();
  110. $masterResponse = new Response();
  111. $masterResponse->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($masterResponse);
  119. $this->assertTrue($masterResponse->headers->hasCacheControlDirective('no-cache'));
  120. $this->assertTrue($masterResponse->headers->hasCacheControlDirective('must-revalidate'));
  121. $this->assertFalse($masterResponse->isFresh());
  122. }
  123. public function testEmbeddingPrivateResponseMakesMainResponsePrivate()
  124. {
  125. $cacheStrategy = new ResponseCacheStrategy();
  126. $masterResponse = new Response();
  127. $masterResponse->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($masterResponse);
  134. $this->assertTrue($masterResponse->headers->hasCacheControlDirective('private'));
  135. $this->assertFalse($masterResponse->headers->hasCacheControlDirective('public'));
  136. }
  137. public function testEmbeddingPublicResponseDoesNotMakeMainResponsePublic()
  138. {
  139. $cacheStrategy = new ResponseCacheStrategy();
  140. $masterResponse = new Response();
  141. $masterResponse->setPrivate(); // this is the default, but let's be explicit
  142. $masterResponse->setMaxAge(100);
  143. $embeddedResponse = new Response();
  144. $embeddedResponse->setPublic();
  145. $embeddedResponse->setSharedMaxAge(100);
  146. $cacheStrategy->add($embeddedResponse);
  147. $cacheStrategy->update($masterResponse);
  148. $this->assertTrue($masterResponse->headers->hasCacheControlDirective('private'));
  149. $this->assertFalse($masterResponse->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. $masterResponse = new Response();
  160. $masterResponse->setSharedMaxAge(3600);
  161. $embeddedResponse = new Response();
  162. $embeddedResponse->setSharedMaxAge(60);
  163. $embeddedResponse->setEtag('foo');
  164. $cacheStrategy->add($embeddedResponse);
  165. $cacheStrategy->update($masterResponse);
  166. $this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
  167. }
  168. public function testResponseIsExpirableButNotValidateableWhenMasterResponseCombinesExpirationAndValidation()
  169. {
  170. $cacheStrategy = new ResponseCacheStrategy();
  171. $masterResponse = new Response();
  172. $masterResponse->setSharedMaxAge(3600);
  173. $masterResponse->setEtag('foo');
  174. $masterResponse->setLastModified(new \DateTime());
  175. $embeddedResponse = new Response();
  176. $embeddedResponse->setSharedMaxAge(60);
  177. $cacheStrategy->add($embeddedResponse);
  178. $cacheStrategy->update($masterResponse);
  179. $this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
  180. $this->assertFalse($masterResponse->isValidateable());
  181. }
  182. /**
  183. * @dataProvider cacheControlMergingProvider
  184. */
  185. public function testCacheControlMerging(array $expects, array $master, array $surrogates)
  186. {
  187. $cacheStrategy = new ResponseCacheStrategy();
  188. $buildResponse = function ($config) {
  189. $response = new Response();
  190. foreach ($config as $key => $value) {
  191. switch ($key) {
  192. case 'age':
  193. $response->headers->set('Age', $value);
  194. break;
  195. case 'expires':
  196. $expires = clone $response->getDate();
  197. $expires->modify('+'.$value.' seconds');
  198. $response->setExpires($expires);
  199. break;
  200. case 'max-age':
  201. $response->setMaxAge($value);
  202. break;
  203. case 's-maxage':
  204. $response->setSharedMaxAge($value);
  205. break;
  206. case 'private':
  207. $response->setPrivate();
  208. break;
  209. case 'public':
  210. $response->setPublic();
  211. break;
  212. default:
  213. $response->headers->addCacheControlDirective($key, $value);
  214. }
  215. }
  216. return $response;
  217. };
  218. foreach ($surrogates as $config) {
  219. $cacheStrategy->add($buildResponse($config));
  220. }
  221. $response = $buildResponse($master);
  222. $cacheStrategy->update($response);
  223. foreach ($expects as $key => $value) {
  224. if ('expires' === $key) {
  225. $this->assertSame($value, $response->getExpires()->format('U') - $response->getDate()->format('U'));
  226. } elseif ('age' === $key) {
  227. $this->assertSame($value, $response->getAge());
  228. } elseif (true === $value) {
  229. $this->assertTrue($response->headers->hasCacheControlDirective($key), sprintf('Cache-Control header must have "%s" flag', $key));
  230. } elseif (false === $value) {
  231. $this->assertFalse(
  232. $response->headers->hasCacheControlDirective($key),
  233. sprintf('Cache-Control header must NOT have "%s" flag', $key)
  234. );
  235. } else {
  236. $this->assertSame($value, $response->headers->getCacheControlDirective($key), sprintf('Cache-Control flag "%s" should be "%s"', $key, $value));
  237. }
  238. }
  239. }
  240. public function cacheControlMergingProvider()
  241. {
  242. yield 'result is public if all responses are public' => [
  243. ['private' => false, 'public' => true],
  244. ['public' => true],
  245. [
  246. ['public' => true],
  247. ],
  248. ];
  249. yield 'result is private by default' => [
  250. ['private' => true, 'public' => false],
  251. ['public' => true],
  252. [
  253. [],
  254. ],
  255. ];
  256. yield 'combines public and private responses' => [
  257. ['must-revalidate' => false, 'private' => true, 'public' => false],
  258. ['public' => true],
  259. [
  260. ['private' => true],
  261. ],
  262. ];
  263. yield 'inherits no-cache from surrogates' => [
  264. ['no-cache' => true, 'public' => false],
  265. ['public' => true],
  266. [
  267. ['no-cache' => true],
  268. ],
  269. ];
  270. yield 'inherits no-store from surrogate' => [
  271. ['no-store' => true, 'public' => false],
  272. ['public' => true],
  273. [
  274. ['no-store' => true],
  275. ],
  276. ];
  277. yield 'resolve to lowest possible max-age' => [
  278. ['public' => false, 'private' => true, 's-maxage' => false, 'max-age' => '60'],
  279. ['public' => true, 'max-age' => 3600],
  280. [
  281. ['private' => true, 'max-age' => 60],
  282. ],
  283. ];
  284. yield 'resolves multiple max-age' => [
  285. ['public' => false, 'private' => true, 's-maxage' => false, 'max-age' => '60'],
  286. ['private' => true, 'max-age' => 100],
  287. [
  288. ['private' => true, 'max-age' => 3600],
  289. ['public' => true, 'max-age' => 60, 's-maxage' => 60],
  290. ['private' => true, 'max-age' => 60],
  291. ],
  292. ];
  293. yield 'merge max-age and s-maxage' => [
  294. ['public' => true, 's-maxage' => '60', 'max-age' => null],
  295. ['public' => true, 's-maxage' => 3600],
  296. [
  297. ['public' => true, 'max-age' => 60],
  298. ],
  299. ];
  300. yield 'result is private when combining private responses' => [
  301. ['no-cache' => false, 'must-revalidate' => false, 'private' => true],
  302. ['s-maxage' => 60, 'private' => true],
  303. [
  304. ['s-maxage' => 60, 'private' => true],
  305. ],
  306. ];
  307. yield 'result can have s-maxage and max-age' => [
  308. ['public' => true, 'private' => false, 's-maxage' => '60', 'max-age' => '30'],
  309. ['s-maxage' => 100, 'max-age' => 2000],
  310. [
  311. ['s-maxage' => 1000, 'max-age' => 30],
  312. ['s-maxage' => 500, 'max-age' => 500],
  313. ['s-maxage' => 60, 'max-age' => 1000],
  314. ],
  315. ];
  316. yield 'does not set headers without value' => [
  317. ['max-age' => null, 's-maxage' => null, 'public' => null],
  318. ['private' => true],
  319. [
  320. ['private' => true],
  321. ],
  322. ];
  323. yield 'max-age 0 is sent to the client' => [
  324. ['private' => true, 'max-age' => '0'],
  325. ['max-age' => 0, 'private' => true],
  326. [
  327. ['max-age' => 60, 'private' => true],
  328. ],
  329. ];
  330. yield 'max-age is relative to age' => [
  331. ['max-age' => '240', 'age' => 60],
  332. ['max-age' => 180],
  333. [
  334. ['max-age' => 600, 'age' => 60],
  335. ],
  336. ];
  337. yield 'retains lowest age of all responses' => [
  338. ['max-age' => '160', 'age' => 60],
  339. ['max-age' => 600, 'age' => 60],
  340. [
  341. ['max-age' => 120, 'age' => 20],
  342. ],
  343. ];
  344. yield 'max-age can be less than age, essentially expiring the response' => [
  345. ['age' => 120, 'max-age' => '90'],
  346. ['max-age' => 90, 'age' => 120],
  347. [
  348. ['max-age' => 120, 'age' => 60],
  349. ],
  350. ];
  351. yield 'max-age is 0 regardless of age' => [
  352. ['max-age' => '0'],
  353. ['max-age' => 60],
  354. [
  355. ['max-age' => 0, 'age' => 60],
  356. ],
  357. ];
  358. yield 'max-age is not negative' => [
  359. ['max-age' => '0'],
  360. ['max-age' => 0],
  361. [
  362. ['max-age' => 0, 'age' => 60],
  363. ],
  364. ];
  365. yield 'calculates lowest Expires header' => [
  366. ['expires' => 60],
  367. ['expires' => 60],
  368. [
  369. ['expires' => 120],
  370. ],
  371. ];
  372. yield 'calculates Expires header relative to age' => [
  373. ['expires' => 210, 'age' => 120],
  374. ['expires' => 90],
  375. [
  376. ['expires' => 600, 'age' => '120'],
  377. ],
  378. ];
  379. }
  380. }