Ei kuvausta

EsiTest.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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\Esi;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class EsiTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testHasSurrogateEsiCapability()
  17. {
  18. $esi = new Esi();
  19. $request = Request::create('/');
  20. $request->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
  21. $this->assertTrue($esi->hasSurrogateCapability($request));
  22. $request = Request::create('/');
  23. $request->headers->set('Surrogate-Capability', 'foobar');
  24. $this->assertFalse($esi->hasSurrogateCapability($request));
  25. $request = Request::create('/');
  26. $this->assertFalse($esi->hasSurrogateCapability($request));
  27. }
  28. public function testAddSurrogateEsiCapability()
  29. {
  30. $esi = new Esi();
  31. $request = Request::create('/');
  32. $esi->addSurrogateCapability($request);
  33. $this->assertEquals('symfony="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
  34. $esi->addSurrogateCapability($request);
  35. $this->assertEquals('symfony="ESI/1.0", symfony="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
  36. }
  37. public function testAddSurrogateControl()
  38. {
  39. $esi = new Esi();
  40. $response = new Response('foo <esi:include src="" />');
  41. $esi->addSurrogateControl($response);
  42. $this->assertEquals('content="ESI/1.0"', $response->headers->get('Surrogate-Control'));
  43. $response = new Response('foo');
  44. $esi->addSurrogateControl($response);
  45. $this->assertEquals('', $response->headers->get('Surrogate-Control'));
  46. }
  47. public function testNeedsEsiParsing()
  48. {
  49. $esi = new Esi();
  50. $response = new Response();
  51. $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
  52. $this->assertTrue($esi->needsParsing($response));
  53. $response = new Response();
  54. $this->assertFalse($esi->needsParsing($response));
  55. }
  56. public function testRenderIncludeTag()
  57. {
  58. $esi = new Esi();
  59. $this->assertEquals('<esi:include src="/" onerror="continue" alt="/alt" />', $esi->renderIncludeTag('/', '/alt', true));
  60. $this->assertEquals('<esi:include src="/" alt="/alt" />', $esi->renderIncludeTag('/', '/alt', false));
  61. $this->assertEquals('<esi:include src="/" onerror="continue" />', $esi->renderIncludeTag('/'));
  62. $this->assertEquals('<esi:comment text="some comment" />'."\n".'<esi:include src="/" onerror="continue" alt="/alt" />', $esi->renderIncludeTag('/', '/alt', true, 'some comment'));
  63. }
  64. public function testProcessDoesNothingIfContentTypeIsNotHtml()
  65. {
  66. $esi = new Esi();
  67. $request = Request::create('/');
  68. $response = new Response();
  69. $response->headers->set('Content-Type', 'text/plain');
  70. $esi->process($request, $response);
  71. $this->assertFalse($response->headers->has('x-body-eval'));
  72. }
  73. public function testMultilineEsiRemoveTagsAreRemoved()
  74. {
  75. $esi = new Esi();
  76. $request = Request::create('/');
  77. $response = new Response('<esi:remove> <a href="http://www.example.com">www.example.com</a> </esi:remove> Keep this'."<esi:remove>\n <a>www.example.com</a> </esi:remove> And this");
  78. $esi->process($request, $response);
  79. $this->assertEquals(' Keep this And this', $response->getContent());
  80. }
  81. public function testCommentTagsAreRemoved()
  82. {
  83. $esi = new Esi();
  84. $request = Request::create('/');
  85. $response = new Response('<esi:comment text="some comment &gt;" /> Keep this');
  86. $esi->process($request, $response);
  87. $this->assertEquals(' Keep this', $response->getContent());
  88. }
  89. public function testProcess()
  90. {
  91. $esi = new Esi();
  92. $request = Request::create('/');
  93. $response = new Response('foo <esi:comment text="some comment" /><esi:include src="..." alt="alt" onerror="continue" />');
  94. $esi->process($request, $response);
  95. $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'alt\', true) ?>'."\n", $response->getContent());
  96. $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
  97. $response = new Response('foo <esi:comment text="some comment" /><esi:include src="foo\'" alt="bar\'" onerror="continue" />');
  98. $esi->process($request, $response);
  99. $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'foo\\\'\', \'bar\\\'\', true) ?>'."\n", $response->getContent());
  100. $response = new Response('foo <esi:include src="..." />');
  101. $esi->process($request, $response);
  102. $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
  103. $response = new Response('foo <esi:include src="..."></esi:include>');
  104. $esi->process($request, $response);
  105. $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
  106. }
  107. public function testProcessEscapesPhpTags()
  108. {
  109. $esi = new Esi();
  110. $request = Request::create('/');
  111. $response = new Response('<?php <? <% <script language=php>');
  112. $esi->process($request, $response);
  113. $this->assertEquals('<?php echo "<?"; ?>php <?php echo "<?"; ?> <?php echo "<%"; ?> <?php echo "<s"; ?>cript language=php>', $response->getContent());
  114. }
  115. /**
  116. * @expectedException \RuntimeException
  117. */
  118. public function testProcessWhenNoSrcInAnEsi()
  119. {
  120. $esi = new Esi();
  121. $request = Request::create('/');
  122. $response = new Response('foo <esi:include />');
  123. $esi->process($request, $response);
  124. }
  125. public function testProcessRemoveSurrogateControlHeader()
  126. {
  127. $esi = new Esi();
  128. $request = Request::create('/');
  129. $response = new Response('foo <esi:include src="..." />');
  130. $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
  131. $esi->process($request, $response);
  132. $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
  133. $response->headers->set('Surrogate-Control', 'no-store, content="ESI/1.0"');
  134. $esi->process($request, $response);
  135. $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
  136. $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
  137. $response->headers->set('Surrogate-Control', 'content="ESI/1.0", no-store');
  138. $esi->process($request, $response);
  139. $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
  140. $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
  141. }
  142. public function testHandle()
  143. {
  144. $esi = new Esi();
  145. $cache = $this->getCache(Request::create('/'), new Response('foo'));
  146. $this->assertEquals('foo', $esi->handle($cache, '/', '/alt', true));
  147. }
  148. /**
  149. * @expectedException \RuntimeException
  150. */
  151. public function testHandleWhenResponseIsNot200()
  152. {
  153. $esi = new Esi();
  154. $response = new Response('foo');
  155. $response->setStatusCode(404);
  156. $cache = $this->getCache(Request::create('/'), $response);
  157. $esi->handle($cache, '/', '/alt', false);
  158. }
  159. public function testHandleWhenResponseIsNot200AndErrorsAreIgnored()
  160. {
  161. $esi = new Esi();
  162. $response = new Response('foo');
  163. $response->setStatusCode(404);
  164. $cache = $this->getCache(Request::create('/'), $response);
  165. $this->assertEquals('', $esi->handle($cache, '/', '/alt', true));
  166. }
  167. public function testHandleWhenResponseIsNot200AndAltIsPresent()
  168. {
  169. $esi = new Esi();
  170. $response1 = new Response('foo');
  171. $response1->setStatusCode(404);
  172. $response2 = new Response('bar');
  173. $cache = $this->getCache(Request::create('/'), array($response1, $response2));
  174. $this->assertEquals('bar', $esi->handle($cache, '/', '/alt', false));
  175. }
  176. protected function getCache($request, $response)
  177. {
  178. $cache = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\HttpCache')->setMethods(array('getRequest', 'handle'))->disableOriginalConstructor()->getMock();
  179. $cache->expects($this->any())
  180. ->method('getRequest')
  181. ->will($this->returnValue($request))
  182. ;
  183. if (is_array($response)) {
  184. $cache->expects($this->any())
  185. ->method('handle')
  186. ->will(call_user_func_array(array($this, 'onConsecutiveCalls'), $response))
  187. ;
  188. } else {
  189. $cache->expects($this->any())
  190. ->method('handle')
  191. ->will($this->returnValue($response))
  192. ;
  193. }
  194. return $cache;
  195. }
  196. }