Set Date header in Response constructor already

This commit is contained in:
Matthias Pigulla 2017-03-17 12:57:37 +01:00 committed by Fabien Potencier
parent 6b4cfd6a25
commit 3a7fa7ede2
2 changed files with 14 additions and 1 deletions

View File

@ -201,6 +201,11 @@ class Response
$this->setContent($content);
$this->setStatusCode($status);
$this->setProtocolVersion('1.0');
/* RFC2616 - 14.18 says all Responses need to have a Date */
if (!$this->headers->has('Date')) {
$this->setDate(new \DateTime(null, new \DateTimeZone('UTC')));
}
}
/**
@ -329,6 +334,7 @@ class Response
return $this;
}
/* RFC2616 - 14.18 says all Responses need to have a Date */
if (!$this->headers->has('Date')) {
$this->setDate(\DateTime::createFromFormat('U', time()));
}
@ -612,6 +618,11 @@ class Response
*/
public function getDate()
{
/*
RFC2616 - 14.18 says all Responses need to have a Date.
Make sure we provide one even if it the header
has been removed in the meantime.
*/
if (!$this->headers->has('Date')) {
$this->setDate(\DateTime::createFromFormat('U', time()));
}

View File

@ -276,8 +276,10 @@ class ResponseTest extends ResponseTestCase
$this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the date when the header has been modified');
$response = new Response('', 200);
$now = $this->createDateTimeNow();
$response->headers->remove('Date');
$this->assertInstanceOf('\DateTime', $response->getDate());
$date = $response->getDate();
$this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the current Date when the header has previously been removed');
}
public function testGetMaxAge()