[HttpKernel] added unit tests for ESI

This commit is contained in:
Fabien Potencier 2010-11-02 20:00:18 +01:00
parent 556bfcb804
commit 5bd03e1c58
5 changed files with 294 additions and 20 deletions

View File

@ -145,7 +145,7 @@ class ControllerResolver extends BaseControllerResolver
$alt = $this->generateInternalUri($options['alt'][0], isset($options['alt'][1]) ? $options['alt'][1] : array(), isset($options['alt'][2]) ? $options['alt'][2] : array());
}
return $this->container->getEsiService()->renderTag($uri, $alt, $options['ignore_errors'], $options['comment']);
return $this->container->getEsiService()->renderIncludeTag($uri, $alt, $options['ignore_errors'], $options['comment']);
}
$request = $this->container->getRequestService();

View File

@ -54,7 +54,7 @@ class Esi
return false;
}
return preg_match('#ESI/1.0#', $value);
return (Boolean) preg_match('#ESI/1.0#', $value);
}
/**
@ -97,7 +97,7 @@ class Esi
return false;
}
return preg_match('#content="[^"]*ESI/1.0[^"]*"#', $control);
return (Boolean) preg_match('#content="[^"]*ESI/1.0[^"]*"#', $control);
}
/**
@ -108,7 +108,7 @@ class Esi
* @param Boolean $ignoreErrors Whether to ignore errors or not
* @param string $comment A comment to add as an esi:include tag
*/
public function renderTag($uri, $alt, $ignoreErrors = true, $comment = '')
public function renderIncludeTag($uri, $alt = null, $ignoreErrors = true, $comment = '')
{
$html = sprintf('<esi:include src="%s"%s%s />',
$uri,
@ -117,7 +117,7 @@ class Esi
);
if (!empty($comment)) {
$html .= sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $output);
return sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $html);
}
return $html;
@ -144,7 +144,7 @@ class Esi
// we don't use a proper XML parser here as we can have ESI tags in a plain text response
$content = $response->getContent();
$content = preg_replace_callback('#<esi\:include\s+(.+?)\s*/>#', array($this, 'handleEsiIncludeTag'), $content);
$content = preg_replace_callback('#<esi\:include\s+(.*?)\s*/>#', array($this, 'handleEsiIncludeTag'), $content);
$content = preg_replace('#<esi\:comment[^>]*/>#', '', $content);
$content = preg_replace('#<esi\:remove>.*?</esi\:remove>#', '', $content);
@ -152,11 +152,15 @@ class Esi
$response->headers->set('X-Body-Eval', 'ESI');
// remove ESI/1.0 from the Surrogate-Control header
$value = $response->headers->get('Surrogate-Control');
if (preg_match('#^content="ESI/1.0"$#', $value)) {
$response->headers->delete('Surrogate-Control');
} else {
$response->headers->set('Surrogate-Control', preg_replace('#ESI/1.0#', '', $value));
if ($response->headers->has('Surrogate-Control')) {
$value = $response->headers->get('Surrogate-Control');
if ('content="ESI/1.0"' == $value) {
$response->headers->delete('Surrogate-Control');
} elseif (preg_match('#,\s*content="ESI/1.0"#', $value)) {
$response->headers->set('Surrogate-Control', preg_replace('#,\s*content="ESI/1.0"#', '', $value));
} elseif (preg_match('#content="ESI/1.0",\s*#', $value)) {
$response->headers->set('Surrogate-Control', preg_replace('#content="ESI/1.0",\s*#', '', $value));
}
}
}

View File

@ -0,0 +1,67 @@
<?php
/*
* This file is part of the symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\HttpKernel\Cache;
use Symfony\Component\HttpKernel\Cache\Esi;
use Symfony\Component\HttpKernel\Cache\EsiListener;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class EsiListenerTest extends \PHPUnit_Framework_TestCase
{
public function testFilterDoesNothingForSubRequests()
{
$dispatcher = new EventDispatcher();
$listener = new EsiListener(new Esi());
$listener->register($dispatcher);
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::SUB_REQUEST));
$dispatcher->filter($event, $response = new Response('foo <esi:include src="" />'));
$this->assertEquals('', $response->headers->get('Surrogate-Control'));
}
public function testNothingIsRegisteredIfEsiIsNull()
{
$dispatcher = new EventDispatcher();
$listener = new EsiListener();
$listener->register($dispatcher);
$this->assertEquals(array(), $dispatcher->getListeners('core.response'));
}
public function testFilterWhenThereIsSomeEsiIncludes()
{
$dispatcher = new EventDispatcher();
$listener = new EsiListener(new Esi());
$listener->register($dispatcher);
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST));
$dispatcher->filter($event, $response = new Response('foo <esi:include src="" />'));
$this->assertEquals('content="ESI/1.0"', $response->headers->get('Surrogate-Control'));
}
public function testFilterWhenThereIsNoEsiIncludes()
{
$dispatcher = new EventDispatcher();
$listener = new EsiListener(new Esi());
$listener->register($dispatcher);
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST));
$dispatcher->filter($event, $response = new Response('foo'));
$this->assertEquals('', $response->headers->get('Surrogate-Control'));
}
}

View File

@ -0,0 +1,202 @@
<?php
/*
* This file is part of the symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\HttpKernel\Cache;
use Symfony\Component\HttpKernel\Cache\Esi;
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"');
$this->assertTrue($esi->hasSurrogateEsiCapability($request));
$request = Request::create('/');
$request->headers->set('Surrogate-Capability', 'foobar');
$this->assertFalse($esi->hasSurrogateEsiCapability($request));
$request = Request::create('/');
$this->assertFalse($esi->hasSurrogateEsiCapability($request));
}
public function testAddSurrogateEsiCapability()
{
$esi = new Esi();
$request = Request::create('/');
$esi->addSurrogateEsiCapability($request);
$this->assertEquals('symfony2="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
$esi->addSurrogateEsiCapability($request);
$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"');
$this->assertTrue($esi->needsEsiParsing($response));
$response = new Response();
$this->assertFalse($esi->needsEsiParsing($response));
}
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->assertEquals(array('content-type' => array('text/plain')), $response->headers->all());
}
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);
$this->assertEquals('foo <?php echo $this->esi->handle($this, \'...\', \'alt\', true) ?>'."\n", $response->getContent());
$this->assertEquals(array('x-body-eval' => array('ESI')), $response->headers->all());
$response = new Response('foo <esi:include src="..." />');
$esi->process($request, $response);
$this->assertEquals('foo <?php echo $this->esi->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
}
/**
* @expectedException RuntimeException
*/
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(array('x-body-eval' => array('ESI')), $response->headers->all());
$response->headers->set('Surrogate-Control', 'no-store, content="ESI/1.0"');
$esi->process($request, $response);
$this->assertEquals(array('surrogate-control' => array('no-store'), 'x-body-eval' => array('ESI')), $response->headers->all());
$response->headers->set('Surrogate-Control', 'content="ESI/1.0", no-store');
$esi->process($request, $response);
$this->assertEquals(array('surrogate-control' => array('no-store'), 'x-body-eval' => array('ESI')), $response->headers->all());
}
public function testHandle()
{
$esi = new Esi();
$cache = $this->getCache(Request::create('/'), new Response('foo'));
$this->assertEquals('foo', $esi->handle($cache, '/', '/alt', true));
}
/**
* @expectedException RuntimeException
*/
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)
{
$cache = $this->getMock('Symfony\Component\HttpKernel\Cache\Cache', array('getRequest', 'handle'), array(), '', false);
$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;
}
}

View File

@ -23,9 +23,9 @@ class ResponseListenerTest extends \PHPUnit_Framework_TestCase
public function testFilterDoesNothingForSubRequests()
{
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::SUB_REQUEST));
$response = new Response('foo');
$this->getDispatcher()->filter($event, $response = new Response('foo'));
$this->assertEquals(array(), $this->getResponseListener()->filter($event, $response)->headers->all());
$this->assertEquals(array(), $response->headers->all());
}
public function testFilterDoesNothingIfContentTypeIsSet()
@ -33,16 +33,17 @@ class ResponseListenerTest extends \PHPUnit_Framework_TestCase
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST));
$response = new Response('foo');
$response->headers->set('Content-Type', 'text/plain');
$this->getDispatcher()->filter($event, $response);
$this->assertEquals(array('content-type' => array('text/plain')), $this->getResponseListener()->filter($event, $response)->headers->all());
$this->assertEquals(array('content-type' => array('text/plain')), $response->headers->all());
}
public function testFilterDoesNothingIfRequestFormatIsNotDefined()
{
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => Request::create('/')));
$response = new Response('foo');
$this->getDispatcher()->filter($event, $response = new Response('foo'));
$this->assertEquals(array(), $this->getResponseListener()->filter($event, $response)->headers->all());
$this->assertEquals(array(), $response->headers->all());
}
public function testFilterSetContentType()
@ -50,17 +51,17 @@ class ResponseListenerTest extends \PHPUnit_Framework_TestCase
$request = Request::create('/');
$request->setRequestFormat('json');
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => $request));
$response = new Response('foo');
$this->getDispatcher()->filter($event, $response = new Response('foo'));
$this->assertEquals(array('content-type' => array('application/json')), $this->getResponseListener()->filter($event, $response)->headers->all());
$this->assertEquals(array('content-type' => array('application/json')), $response->headers->all());
}
protected function getResponseListener()
protected function getDispatcher()
{
$dispatcher = new EventDispatcher();
$listener = new ResponseListener();
$listener->register($dispatcher);
return $listener;
return $dispatcher;
}
}