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