EsiTest.php 7.6 KB

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