This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpKernel/Tests/HttpCache/EsiTest.php

248 lines
9.0 KiB
PHP
Raw Normal View History

2010-11-02 19:00:18 +00:00
<?php
/*
* This file is part of the Symfony package.
2010-11-02 19:00:18 +00:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-11-02 19:00:18 +00:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Tests\HttpCache;
2010-11-02 19:00:18 +00:00
use Symfony\Component\HttpKernel\HttpCache\Esi;
2010-11-02 19:00:18 +00:00
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class EsiTest extends \PHPUnit_Framework_TestCase
{
public function testHasSurrogateEsiCapability()
{
$esi = new Esi();
$request = Request::create('/');
$request->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
2014-03-23 00:04:57 +00:00
$this->assertTrue($esi->hasSurrogateCapability($request));
2010-11-02 19:00:18 +00:00
$request = Request::create('/');
$request->headers->set('Surrogate-Capability', 'foobar');
2014-03-23 00:04:57 +00:00
$this->assertFalse($esi->hasSurrogateCapability($request));
2010-11-02 19:00:18 +00:00
$request = Request::create('/');
2014-03-23 00:04:57 +00:00
$this->assertFalse($esi->hasSurrogateCapability($request));
2010-11-02 19:00:18 +00:00
}
public function testAddSurrogateEsiCapability()
{
$esi = new Esi();
$request = Request::create('/');
2014-03-23 00:04:57 +00:00
$esi->addSurrogateCapability($request);
2010-11-02 19:00:18 +00:00
$this->assertEquals('symfony2="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
2014-03-23 00:04:57 +00:00
$esi->addSurrogateCapability($request);
2010-11-02 19:00:18 +00:00
$this->assertEquals('symfony2="ESI/1.0", symfony2="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
}
public function testAddSurrogateControl()
{
$esi = new Esi();
$response = new Response('foo <esi:include src="" />');
$esi->addSurrogateControl($response);
$this->assertEquals('content="ESI/1.0"', $response->headers->get('Surrogate-Control'));
$response = new Response('foo');
$esi->addSurrogateControl($response);
$this->assertEquals('', $response->headers->get('Surrogate-Control'));
}
public function testNeedsEsiParsing()
{
$esi = new Esi();
$response = new Response();
$response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
2014-03-23 00:04:57 +00:00
$this->assertTrue($esi->needsParsing($response));
2010-11-02 19:00:18 +00:00
$response = new Response();
2014-03-23 00:04:57 +00:00
$this->assertFalse($esi->needsParsing($response));
2010-11-02 19:00:18 +00:00
}
public function testRenderIncludeTag()
{
$esi = new Esi();
$this->assertEquals('<esi:include src="/" onerror="continue" alt="/alt" />', $esi->renderIncludeTag('/', '/alt', true));
$this->assertEquals('<esi:include src="/" alt="/alt" />', $esi->renderIncludeTag('/', '/alt', false));
$this->assertEquals('<esi:include src="/" onerror="continue" />', $esi->renderIncludeTag('/'));
$this->assertEquals('<esi:comment text="some comment" />'."\n".'<esi:include src="/" onerror="continue" alt="/alt" />', $esi->renderIncludeTag('/', '/alt', true, 'some comment'));
}
public function testProcessDoesNothingIfContentTypeIsNotHtml()
{
$esi = new Esi();
$request = Request::create('/');
$response = new Response();
$response->headers->set('Content-Type', 'text/plain');
$esi->process($request, $response);
$this->assertFalse($response->headers->has('x-body-eval'));
2010-11-02 19:00:18 +00:00
}
public function testMultilineEsiRemoveTagsAreRemoved()
{
$esi = new Esi();
$request = Request::create('/');
$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");
$esi->process($request, $response);
$this->assertEquals(' Keep this And this', $response->getContent());
}
public function testCommentTagsAreRemoved()
{
$esi = new Esi();
$request = Request::create('/');
$response = new Response('<esi:comment text="some comment &gt;" /> Keep this');
$esi->process($request, $response);
$this->assertEquals(' Keep this', $response->getContent());
}
2010-11-02 19:00:18 +00:00
public function testProcess()
{
$esi = new Esi();
$request = Request::create('/');
$response = new Response('foo <esi:comment text="some comment" /><esi:include src="..." alt="alt" onerror="continue" />');
$esi->process($request, $response);
2014-03-23 00:04:57 +00:00
$this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'alt\', true) ?>'."\n", $response->getContent());
$this->assertEquals('ESI', $response->headers->get('x-body-eval'));
2010-11-02 19:00:18 +00:00
2014-09-03 21:47:06 +01:00
$response = new Response('foo <esi:comment text="some comment" /><esi:include src="foo\'" alt="bar\'" onerror="continue" />');
$esi->process($request, $response);
$this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'foo\\\'\', \'bar\\\'\', true) ?>'."\n", $response->getContent());
2014-09-03 21:47:06 +01:00
2010-11-02 19:00:18 +00:00
$response = new Response('foo <esi:include src="..." />');
$esi->process($request, $response);
2014-03-23 00:04:57 +00:00
$this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
$response = new Response('foo <esi:include src="..."></esi:include>');
$esi->process($request, $response);
2014-03-23 00:04:57 +00:00
$this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
2010-11-02 19:00:18 +00:00
}
public function testProcessEscapesPhpTags()
{
$esi = new Esi();
$request = Request::create('/');
2015-03-16 14:12:02 +00:00
$response = new Response('<?php <? <% <script language=php>');
$esi->process($request, $response);
2015-03-16 14:12:02 +00:00
$this->assertEquals('<?php echo "<?"; ?>php <?php echo "<?"; ?> <?php echo "<%"; ?> <?php echo "<s"; ?>cript language=php>', $response->getContent());
}
2010-11-02 19:00:18 +00:00
/**
2013-11-25 08:44:14 +00:00
* @expectedException \RuntimeException
2010-11-02 19:00:18 +00:00
*/
public function testProcessWhenNoSrcInAnEsi()
{
$esi = new Esi();
$request = Request::create('/');
$response = new Response('foo <esi:include />');
$esi->process($request, $response);
}
public function testProcessRemoveSurrogateControlHeader()
{
$esi = new Esi();
$request = Request::create('/');
$response = new Response('foo <esi:include src="..." />');
$response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
$esi->process($request, $response);
$this->assertEquals('ESI', $response->headers->get('x-body-eval'));
2010-11-02 19:00:18 +00:00
$response->headers->set('Surrogate-Control', 'no-store, content="ESI/1.0"');
$esi->process($request, $response);
$this->assertEquals('ESI', $response->headers->get('x-body-eval'));
$this->assertEquals('no-store', $response->headers->get('surrogate-control'));
2010-11-02 19:00:18 +00:00
$response->headers->set('Surrogate-Control', 'content="ESI/1.0", no-store');
$esi->process($request, $response);
$this->assertEquals('ESI', $response->headers->get('x-body-eval'));
$this->assertEquals('no-store', $response->headers->get('surrogate-control'));
2010-11-02 19:00:18 +00:00
}
public function testHandle()
{
$esi = new Esi();
$cache = $this->getCache(Request::create('/'), new Response('foo'));
$this->assertEquals('foo', $esi->handle($cache, '/', '/alt', true));
}
/**
2013-11-25 08:44:14 +00:00
* @expectedException \RuntimeException
2010-11-02 19:00:18 +00:00
*/
public function testHandleWhenResponseIsNot200()
{
$esi = new Esi();
$response = new Response('foo');
$response->setStatusCode(404);
$cache = $this->getCache(Request::create('/'), $response);
$esi->handle($cache, '/', '/alt', false);
}
public function testHandleWhenResponseIsNot200AndErrorsAreIgnored()
{
$esi = new Esi();
$response = new Response('foo');
$response->setStatusCode(404);
$cache = $this->getCache(Request::create('/'), $response);
$this->assertEquals('', $esi->handle($cache, '/', '/alt', true));
}
public function testHandleWhenResponseIsNot200AndAltIsPresent()
{
$esi = new Esi();
$response1 = new Response('foo');
$response1->setStatusCode(404);
$response2 = new Response('bar');
$cache = $this->getCache(Request::create('/'), array($response1, $response2));
$this->assertEquals('bar', $esi->handle($cache, '/', '/alt', false));
}
protected function getCache($request, $response)
{
2016-12-19 09:02:29 +00:00
$cache = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\HttpCache')->setMethods(array('getRequest', 'handle'))->disableOriginalConstructor()->getMock();
2010-11-02 19:00:18 +00:00
$cache->expects($this->any())
->method('getRequest')
->will($this->returnValue($request))
;
if (is_array($response)) {
$cache->expects($this->any())
->method('handle')
->will(call_user_func_array(array($this, 'onConsecutiveCalls'), $response))
;
} else {
$cache->expects($this->any())
->method('handle')
->will($this->returnValue($response))
;
}
return $cache;
}
}