4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\HttpCache;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpKernel\HttpCache\Esi;
19 class EsiTest extends TestCase
21 public function testHasSurrogateEsiCapability()
25 $request = Request::create('/');
26 $request->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
27 $this->assertTrue($esi->hasSurrogateCapability($request));
29 $request = Request::create('/');
30 $request->headers->set('Surrogate-Capability', 'foobar');
31 $this->assertFalse($esi->hasSurrogateCapability($request));
33 $request = Request::create('/');
34 $this->assertFalse($esi->hasSurrogateCapability($request));
37 public function testAddSurrogateEsiCapability()
41 $request = Request::create('/');
42 $esi->addSurrogateCapability($request);
43 $this->assertEquals('symfony="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
45 $esi->addSurrogateCapability($request);
46 $this->assertEquals('symfony="ESI/1.0", symfony="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
49 public function testAddSurrogateControl()
53 $response = new Response('foo <esi:include src="" />');
54 $esi->addSurrogateControl($response);
55 $this->assertEquals('content="ESI/1.0"', $response->headers->get('Surrogate-Control'));
57 $response = new Response('foo');
58 $esi->addSurrogateControl($response);
59 $this->assertEquals('', $response->headers->get('Surrogate-Control'));
62 public function testNeedsEsiParsing()
66 $response = new Response();
67 $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
68 $this->assertTrue($esi->needsParsing($response));
70 $response = new Response();
71 $this->assertFalse($esi->needsParsing($response));
74 public function testRenderIncludeTag()
78 $this->assertEquals('<esi:include src="/" onerror="continue" alt="/alt" />', $esi->renderIncludeTag('/', '/alt', true));
79 $this->assertEquals('<esi:include src="/" alt="/alt" />', $esi->renderIncludeTag('/', '/alt', false));
80 $this->assertEquals('<esi:include src="/" onerror="continue" />', $esi->renderIncludeTag('/'));
81 $this->assertEquals('<esi:comment text="some comment" />'."\n".'<esi:include src="/" onerror="continue" alt="/alt" />', $esi->renderIncludeTag('/', '/alt', true, 'some comment'));
84 public function testProcessDoesNothingIfContentTypeIsNotHtml()
88 $request = Request::create('/');
89 $response = new Response();
90 $response->headers->set('Content-Type', 'text/plain');
91 $esi->process($request, $response);
93 $this->assertFalse($response->headers->has('x-body-eval'));
96 public function testMultilineEsiRemoveTagsAreRemoved()
100 $request = Request::create('/');
101 $response = new Response('<esi:remove> <a href="http://www.example.com">www.example.com</a> </esi:remove> Keep this'."<esi:remove>\n <a>www.example.com</a> </esi:remove> And this");
102 $esi->process($request, $response);
104 $this->assertEquals(' Keep this And this', $response->getContent());
107 public function testCommentTagsAreRemoved()
111 $request = Request::create('/');
112 $response = new Response('<esi:comment text="some comment >" /> Keep this');
113 $esi->process($request, $response);
115 $this->assertEquals(' Keep this', $response->getContent());
118 public function testProcess()
122 $request = Request::create('/');
123 $response = new Response('foo <esi:comment text="some comment" /><esi:include src="..." alt="alt" onerror="continue" />');
124 $esi->process($request, $response);
126 $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'alt\', true) ?>'."\n", $response->getContent());
127 $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
129 $response = new Response('foo <esi:comment text="some comment" /><esi:include src="foo\'" alt="bar\'" onerror="continue" />');
130 $esi->process($request, $response);
132 $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'foo\\\'\', \'bar\\\'\', true) ?>'."\n", $response->getContent());
134 $response = new Response('foo <esi:include src="..." />');
135 $esi->process($request, $response);
137 $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
139 $response = new Response('foo <esi:include src="..."></esi:include>');
140 $esi->process($request, $response);
142 $this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
145 public function testProcessEscapesPhpTags()
149 $request = Request::create('/');
150 $response = new Response('<?php <? <% <script language=php>');
151 $esi->process($request, $response);
153 $this->assertEquals('<?php echo "<?"; ?>php <?php echo "<?"; ?> <?php echo "<%"; ?> <?php echo "<s"; ?>cript language=php>', $response->getContent());
157 * @expectedException \RuntimeException
159 public function testProcessWhenNoSrcInAnEsi()
163 $request = Request::create('/');
164 $response = new Response('foo <esi:include />');
165 $esi->process($request, $response);
168 public function testProcessRemoveSurrogateControlHeader()
172 $request = Request::create('/');
173 $response = new Response('foo <esi:include src="..." />');
174 $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
175 $esi->process($request, $response);
176 $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
178 $response->headers->set('Surrogate-Control', 'no-store, content="ESI/1.0"');
179 $esi->process($request, $response);
180 $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
181 $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
183 $response->headers->set('Surrogate-Control', 'content="ESI/1.0", no-store');
184 $esi->process($request, $response);
185 $this->assertEquals('ESI', $response->headers->get('x-body-eval'));
186 $this->assertEquals('no-store', $response->headers->get('surrogate-control'));
189 public function testHandle()
192 $cache = $this->getCache(Request::create('/'), new Response('foo'));
193 $this->assertEquals('foo', $esi->handle($cache, '/', '/alt', true));
197 * @expectedException \RuntimeException
199 public function testHandleWhenResponseIsNot200()
202 $response = new Response('foo');
203 $response->setStatusCode(404);
204 $cache = $this->getCache(Request::create('/'), $response);
205 $esi->handle($cache, '/', '/alt', false);
208 public function testHandleWhenResponseIsNot200AndErrorsAreIgnored()
211 $response = new Response('foo');
212 $response->setStatusCode(404);
213 $cache = $this->getCache(Request::create('/'), $response);
214 $this->assertEquals('', $esi->handle($cache, '/', '/alt', true));
217 public function testHandleWhenResponseIsNot200AndAltIsPresent()
220 $response1 = new Response('foo');
221 $response1->setStatusCode(404);
222 $response2 = new Response('bar');
223 $cache = $this->getCache(Request::create('/'), array($response1, $response2));
224 $this->assertEquals('bar', $esi->handle($cache, '/', '/alt', false));
227 protected function getCache($request, $response)
229 $cache = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\HttpCache')->setMethods(array('getRequest', 'handle'))->disableOriginalConstructor()->getMock();
230 $cache->expects($this->any())
231 ->method('getRequest')
232 ->will($this->returnValue($request))
234 if (\is_array($response)) {
235 $cache->expects($this->any())
237 ->will(\call_user_func_array(array($this, 'onConsecutiveCalls'), $response))
240 $cache->expects($this->any())
242 ->will($this->returnValue($response))