Skip to content

Commit 3a7fa7e

Browse files
mpdudefabpot
authored andcommitted
Set Date header in Response constructor already
1 parent 6b4cfd6 commit 3a7fa7e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Symfony/Component/HttpFoundation/Response.php

+11
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ public function __construct($content = '', $status = 200, $headers = array())
201201
$this->setContent($content);
202202
$this->setStatusCode($status);
203203
$this->setProtocolVersion('1.0');
204+
205+
/* RFC2616 - 14.18 says all Responses need to have a Date */
206+
if (!$this->headers->has('Date')) {
207+
$this->setDate(new \DateTime(null, new \DateTimeZone('UTC')));
208+
}
204209
}
205210

206211
/**
@@ -329,6 +334,7 @@ public function sendHeaders()
329334
return $this;
330335
}
331336

337+
/* RFC2616 - 14.18 says all Responses need to have a Date */
332338
if (!$this->headers->has('Date')) {
333339
$this->setDate(\DateTime::createFromFormat('U', time()));
334340
}
@@ -612,6 +618,11 @@ public function mustRevalidate()
612618
*/
613619
public function getDate()
614620
{
621+
/*
622+
RFC2616 - 14.18 says all Responses need to have a Date.
623+
Make sure we provide one even if it the header
624+
has been removed in the meantime.
625+
*/
615626
if (!$this->headers->has('Date')) {
616627
$this->setDate(\DateTime::createFromFormat('U', time()));
617628
}

src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,10 @@ public function testGetDate()
276276
$this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the date when the header has been modified');
277277

278278
$response = new Response('', 200);
279+
$now = $this->createDateTimeNow();
279280
$response->headers->remove('Date');
280-
$this->assertInstanceOf('\DateTime', $response->getDate());
281+
$date = $response->getDate();
282+
$this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the current Date when the header has previously been removed');
281283
}
282284

283285
public function testGetMaxAge()

0 commit comments

Comments
 (0)