Brak opisu

SsiTest.php 7.4KB

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