changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
namespace Symfony\Component\HttpFoundation\Tests;
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
2011-05-19 08:49:34 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Cookie;
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
|
2015-10-11 10:00:41 +01:00
|
|
|
/**
|
|
|
|
* @group time-sensitive
|
|
|
|
*/
|
2017-02-08 07:24:27 +00:00
|
|
|
class ResponseHeaderBagTest extends TestCase
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
{
|
2017-03-23 09:24:24 +00:00
|
|
|
public function testAllPreserveCase()
|
2012-10-04 13:45:19 +01:00
|
|
|
{
|
2017-03-23 09:24:24 +00:00
|
|
|
$headers = array(
|
|
|
|
'fOo' => 'BAR',
|
|
|
|
'ETag' => 'xyzzy',
|
|
|
|
'Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ==',
|
|
|
|
'P3P' => 'CP="CAO PSA OUR"',
|
|
|
|
'WWW-Authenticate' => 'Basic realm="WallyWorld"',
|
|
|
|
'X-UA-Compatible' => 'IE=edge,chrome=1',
|
|
|
|
'X-XSS-Protection' => '1; mode=block',
|
|
|
|
);
|
|
|
|
|
2012-10-04 13:45:19 +01:00
|
|
|
$bag = new ResponseHeaderBag($headers);
|
2017-03-23 09:24:24 +00:00
|
|
|
$allPreservedCase = $bag->allPreserveCase();
|
2012-10-04 13:45:19 +01:00
|
|
|
|
2017-03-23 09:24:24 +00:00
|
|
|
foreach (array_keys($headers) as $headerName) {
|
|
|
|
$this->assertArrayHasKey($headerName, $allPreservedCase, '->allPreserveCase() gets all input keys in original case');
|
|
|
|
}
|
2012-10-04 13:45:19 +01:00
|
|
|
}
|
|
|
|
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
public function testCacheControlHeader()
|
|
|
|
{
|
2011-02-15 18:59:11 +00:00
|
|
|
$bag = new ResponseHeaderBag(array());
|
2016-06-22 14:14:54 +01:00
|
|
|
$this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$this->assertTrue($bag->hasCacheControlDirective('no-cache'));
|
|
|
|
|
2011-02-15 18:59:11 +00:00
|
|
|
$bag = new ResponseHeaderBag(array('Cache-Control' => 'public'));
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$this->assertEquals('public', $bag->get('Cache-Control'));
|
|
|
|
$this->assertTrue($bag->hasCacheControlDirective('public'));
|
|
|
|
|
2011-02-15 18:59:11 +00:00
|
|
|
$bag = new ResponseHeaderBag(array('ETag' => 'abcde'));
|
2011-02-16 15:36:12 +00:00
|
|
|
$this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$this->assertTrue($bag->hasCacheControlDirective('private'));
|
|
|
|
$this->assertTrue($bag->hasCacheControlDirective('must-revalidate'));
|
2011-02-16 15:36:12 +00:00
|
|
|
$this->assertFalse($bag->hasCacheControlDirective('max-age'));
|
|
|
|
|
|
|
|
$bag = new ResponseHeaderBag(array('Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT'));
|
|
|
|
$this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
|
|
|
|
|
|
|
|
$bag = new ResponseHeaderBag(array(
|
|
|
|
'Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT',
|
2014-09-21 19:53:12 +01:00
|
|
|
'Cache-Control' => 'max-age=3600',
|
2011-02-16 15:36:12 +00:00
|
|
|
));
|
|
|
|
$this->assertEquals('max-age=3600, private', $bag->get('Cache-Control'));
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
|
2011-02-15 18:59:11 +00:00
|
|
|
$bag = new ResponseHeaderBag(array('Last-Modified' => 'abcde'));
|
2011-02-16 15:36:12 +00:00
|
|
|
$this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
|
2011-02-15 18:59:11 +00:00
|
|
|
$bag = new ResponseHeaderBag(array('Etag' => 'abcde', 'Last-Modified' => 'abcde'));
|
2011-02-16 15:36:12 +00:00
|
|
|
$this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
|
2011-02-15 18:59:11 +00:00
|
|
|
$bag = new ResponseHeaderBag(array('cache-control' => 'max-age=100'));
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
|
|
|
|
|
2011-02-15 18:59:11 +00:00
|
|
|
$bag = new ResponseHeaderBag(array('cache-control' => 's-maxage=100'));
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$this->assertEquals('s-maxage=100', $bag->get('Cache-Control'));
|
|
|
|
|
2011-02-15 18:59:11 +00:00
|
|
|
$bag = new ResponseHeaderBag(array('cache-control' => 'private, max-age=100'));
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
|
|
|
|
|
2011-02-15 18:59:11 +00:00
|
|
|
$bag = new ResponseHeaderBag(array('cache-control' => 'public, max-age=100'));
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$this->assertEquals('max-age=100, public', $bag->get('Cache-Control'));
|
2011-02-15 19:00:52 +00:00
|
|
|
|
|
|
|
$bag = new ResponseHeaderBag();
|
|
|
|
$bag->set('Last-Modified', 'abcde');
|
|
|
|
$this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
|
2017-10-08 15:27:20 +01:00
|
|
|
|
|
|
|
$bag = new ResponseHeaderBag();
|
|
|
|
$bag->set('Cache-Control', array('public', 'must-revalidate'));
|
|
|
|
$this->assertCount(1, $bag->get('Cache-Control', null, false));
|
|
|
|
$this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
|
|
|
|
|
|
|
|
$bag = new ResponseHeaderBag();
|
|
|
|
$bag->set('Cache-Control', 'public');
|
|
|
|
$bag->set('Cache-Control', 'must-revalidate', false);
|
|
|
|
$this->assertCount(1, $bag->get('Cache-Control', null, false));
|
|
|
|
$this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
}
|
2011-05-19 16:38:16 +01:00
|
|
|
|
2016-06-22 14:14:54 +01:00
|
|
|
public function testCacheControlClone()
|
|
|
|
{
|
|
|
|
$headers = array('foo' => 'bar');
|
|
|
|
$bag1 = new ResponseHeaderBag($headers);
|
|
|
|
$bag2 = new ResponseHeaderBag($bag1->allPreserveCase());
|
|
|
|
$this->assertEquals($bag1->allPreserveCase(), $bag2->allPreserveCase());
|
|
|
|
}
|
|
|
|
|
2011-05-19 08:49:34 +01:00
|
|
|
public function testToStringIncludesCookieHeaders()
|
|
|
|
{
|
|
|
|
$bag = new ResponseHeaderBag(array());
|
|
|
|
$bag->setCookie(new Cookie('foo', 'bar'));
|
2011-05-19 16:38:16 +01:00
|
|
|
|
2016-11-19 14:10:17 +00:00
|
|
|
$this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
|
2011-05-19 16:38:16 +01:00
|
|
|
|
2011-05-19 08:49:34 +01:00
|
|
|
$bag->clearCookie('foo');
|
|
|
|
|
2016-11-19 14:10:17 +00:00
|
|
|
$this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001; path=/; httponly', $bag);
|
2011-05-19 08:49:34 +01:00
|
|
|
}
|
2011-07-11 21:54:08 +01:00
|
|
|
|
2014-06-19 19:53:04 +01:00
|
|
|
public function testClearCookieSecureNotHttpOnly()
|
|
|
|
{
|
|
|
|
$bag = new ResponseHeaderBag(array());
|
|
|
|
|
|
|
|
$bag->clearCookie('foo', '/', null, true, false);
|
|
|
|
|
2016-11-19 14:10:17 +00:00
|
|
|
$this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001; path=/; secure', $bag);
|
2014-06-19 19:53:04 +01:00
|
|
|
}
|
|
|
|
|
2012-09-01 12:18:47 +01:00
|
|
|
public function testReplace()
|
|
|
|
{
|
|
|
|
$bag = new ResponseHeaderBag(array());
|
2016-06-22 14:14:54 +01:00
|
|
|
$this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
|
2012-09-01 12:18:47 +01:00
|
|
|
$this->assertTrue($bag->hasCacheControlDirective('no-cache'));
|
|
|
|
|
|
|
|
$bag->replace(array('Cache-Control' => 'public'));
|
|
|
|
$this->assertEquals('public', $bag->get('Cache-Control'));
|
|
|
|
$this->assertTrue($bag->hasCacheControlDirective('public'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReplaceWithRemove()
|
|
|
|
{
|
|
|
|
$bag = new ResponseHeaderBag(array());
|
2016-06-22 14:14:54 +01:00
|
|
|
$this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
|
2012-09-01 12:18:47 +01:00
|
|
|
$this->assertTrue($bag->hasCacheControlDirective('no-cache'));
|
|
|
|
|
|
|
|
$bag->remove('Cache-Control');
|
|
|
|
$bag->replace(array());
|
2016-06-22 14:14:54 +01:00
|
|
|
$this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
|
2012-09-01 12:18:47 +01:00
|
|
|
$this->assertTrue($bag->hasCacheControlDirective('no-cache'));
|
|
|
|
}
|
|
|
|
|
2011-07-11 21:54:08 +01:00
|
|
|
public function testCookiesWithSameNames()
|
|
|
|
{
|
|
|
|
$bag = new ResponseHeaderBag();
|
|
|
|
$bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
|
|
|
|
$bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'foo.bar'));
|
|
|
|
$bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'bar.foo'));
|
|
|
|
$bag->setCookie(new Cookie('foo', 'bar'));
|
|
|
|
|
2012-01-18 13:42:47 +00:00
|
|
|
$this->assertCount(4, $bag->getCookies());
|
2016-11-19 14:10:17 +00:00
|
|
|
$this->assertEquals('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag->get('set-cookie'));
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'foo=bar; path=/path/foo; domain=foo.bar; httponly',
|
|
|
|
'foo=bar; path=/path/bar; domain=foo.bar; httponly',
|
|
|
|
'foo=bar; path=/path/bar; domain=bar.foo; httponly',
|
|
|
|
'foo=bar; path=/; httponly',
|
|
|
|
), $bag->get('set-cookie', null, false));
|
|
|
|
|
|
|
|
$this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag);
|
|
|
|
$this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly', $bag);
|
|
|
|
$this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly', $bag);
|
|
|
|
$this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
|
2011-07-11 21:54:08 +01:00
|
|
|
|
|
|
|
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
|
2016-11-19 14:10:17 +00:00
|
|
|
|
2017-12-12 11:55:28 +00:00
|
|
|
$this->assertArrayHasKey('foo', $cookies['foo.bar']['/path/foo']);
|
|
|
|
$this->assertArrayHasKey('foo', $cookies['foo.bar']['/path/bar']);
|
|
|
|
$this->assertArrayHasKey('foo', $cookies['bar.foo']['/path/bar']);
|
|
|
|
$this->assertArrayHasKey('foo', $cookies['']['/']);
|
2011-07-11 21:54:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveCookie()
|
|
|
|
{
|
|
|
|
$bag = new ResponseHeaderBag();
|
2016-11-19 14:10:17 +00:00
|
|
|
$this->assertFalse($bag->has('set-cookie'));
|
|
|
|
|
2011-07-11 21:54:08 +01:00
|
|
|
$bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
|
|
|
|
$bag->setCookie(new Cookie('bar', 'foo', 0, '/path/bar', 'foo.bar'));
|
2016-11-19 14:10:17 +00:00
|
|
|
$this->assertTrue($bag->has('set-cookie'));
|
2011-07-11 21:54:08 +01:00
|
|
|
|
|
|
|
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
|
2017-12-11 21:55:31 +00:00
|
|
|
$this->assertArrayHasKey('/path/foo', $cookies['foo.bar']);
|
2011-07-11 21:54:08 +01:00
|
|
|
|
|
|
|
$bag->removeCookie('foo', '/path/foo', 'foo.bar');
|
2016-11-19 14:10:17 +00:00
|
|
|
$this->assertTrue($bag->has('set-cookie'));
|
2011-07-11 21:54:08 +01:00
|
|
|
|
|
|
|
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
|
2017-12-11 21:55:31 +00:00
|
|
|
$this->assertArrayNotHasKey('/path/foo', $cookies['foo.bar']);
|
2011-07-11 21:54:08 +01:00
|
|
|
|
|
|
|
$bag->removeCookie('bar', '/path/bar', 'foo.bar');
|
2016-11-19 14:10:17 +00:00
|
|
|
$this->assertFalse($bag->has('set-cookie'));
|
2011-07-11 21:54:08 +01:00
|
|
|
|
|
|
|
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
|
2017-12-11 21:55:31 +00:00
|
|
|
$this->assertArrayNotHasKey('foo.bar', $cookies);
|
2011-07-11 21:54:08 +01:00
|
|
|
}
|
2011-09-02 14:54:04 +01:00
|
|
|
|
2012-09-01 12:18:47 +01:00
|
|
|
public function testRemoveCookieWithNullRemove()
|
|
|
|
{
|
|
|
|
$bag = new ResponseHeaderBag();
|
|
|
|
$bag->setCookie(new Cookie('foo', 'bar', 0));
|
|
|
|
$bag->setCookie(new Cookie('bar', 'foo', 0));
|
|
|
|
|
|
|
|
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
|
2017-12-11 21:55:31 +00:00
|
|
|
$this->assertArrayHasKey('/', $cookies['']);
|
2012-09-01 12:18:47 +01:00
|
|
|
|
|
|
|
$bag->removeCookie('foo', null);
|
|
|
|
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
|
2017-12-11 21:55:31 +00:00
|
|
|
$this->assertArrayNotHasKey('foo', $cookies['']['/']);
|
2012-09-01 12:18:47 +01:00
|
|
|
|
|
|
|
$bag->removeCookie('bar', null);
|
|
|
|
$cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
|
|
|
|
$this->assertFalse(isset($cookies['']['/']['bar']));
|
|
|
|
}
|
|
|
|
|
2016-11-19 14:10:17 +00:00
|
|
|
public function testSetCookieHeader()
|
|
|
|
{
|
|
|
|
$bag = new ResponseHeaderBag();
|
|
|
|
$bag->set('set-cookie', 'foo=bar');
|
2017-07-06 09:48:01 +01:00
|
|
|
$this->assertEquals(array(new Cookie('foo', 'bar', 0, '/', null, false, false, true)), $bag->getCookies());
|
2016-11-19 14:10:17 +00:00
|
|
|
|
|
|
|
$bag->set('set-cookie', 'foo2=bar2', false);
|
|
|
|
$this->assertEquals(array(
|
2017-07-06 09:48:01 +01:00
|
|
|
new Cookie('foo', 'bar', 0, '/', null, false, false, true),
|
|
|
|
new Cookie('foo2', 'bar2', 0, '/', null, false, false, true),
|
2016-11-19 14:10:17 +00:00
|
|
|
), $bag->getCookies());
|
|
|
|
|
|
|
|
$bag->remove('set-cookie');
|
|
|
|
$this->assertEquals(array(), $bag->getCookies());
|
|
|
|
}
|
|
|
|
|
2012-09-01 12:18:47 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testGetCookiesWithInvalidArgument()
|
|
|
|
{
|
|
|
|
$bag = new ResponseHeaderBag();
|
|
|
|
|
2016-11-19 14:10:17 +00:00
|
|
|
$bag->getCookies('invalid_argument');
|
2012-09-01 12:18:47 +01:00
|
|
|
}
|
|
|
|
|
2011-09-08 10:20:37 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testMakeDispositionInvalidDisposition()
|
|
|
|
{
|
|
|
|
$headers = new ResponseHeaderBag();
|
|
|
|
|
|
|
|
$headers->makeDisposition('invalid', 'foo.html');
|
|
|
|
}
|
|
|
|
|
2011-09-02 14:54:04 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideMakeDisposition
|
|
|
|
*/
|
|
|
|
public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
|
|
|
|
{
|
|
|
|
$headers = new ResponseHeaderBag();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
|
|
|
|
}
|
|
|
|
|
2013-02-26 08:53:49 +00:00
|
|
|
public function testToStringDoesntMessUpHeaders()
|
|
|
|
{
|
|
|
|
$headers = new ResponseHeaderBag();
|
|
|
|
|
|
|
|
$headers->set('Location', 'http://www.symfony.com');
|
|
|
|
$headers->set('Content-type', 'text/html');
|
|
|
|
|
|
|
|
(string) $headers;
|
|
|
|
|
|
|
|
$allHeaders = $headers->allPreserveCase();
|
|
|
|
$this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
|
|
|
|
$this->assertEquals(array('text/html'), $allHeaders['Content-type']);
|
|
|
|
}
|
|
|
|
|
2011-09-02 14:54:04 +01:00
|
|
|
public function provideMakeDisposition()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('attachment', 'foo.html', 'foo.html', 'attachment; filename="foo.html"'),
|
|
|
|
array('attachment', 'foo.html', '', 'attachment; filename="foo.html"'),
|
|
|
|
array('attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'),
|
|
|
|
array('attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'),
|
|
|
|
array('attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'),
|
|
|
|
array('attachment', 'föö.html', 'foo.html', 'attachment; filename="foo.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideMakeDispositionFail
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testMakeDispositionFail($disposition, $filename)
|
|
|
|
{
|
|
|
|
$headers = new ResponseHeaderBag();
|
|
|
|
|
|
|
|
$headers->makeDisposition($disposition, $filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideMakeDispositionFail()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('attachment', 'foo%20bar.html'),
|
|
|
|
array('attachment', 'foo/bar.html'),
|
|
|
|
array('attachment', '/foo.html'),
|
|
|
|
array('attachment', 'foo\bar.html'),
|
|
|
|
array('attachment', '\foo.html'),
|
|
|
|
array('attachment', 'föö.html'),
|
|
|
|
);
|
|
|
|
}
|
2016-11-19 14:10:17 +00:00
|
|
|
|
2017-03-23 09:24:24 +00:00
|
|
|
public function testDateHeaderAddedOnCreation()
|
|
|
|
{
|
|
|
|
$now = time();
|
|
|
|
|
|
|
|
$bag = new ResponseHeaderBag();
|
|
|
|
$this->assertTrue($bag->has('Date'));
|
|
|
|
|
|
|
|
$this->assertEquals($now, $bag->getDate('Date')->getTimestamp());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDateHeaderCanBeSetOnCreation()
|
|
|
|
{
|
|
|
|
$someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
|
|
|
|
$bag = new ResponseHeaderBag(array('Date' => $someDate));
|
|
|
|
|
|
|
|
$this->assertEquals($someDate, $bag->get('Date'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDateHeaderWillBeRecreatedWhenRemoved()
|
|
|
|
{
|
|
|
|
$someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
|
|
|
|
$bag = new ResponseHeaderBag(array('Date' => $someDate));
|
|
|
|
$bag->remove('Date');
|
|
|
|
|
|
|
|
// a (new) Date header is still present
|
|
|
|
$this->assertTrue($bag->has('Date'));
|
|
|
|
$this->assertNotEquals($someDate, $bag->get('Date'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDateHeaderWillBeRecreatedWhenHeadersAreReplaced()
|
|
|
|
{
|
|
|
|
$bag = new ResponseHeaderBag();
|
|
|
|
$bag->replace(array());
|
|
|
|
|
|
|
|
$this->assertTrue($bag->has('Date'));
|
|
|
|
}
|
|
|
|
|
2016-12-17 14:58:06 +00:00
|
|
|
private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
|
2016-11-19 14:10:17 +00:00
|
|
|
{
|
|
|
|
$this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
|
|
|
|
}
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
}
|