EsiTest.php 8.0 KB

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