bug #33353 Return null as Expire header if it was set to null (danrot)

This PR was squashed before being merged into the 3.4 branch (closes #33353).

Discussion
----------

Return null as Expire header if it was set to null

| Q             | A
| ------------- | ---
| Branch?       | 3.4 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets |    <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

This PR fixes a regression introduces in #33332. If you set the `Expires` header to null when creating a `Response`, the `getExpires` function returned a date instead of null.

```php
$response = new Response(null, 200, ['Expires' => null]);
$response->getExpires(); // Returns a date currently, but should return null
```

See also [the comment](https://github.com/symfony/symfony/pull/33332#discussion_r317934607) in the PR introducing this regression.

Commits
-------

5e3c7ea452 Return null as Expire header if it was set to null
This commit is contained in:
Nicolas Grekas 2019-08-29 18:18:10 +02:00
commit bff95742eb
3 changed files with 25 additions and 1 deletions

View File

@ -121,7 +121,15 @@ class HeaderBag implements \IteratorAggregate, \Countable
}
if ($first) {
return \count($headers[$key]) ? (string) $headers[$key][0] : $default;
if (!$headers[$key]) {
return $default;
}
if (null === $headers[$key][0]) {
return null;
}
return (string) $headers[$key][0];
}
return $headers[$key];

View File

@ -48,6 +48,13 @@ class HeaderBagTest extends TestCase
$this->assertInstanceOf('DateTime', $headerDate);
}
public function testGetDateNull()
{
$bag = new HeaderBag(['foo' => null]);
$headerDate = $bag->getDate('foo');
$this->assertNull($headerDate);
}
public function testGetDateException()
{
$this->expectException('RuntimeException');
@ -96,6 +103,9 @@ class HeaderBagTest extends TestCase
$bag->set('foo', 'bor', false);
$this->assertEquals('bar', $bag->get('foo'), '->get return first value');
$this->assertEquals(['bar', 'bor'], $bag->get('foo', 'nope', false), '->get return all values as array');
$bag->set('baz', null);
$this->assertNull($bag->get('baz', 'nope'), '->get return null although different default value is given');
}
public function testSetAssociativeArray()

View File

@ -369,6 +369,12 @@ class ResponseTest extends ResponseTestCase
$this->assertNull($response->headers->get('Expires'), '->expire() removes the Expires header when the response is fresh');
}
public function testNullExpireHeader()
{
$response = new Response(null, 200, ['Expires' => null]);
$this->assertNull($response->getExpires());
}
public function testGetTtl()
{
$response = new Response();