ResponseCacheStrategyTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. }