StoreTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\HttpCache\Store;
  15. class StoreTest extends TestCase
  16. {
  17. protected $request;
  18. protected $response;
  19. /**
  20. * @var Store
  21. */
  22. protected $store;
  23. protected function setUp()
  24. {
  25. $this->request = Request::create('/');
  26. $this->response = new Response('hello world', 200, array());
  27. HttpCacheTestCase::clearDirectory(sys_get_temp_dir().'/http_cache');
  28. $this->store = new Store(sys_get_temp_dir().'/http_cache');
  29. }
  30. protected function tearDown()
  31. {
  32. $this->store = null;
  33. $this->request = null;
  34. $this->response = null;
  35. HttpCacheTestCase::clearDirectory(sys_get_temp_dir().'/http_cache');
  36. }
  37. public function testReadsAnEmptyArrayWithReadWhenNothingCachedAtKey()
  38. {
  39. $this->assertEmpty($this->getStoreMetadata('/nothing'));
  40. }
  41. public function testUnlockFileThatDoesExist()
  42. {
  43. $cacheKey = $this->storeSimpleEntry();
  44. $this->store->lock($this->request);
  45. $this->assertTrue($this->store->unlock($this->request));
  46. }
  47. public function testUnlockFileThatDoesNotExist()
  48. {
  49. $this->assertFalse($this->store->unlock($this->request));
  50. }
  51. public function testRemovesEntriesForKeyWithPurge()
  52. {
  53. $request = Request::create('/foo');
  54. $this->store->write($request, new Response('foo'));
  55. $metadata = $this->getStoreMetadata($request);
  56. $this->assertNotEmpty($metadata);
  57. $this->assertTrue($this->store->purge('/foo'));
  58. $this->assertEmpty($this->getStoreMetadata($request));
  59. // cached content should be kept after purging
  60. $path = $this->store->getPath($metadata[0][1]['x-content-digest'][0]);
  61. $this->assertTrue(is_file($path));
  62. $this->assertFalse($this->store->purge('/bar'));
  63. }
  64. public function testStoresACacheEntry()
  65. {
  66. $cacheKey = $this->storeSimpleEntry();
  67. $this->assertNotEmpty($this->getStoreMetadata($cacheKey));
  68. }
  69. public function testSetsTheXContentDigestResponseHeaderBeforeStoring()
  70. {
  71. $cacheKey = $this->storeSimpleEntry();
  72. $entries = $this->getStoreMetadata($cacheKey);
  73. list($req, $res) = $entries[0];
  74. $this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $res['x-content-digest'][0]);
  75. }
  76. public function testFindsAStoredEntryWithLookup()
  77. {
  78. $this->storeSimpleEntry();
  79. $response = $this->store->lookup($this->request);
  80. $this->assertNotNull($response);
  81. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  82. }
  83. public function testDoesNotFindAnEntryWithLookupWhenNoneExists()
  84. {
  85. $request = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  86. $this->assertNull($this->store->lookup($request));
  87. }
  88. public function testCanonizesUrlsForCacheKeys()
  89. {
  90. $this->storeSimpleEntry($path = '/test?x=y&p=q');
  91. $hitsReq = Request::create($path);
  92. $missReq = Request::create('/test?p=x');
  93. $this->assertNotNull($this->store->lookup($hitsReq));
  94. $this->assertNull($this->store->lookup($missReq));
  95. }
  96. public function testDoesNotFindAnEntryWithLookupWhenTheBodyDoesNotExist()
  97. {
  98. $this->storeSimpleEntry();
  99. $this->assertNotNull($this->response->headers->get('X-Content-Digest'));
  100. $path = $this->getStorePath($this->response->headers->get('X-Content-Digest'));
  101. @unlink($path);
  102. $this->assertNull($this->store->lookup($this->request));
  103. }
  104. public function testRestoresResponseHeadersProperlyWithLookup()
  105. {
  106. $this->storeSimpleEntry();
  107. $response = $this->store->lookup($this->request);
  108. $this->assertEquals($response->headers->all(), array_merge(array('content-length' => 4, 'x-body-file' => array($this->getStorePath($response->headers->get('X-Content-Digest')))), $this->response->headers->all()));
  109. }
  110. public function testRestoresResponseContentFromEntityStoreWithLookup()
  111. {
  112. $this->storeSimpleEntry();
  113. $response = $this->store->lookup($this->request);
  114. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test')), $response->getContent());
  115. }
  116. public function testInvalidatesMetaAndEntityStoreEntriesWithInvalidate()
  117. {
  118. $this->storeSimpleEntry();
  119. $this->store->invalidate($this->request);
  120. $response = $this->store->lookup($this->request);
  121. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  122. $this->assertFalse($response->isFresh());
  123. }
  124. public function testSucceedsQuietlyWhenInvalidateCalledWithNoMatchingEntries()
  125. {
  126. $req = Request::create('/test');
  127. $this->store->invalidate($req);
  128. $this->assertNull($this->store->lookup($this->request));
  129. }
  130. public function testDoesNotReturnEntriesThatVaryWithLookup()
  131. {
  132. $req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  133. $req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
  134. $res = new Response('test', 200, array('Vary' => 'Foo Bar'));
  135. $this->store->write($req1, $res);
  136. $this->assertNull($this->store->lookup($req2));
  137. }
  138. public function testDoesNotReturnEntriesThatSlightlyVaryWithLookup()
  139. {
  140. $req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  141. $req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bam'));
  142. $res = new Response('test', 200, array('Vary' => array('Foo', 'Bar')));
  143. $this->store->write($req1, $res);
  144. $this->assertNull($this->store->lookup($req2));
  145. }
  146. public function testStoresMultipleResponsesForEachVaryCombination()
  147. {
  148. $req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  149. $res1 = new Response('test 1', 200, array('Vary' => 'Foo Bar'));
  150. $key = $this->store->write($req1, $res1);
  151. $req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
  152. $res2 = new Response('test 2', 200, array('Vary' => 'Foo Bar'));
  153. $this->store->write($req2, $res2);
  154. $req3 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Baz', 'HTTP_BAR' => 'Boom'));
  155. $res3 = new Response('test 3', 200, array('Vary' => 'Foo Bar'));
  156. $this->store->write($req3, $res3);
  157. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->getContent());
  158. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->getContent());
  159. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->getContent());
  160. $this->assertCount(3, $this->getStoreMetadata($key));
  161. }
  162. public function testOverwritesNonVaryingResponseWithStore()
  163. {
  164. $req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  165. $res1 = new Response('test 1', 200, array('Vary' => 'Foo Bar'));
  166. $key = $this->store->write($req1, $res1);
  167. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->getContent());
  168. $req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
  169. $res2 = new Response('test 2', 200, array('Vary' => 'Foo Bar'));
  170. $this->store->write($req2, $res2);
  171. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->getContent());
  172. $req3 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  173. $res3 = new Response('test 3', 200, array('Vary' => 'Foo Bar'));
  174. $key = $this->store->write($req3, $res3);
  175. $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->getContent());
  176. $this->assertCount(2, $this->getStoreMetadata($key));
  177. }
  178. public function testLocking()
  179. {
  180. $req = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
  181. $this->assertTrue($this->store->lock($req));
  182. $path = $this->store->lock($req);
  183. $this->assertTrue($this->store->isLocked($req));
  184. $this->store->unlock($req);
  185. $this->assertFalse($this->store->isLocked($req));
  186. }
  187. public function testPurgeHttps()
  188. {
  189. $request = Request::create('https://example.com/foo');
  190. $this->store->write($request, new Response('foo'));
  191. $this->assertNotEmpty($this->getStoreMetadata($request));
  192. $this->assertTrue($this->store->purge('https://example.com/foo'));
  193. $this->assertEmpty($this->getStoreMetadata($request));
  194. }
  195. public function testPurgeHttpAndHttps()
  196. {
  197. $requestHttp = Request::create('https://example.com/foo');
  198. $this->store->write($requestHttp, new Response('foo'));
  199. $requestHttps = Request::create('http://example.com/foo');
  200. $this->store->write($requestHttps, new Response('foo'));
  201. $this->assertNotEmpty($this->getStoreMetadata($requestHttp));
  202. $this->assertNotEmpty($this->getStoreMetadata($requestHttps));
  203. $this->assertTrue($this->store->purge('http://example.com/foo'));
  204. $this->assertEmpty($this->getStoreMetadata($requestHttp));
  205. $this->assertEmpty($this->getStoreMetadata($requestHttps));
  206. }
  207. protected function storeSimpleEntry($path = null, $headers = array())
  208. {
  209. if (null === $path) {
  210. $path = '/test';
  211. }
  212. $this->request = Request::create($path, 'get', array(), array(), array(), $headers);
  213. $this->response = new Response('test', 200, array('Cache-Control' => 'max-age=420'));
  214. return $this->store->write($this->request, $this->response);
  215. }
  216. protected function getStoreMetadata($key)
  217. {
  218. $r = new \ReflectionObject($this->store);
  219. $m = $r->getMethod('getMetadata');
  220. $m->setAccessible(true);
  221. if ($key instanceof Request) {
  222. $m1 = $r->getMethod('getCacheKey');
  223. $m1->setAccessible(true);
  224. $key = $m1->invoke($this->store, $key);
  225. }
  226. return $m->invoke($this->store, $key);
  227. }
  228. protected function getStorePath($key)
  229. {
  230. $r = new \ReflectionObject($this->store);
  231. $m = $r->getMethod('getPath');
  232. $m->setAccessible(true);
  233. return $m->invoke($this->store, $key);
  234. }
  235. }