Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return null as Expire header if it was set to null #33353

Merged
merged 1 commit into from Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Symfony/Component/HttpFoundation/HeaderBag.php
Expand Up @@ -121,7 +121,15 @@ public function get($key, $default = null, $first = true)
}

if ($first) {
return \count($headers[$key]) ? (string) $headers[$key][0] : $default;
if (!$headers[$key]) {
xabbuh marked this conversation as resolved.
Show resolved Hide resolved
return $default;
}

if (null === $headers[$key][0]) {
return null;
}

return (string) $headers[$key][0];
}

return $headers[$key];
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php
Expand Up @@ -48,6 +48,13 @@ public function testGetDate()
$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');
Expand Down Expand Up @@ -96,6 +103,9 @@ public function testGet()
$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()
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Expand Up @@ -369,6 +369,12 @@ public function testExpire()
$this->assertNull($response->headers->get('Expires'), '->expire() removes the Expires header when the response is fresh');
}

public function testNullExpireHeader()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think, you'd better place this test into testExpire() method (see line 339 in this file) and use more consistent format, like this:

$response = new Response();
$response->headers->set('Expires', null);
$response->expire();
$this->assertNull($response->headers->get('Expires'), 'Place description of expected behavior here')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it, but this bug is not about calling expire on the response, actually this call is not necessary at all. That's why I've decided against that, in order to keep the test minimal.

{
$response = new Response(null, 200, ['Expires' => null]);
$this->assertNull($response->getExpires());
}

public function testGetTtl()
{
$response = new Response();
Expand Down