No Description

EsiTest.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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('symfony2="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
  34. $esi->addSurrogateCapability($request);
  35. $this->assertEquals('symfony2="ESI/1.0", symfony2="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 testProcess()
  74. {
  75. $esi = new Esi();
  76. $request = Request::create('/');
  77. $response = new Response('foo <esi:comment text="some comment" /><esi:include src="..." alt="alt" onerror="continue" />');
  78. $esi->process($request, $response);
  79. $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'alt\', true) ?>'."\n", $response->getContent());
  80. $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
  81. $response = new Response('foo <esi:comment text="some comment" /><esi:include src="foo\'" alt="bar\'" onerror="continue" />');
  82. $esi->process($request, $response);
  83. $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'foo\\\'\', \'bar\\\'\', true) ?>'."\n", $response->getContent());
  84. $response = new Response('foo <esi:include src="..." />');
  85. $esi->process($request, $response);
  86. $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
  87. $response = new Response('foo <esi:include src="..."></esi:include>');
  88. $esi->process($request, $response);
  89. $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
  90. }
  91. public function testProcessEscapesPhpTags()
  92. {
  93. $esi = new Esi();
  94. $request = Request::create('/');
  95. $response = new Response('foo <?php die("foo"); ?><%= "lala" %>');
  96. $esi->process($request, $response);
  97. $this->assertEquals('foo <?php echo "<?"; ?>php die("foo"); ?><?php echo "<%"; ?>= "lala" %>', $response->getContent());
  98. }
  99. /**
  100. * @expectedException \RuntimeException
  101. */
  102. public function testProcessWhenNoSrcInAnEsi()
  103. {
  104. $esi = new Esi();
  105. $request = Request::create('/');
  106. $response = new Response('foo <esi:include />');
  107. $esi->process($request, $response);
  108. }
  109. public function testProcessRemoveSurrogateControlHeader()
  110. {
  111. $esi = new Esi();
  112. $request = Request::create('/');
  113. $response = new Response('foo <esi:include src="..." />');
  114. $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
  115. $esi->process($request, $response);
  116. $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
  117. $response->headers->set('Surrogate-Control', 'no-store, content="ESI/1.0"');
  118. $esi->process($request, $response);
  119. $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
  120. $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
  121. $response->headers->set('Surrogate-Control', 'content="ESI/1.0", no-store');
  122. $esi->process($request, $response);
  123. $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
  124. $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
  125. }
  126. public function testHandle()
  127. {
  128. $esi = new Esi();
  129. $cache = $this->getCache(Request::create('/'), new Response('foo'));
  130. $this->assertEquals('foo', $esi->handle($cache, '/', '/alt', true));
  131. }
  132. /**
  133. * @expectedException \RuntimeException
  134. */
  135. public function testHandleWhenResponseIsNot200()
  136. {
  137. $esi = new Esi();
  138. $response = new Response('foo');
  139. $response->setStatusCode(404);
  140. $cache = $this->getCache(Request::create('/'), $response);
  141. $esi->handle($cache, '/', '/alt', false);
  142. }
  143. public function testHandleWhenResponseIsNot200AndErrorsAreIgnored()
  144. {
  145. $esi = new Esi();
  146. $response = new Response('foo');
  147. $response->setStatusCode(404);
  148. $cache = $this->getCache(Request::create('/'), $response);
  149. $this->assertEquals('', $esi->handle($cache, '/', '/alt', true));
  150. }
  151. public function testHandleWhenResponseIsNot200AndAltIsPresent()
  152. {
  153. $esi = new Esi();
  154. $response1 = new Response('foo');
  155. $response1->setStatusCode(404);
  156. $response2 = new Response('bar');
  157. $cache = $this->getCache(Request::create('/'), array($response1, $response2));
  158. $this->assertEquals('bar', $esi->handle($cache, '/', '/alt', false));
  159. }
  160. protected function getCache($request, $response)
  161. {
  162. $cache = $this->getMock('Symfony\Component\HttpKernel\HttpCache\HttpCache', array('getRequest', 'handle'), array(), '', false);
  163. $cache->expects($this->any())
  164. ->method('getRequest')
  165. ->will($this->returnValue($request))
  166. ;
  167. if (is_array($response)) {
  168. $cache->expects($this->any())
  169. ->method('handle')
  170. ->will(call_user_func_array(array($this, 'onConsecutiveCalls'), $response))
  171. ;
  172. } else {
  173. $cache->expects($this->any())
  174. ->method('handle')
  175. ->will($this->returnValue($response))
  176. ;
  177. }
  178. return $cache;
  179. }
  180. }