提交代码
This commit is contained in:
244
vendor/symfony/http-kernel/Tests/HttpCache/EsiTest.php
vendored
Normal file
244
vendor/symfony/http-kernel/Tests/HttpCache/EsiTest.php
vendored
Normal file
@@ -0,0 +1,244 @@
|
||||
<?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\Esi;
|
||||
|
||||
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');
|
||||
$this->assertSame($response, $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");
|
||||
$this->assertSame($response, $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 >" /> Keep this');
|
||||
$this->assertSame($response, $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" />');
|
||||
$this->assertSame($response, $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" />');
|
||||
$this->assertSame($response, $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="..." />');
|
||||
$this->assertSame($response, $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>');
|
||||
$this->assertSame($response, $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>');
|
||||
$this->assertSame($response, $esi->process($request, $response));
|
||||
|
||||
$this->assertEquals('<?php echo "<?"; ?>php <?php echo "<?"; ?> <?php echo "<%"; ?> <?php echo "<s"; ?>cript language=php>', $response->getContent());
|
||||
}
|
||||
|
||||
public function testProcessWhenNoSrcInAnEsi()
|
||||
{
|
||||
$this->expectException('RuntimeException');
|
||||
$esi = new Esi();
|
||||
|
||||
$request = Request::create('/');
|
||||
$response = new Response('foo <esi:include />');
|
||||
$this->assertSame($response, $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"');
|
||||
$this->assertSame($response, $esi->process($request, $response));
|
||||
$this->assertEquals('ESI', $response->headers->get('x-body-eval'));
|
||||
|
||||
$response->headers->set('Surrogate-Control', 'no-store, content="ESI/1.0"');
|
||||
$this->assertSame($response, $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');
|
||||
$this->assertSame($response, $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));
|
||||
}
|
||||
|
||||
public function testHandleWhenResponseIsNot200()
|
||||
{
|
||||
$this->expectException('RuntimeException');
|
||||
$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('/'), [$response1, $response2]);
|
||||
$this->assertEquals('bar', $esi->handle($cache, '/', '/alt', false));
|
||||
}
|
||||
|
||||
protected function getCache($request, $response)
|
||||
{
|
||||
$cache = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\HttpCache')->setMethods(['getRequest', 'handle'])->disableOriginalConstructor()->getMock();
|
||||
$cache->expects($this->any())
|
||||
->method('getRequest')
|
||||
->willReturn($request)
|
||||
;
|
||||
if (\is_array($response)) {
|
||||
$cache->expects($this->any())
|
||||
->method('handle')
|
||||
->will($this->onConsecutiveCalls(...$response))
|
||||
;
|
||||
} else {
|
||||
$cache->expects($this->any())
|
||||
->method('handle')
|
||||
->willReturn($response)
|
||||
;
|
||||
}
|
||||
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
1563
vendor/symfony/http-kernel/Tests/HttpCache/HttpCacheTest.php
vendored
Normal file
1563
vendor/symfony/http-kernel/Tests/HttpCache/HttpCacheTest.php
vendored
Normal file
File diff suppressed because it is too large
Load Diff
187
vendor/symfony/http-kernel/Tests/HttpCache/HttpCacheTestCase.php
vendored
Normal file
187
vendor/symfony/http-kernel/Tests/HttpCache/HttpCacheTestCase.php
vendored
Normal file
@@ -0,0 +1,187 @@
|
||||
<?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(): void
|
||||
{
|
||||
$this->kernel = null;
|
||||
|
||||
$this->cache = null;
|
||||
$this->esi = null;
|
||||
$this->caches = [];
|
||||
$this->cacheConfig = [];
|
||||
|
||||
$this->request = null;
|
||||
$this->response = null;
|
||||
$this->responses = [];
|
||||
|
||||
$this->catch = false;
|
||||
|
||||
$this->clearDirectory(sys_get_temp_dir().'/http_cache');
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
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 = [], $cookies = [], $esi = false, $headers = [])
|
||||
{
|
||||
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');
|
||||
|
||||
if (!isset($this->cacheConfig['debug'])) {
|
||||
$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, [], $cookies, [], $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 = [];
|
||||
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 = [], $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, ['.', '..'])) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
469
vendor/symfony/http-kernel/Tests/HttpCache/ResponseCacheStrategyTest.php
vendored
Normal file
469
vendor/symfony/http-kernel/Tests/HttpCache/ResponseCacheStrategyTest.php
vendored
Normal file
@@ -0,0 +1,469 @@
|
||||
<?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'));
|
||||
$this->assertFalse($masterResponse->headers->hasCacheControlDirective('public'));
|
||||
}
|
||||
|
||||
public function testEmbeddingPublicResponseDoesNotMakeMainResponsePublic()
|
||||
{
|
||||
$cacheStrategy = new ResponseCacheStrategy();
|
||||
|
||||
$masterResponse = new Response();
|
||||
$masterResponse->setPrivate(); // this is the default, but let's be explicit
|
||||
$masterResponse->setMaxAge(100);
|
||||
|
||||
$embeddedResponse = new Response();
|
||||
$embeddedResponse->setPublic();
|
||||
$embeddedResponse->setSharedMaxAge(100);
|
||||
|
||||
$cacheStrategy->add($embeddedResponse);
|
||||
$cacheStrategy->update($masterResponse);
|
||||
|
||||
$this->assertTrue($masterResponse->headers->hasCacheControlDirective('private'));
|
||||
$this->assertFalse($masterResponse->headers->hasCacheControlDirective('public'));
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider cacheControlMergingProvider
|
||||
*/
|
||||
public function testCacheControlMerging(array $expects, array $master, array $surrogates)
|
||||
{
|
||||
$cacheStrategy = new ResponseCacheStrategy();
|
||||
$buildResponse = function ($config) {
|
||||
$response = new Response();
|
||||
|
||||
foreach ($config as $key => $value) {
|
||||
switch ($key) {
|
||||
case 'age':
|
||||
$response->headers->set('Age', $value);
|
||||
break;
|
||||
|
||||
case 'expires':
|
||||
$expires = clone $response->getDate();
|
||||
$expires->modify('+'.$value.' seconds');
|
||||
$response->setExpires($expires);
|
||||
break;
|
||||
|
||||
case 'max-age':
|
||||
$response->setMaxAge($value);
|
||||
break;
|
||||
|
||||
case 's-maxage':
|
||||
$response->setSharedMaxAge($value);
|
||||
break;
|
||||
|
||||
case 'private':
|
||||
$response->setPrivate();
|
||||
break;
|
||||
|
||||
case 'public':
|
||||
$response->setPublic();
|
||||
break;
|
||||
|
||||
default:
|
||||
$response->headers->addCacheControlDirective($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
};
|
||||
|
||||
foreach ($surrogates as $config) {
|
||||
$cacheStrategy->add($buildResponse($config));
|
||||
}
|
||||
|
||||
$response = $buildResponse($master);
|
||||
$cacheStrategy->update($response);
|
||||
|
||||
foreach ($expects as $key => $value) {
|
||||
if ('expires' === $key) {
|
||||
$this->assertSame($value, $response->getExpires()->format('U') - $response->getDate()->format('U'));
|
||||
} elseif ('age' === $key) {
|
||||
$this->assertSame($value, $response->getAge());
|
||||
} elseif (true === $value) {
|
||||
$this->assertTrue($response->headers->hasCacheControlDirective($key), sprintf('Cache-Control header must have "%s" flag', $key));
|
||||
} elseif (false === $value) {
|
||||
$this->assertFalse(
|
||||
$response->headers->hasCacheControlDirective($key),
|
||||
sprintf('Cache-Control header must NOT have "%s" flag', $key)
|
||||
);
|
||||
} else {
|
||||
$this->assertSame($value, $response->headers->getCacheControlDirective($key), sprintf('Cache-Control flag "%s" should be "%s"', $key, $value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function cacheControlMergingProvider()
|
||||
{
|
||||
yield 'result is public if all responses are public' => [
|
||||
['private' => false, 'public' => true],
|
||||
['public' => true],
|
||||
[
|
||||
['public' => true],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'result is private by default' => [
|
||||
['private' => true, 'public' => false],
|
||||
['public' => true],
|
||||
[
|
||||
[],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'combines public and private responses' => [
|
||||
['must-revalidate' => false, 'private' => true, 'public' => false],
|
||||
['public' => true],
|
||||
[
|
||||
['private' => true],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'inherits no-cache from surrogates' => [
|
||||
['no-cache' => true, 'public' => false],
|
||||
['public' => true],
|
||||
[
|
||||
['no-cache' => true],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'inherits no-store from surrogate' => [
|
||||
['no-store' => true, 'public' => false],
|
||||
['public' => true],
|
||||
[
|
||||
['no-store' => true],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'resolve to lowest possible max-age' => [
|
||||
['public' => false, 'private' => true, 's-maxage' => false, 'max-age' => '60'],
|
||||
['public' => true, 'max-age' => 3600],
|
||||
[
|
||||
['private' => true, 'max-age' => 60],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'resolves multiple max-age' => [
|
||||
['public' => false, 'private' => true, 's-maxage' => false, 'max-age' => '60'],
|
||||
['private' => true, 'max-age' => 100],
|
||||
[
|
||||
['private' => true, 'max-age' => 3600],
|
||||
['public' => true, 'max-age' => 60, 's-maxage' => 60],
|
||||
['private' => true, 'max-age' => 60],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'merge max-age and s-maxage' => [
|
||||
['public' => true, 's-maxage' => '60', 'max-age' => null],
|
||||
['public' => true, 's-maxage' => 3600],
|
||||
[
|
||||
['public' => true, 'max-age' => 60],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'result is private when combining private responses' => [
|
||||
['no-cache' => false, 'must-revalidate' => false, 'private' => true],
|
||||
['s-maxage' => 60, 'private' => true],
|
||||
[
|
||||
['s-maxage' => 60, 'private' => true],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'result can have s-maxage and max-age' => [
|
||||
['public' => true, 'private' => false, 's-maxage' => '60', 'max-age' => '30'],
|
||||
['s-maxage' => 100, 'max-age' => 2000],
|
||||
[
|
||||
['s-maxage' => 1000, 'max-age' => 30],
|
||||
['s-maxage' => 500, 'max-age' => 500],
|
||||
['s-maxage' => 60, 'max-age' => 1000],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'does not set headers without value' => [
|
||||
['max-age' => null, 's-maxage' => null, 'public' => null],
|
||||
['private' => true],
|
||||
[
|
||||
['private' => true],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'max-age 0 is sent to the client' => [
|
||||
['private' => true, 'max-age' => '0'],
|
||||
['max-age' => 0, 'private' => true],
|
||||
[
|
||||
['max-age' => 60, 'private' => true],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'max-age is relative to age' => [
|
||||
['max-age' => '240', 'age' => 60],
|
||||
['max-age' => 180],
|
||||
[
|
||||
['max-age' => 600, 'age' => 60],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'retains lowest age of all responses' => [
|
||||
['max-age' => '160', 'age' => 60],
|
||||
['max-age' => 600, 'age' => 60],
|
||||
[
|
||||
['max-age' => 120, 'age' => 20],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'max-age can be less than age, essentially expiring the response' => [
|
||||
['age' => 120, 'max-age' => '90'],
|
||||
['max-age' => 90, 'age' => 120],
|
||||
[
|
||||
['max-age' => 120, 'age' => 60],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'max-age is 0 regardless of age' => [
|
||||
['max-age' => '0'],
|
||||
['max-age' => 60],
|
||||
[
|
||||
['max-age' => 0, 'age' => 60],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'max-age is not negative' => [
|
||||
['max-age' => '0'],
|
||||
['max-age' => 0],
|
||||
[
|
||||
['max-age' => 0, 'age' => 60],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'calculates lowest Expires header' => [
|
||||
['expires' => 60],
|
||||
['expires' => 60],
|
||||
[
|
||||
['expires' => 120],
|
||||
],
|
||||
];
|
||||
|
||||
yield 'calculates Expires header relative to age' => [
|
||||
['expires' => 210, 'age' => 120],
|
||||
['expires' => 90],
|
||||
[
|
||||
['expires' => 600, 'age' => '120'],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
211
vendor/symfony/http-kernel/Tests/HttpCache/SsiTest.php
vendored
Normal file
211
vendor/symfony/http-kernel/Tests/HttpCache/SsiTest.php
vendored
Normal file
@@ -0,0 +1,211 @@
|
||||
<?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());
|
||||
}
|
||||
|
||||
public function testProcessWhenNoSrcInAnSsi()
|
||||
{
|
||||
$this->expectException('RuntimeException');
|
||||
$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));
|
||||
}
|
||||
|
||||
public function testHandleWhenResponseIsNot200()
|
||||
{
|
||||
$this->expectException('RuntimeException');
|
||||
$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('/'), [$response1, $response2]);
|
||||
$this->assertEquals('bar', $ssi->handle($cache, '/', '/alt', false));
|
||||
}
|
||||
|
||||
protected function getCache($request, $response)
|
||||
{
|
||||
$cache = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\HttpCache')->setMethods(['getRequest', 'handle'])->disableOriginalConstructor()->getMock();
|
||||
$cache->expects($this->any())
|
||||
->method('getRequest')
|
||||
->willReturn($request)
|
||||
;
|
||||
if (\is_array($response)) {
|
||||
$cache->expects($this->any())
|
||||
->method('handle')
|
||||
->will($this->onConsecutiveCalls(...$response))
|
||||
;
|
||||
} else {
|
||||
$cache->expects($this->any())
|
||||
->method('handle')
|
||||
->willReturn($response)
|
||||
;
|
||||
}
|
||||
|
||||
return $cache;
|
||||
}
|
||||
}
|
||||
301
vendor/symfony/http-kernel/Tests/HttpCache/StoreTest.php
vendored
Normal file
301
vendor/symfony/http-kernel/Tests/HttpCache/StoreTest.php
vendored
Normal 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(): void
|
||||
{
|
||||
$this->request = Request::create('/');
|
||||
$this->response = new Response('hello world', 200, []);
|
||||
|
||||
HttpCacheTestCase::clearDirectory(sys_get_temp_dir().'/http_cache');
|
||||
|
||||
$this->store = new Store(sys_get_temp_dir().'/http_cache');
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$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', [], [], [], ['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(['content-length' => 4, 'x-body-file' => [$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', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
|
||||
$req2 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam']);
|
||||
$res = new Response('test', 200, ['Vary' => 'Foo Bar']);
|
||||
$this->store->write($req1, $res);
|
||||
|
||||
$this->assertNull($this->store->lookup($req2));
|
||||
}
|
||||
|
||||
public function testDoesNotReturnEntriesThatSlightlyVaryWithLookup()
|
||||
{
|
||||
$req1 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
|
||||
$req2 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bam']);
|
||||
$res = new Response('test', 200, ['Vary' => ['Foo', 'Bar']]);
|
||||
$this->store->write($req1, $res);
|
||||
|
||||
$this->assertNull($this->store->lookup($req2));
|
||||
}
|
||||
|
||||
public function testStoresMultipleResponsesForEachVaryCombination()
|
||||
{
|
||||
$req1 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
|
||||
$res1 = new Response('test 1', 200, ['Vary' => 'Foo Bar']);
|
||||
$key = $this->store->write($req1, $res1);
|
||||
|
||||
$req2 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam']);
|
||||
$res2 = new Response('test 2', 200, ['Vary' => 'Foo Bar']);
|
||||
$this->store->write($req2, $res2);
|
||||
|
||||
$req3 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Baz', 'HTTP_BAR' => 'Boom']);
|
||||
$res3 = new Response('test 3', 200, ['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', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
|
||||
$res1 = new Response('test 1', 200, ['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', [], [], [], ['HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam']);
|
||||
$res2 = new Response('test 2', 200, ['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', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
|
||||
$res3 = new Response('test 3', 200, ['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', [], [], [], ['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 = [])
|
||||
{
|
||||
if (null === $path) {
|
||||
$path = '/test';
|
||||
}
|
||||
|
||||
$this->request = Request::create($path, 'get', [], [], [], $headers);
|
||||
$this->response = new Response('test', 200, ['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);
|
||||
}
|
||||
}
|
||||
153
vendor/symfony/http-kernel/Tests/HttpCache/SubRequestHandlerTest.php
vendored
Normal file
153
vendor/symfony/http-kernel/Tests/HttpCache/SubRequestHandlerTest.php
vendored
Normal file
@@ -0,0 +1,153 @@
|
||||
<?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\SubRequestHandler;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
|
||||
class SubRequestHandlerTest extends TestCase
|
||||
{
|
||||
private static $globalState;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::$globalState = $this->getGlobalState();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Request::setTrustedProxies(self::$globalState[0], self::$globalState[1]);
|
||||
}
|
||||
|
||||
public function testTrustedHeadersAreKept()
|
||||
{
|
||||
Request::setTrustedProxies(['10.0.0.1'], -1);
|
||||
$globalState = $this->getGlobalState();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->server->set('REMOTE_ADDR', '10.0.0.1');
|
||||
$request->headers->set('X-Forwarded-For', '10.0.0.2');
|
||||
$request->headers->set('X-Forwarded-Host', 'Good');
|
||||
$request->headers->set('X-Forwarded-Port', '1234');
|
||||
$request->headers->set('X-Forwarded-Proto', 'https');
|
||||
|
||||
$kernel = new TestSubRequestHandlerKernel(function ($request, $type, $catch) {
|
||||
$this->assertSame('127.0.0.1', $request->server->get('REMOTE_ADDR'));
|
||||
$this->assertSame('10.0.0.2', $request->getClientIp());
|
||||
$this->assertSame('Good', $request->headers->get('X-Forwarded-Host'));
|
||||
$this->assertSame('1234', $request->headers->get('X-Forwarded-Port'));
|
||||
$this->assertSame('https', $request->headers->get('X-Forwarded-Proto'));
|
||||
});
|
||||
|
||||
SubRequestHandler::handle($kernel, $request, HttpKernelInterface::MASTER_REQUEST, true);
|
||||
|
||||
$this->assertSame($globalState, $this->getGlobalState());
|
||||
}
|
||||
|
||||
public function testUntrustedHeadersAreRemoved()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->server->set('REMOTE_ADDR', '10.0.0.1');
|
||||
$request->headers->set('X-Forwarded-For', '10.0.0.2');
|
||||
$request->headers->set('X-Forwarded-Host', 'Evil');
|
||||
$request->headers->set('X-Forwarded-Port', '1234');
|
||||
$request->headers->set('X-Forwarded-Proto', 'http');
|
||||
$request->headers->set('Forwarded', 'Evil2');
|
||||
|
||||
$kernel = new TestSubRequestHandlerKernel(function ($request, $type, $catch) {
|
||||
$this->assertSame('127.0.0.1', $request->server->get('REMOTE_ADDR'));
|
||||
$this->assertSame('10.0.0.1', $request->getClientIp());
|
||||
$this->assertFalse($request->headers->has('X-Forwarded-Host'));
|
||||
$this->assertFalse($request->headers->has('X-Forwarded-Port'));
|
||||
$this->assertFalse($request->headers->has('X-Forwarded-Proto'));
|
||||
$this->assertSame('for="10.0.0.1";host="localhost";proto=http', $request->headers->get('Forwarded'));
|
||||
});
|
||||
|
||||
SubRequestHandler::handle($kernel, $request, HttpKernelInterface::MASTER_REQUEST, true);
|
||||
|
||||
$this->assertSame(self::$globalState, $this->getGlobalState());
|
||||
}
|
||||
|
||||
public function testTrustedForwardedHeader()
|
||||
{
|
||||
Request::setTrustedProxies(['10.0.0.1'], -1);
|
||||
$globalState = $this->getGlobalState();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->server->set('REMOTE_ADDR', '10.0.0.1');
|
||||
$request->headers->set('Forwarded', 'for="10.0.0.2";host="foo.bar:1234";proto=https');
|
||||
|
||||
$kernel = new TestSubRequestHandlerKernel(function ($request, $type, $catch) {
|
||||
$this->assertSame('127.0.0.1', $request->server->get('REMOTE_ADDR'));
|
||||
$this->assertSame('10.0.0.2', $request->getClientIp());
|
||||
$this->assertSame('foo.bar:1234', $request->getHttpHost());
|
||||
$this->assertSame('https', $request->getScheme());
|
||||
$this->assertSame(1234, $request->getPort());
|
||||
});
|
||||
|
||||
SubRequestHandler::handle($kernel, $request, HttpKernelInterface::MASTER_REQUEST, true);
|
||||
|
||||
$this->assertSame($globalState, $this->getGlobalState());
|
||||
}
|
||||
|
||||
public function testTrustedXForwardedForHeader()
|
||||
{
|
||||
Request::setTrustedProxies(['10.0.0.1'], -1);
|
||||
$globalState = $this->getGlobalState();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->server->set('REMOTE_ADDR', '10.0.0.1');
|
||||
$request->headers->set('X-Forwarded-For', '10.0.0.2');
|
||||
$request->headers->set('X-Forwarded-Host', 'foo.bar');
|
||||
$request->headers->set('X-Forwarded-Proto', 'https');
|
||||
|
||||
$kernel = new TestSubRequestHandlerKernel(function ($request, $type, $catch) {
|
||||
$this->assertSame('127.0.0.1', $request->server->get('REMOTE_ADDR'));
|
||||
$this->assertSame('10.0.0.2', $request->getClientIp());
|
||||
$this->assertSame('foo.bar', $request->getHttpHost());
|
||||
$this->assertSame('https', $request->getScheme());
|
||||
});
|
||||
|
||||
SubRequestHandler::handle($kernel, $request, HttpKernelInterface::MASTER_REQUEST, true);
|
||||
|
||||
$this->assertSame($globalState, $this->getGlobalState());
|
||||
}
|
||||
|
||||
private function getGlobalState()
|
||||
{
|
||||
return [
|
||||
Request::getTrustedProxies(),
|
||||
Request::getTrustedHeaderSet(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class TestSubRequestHandlerKernel implements HttpKernelInterface
|
||||
{
|
||||
private $assertCallback;
|
||||
|
||||
public function __construct(\Closure $assertCallback)
|
||||
{
|
||||
$this->assertCallback = $assertCallback;
|
||||
}
|
||||
|
||||
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
|
||||
{
|
||||
$assertCallback = $this->assertCallback;
|
||||
$assertCallback($request, $type, $catch);
|
||||
|
||||
return new Response();
|
||||
}
|
||||
}
|
||||
102
vendor/symfony/http-kernel/Tests/HttpCache/TestHttpKernel.php
vendored
Normal file
102
vendor/symfony/http-kernel/Tests/HttpCache/TestHttpKernel.php
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
<?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\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
|
||||
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
|
||||
use Symfony\Component\HttpKernel\HttpKernel;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
|
||||
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 assert(\Closure $callback)
|
||||
{
|
||||
$trustedConfig = [Request::getTrustedProxies(), Request::getTrustedHeaderSet()];
|
||||
|
||||
list($trustedProxies, $trustedHeaderSet, $backendRequest) = $this->backendRequest;
|
||||
Request::setTrustedProxies($trustedProxies, $trustedHeaderSet);
|
||||
|
||||
try {
|
||||
$callback($backendRequest);
|
||||
} finally {
|
||||
list($trustedProxies, $trustedHeaderSet) = $trustedConfig;
|
||||
Request::setTrustedProxies($trustedProxies, $trustedHeaderSet);
|
||||
}
|
||||
}
|
||||
|
||||
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
|
||||
{
|
||||
$this->catch = $catch;
|
||||
$this->backendRequest = [Request::getTrustedProxies(), Request::getTrustedHeaderSet(), $request];
|
||||
|
||||
return parent::handle($request, $type, $catch);
|
||||
}
|
||||
|
||||
public function isCatchingExceptions()
|
||||
{
|
||||
return $this->catch;
|
||||
}
|
||||
|
||||
public function getController(Request $request)
|
||||
{
|
||||
return [$this, 'callController'];
|
||||
}
|
||||
|
||||
public function getArguments(Request $request, $controller)
|
||||
{
|
||||
return [$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;
|
||||
}
|
||||
}
|
||||
81
vendor/symfony/http-kernel/Tests/HttpCache/TestMultipleHttpKernel.php
vendored
Normal file
81
vendor/symfony/http-kernel/Tests/HttpCache/TestMultipleHttpKernel.php
vendored
Normal 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\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
|
||||
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
|
||||
use Symfony\Component\HttpKernel\HttpKernel;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
|
||||
class TestMultipleHttpKernel extends HttpKernel implements ControllerResolverInterface, ArgumentResolverInterface
|
||||
{
|
||||
protected $bodies = [];
|
||||
protected $statuses = [];
|
||||
protected $headers = [];
|
||||
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 [$this, 'callController'];
|
||||
}
|
||||
|
||||
public function getArguments(Request $request, $controller)
|
||||
{
|
||||
return [$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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user