ResponseHeaderBagTest.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  12. use Symfony\Component\HttpFoundation\Cookie;
  13. class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testCacheControlHeader()
  16. {
  17. $bag = new ResponseHeaderBag(array());
  18. $this->assertEquals('no-cache', $bag->get('Cache-Control'));
  19. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  20. $bag = new ResponseHeaderBag(array('Cache-Control' => 'public'));
  21. $this->assertEquals('public', $bag->get('Cache-Control'));
  22. $this->assertTrue($bag->hasCacheControlDirective('public'));
  23. $bag = new ResponseHeaderBag(array('ETag' => 'abcde'));
  24. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  25. $this->assertTrue($bag->hasCacheControlDirective('private'));
  26. $this->assertTrue($bag->hasCacheControlDirective('must-revalidate'));
  27. $this->assertFalse($bag->hasCacheControlDirective('max-age'));
  28. $bag = new ResponseHeaderBag(array('Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT'));
  29. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  30. $bag = new ResponseHeaderBag(array(
  31. 'Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT',
  32. 'Cache-Control' => 'max-age=3600'
  33. ));
  34. $this->assertEquals('max-age=3600, private', $bag->get('Cache-Control'));
  35. $bag = new ResponseHeaderBag(array('Last-Modified' => 'abcde'));
  36. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  37. $bag = new ResponseHeaderBag(array('Etag' => 'abcde', 'Last-Modified' => 'abcde'));
  38. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  39. $bag = new ResponseHeaderBag(array('cache-control' => 'max-age=100'));
  40. $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
  41. $bag = new ResponseHeaderBag(array('cache-control' => 's-maxage=100'));
  42. $this->assertEquals('s-maxage=100', $bag->get('Cache-Control'));
  43. $bag = new ResponseHeaderBag(array('cache-control' => 'private, max-age=100'));
  44. $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
  45. $bag = new ResponseHeaderBag(array('cache-control' => 'public, max-age=100'));
  46. $this->assertEquals('max-age=100, public', $bag->get('Cache-Control'));
  47. $bag = new ResponseHeaderBag();
  48. $bag->set('Last-Modified', 'abcde');
  49. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  50. }
  51. public function testToStringIncludesCookieHeaders()
  52. {
  53. $bag = new ResponseHeaderBag(array());
  54. $bag->setCookie(new Cookie('foo', 'bar'));
  55. $this->assertContains("Set-Cookie: foo=bar; httponly", explode("\r\n", $bag->__toString()));
  56. $bag->clearCookie('foo');
  57. $this->assertContains("Set-Cookie: foo=deleted; expires=".gmdate("D, d-M-Y H:i:s T", time() - 31536001)."; httponly", explode("\r\n", $bag->__toString()));
  58. }
  59. public function testCookiesWithSameNames()
  60. {
  61. $bag = new ResponseHeaderBag();
  62. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  63. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'foo.bar'));
  64. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'bar.foo'));
  65. $bag->setCookie(new Cookie('foo', 'bar'));
  66. $this->assertEquals(4, count($bag->getCookies()));
  67. $headers = explode("\r\n", $bag->__toString());
  68. $this->assertContains("Set-Cookie: foo=bar; path=/path/foo; domain=foo.bar; httponly", $headers);
  69. $this->assertContains("Set-Cookie: foo=bar; path=/path/foo; domain=foo.bar; httponly", $headers);
  70. $this->assertContains("Set-Cookie: foo=bar; path=/path/bar; domain=bar.foo; httponly", $headers);
  71. $this->assertContains("Set-Cookie: foo=bar; httponly", $headers);
  72. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  73. $this->assertTrue(isset($cookies['foo.bar']['/path/foo']['foo']));
  74. $this->assertTrue(isset($cookies['foo.bar']['/path/bar']['foo']));
  75. $this->assertTrue(isset($cookies['bar.foo']['/path/bar']['foo']));
  76. $this->assertTrue(isset($cookies['']['/']['foo']));
  77. }
  78. public function testRemoveCookie()
  79. {
  80. $bag = new ResponseHeaderBag();
  81. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  82. $bag->setCookie(new Cookie('bar', 'foo', 0, '/path/bar', 'foo.bar'));
  83. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  84. $this->assertTrue(isset($cookies['foo.bar']['/path/foo']));
  85. $bag->removeCookie('foo', '/path/foo', 'foo.bar');
  86. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  87. $this->assertFalse(isset($cookies['foo.bar']['/path/foo']));
  88. $bag->removeCookie('bar', '/path/bar', 'foo.bar');
  89. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  90. $this->assertFalse(isset($cookies['foo.bar']));
  91. }
  92. }