Laravel version update

Laravel version update
This commit is contained in:
Manish Verma
2018-08-06 18:48:58 +05:30
parent d143048413
commit 126fbb0255
13678 changed files with 1031482 additions and 778530 deletions

View File

@@ -11,11 +11,12 @@
namespace Symfony\Component\HttpKernel\Tests\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\Esi;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\Esi;
class EsiTest extends \PHPUnit_Framework_TestCase
class EsiTest extends TestCase
{
public function testHasSurrogateEsiCapability()
{
@@ -39,10 +40,10 @@ class EsiTest extends \PHPUnit_Framework_TestCase
$request = Request::create('/');
$esi->addSurrogateCapability($request);
$this->assertEquals('symfony2="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
$this->assertEquals('symfony="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
$esi->addSurrogateCapability($request);
$this->assertEquals('symfony2="ESI/1.0", symfony2="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
$this->assertEquals('symfony="ESI/1.0", symfony="ESI/1.0"', $request->headers->get('Surrogate-Capability'));
}
public function testAddSurrogateControl()
@@ -225,15 +226,15 @@ class EsiTest extends \PHPUnit_Framework_TestCase
protected function getCache($request, $response)
{
$cache = $this->getMock('Symfony\Component\HttpKernel\HttpCache\HttpCache', array('getRequest', 'handle'), array(), '', false);
$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)) {
if (\is_array($response)) {
$cache->expects($this->any())
->method('handle')
->will(call_user_func_array(array($this, 'onConsecutiveCalls'), $response))
->will(\call_user_func_array(array($this, 'onConsecutiveCalls'), $response))
;
} else {
$cache->expects($this->any())

View File

@@ -11,9 +11,11 @@
namespace Symfony\Component\HttpKernel\Tests\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
@@ -213,7 +215,7 @@ class HttpCacheTest extends HttpCacheTestCase
if ($request->cookies->has('authenticated')) {
$response->headers->set('Cache-Control', 'private, no-store');
$response->setETag('"private tag"');
if (in_array('"private tag"', $etags)) {
if (\in_array('"private tag"', $etags)) {
$response->setStatusCode(304);
} else {
$response->setStatusCode(200);
@@ -223,7 +225,7 @@ class HttpCacheTest extends HttpCacheTestCase
} else {
$response->headers->set('Cache-Control', 'public');
$response->setETag('"public tag"');
if (in_array('"public tag"', $etags)) {
if (\in_array('"public tag"', $etags)) {
$response->setStatusCode(304);
} else {
$response->setStatusCode(200);
@@ -530,8 +532,8 @@ class HttpCacheTest extends HttpCacheTestCase
$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
$this->assertTrue($this->response->headers->get('Age') > 0);
$this->assertLessThan(2, strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')));
$this->assertGreaterThan(0, $this->response->headers->get('Age'));
$this->assertNotNull($this->response->headers->get('X-Content-Digest'));
$this->assertTraceContains('fresh');
$this->assertTraceNotContains('store');
@@ -554,14 +556,54 @@ class HttpCacheTest extends HttpCacheTestCase
$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
$this->assertTrue($this->response->headers->get('Age') > 0);
$this->assertLessThan(2, strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')));
$this->assertGreaterThan(0, $this->response->headers->get('Age'));
$this->assertNotNull($this->response->headers->get('X-Content-Digest'));
$this->assertTraceContains('fresh');
$this->assertTraceNotContains('store');
$this->assertEquals('Hello World', $this->response->getContent());
}
public function testDegradationWhenCacheLocked()
{
if ('\\' === \DIRECTORY_SEPARATOR) {
$this->markTestSkipped('Skips on windows to avoid permissions issues.');
}
$this->cacheConfig['stale_while_revalidate'] = 10;
// The prescence of Last-Modified makes this cacheable (because Response::isValidateable() then).
$this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=5', 'Last-Modified' => 'some while ago'), 'Old response');
$this->request('GET', '/'); // warm the cache
// Now, lock the cache
$concurrentRequest = Request::create('/', 'GET');
$this->store->lock($concurrentRequest);
/*
* After 10s, the cached response has become stale. Yet, we're still within the "stale_while_revalidate"
* timeout so we may serve the stale response.
*/
sleep(10);
$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTraceContains('stale-while-revalidate');
$this->assertEquals('Old response', $this->response->getContent());
/*
* Another 10s later, stale_while_revalidate is over. Resort to serving the old response, but
* do so with a "server unavailable" message.
*/
sleep(10);
$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(503, $this->response->getStatusCode());
$this->assertEquals('Old response', $this->response->getContent());
}
public function testHitsCachedResponseWithSMaxAgeDirective()
{
$time = \DateTime::createFromFormat('U', time() - 5);
@@ -578,8 +620,8 @@ class HttpCacheTest extends HttpCacheTestCase
$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTrue(strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')) < 2);
$this->assertTrue($this->response->headers->get('Age') > 0);
$this->assertLessThan(2, strtotime($this->responses[0]->headers->get('Date')) - strtotime($this->response->headers->get('Date')));
$this->assertGreaterThan(0, $this->response->headers->get('Age'));
$this->assertNotNull($this->response->headers->get('X-Content-Digest'));
$this->assertTraceContains('fresh');
$this->assertTraceNotContains('store');
@@ -753,7 +795,7 @@ class HttpCacheTest extends HttpCacheTestCase
$this->request('GET', '/');
$this->assertHttpKernelIsCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTrue($this->response->headers->get('Age') <= 1);
$this->assertLessThanOrEqual(1, $this->response->headers->get('Age'));
$this->assertNotNull($this->response->headers->get('X-Content-Digest'));
$this->assertTraceContains('stale');
$this->assertTraceNotContains('fresh');
@@ -791,7 +833,7 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertNotNull($this->response->headers->get('Last-Modified'));
$this->assertNotNull($this->response->headers->get('X-Content-Digest'));
$this->assertTrue($this->response->headers->get('Age') <= 1);
$this->assertLessThanOrEqual(1, $this->response->headers->get('Age'));
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertTraceContains('stale');
$this->assertTraceContains('valid');
@@ -799,6 +841,21 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertTraceNotContains('miss');
}
public function testValidatesCachedResponsesUseSameHttpMethod()
{
$test = $this;
$this->setNextResponse(200, array(), 'Hello World', function ($request, $response) use ($test) {
$test->assertSame('OPTIONS', $request->getMethod());
});
// build initial request
$this->request('OPTIONS', '/');
// build subsequent request
$this->request('OPTIONS', '/');
}
public function testValidatesCachedResponsesWithETagAndNoFreshnessInformation()
{
$this->setNextResponse(200, array(), 'Hello World', function ($request, $response) {
@@ -826,7 +883,7 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertNotNull($this->response->headers->get('ETag'));
$this->assertNotNull($this->response->headers->get('X-Content-Digest'));
$this->assertTrue($this->response->headers->get('Age') <= 1);
$this->assertLessThanOrEqual(1, $this->response->headers->get('Age'));
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertTraceContains('stale');
$this->assertTraceContains('valid');
@@ -834,6 +891,40 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertTraceNotContains('miss');
}
public function testServesResponseWhileFreshAndRevalidatesWithLastModifiedInformation()
{
$time = \DateTime::createFromFormat('U', time());
$this->setNextResponse(200, array(), 'Hello World', function (Request $request, Response $response) use ($time) {
$response->setSharedMaxAge(10);
$response->headers->set('Last-Modified', $time->format(DATE_RFC2822));
});
// prime the cache
$this->request('GET', '/');
// next request before s-maxage has expired: Serve from cache
// without hitting the backend
$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertTraceContains('fresh');
sleep(15); // expire the cache
$this->setNextResponse(304, array(), '', function (Request $request, Response $response) use ($time) {
$this->assertEquals($time->format(DATE_RFC2822), $request->headers->get('IF_MODIFIED_SINCE'));
});
$this->request('GET', '/');
$this->assertHttpKernelIsCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertEquals('Hello World', $this->response->getContent());
$this->assertTraceContains('stale');
$this->assertTraceContains('valid');
}
public function testReplacesCachedResponsesWhenValidationResultsInNon304Response()
{
$time = \DateTime::createFromFormat('U', time());
@@ -903,7 +994,7 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertEquals('', $this->response->getContent());
$this->assertEquals(strlen('Hello World'), $this->response->headers->get('Content-Length'));
$this->assertEquals(\strlen('Hello World'), $this->response->headers->get('Content-Length'));
}
public function testSendsNoContentWhenFresh()
@@ -1079,7 +1170,7 @@ class HttpCacheTest extends HttpCacheTestCase
array(
'status' => 200,
'body' => 'Hello World!',
'headers' => array('Cache-Control' => 's-maxage=300'),
'headers' => array('Cache-Control' => 's-maxage=200'),
),
array(
'status' => 200,
@@ -1093,8 +1184,33 @@ class HttpCacheTest extends HttpCacheTestCase
$this->request('GET', '/', array(), array(), true);
$this->assertEquals('Hello World! My name is Bobby.', $this->response->getContent());
// check for 100 or 99 as the test can be executed after a second change
$this->assertTrue(in_array($this->response->getTtl(), array(99, 100)));
$this->assertEquals(100, $this->response->getTtl());
}
public function testEsiCacheSendsTheLowestTtlForHeadRequests()
{
$responses = array(
array(
'status' => 200,
'body' => 'I am a long-lived master response, but I embed a short-lived resource: <esi:include src="/foo" />',
'headers' => array(
'Cache-Control' => 's-maxage=300',
'Surrogate-Control' => 'content="ESI/1.0"',
),
),
array(
'status' => 200,
'body' => 'I am a short-lived resource',
'headers' => array('Cache-Control' => 's-maxage=100'),
),
);
$this->setNextResponses($responses);
$this->request('HEAD', '/', array(), array(), true);
$this->assertEmpty($this->response->getContent());
$this->assertEquals(100, $this->response->getTtl());
}
public function testEsiCacheForceValidation()
@@ -1130,6 +1246,37 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
}
public function testEsiCacheForceValidationForHeadRequests()
{
$responses = array(
array(
'status' => 200,
'body' => 'I am the master response and use expiration caching, but I embed another resource: <esi:include src="/foo" />',
'headers' => array(
'Cache-Control' => 's-maxage=300',
'Surrogate-Control' => 'content="ESI/1.0"',
),
),
array(
'status' => 200,
'body' => 'I am the embedded resource and use validation caching',
'headers' => array('ETag' => 'foobar'),
),
);
$this->setNextResponses($responses);
$this->request('HEAD', '/', array(), array(), true);
// The response has been assembled from expiration and validation based resources
// This can neither be cached nor revalidated, so it should be private/no cache
$this->assertEmpty($this->response->getContent());
$this->assertNull($this->response->getTtl());
$this->assertTrue($this->response->mustRevalidate());
$this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
$this->assertTrue($this->response->headers->hasCacheControlDirective('no-cache'));
}
public function testEsiRecalculateContentLengthHeader()
{
$responses = array(
@@ -1138,7 +1285,6 @@ class HttpCacheTest extends HttpCacheTestCase
'body' => '<esi:include src="/foo" />',
'headers' => array(
'Content-Length' => 26,
'Cache-Control' => 's-maxage=300',
'Surrogate-Control' => 'content="ESI/1.0"',
),
),
@@ -1156,69 +1302,105 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertEquals(12, $this->response->headers->get('Content-Length'));
}
public function testEsiRecalculateContentLengthHeaderForHeadRequest()
{
$responses = array(
array(
'status' => 200,
'body' => '<esi:include src="/foo" />',
'headers' => array(
'Content-Length' => 26,
'Surrogate-Control' => 'content="ESI/1.0"',
),
),
array(
'status' => 200,
'body' => 'Hello World!',
'headers' => array(),
),
);
$this->setNextResponses($responses);
$this->request('HEAD', '/', array(), array(), true);
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13
// "The Content-Length entity-header field indicates the size of the entity-body,
// in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD
// method, the size of the entity-body that would have been sent had the request
// been a GET."
$this->assertEmpty($this->response->getContent());
$this->assertEquals(12, $this->response->headers->get('Content-Length'));
}
public function testClientIpIsAlwaysLocalhostForForwardedRequests()
{
$this->setNextResponse();
$this->request('GET', '/', array('REMOTE_ADDR' => '10.0.0.1'));
$this->assertEquals('127.0.0.1', $this->kernel->getBackendRequest()->server->get('REMOTE_ADDR'));
$this->kernel->assert(function ($backendRequest) {
$this->assertSame('127.0.0.1', $backendRequest->server->get('REMOTE_ADDR'));
});
}
/**
* @dataProvider getTrustedProxyData
*/
public function testHttpCacheIsSetAsATrustedProxy(array $existing, array $expected)
public function testHttpCacheIsSetAsATrustedProxy(array $existing)
{
Request::setTrustedProxies($existing);
Request::setTrustedProxies($existing, Request::HEADER_X_FORWARDED_ALL);
$this->setNextResponse();
$this->request('GET', '/', array('REMOTE_ADDR' => '10.0.0.1'));
$this->assertSame($existing, Request::getTrustedProxies());
$this->assertEquals($expected, Request::getTrustedProxies());
$existing = array_unique(array_merge($existing, array('127.0.0.1')));
$this->kernel->assert(function ($backendRequest) use ($existing) {
$this->assertSame($existing, Request::getTrustedProxies());
$this->assertsame('10.0.0.1', $backendRequest->getClientIp());
});
Request::setTrustedProxies(array(), -1);
}
public function getTrustedProxyData()
{
return array(
array(array(), array('127.0.0.1')),
array(array('10.0.0.2'), array('10.0.0.2', '127.0.0.1')),
array(array('10.0.0.2', '127.0.0.1'), array('10.0.0.2', '127.0.0.1')),
array(array()),
array(array('10.0.0.2')),
array(array('10.0.0.2', '127.0.0.1')),
);
}
/**
* @dataProvider getXForwardedForData
* @dataProvider getForwardedData
*/
public function testXForwarderForHeaderForForwardedRequests($xForwardedFor, $expected)
public function testForwarderHeaderForForwardedRequests($forwarded, $expected)
{
$this->setNextResponse();
$server = array('REMOTE_ADDR' => '10.0.0.1');
if (false !== $xForwardedFor) {
$server['HTTP_X_FORWARDED_FOR'] = $xForwardedFor;
if (null !== $forwarded) {
Request::setTrustedProxies($server, -1);
$server['HTTP_FORWARDED'] = $forwarded;
}
$this->request('GET', '/', $server);
$this->assertEquals($expected, $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For'));
$this->kernel->assert(function ($backendRequest) use ($expected) {
$this->assertSame($expected, $backendRequest->headers->get('Forwarded'));
});
Request::setTrustedProxies(array(), -1);
}
public function getXForwardedForData()
public function getForwardedData()
{
return array(
array(false, '10.0.0.1'),
array('10.0.0.2', '10.0.0.2, 10.0.0.1'),
array('10.0.0.2, 10.0.0.3', '10.0.0.2, 10.0.0.3, 10.0.0.1'),
array(null, 'for="10.0.0.1";host="localhost";proto=http'),
array('for=10.0.0.2', 'for="10.0.0.2";host="localhost";proto=http, for="10.0.0.1"'),
array('for=10.0.0.2, for=10.0.0.3', 'for="10.0.0.2";host="localhost";proto=http, for="10.0.0.3", for="10.0.0.1"'),
);
}
public function testXForwarderForHeaderForPassRequests()
{
$this->setNextResponse();
$server = array('REMOTE_ADDR' => '10.0.0.1');
$this->request('POST', '/', $server);
$this->assertEquals('10.0.0.1', $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For'));
}
public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponses()
{
$time = \DateTime::createFromFormat('U', time());
@@ -1246,6 +1428,86 @@ class HttpCacheTest extends HttpCacheTestCase
$this->assertNull($this->response->getETag());
$this->assertNull($this->response->getLastModified());
}
public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponsesAndHeadRequest()
{
$time = \DateTime::createFromFormat('U', time());
$responses = array(
array(
'status' => 200,
'body' => '<esi:include src="/hey" />',
'headers' => array(
'Surrogate-Control' => 'content="ESI/1.0"',
'ETag' => 'hey',
'Last-Modified' => $time->format(DATE_RFC2822),
),
),
array(
'status' => 200,
'body' => 'Hey!',
'headers' => array(),
),
);
$this->setNextResponses($responses);
$this->request('HEAD', '/', array(), array(), true);
$this->assertEmpty($this->response->getContent());
$this->assertNull($this->response->getETag());
$this->assertNull($this->response->getLastModified());
}
public function testDoesNotCacheOptionsRequest()
{
$this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=60'), 'get');
$this->request('GET', '/');
$this->assertHttpKernelIsCalled();
$this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=60'), 'options');
$this->request('OPTIONS', '/');
$this->assertHttpKernelIsCalled();
$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertSame('get', $this->response->getContent());
}
public function testUsesOriginalRequestForSurrogate()
{
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
$store = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\StoreInterface')->getMock();
$kernel
->expects($this->exactly(2))
->method('handle')
->willReturnCallback(function (Request $request) {
$this->assertSame('127.0.0.1', $request->server->get('REMOTE_ADDR'));
return new Response();
});
$cache = new HttpCache($kernel,
$store,
new Esi()
);
$request = Request::create('/');
$request->server->set('REMOTE_ADDR', '10.0.0.1');
// Main request
$cache->handle($request, HttpKernelInterface::MASTER_REQUEST);
// Main request was now modified by HttpCache
// The surrogate will ask for the request using $this->cache->getRequest()
// which MUST return the original request so the surrogate
// can actually behave like a reverse proxy like e.g. Varnish would.
$this->assertSame('10.0.0.1', $cache->getRequest()->getClientIp());
$this->assertSame('10.0.0.1', $cache->getRequest()->server->get('REMOTE_ADDR'));
// Surrogate request
$cache->handle($request, HttpKernelInterface::SUB_REQUEST);
}
}
class TestKernel implements HttpKernelInterface

View File

@@ -11,13 +11,14 @@
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 \PHPUnit_Framework_TestCase
class HttpCacheTestCase extends TestCase
{
protected $kernel;
protected $cache;
@@ -28,6 +29,10 @@ class HttpCacheTestCase extends \PHPUnit_Framework_TestCase
protected $responses;
protected $catch;
protected $esi;
/**
* @var Store
*/
protected $store;
protected function setUp()
@@ -163,7 +168,7 @@ class HttpCacheTestCase extends \PHPUnit_Framework_TestCase
$fp = opendir($directory);
while (false !== $file = readdir($fp)) {
if (!in_array($file, array('.', '..'))) {
if (!\in_array($file, array('.', '..'))) {
if (is_link($directory.'/'.$file)) {
unlink($directory.'/'.$file);
} elseif (is_dir($directory.'/'.$file)) {

View File

@@ -15,10 +15,11 @@
namespace Symfony\Component\HttpKernel\Tests\HttpCache;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\ResponseCacheStrategy;
class ResponseCacheStrategyTest extends \PHPUnit_Framework_TestCase
class ResponseCacheStrategyTest extends TestCase
{
public function testMinimumSharedMaxAgeWins()
{
@@ -74,4 +75,166 @@ class ResponseCacheStrategyTest extends \PHPUnit_Framework_TestCase
$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());
}
}

View File

@@ -11,11 +11,12 @@
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 \PHPUnit_Framework_TestCase
class SsiTest extends TestCase
{
public function testHasSurrogateSsiCapability()
{
@@ -39,10 +40,10 @@ class SsiTest extends \PHPUnit_Framework_TestCase
$request = Request::create('/');
$ssi->addSurrogateCapability($request);
$this->assertEquals('symfony2="SSI/1.0"', $request->headers->get('Surrogate-Capability'));
$this->assertEquals('symfony="SSI/1.0"', $request->headers->get('Surrogate-Capability'));
$ssi->addSurrogateCapability($request);
$this->assertEquals('symfony2="SSI/1.0", symfony2="SSI/1.0"', $request->headers->get('Surrogate-Capability'));
$this->assertEquals('symfony="SSI/1.0", symfony="SSI/1.0"', $request->headers->get('Surrogate-Capability'));
}
public function testAddSurrogateControl()
@@ -192,15 +193,15 @@ class SsiTest extends \PHPUnit_Framework_TestCase
protected function getCache($request, $response)
{
$cache = $this->getMock('Symfony\Component\HttpKernel\HttpCache\HttpCache', array('getRequest', 'handle'), array(), '', false);
$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)) {
if (\is_array($response)) {
$cache->expects($this->any())
->method('handle')
->will(call_user_func_array(array($this, 'onConsecutiveCalls'), $response))
->will(\call_user_func_array(array($this, 'onConsecutiveCalls'), $response))
;
} else {
$cache->expects($this->any())

View File

@@ -11,11 +11,12 @@
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 \PHPUnit_Framework_TestCase
class StoreTest extends TestCase
{
protected $request;
protected $response;
@@ -235,6 +236,33 @@ class StoreTest extends \PHPUnit_Framework_TestCase
$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) {

View 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()
{
self::$globalState = $this->getGlobalState();
}
protected function tearDown()
{
Request::setTrustedProxies(self::$globalState[0], self::$globalState[1]);
}
public function testTrustedHeadersAreKept()
{
Request::setTrustedProxies(array('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(array('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(array('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 array(
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();
}
}

View File

@@ -11,14 +11,15 @@
namespace Symfony\Component\HttpKernel\Tests\HttpCache;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
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\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class TestHttpKernel extends HttpKernel implements ControllerResolverInterface
class TestHttpKernel extends HttpKernel implements ControllerResolverInterface, ArgumentResolverInterface
{
protected $body;
protected $status;
@@ -35,18 +36,28 @@ class TestHttpKernel extends HttpKernel implements ControllerResolverInterface
$this->headers = $headers;
$this->customizer = $customizer;
parent::__construct(new EventDispatcher(), $this);
parent::__construct(new EventDispatcher(), $this, null, $this);
}
public function getBackendRequest()
public function assert(\Closure $callback)
{
return $this->backendRequest;
$trustedConfig = array(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;
$this->backendRequest = array(Request::getTrustedProxies(), Request::getTrustedHeaderSet(), $request);
return parent::handle($request, $type, $catch);
}

View File

@@ -11,14 +11,15 @@
namespace Symfony\Component\HttpKernel\Tests\HttpCache;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
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\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class TestMultipleHttpKernel extends HttpKernel implements ControllerResolverInterface
class TestMultipleHttpKernel extends HttpKernel implements ControllerResolverInterface, ArgumentResolverInterface
{
protected $bodies = array();
protected $statuses = array();
@@ -34,7 +35,7 @@ class TestMultipleHttpKernel extends HttpKernel implements ControllerResolverInt
$this->headers[] = $response['headers'];
}
parent::__construct(new EventDispatcher(), $this);
parent::__construct(new EventDispatcher(), $this, null, $this);
}
public function getBackendRequest()