Skip to content

Commit 089da40

Browse files
danrotnicolas-grekas
authored andcommitted
Return null as Expire header if it was set to null
1 parent b3d57a1 commit 089da40

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

HeaderBag.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,15 @@ public function get($key, $default = null, $first = true)
121121
}
122122

123123
if ($first) {
124-
return \count($headers[$key]) ? (string) $headers[$key][0] : $default;
124+
if (!$headers[$key]) {
125+
return $default;
126+
}
127+
128+
if (null === $headers[$key][0]) {
129+
return null;
130+
}
131+
132+
return (string) $headers[$key][0];
125133
}
126134

127135
return $headers[$key];

Tests/HeaderBagTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public function testGetDate()
4848
$this->assertInstanceOf('DateTime', $headerDate);
4949
}
5050

51+
public function testGetDateNull()
52+
{
53+
$bag = new HeaderBag(['foo' => null]);
54+
$headerDate = $bag->getDate('foo');
55+
$this->assertNull($headerDate);
56+
}
57+
5158
public function testGetDateException()
5259
{
5360
$this->expectException('RuntimeException');
@@ -96,6 +103,9 @@ public function testGet()
96103
$bag->set('foo', 'bor', false);
97104
$this->assertEquals('bar', $bag->get('foo'), '->get return first value');
98105
$this->assertEquals(['bar', 'bor'], $bag->get('foo', 'nope', false), '->get return all values as array');
106+
107+
$bag->set('baz', null);
108+
$this->assertNull($bag->get('baz', 'nope'), '->get return null although different default value is given');
99109
}
100110

101111
public function testSetAssociativeArray()

Tests/ResponseTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ public function testExpire()
369369
$this->assertNull($response->headers->get('Expires'), '->expire() removes the Expires header when the response is fresh');
370370
}
371371

372+
public function testNullExpireHeader()
373+
{
374+
$response = new Response(null, 200, ['Expires' => null]);
375+
$this->assertNull($response->getExpires());
376+
}
377+
372378
public function testGetTtl()
373379
{
374380
$response = new Response();

0 commit comments

Comments
 (0)