PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/myCore/lib/laravel/symfony/http-kernel/Symfony/Component/HttpKernel/Tests/HttpCache/EsiTest.php

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