This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/HttpFoundation/HeaderBagTest.php
Fabien Potencier b6923dd7b9 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-18 17:05:05 +01:00

131 lines
5.3 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\HeaderBag;
class HeaderBagTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Symfony\Component\HttpFoundation\HeaderBag::__construct
*/
public function testConstructor()
{
$bag = new HeaderBag(array('foo' => 'bar'));
$this->assertTrue($bag->has('foo'));
}
/**
* @covers Symfony\Component\HttpFoundation\HeaderBag::all
*/
public function testAll()
{
$bag = new HeaderBag(array('foo' => 'bar'));
$this->assertEquals(array('foo' => array('bar')), $bag->all(), '->all() gets all the input');
$bag = new HeaderBag(array('FOO' => 'BAR'));
$this->assertEquals(array('foo' => array('BAR')), $bag->all(), '->all() gets all the input key are lower case');
}
/**
* @covers Symfony\Component\HttpFoundation\HeaderBag::replace
*/
public function testReplace()
{
$bag = new HeaderBag(array('foo' => 'bar'));
$bag->replace(array('NOPE' => 'BAR'));
$this->assertEquals(array('nope' => array('BAR')), $bag->all(), '->replace() replaces the input with the argument');
$this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
}
/**
* @covers Symfony\Component\HttpFoundation\HeaderBag::get
*/
public function testGet()
{
$bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
$this->assertEquals( 'bar', $bag->get('foo'), '->get return current value');
$this->assertEquals( 'bar', $bag->get('FoO'), '->get key in case insensitive');
$this->assertEquals( array('bar'), $bag->get('foo', 'nope', false), '->get return the value as array');
// defaults
$this->assertNull($bag->get('none'), '->get unknown values returns null');
$this->assertEquals( 'default', $bag->get('none', 'default'), '->get unknown values returns default');
$this->assertEquals( array('default'), $bag->get('none', 'default', false), '->get unknown values returns default as array');
$bag->set('foo', 'bor', false);
$this->assertEquals( 'bar', $bag->get('foo'), '->get return first value');
$this->assertEquals( array('bar', 'bor'), $bag->get('foo', 'nope', false), '->get return all values as array');
}
/**
* @covers Symfony\Component\HttpFoundation\HeaderBag::contains
*/
public function testContains()
{
$bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
$this->assertTrue( $bag->contains('foo', 'bar'), '->contains first value');
$this->assertTrue( $bag->contains('fuzz', 'bizz'), '->contains second value');
$this->assertFalse( $bag->contains('nope', 'nope'), '->contains unknown value');
$this->assertFalse( $bag->contains('foo', 'nope'), '->contains unknown value');
// Multiple values
$bag->set('foo', 'bor', false);
$this->assertTrue( $bag->contains('foo', 'bar'), '->contains first value');
$this->assertTrue( $bag->contains('foo', 'bor'), '->contains second value');
$this->assertFalse( $bag->contains('foo', 'nope'), '->contains unknown value');
}
public function testCacheControlDirectiveAccessors()
{
$bag = new HeaderBag();
$bag->addCacheControlDirective('public');
$this->assertTrue($bag->hasCacheControlDirective('public'));
$this->assertEquals(true, $bag->getCacheControlDirective('public'));
$this->assertEquals('public', $bag->get('cache-control'));
$bag->addCacheControlDirective('max-age', 10);
$this->assertTrue($bag->hasCacheControlDirective('max-age'));
$this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
$this->assertEquals('max-age=10, public', $bag->get('cache-control'));
$bag->removeCacheControlDirective('max-age');
$this->assertFalse($bag->hasCacheControlDirective('max-age'));
}
public function testCacheControlDirectiveParsing()
{
$bag = new HeaderBag(array('cache-control' => 'public, max-age=10'));
$this->assertTrue($bag->hasCacheControlDirective('public'));
$this->assertEquals(true, $bag->getCacheControlDirective('public'));
$this->assertTrue($bag->hasCacheControlDirective('max-age'));
$this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
$bag->addCacheControlDirective('s-maxage', 100);
$this->assertEquals('max-age=10, public, s-maxage=100', $bag->get('cache-control'));
}
public function testCacheControlDirectiveOverrideWithReplace()
{
$bag = new HeaderBag(array('cache-control' => 'private, max-age=100'));
$bag->replace(array('cache-control' => 'public, max-age=10'));
$this->assertTrue($bag->hasCacheControlDirective('public'));
$this->assertEquals(true, $bag->getCacheControlDirective('public'));
$this->assertTrue($bag->hasCacheControlDirective('max-age'));
$this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
}
}