EsiTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. /**
  86. * @expectedException RuntimeException
  87. */
  88. public function testProcessWhenNoSrcInAnEsi()
  89. {
  90. $esi = new Esi();
  91. $request = Request::create('/');
  92. $response = new Response('foo <esi:include />');
  93. $esi->process($request, $response);
  94. }
  95. public function testProcessRemoveSurrogateControlHeader()
  96. {
  97. $esi = new Esi();
  98. $request = Request::create('/');
  99. $response = new Response('foo <esi:include src="..." />');
  100. $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
  101. $esi->process($request, $response);
  102. $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
  103. $response->headers->set('Surrogate-Control', 'no-store, content="ESI/1.0"');
  104. $esi->process($request, $response);
  105. $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
  106. $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
  107. $response->headers->set('Surrogate-Control', 'content="ESI/1.0", no-store');
  108. $esi->process($request, $response);
  109. $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
  110. $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
  111. }
  112. public function testHandle()
  113. {
  114. $esi = new Esi();
  115. $cache = $this->getCache(Request::create('/'), new Response('foo'));
  116. $this->assertEquals('foo', $esi->handle($cache, '/', '/alt', true));
  117. }
  118. /**
  119. * @expectedException RuntimeException
  120. */
  121. public function testHandleWhenResponseIsNot200()
  122. {
  123. $esi = new Esi();
  124. $response = new Response('foo');
  125. $response->setStatusCode(404);
  126. $cache = $this->getCache(Request::create('/'), $response);
  127. $esi->handle($cache, '/', '/alt', false);
  128. }
  129. public function testHandleWhenResponseIsNot200AndErrorsAreIgnored()
  130. {
  131. $esi = new Esi();
  132. $response = new Response('foo');
  133. $response->setStatusCode(404);
  134. $cache = $this->getCache(Request::create('/'), $response);
  135. $this->assertEquals('', $esi->handle($cache, '/', '/alt', true));
  136. }
  137. public function testHandleWhenResponseIsNot200AndAltIsPresent()
  138. {
  139. $esi = new Esi();
  140. $response1 = new Response('foo');
  141. $response1->setStatusCode(404);
  142. $response2 = new Response('bar');
  143. $cache = $this->getCache(Request::create('/'), array($response1, $response2));
  144. $this->assertEquals('bar', $esi->handle($cache, '/', '/alt', false));
  145. }
  146. protected function getCache($request, $response)
  147. {
  148. $cache = $this->getMock('Symfony\Component\HttpKernel\HttpCache\HttpCache', array('getRequest', 'handle'), array(), '', false);
  149. $cache->expects($this->any())
  150. ->method('getRequest')
  151. ->will($this->returnValue($request))
  152. ;
  153. if (is_array($response)) {
  154. $cache->expects($this->any())
  155. ->method('handle')
  156. ->will(call_user_func_array(array($this, 'onConsecutiveCalls'), $response))
  157. ;
  158. } else {
  159. $cache->expects($this->any())
  160. ->method('handle')
  161. ->will($this->returnValue($response))
  162. ;
  163. }
  164. return $cache;
  165. }
  166. }