SsiTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\HttpCache\Ssi;
  15. class SsiTest extends TestCase
  16. {
  17. public function testHasSurrogateSsiCapability()
  18. {
  19. $ssi = new Ssi();
  20. $request = Request::create('/');
  21. $request->headers->set('Surrogate-Capability', 'abc="SSI/1.0"');
  22. $this->assertTrue($ssi->hasSurrogateCapability($request));
  23. $request = Request::create('/');
  24. $request->headers->set('Surrogate-Capability', 'foobar');
  25. $this->assertFalse($ssi->hasSurrogateCapability($request));
  26. $request = Request::create('/');
  27. $this->assertFalse($ssi->hasSurrogateCapability($request));
  28. }
  29. public function testAddSurrogateSsiCapability()
  30. {
  31. $ssi = new Ssi();
  32. $request = Request::create('/');
  33. $ssi->addSurrogateCapability($request);
  34. $this->assertEquals('symfony="SSI/1.0"', $request->headers->get('Surrogate-Capability'));
  35. $ssi->addSurrogateCapability($request);
  36. $this->assertEquals('symfony="SSI/1.0", symfony="SSI/1.0"', $request->headers->get('Surrogate-Capability'));
  37. }
  38. public function testAddSurrogateControl()
  39. {
  40. $ssi = new Ssi();
  41. $response = new Response('foo <!--#include virtual="" -->');
  42. $ssi->addSurrogateControl($response);
  43. $this->assertEquals('content="SSI/1.0"', $response->headers->get('Surrogate-Control'));
  44. $response = new Response('foo');
  45. $ssi->addSurrogateControl($response);
  46. $this->assertEquals('', $response->headers->get('Surrogate-Control'));
  47. }
  48. public function testNeedsSsiParsing()
  49. {
  50. $ssi = new Ssi();
  51. $response = new Response();
  52. $response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
  53. $this->assertTrue($ssi->needsParsing($response));
  54. $response = new Response();
  55. $this->assertFalse($ssi->needsParsing($response));
  56. }
  57. public function testRenderIncludeTag()
  58. {
  59. $ssi = new Ssi();
  60. $this->assertEquals('<!--#include virtual="/" -->', $ssi->renderIncludeTag('/', '/alt', true));
  61. $this->assertEquals('<!--#include virtual="/" -->', $ssi->renderIncludeTag('/', '/alt', false));
  62. $this->assertEquals('<!--#include virtual="/" -->', $ssi->renderIncludeTag('/'));
  63. }
  64. public function testProcessDoesNothingIfContentTypeIsNotHtml()
  65. {
  66. $ssi = new Ssi();
  67. $request = Request::create('/');
  68. $response = new Response();
  69. $response->headers->set('Content-Type', 'text/plain');
  70. $ssi->process($request, $response);
  71. $this->assertFalse($response->headers->has('x-body-eval'));
  72. }
  73. public function testProcess()
  74. {
  75. $ssi = new Ssi();
  76. $request = Request::create('/');
  77. $response = new Response('foo <!--#include virtual="..." -->');
  78. $ssi->process($request, $response);
  79. $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
  80. $this->assertEquals('SSI', $response->headers->get('x-body-eval'));
  81. $response = new Response('foo <!--#include virtual="foo\'" -->');
  82. $ssi->process($request, $response);
  83. $this->assertEquals("foo <?php echo \$this->surrogate->handle(\$this, 'foo\\'', '', false) ?>"."\n", $response->getContent());
  84. }
  85. public function testProcessEscapesPhpTags()
  86. {
  87. $ssi = new Ssi();
  88. $request = Request::create('/');
  89. $response = new Response('<?php <? <% <script language=php>');
  90. $ssi->process($request, $response);
  91. $this->assertEquals('<?php echo "<?"; ?>php <?php echo "<?"; ?> <?php echo "<%"; ?> <?php echo "<s"; ?>cript language=php>', $response->getContent());
  92. }
  93. /**
  94. * @expectedException \RuntimeException
  95. */
  96. public function testProcessWhenNoSrcInAnSsi()
  97. {
  98. $ssi = new Ssi();
  99. $request = Request::create('/');
  100. $response = new Response('foo <!--#include -->');
  101. $ssi->process($request, $response);
  102. }
  103. public function testProcessRemoveSurrogateControlHeader()
  104. {
  105. $ssi = new Ssi();
  106. $request = Request::create('/');
  107. $response = new Response('foo <!--#include virtual="..." -->');
  108. $response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
  109. $ssi->process($request, $response);
  110. $this->assertEquals('SSI', $response->headers->get('x-body-eval'));
  111. $response->headers->set('Surrogate-Control', 'no-store, content="SSI/1.0"');
  112. $ssi->process($request, $response);
  113. $this->assertEquals('SSI', $response->headers->get('x-body-eval'));
  114. $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
  115. $response->headers->set('Surrogate-Control', 'content="SSI/1.0", no-store');
  116. $ssi->process($request, $response);
  117. $this->assertEquals('SSI', $response->headers->get('x-body-eval'));
  118. $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
  119. }
  120. public function testHandle()
  121. {
  122. $ssi = new Ssi();
  123. $cache = $this->getCache(Request::create('/'), new Response('foo'));
  124. $this->assertEquals('foo', $ssi->handle($cache, '/', '/alt', true));
  125. }
  126. /**
  127. * @expectedException \RuntimeException
  128. */
  129. public function testHandleWhenResponseIsNot200()
  130. {
  131. $ssi = new Ssi();
  132. $response = new Response('foo');
  133. $response->setStatusCode(404);
  134. $cache = $this->getCache(Request::create('/'), $response);
  135. $ssi->handle($cache, '/', '/alt', false);
  136. }
  137. public function testHandleWhenResponseIsNot200AndErrorsAreIgnored()
  138. {
  139. $ssi = new Ssi();
  140. $response = new Response('foo');
  141. $response->setStatusCode(404);
  142. $cache = $this->getCache(Request::create('/'), $response);
  143. $this->assertEquals('', $ssi->handle($cache, '/', '/alt', true));
  144. }
  145. public function testHandleWhenResponseIsNot200AndAltIsPresent()
  146. {
  147. $ssi = new Ssi();
  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', $ssi->handle($cache, '/', '/alt', false));
  153. }
  154. protected function getCache($request, $response)
  155. {
  156. $cache = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\HttpCache')->setMethods(array('getRequest', 'handle'))->disableOriginalConstructor()->getMock();
  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. }