Added Laravel project

This commit is contained in:
2017-09-17 00:35:10 +02:00
parent a3c19304d5
commit ecf605b8f5
6246 changed files with 682270 additions and 2 deletions

View File

@@ -0,0 +1,248 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class EsiTest extends TestCase
{
public function testHasSurrogateEsiCapability()
{
$esi = new Esi();
$request = Request::create('/');
$request->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
$this->assertTrue($esi->hasSurrogateCapability($request));
$request = Request::create('/');
$request->headers->set('Surrogate-Capability', 'foobar');
$this->assertFalse($esi->hasSurrogateCapability($request));
$request = Request::create('/');
$this->assertFalse($esi->hasSurrogateCapability($request));
}
public function testAddSurrogateEsiCapability()
{
$esi = new Esi();
$request = Request::create('/');
$esi->addSurrogateCapability($request);
$this->assertEquals('symfony="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
$esi->addSurrogateCapability($request);
$this->assertEquals('symfony="ESI/1.0", symfony="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->needsParsing($response));
$response = new Response();
$this->assertFalse($esi->needsParsing($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->assertFalse($response->headers->has('x-body-eval'));
}
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());
}
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->surrogate->handle($this, \'...\', \'alt\', true) ?>'."\n", $response->getContent());
$this->assertEquals('ESI', $response->headers->get('x-body-eval'));
$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());
$response = new Response('foo <esi:include src="..." />');
$esi->process($request, $response);
$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);
$this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
}
public function testProcessEscapesPhpTags()
{
$esi = new Esi();
$request = Request::create('/');
$response = new Response('<?php <? <% <script language=php>');
$esi->process($request, $response);
$this->assertEquals('<?php echo "<?"; ?>php <?php echo "<?"; ?> <?php echo "<%"; ?> <?php echo "<s"; ?>cript language=php>', $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('ESI', $response->headers->get('x-body-eval'));
$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'));
$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'));
}
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->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\HttpCache')->setMethods(array('getRequest', 'handle'))->disableOriginalConstructor()->getMock();
$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;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,185 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class HttpCacheTestCase extends TestCase
{
protected $kernel;
protected $cache;
protected $caches;
protected $cacheConfig;
protected $request;
protected $response;
protected $responses;
protected $catch;
protected $esi;
/**
* @var Store
*/
protected $store;
protected function setUp()
{
$this->kernel = null;
$this->cache = null;
$this->esi = null;
$this->caches = array();
$this->cacheConfig = array();
$this->request = null;
$this->response = null;
$this->responses = array();
$this->catch = false;
$this->clearDirectory(sys_get_temp_dir().'/http_cache');
}
protected function tearDown()
{
if ($this->cache) {
$this->cache->getStore()->cleanup();
}
$this->kernel = null;
$this->cache = null;
$this->caches = null;
$this->request = null;
$this->response = null;
$this->responses = null;
$this->cacheConfig = null;
$this->catch = null;
$this->esi = null;
$this->clearDirectory(sys_get_temp_dir().'/http_cache');
}
public function assertHttpKernelIsCalled()
{
$this->assertTrue($this->kernel->hasBeenCalled());
}
public function assertHttpKernelIsNotCalled()
{
$this->assertFalse($this->kernel->hasBeenCalled());
}
public function assertResponseOk()
{
$this->assertEquals(200, $this->response->getStatusCode());
}
public function assertTraceContains($trace)
{
$traces = $this->cache->getTraces();
$traces = current($traces);
$this->assertRegExp('/'.$trace.'/', implode(', ', $traces));
}
public function assertTraceNotContains($trace)
{
$traces = $this->cache->getTraces();
$traces = current($traces);
$this->assertNotRegExp('/'.$trace.'/', implode(', ', $traces));
}
public function assertExceptionsAreCaught()
{
$this->assertTrue($this->kernel->isCatchingExceptions());
}
public function assertExceptionsAreNotCaught()
{
$this->assertFalse($this->kernel->isCatchingExceptions());
}
public function request($method, $uri = '/', $server = array(), $cookies = array(), $esi = false, $headers = array())
{
if (null === $this->kernel) {
throw new \LogicException('You must call setNextResponse() before calling request().');
}
$this->kernel->reset();
$this->store = new Store(sys_get_temp_dir().'/http_cache');
$this->cacheConfig['debug'] = true;
$this->esi = $esi ? new Esi() : null;
$this->cache = new HttpCache($this->kernel, $this->store, $this->esi, $this->cacheConfig);
$this->request = Request::create($uri, $method, array(), $cookies, array(), $server);
$this->request->headers->add($headers);
$this->response = $this->cache->handle($this->request, HttpKernelInterface::MASTER_REQUEST, $this->catch);
$this->responses[] = $this->response;
}
public function getMetaStorageValues()
{
$values = array();
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/http_cache/md', \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
$values[] = file_get_contents($file);
}
return $values;
}
// A basic response with 200 status code and a tiny body.
public function setNextResponse($statusCode = 200, array $headers = array(), $body = 'Hello World', \Closure $customizer = null)
{
$this->kernel = new TestHttpKernel($body, $statusCode, $headers, $customizer);
}
public function setNextResponses($responses)
{
$this->kernel = new TestMultipleHttpKernel($responses);
}
public function catchExceptions($catch = true)
{
$this->catch = $catch;
}
public static function clearDirectory($directory)
{
if (!is_dir($directory)) {
return;
}
$fp = opendir($directory);
while (false !== $file = readdir($fp)) {
if (!in_array($file, array('.', '..'))) {
if (is_link($directory.'/'.$file)) {
unlink($directory.'/'.$file);
} elseif (is_dir($directory.'/'.$file)) {
self::clearDirectory($directory.'/'.$file);
rmdir($directory.'/'.$file);
} else {
unlink($directory.'/'.$file);
}
}
}
closedir($fp);
}
}

View File

@@ -0,0 +1,222 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This code is partially based on the Rack-Cache library by Ryan Tomayko,
* which is released under the MIT license.
* (based on commit 02d2b48d75bcb63cf1c0c7149c077ad256542801)
*
* 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;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\ResponseCacheStrategy;
class ResponseCacheStrategyTest extends TestCase
{
public function testMinimumSharedMaxAgeWins()
{
$cacheStrategy = new ResponseCacheStrategy();
$response1 = new Response();
$response1->setSharedMaxAge(60);
$cacheStrategy->add($response1);
$response2 = new Response();
$response2->setSharedMaxAge(3600);
$cacheStrategy->add($response2);
$response = new Response();
$response->setSharedMaxAge(86400);
$cacheStrategy->update($response);
$this->assertSame('60', $response->headers->getCacheControlDirective('s-maxage'));
}
public function testSharedMaxAgeNotSetIfNotSetInAnyEmbeddedRequest()
{
$cacheStrategy = new ResponseCacheStrategy();
$response1 = new Response();
$response1->setSharedMaxAge(60);
$cacheStrategy->add($response1);
$response2 = new Response();
$cacheStrategy->add($response2);
$response = new Response();
$response->setSharedMaxAge(86400);
$cacheStrategy->update($response);
$this->assertFalse($response->headers->hasCacheControlDirective('s-maxage'));
}
public function testSharedMaxAgeNotSetIfNotSetInMasterRequest()
{
$cacheStrategy = new ResponseCacheStrategy();
$response1 = new Response();
$response1->setSharedMaxAge(60);
$cacheStrategy->add($response1);
$response2 = new Response();
$response2->setSharedMaxAge(3600);
$cacheStrategy->add($response2);
$response = new Response();
$cacheStrategy->update($response);
$this->assertFalse($response->headers->hasCacheControlDirective('s-maxage'));
}
public function testMasterResponseNotCacheableWhenEmbeddedResponseRequiresValidation()
{
$cacheStrategy = new ResponseCacheStrategy();
$embeddedResponse = new Response();
$embeddedResponse->setLastModified(new \DateTime());
$cacheStrategy->add($embeddedResponse);
$masterResponse = new Response();
$masterResponse->setSharedMaxAge(3600);
$cacheStrategy->update($masterResponse);
$this->assertTrue($masterResponse->headers->hasCacheControlDirective('no-cache'));
$this->assertTrue($masterResponse->headers->hasCacheControlDirective('must-revalidate'));
$this->assertFalse($masterResponse->isFresh());
}
public function testValidationOnMasterResponseIsNotPossibleWhenItContainsEmbeddedResponses()
{
$cacheStrategy = new ResponseCacheStrategy();
// This master response uses the "validation" model
$masterResponse = new Response();
$masterResponse->setLastModified(new \DateTime());
$masterResponse->setEtag('foo');
// Embedded response uses "expiry" model
$embeddedResponse = new Response();
$masterResponse->setSharedMaxAge(3600);
$cacheStrategy->add($embeddedResponse);
$cacheStrategy->update($masterResponse);
$this->assertFalse($masterResponse->isValidateable());
$this->assertFalse($masterResponse->headers->has('Last-Modified'));
$this->assertFalse($masterResponse->headers->has('ETag'));
$this->assertTrue($masterResponse->headers->hasCacheControlDirective('no-cache'));
$this->assertTrue($masterResponse->headers->hasCacheControlDirective('must-revalidate'));
}
public function testMasterResponseWithValidationIsUnchangedWhenThereIsNoEmbeddedResponse()
{
$cacheStrategy = new ResponseCacheStrategy();
$masterResponse = new Response();
$masterResponse->setLastModified(new \DateTime());
$cacheStrategy->update($masterResponse);
$this->assertTrue($masterResponse->isValidateable());
}
public function testMasterResponseWithExpirationIsUnchangedWhenThereIsNoEmbeddedResponse()
{
$cacheStrategy = new ResponseCacheStrategy();
$masterResponse = new Response();
$masterResponse->setSharedMaxAge(3600);
$cacheStrategy->update($masterResponse);
$this->assertTrue($masterResponse->isFresh());
}
public function testMasterResponseIsNotCacheableWhenEmbeddedResponseIsNotCacheable()
{
$cacheStrategy = new ResponseCacheStrategy();
$masterResponse = new Response();
$masterResponse->setSharedMaxAge(3600); // Public, cacheable
/* This response has no validation or expiration information.
That makes it uncacheable, it is always stale.
(It does *not* make this private, though.) */
$embeddedResponse = new Response();
$this->assertFalse($embeddedResponse->isFresh()); // not fresh, as no lifetime is provided
$cacheStrategy->add($embeddedResponse);
$cacheStrategy->update($masterResponse);
$this->assertTrue($masterResponse->headers->hasCacheControlDirective('no-cache'));
$this->assertTrue($masterResponse->headers->hasCacheControlDirective('must-revalidate'));
$this->assertFalse($masterResponse->isFresh());
}
public function testEmbeddingPrivateResponseMakesMainResponsePrivate()
{
$cacheStrategy = new ResponseCacheStrategy();
$masterResponse = new Response();
$masterResponse->setSharedMaxAge(3600); // public, cacheable
// The embedded response might for example contain per-user data that remains valid for 60 seconds
$embeddedResponse = new Response();
$embeddedResponse->setPrivate();
$embeddedResponse->setMaxAge(60); // this would implicitly set "private" as well, but let's be explicit
$cacheStrategy->add($embeddedResponse);
$cacheStrategy->update($masterResponse);
$this->assertTrue($masterResponse->headers->hasCacheControlDirective('private'));
// Not sure if we should pass "max-age: 60" in this case, as long as the response is private and
// that's the more conservative of both the master and embedded response...?
}
public function testResponseIsExiprableWhenEmbeddedResponseCombinesExpiryAndValidation()
{
/* When "expiration wins over validation" (https://symfony.com/doc/current/http_cache/validation.html)
* and both the main and embedded response provide s-maxage, then the more restricting value of both
* should be fine, regardless of whether the embedded response can be validated later on or must be
* completely regenerated.
*/
$cacheStrategy = new ResponseCacheStrategy();
$masterResponse = new Response();
$masterResponse->setSharedMaxAge(3600);
$embeddedResponse = new Response();
$embeddedResponse->setSharedMaxAge(60);
$embeddedResponse->setEtag('foo');
$cacheStrategy->add($embeddedResponse);
$cacheStrategy->update($masterResponse);
$this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
}
public function testResponseIsExpirableButNotValidateableWhenMasterResponseCombinesExpirationAndValidation()
{
$cacheStrategy = new ResponseCacheStrategy();
$masterResponse = new Response();
$masterResponse->setSharedMaxAge(3600);
$masterResponse->setEtag('foo');
$masterResponse->setLastModified(new \DateTime());
$embeddedResponse = new Response();
$embeddedResponse->setSharedMaxAge(60);
$cacheStrategy->add($embeddedResponse);
$cacheStrategy->update($masterResponse);
$this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
$this->assertFalse($masterResponse->isValidateable());
}
}

View File

@@ -0,0 +1,215 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\Ssi;
class SsiTest extends TestCase
{
public function testHasSurrogateSsiCapability()
{
$ssi = new Ssi();
$request = Request::create('/');
$request->headers->set('Surrogate-Capability', 'abc="SSI/1.0"');
$this->assertTrue($ssi->hasSurrogateCapability($request));
$request = Request::create('/');
$request->headers->set('Surrogate-Capability', 'foobar');
$this->assertFalse($ssi->hasSurrogateCapability($request));
$request = Request::create('/');
$this->assertFalse($ssi->hasSurrogateCapability($request));
}
public function testAddSurrogateSsiCapability()
{
$ssi = new Ssi();
$request = Request::create('/');
$ssi->addSurrogateCapability($request);
$this->assertEquals('symfony="SSI/1.0"', $request->headers->get('Surrogate-Capability'));
$ssi->addSurrogateCapability($request);
$this->assertEquals('symfony="SSI/1.0", symfony="SSI/1.0"', $request->headers->get('Surrogate-Capability'));
}
public function testAddSurrogateControl()
{
$ssi = new Ssi();
$response = new Response('foo <!--#include virtual="" -->');
$ssi->addSurrogateControl($response);
$this->assertEquals('content="SSI/1.0"', $response->headers->get('Surrogate-Control'));
$response = new Response('foo');
$ssi->addSurrogateControl($response);
$this->assertEquals('', $response->headers->get('Surrogate-Control'));
}
public function testNeedsSsiParsing()
{
$ssi = new Ssi();
$response = new Response();
$response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
$this->assertTrue($ssi->needsParsing($response));
$response = new Response();
$this->assertFalse($ssi->needsParsing($response));
}
public function testRenderIncludeTag()
{
$ssi = new Ssi();
$this->assertEquals('<!--#include virtual="/" -->', $ssi->renderIncludeTag('/', '/alt', true));
$this->assertEquals('<!--#include virtual="/" -->', $ssi->renderIncludeTag('/', '/alt', false));
$this->assertEquals('<!--#include virtual="/" -->', $ssi->renderIncludeTag('/'));
}
public function testProcessDoesNothingIfContentTypeIsNotHtml()
{
$ssi = new Ssi();
$request = Request::create('/');
$response = new Response();
$response->headers->set('Content-Type', 'text/plain');
$ssi->process($request, $response);
$this->assertFalse($response->headers->has('x-body-eval'));
}
public function testProcess()
{
$ssi = new Ssi();
$request = Request::create('/');
$response = new Response('foo <!--#include virtual="..." -->');
$ssi->process($request, $response);
$this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent());
$this->assertEquals('SSI', $response->headers->get('x-body-eval'));
$response = new Response('foo <!--#include virtual="foo\'" -->');
$ssi->process($request, $response);
$this->assertEquals("foo <?php echo \$this->surrogate->handle(\$this, 'foo\\'', '', false) ?>"."\n", $response->getContent());
}
public function testProcessEscapesPhpTags()
{
$ssi = new Ssi();
$request = Request::create('/');
$response = new Response('<?php <? <% <script language=php>');
$ssi->process($request, $response);
$this->assertEquals('<?php echo "<?"; ?>php <?php echo "<?"; ?> <?php echo "<%"; ?> <?php echo "<s"; ?>cript language=php>', $response->getContent());
}
/**
* @expectedException \RuntimeException
*/
public function testProcessWhenNoSrcInAnSsi()
{
$ssi = new Ssi();
$request = Request::create('/');
$response = new Response('foo <!--#include -->');
$ssi->process($request, $response);
}
public function testProcessRemoveSurrogateControlHeader()
{
$ssi = new Ssi();
$request = Request::create('/');
$response = new Response('foo <!--#include virtual="..." -->');
$response->headers->set('Surrogate-Control', 'content="SSI/1.0"');
$ssi->process($request, $response);
$this->assertEquals('SSI', $response->headers->get('x-body-eval'));
$response->headers->set('Surrogate-Control', 'no-store, content="SSI/1.0"');
$ssi->process($request, $response);
$this->assertEquals('SSI', $response->headers->get('x-body-eval'));
$this->assertEquals('no-store', $response->headers->get('surrogate-control'));
$response->headers->set('Surrogate-Control', 'content="SSI/1.0", no-store');
$ssi->process($request, $response);
$this->assertEquals('SSI', $response->headers->get('x-body-eval'));
$this->assertEquals('no-store', $response->headers->get('surrogate-control'));
}
public function testHandle()
{
$ssi = new Ssi();
$cache = $this->getCache(Request::create('/'), new Response('foo'));
$this->assertEquals('foo', $ssi->handle($cache, '/', '/alt', true));
}
/**
* @expectedException \RuntimeException
*/
public function testHandleWhenResponseIsNot200()
{
$ssi = new Ssi();
$response = new Response('foo');
$response->setStatusCode(404);
$cache = $this->getCache(Request::create('/'), $response);
$ssi->handle($cache, '/', '/alt', false);
}
public function testHandleWhenResponseIsNot200AndErrorsAreIgnored()
{
$ssi = new Ssi();
$response = new Response('foo');
$response->setStatusCode(404);
$cache = $this->getCache(Request::create('/'), $response);
$this->assertEquals('', $ssi->handle($cache, '/', '/alt', true));
}
public function testHandleWhenResponseIsNot200AndAltIsPresent()
{
$ssi = new Ssi();
$response1 = new Response('foo');
$response1->setStatusCode(404);
$response2 = new Response('bar');
$cache = $this->getCache(Request::create('/'), array($response1, $response2));
$this->assertEquals('bar', $ssi->handle($cache, '/', '/alt', false));
}
protected function getCache($request, $response)
{
$cache = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\HttpCache')->setMethods(array('getRequest', 'handle'))->disableOriginalConstructor()->getMock();
$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

@@ -0,0 +1,301 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\Store;
class StoreTest extends TestCase
{
protected $request;
protected $response;
/**
* @var Store
*/
protected $store;
protected function setUp()
{
$this->request = Request::create('/');
$this->response = new Response('hello world', 200, array());
HttpCacheTestCase::clearDirectory(sys_get_temp_dir().'/http_cache');
$this->store = new Store(sys_get_temp_dir().'/http_cache');
}
protected function tearDown()
{
$this->store = null;
$this->request = null;
$this->response = null;
HttpCacheTestCase::clearDirectory(sys_get_temp_dir().'/http_cache');
}
public function testReadsAnEmptyArrayWithReadWhenNothingCachedAtKey()
{
$this->assertEmpty($this->getStoreMetadata('/nothing'));
}
public function testUnlockFileThatDoesExist()
{
$cacheKey = $this->storeSimpleEntry();
$this->store->lock($this->request);
$this->assertTrue($this->store->unlock($this->request));
}
public function testUnlockFileThatDoesNotExist()
{
$this->assertFalse($this->store->unlock($this->request));
}
public function testRemovesEntriesForKeyWithPurge()
{
$request = Request::create('/foo');
$this->store->write($request, new Response('foo'));
$metadata = $this->getStoreMetadata($request);
$this->assertNotEmpty($metadata);
$this->assertTrue($this->store->purge('/foo'));
$this->assertEmpty($this->getStoreMetadata($request));
// cached content should be kept after purging
$path = $this->store->getPath($metadata[0][1]['x-content-digest'][0]);
$this->assertTrue(is_file($path));
$this->assertFalse($this->store->purge('/bar'));
}
public function testStoresACacheEntry()
{
$cacheKey = $this->storeSimpleEntry();
$this->assertNotEmpty($this->getStoreMetadata($cacheKey));
}
public function testSetsTheXContentDigestResponseHeaderBeforeStoring()
{
$cacheKey = $this->storeSimpleEntry();
$entries = $this->getStoreMetadata($cacheKey);
list($req, $res) = $entries[0];
$this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $res['x-content-digest'][0]);
}
public function testFindsAStoredEntryWithLookup()
{
$this->storeSimpleEntry();
$response = $this->store->lookup($this->request);
$this->assertNotNull($response);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
}
public function testDoesNotFindAnEntryWithLookupWhenNoneExists()
{
$request = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
$this->assertNull($this->store->lookup($request));
}
public function testCanonizesUrlsForCacheKeys()
{
$this->storeSimpleEntry($path = '/test?x=y&p=q');
$hitsReq = Request::create($path);
$missReq = Request::create('/test?p=x');
$this->assertNotNull($this->store->lookup($hitsReq));
$this->assertNull($this->store->lookup($missReq));
}
public function testDoesNotFindAnEntryWithLookupWhenTheBodyDoesNotExist()
{
$this->storeSimpleEntry();
$this->assertNotNull($this->response->headers->get('X-Content-Digest'));
$path = $this->getStorePath($this->response->headers->get('X-Content-Digest'));
@unlink($path);
$this->assertNull($this->store->lookup($this->request));
}
public function testRestoresResponseHeadersProperlyWithLookup()
{
$this->storeSimpleEntry();
$response = $this->store->lookup($this->request);
$this->assertEquals($response->headers->all(), array_merge(array('content-length' => 4, 'x-body-file' => array($this->getStorePath($response->headers->get('X-Content-Digest')))), $this->response->headers->all()));
}
public function testRestoresResponseContentFromEntityStoreWithLookup()
{
$this->storeSimpleEntry();
$response = $this->store->lookup($this->request);
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test')), $response->getContent());
}
public function testInvalidatesMetaAndEntityStoreEntriesWithInvalidate()
{
$this->storeSimpleEntry();
$this->store->invalidate($this->request);
$response = $this->store->lookup($this->request);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$this->assertFalse($response->isFresh());
}
public function testSucceedsQuietlyWhenInvalidateCalledWithNoMatchingEntries()
{
$req = Request::create('/test');
$this->store->invalidate($req);
$this->assertNull($this->store->lookup($this->request));
}
public function testDoesNotReturnEntriesThatVaryWithLookup()
{
$req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
$req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
$res = new Response('test', 200, array('Vary' => 'Foo Bar'));
$this->store->write($req1, $res);
$this->assertNull($this->store->lookup($req2));
}
public function testDoesNotReturnEntriesThatSlightlyVaryWithLookup()
{
$req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
$req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bam'));
$res = new Response('test', 200, array('Vary' => array('Foo', 'Bar')));
$this->store->write($req1, $res);
$this->assertNull($this->store->lookup($req2));
}
public function testStoresMultipleResponsesForEachVaryCombination()
{
$req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
$res1 = new Response('test 1', 200, array('Vary' => 'Foo Bar'));
$key = $this->store->write($req1, $res1);
$req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
$res2 = new Response('test 2', 200, array('Vary' => 'Foo Bar'));
$this->store->write($req2, $res2);
$req3 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Baz', 'HTTP_BAR' => 'Boom'));
$res3 = new Response('test 3', 200, array('Vary' => 'Foo Bar'));
$this->store->write($req3, $res3);
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->getContent());
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->getContent());
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->getContent());
$this->assertCount(3, $this->getStoreMetadata($key));
}
public function testOverwritesNonVaryingResponseWithStore()
{
$req1 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
$res1 = new Response('test 1', 200, array('Vary' => 'Foo Bar'));
$key = $this->store->write($req1, $res1);
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->getContent());
$req2 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam'));
$res2 = new Response('test 2', 200, array('Vary' => 'Foo Bar'));
$this->store->write($req2, $res2);
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->getContent());
$req3 = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
$res3 = new Response('test 3', 200, array('Vary' => 'Foo Bar'));
$key = $this->store->write($req3, $res3);
$this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->getContent());
$this->assertCount(2, $this->getStoreMetadata($key));
}
public function testLocking()
{
$req = Request::create('/test', 'get', array(), array(), array(), array('HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar'));
$this->assertTrue($this->store->lock($req));
$path = $this->store->lock($req);
$this->assertTrue($this->store->isLocked($req));
$this->store->unlock($req);
$this->assertFalse($this->store->isLocked($req));
}
public function testPurgeHttps()
{
$request = Request::create('https://example.com/foo');
$this->store->write($request, new Response('foo'));
$this->assertNotEmpty($this->getStoreMetadata($request));
$this->assertTrue($this->store->purge('https://example.com/foo'));
$this->assertEmpty($this->getStoreMetadata($request));
}
public function testPurgeHttpAndHttps()
{
$requestHttp = Request::create('https://example.com/foo');
$this->store->write($requestHttp, new Response('foo'));
$requestHttps = Request::create('http://example.com/foo');
$this->store->write($requestHttps, new Response('foo'));
$this->assertNotEmpty($this->getStoreMetadata($requestHttp));
$this->assertNotEmpty($this->getStoreMetadata($requestHttps));
$this->assertTrue($this->store->purge('http://example.com/foo'));
$this->assertEmpty($this->getStoreMetadata($requestHttp));
$this->assertEmpty($this->getStoreMetadata($requestHttps));
}
protected function storeSimpleEntry($path = null, $headers = array())
{
if (null === $path) {
$path = '/test';
}
$this->request = Request::create($path, 'get', array(), array(), array(), $headers);
$this->response = new Response('test', 200, array('Cache-Control' => 'max-age=420'));
return $this->store->write($this->request, $this->response);
}
protected function getStoreMetadata($key)
{
$r = new \ReflectionObject($this->store);
$m = $r->getMethod('getMetadata');
$m->setAccessible(true);
if ($key instanceof Request) {
$m1 = $r->getMethod('getCacheKey');
$m1->setAccessible(true);
$key = $m1->invoke($this->store, $key);
}
return $m->invoke($this->store, $key);
}
protected function getStorePath($key)
{
$r = new \ReflectionObject($this->store);
$m = $r->getMethod('getPath');
$m->setAccessible(true);
return $m->invoke($this->store, $key);
}
}

View File

@@ -0,0 +1,92 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
class TestHttpKernel extends HttpKernel implements ControllerResolverInterface, ArgumentResolverInterface
{
protected $body;
protected $status;
protected $headers;
protected $called = false;
protected $customizer;
protected $catch = false;
protected $backendRequest;
public function __construct($body, $status, $headers, \Closure $customizer = null)
{
$this->body = $body;
$this->status = $status;
$this->headers = $headers;
$this->customizer = $customizer;
parent::__construct(new EventDispatcher(), $this, null, $this);
}
public function getBackendRequest()
{
return $this->backendRequest;
}
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
{
$this->catch = $catch;
$this->backendRequest = $request;
return parent::handle($request, $type, $catch);
}
public function isCatchingExceptions()
{
return $this->catch;
}
public function getController(Request $request)
{
return array($this, 'callController');
}
public function getArguments(Request $request, $controller)
{
return array($request);
}
public function callController(Request $request)
{
$this->called = true;
$response = new Response($this->body, $this->status, $this->headers);
if (null !== $customizer = $this->customizer) {
$customizer($request, $response);
}
return $response;
}
public function hasBeenCalled()
{
return $this->called;
}
public function reset()
{
$this->called = false;
}
}

View File

@@ -0,0 +1,81 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
class TestMultipleHttpKernel extends HttpKernel implements ControllerResolverInterface, ArgumentResolverInterface
{
protected $bodies = array();
protected $statuses = array();
protected $headers = array();
protected $called = false;
protected $backendRequest;
public function __construct($responses)
{
foreach ($responses as $response) {
$this->bodies[] = $response['body'];
$this->statuses[] = $response['status'];
$this->headers[] = $response['headers'];
}
parent::__construct(new EventDispatcher(), $this, null, $this);
}
public function getBackendRequest()
{
return $this->backendRequest;
}
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
{
$this->backendRequest = $request;
return parent::handle($request, $type, $catch);
}
public function getController(Request $request)
{
return array($this, 'callController');
}
public function getArguments(Request $request, $controller)
{
return array($request);
}
public function callController(Request $request)
{
$this->called = true;
$response = new Response(array_shift($this->bodies), array_shift($this->statuses), array_shift($this->headers));
return $response;
}
public function hasBeenCalled()
{
return $this->called;
}
public function reset()
{
$this->called = false;
}
}