Skip to content

Commit

Permalink
Response::isNotModified returns true when If-Modified-Since is later …
Browse files Browse the repository at this point in the history
…than Last-Modified
  • Loading branch information
skolodyazhnyy authored and fabpot committed Sep 23, 2014
1 parent e47e4fa commit 42ec76e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -1033,12 +1033,16 @@ public function isNotModified(Request $request)
return false;
}

$lastModified = $request->headers->get('If-Modified-Since');
$notModified = false;
$notModified = false;
$lastModified = $this->headers->get('Last-Modified');
$modifiedSince = $request->headers->get('If-Modified-Since');

if ($etags = $request->getEtags()) {
$notModified = (in_array($this->getEtag(), $etags) || in_array('*', $etags)) && (!$lastModified || $this->headers->get('Last-Modified') == $lastModified);
} elseif ($lastModified) {
$notModified = $lastModified == $this->headers->get('Last-Modified');
$notModified = in_array($this->getEtag(), $etags) || in_array('*', $etags);
}

if ($modifiedSince && $lastModified) {
$notModified = strtotime($modifiedSince) >= strtotime($lastModified) && (!$etags || $notModified);
}

if ($notModified) {
Expand Down
96 changes: 96 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Expand Up @@ -118,6 +118,102 @@ public function testIsNotModified()
$this->assertFalse($modified);
}

public function testIsNotModifiedNotSafe()
{
$request = Request::create('/homepage', 'POST');

$response = new Response();
$this->assertFalse($response->isNotModified($request));
}

public function testIsNotModifiedLastModified()
{
$before = 'Sun, 25 Aug 2013 18:32:31 GMT';
$modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
$after = 'Sun, 25 Aug 2013 19:33:31 GMT';

$request = new Request();
$request->headers->set('If-Modified-Since', $modified);

$response = new Response();

$response->headers->set('Last-Modified', $modified);
$this->assertTrue($response->isNotModified($request));

$response->headers->set('Last-Modified', $before);
$this->assertTrue($response->isNotModified($request));

$response->headers->set('Last-Modified', $after);
$this->assertFalse($response->isNotModified($request));

$response->headers->set('Last-Modified', '');
$this->assertFalse($response->isNotModified($request));
}

public function testIsNotModifiedEtag()
{
$etagOne = 'randomly_generated_etag';
$etagTwo = 'randomly_generated_etag_2';

$request = new Request();
$request->headers->set('if_none_match', sprintf('%s, %s, %s', $etagOne, $etagTwo, 'etagThree'));

$response = new Response();

$response->headers->set('ETag', $etagOne);
$this->assertTrue($response->isNotModified($request));

$response->headers->set('ETag', $etagTwo);
$this->assertTrue($response->isNotModified($request));

$response->headers->set('ETag', '');
$this->assertFalse($response->isNotModified($request));
}

public function testIsNotModifiedLastModifiedAndEtag()
{
$before = 'Sun, 25 Aug 2013 18:32:31 GMT';
$modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
$after = 'Sun, 25 Aug 2013 19:33:31 GMT';
$etag = 'randomly_generated_etag';

$request = new Request();
$request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
$request->headers->set('If-Modified-Since', $modified);

$response = new Response();

$response->headers->set('ETag', $etag);
$response->headers->set('Last-Modified', $after);
$this->assertFalse($response->isNotModified($request));

$response->headers->set('ETag', 'non-existent-etag');
$response->headers->set('Last-Modified', $before);
$this->assertFalse($response->isNotModified($request));

$response->headers->set('ETag', $etag);
$response->headers->set('Last-Modified', $modified);
$this->assertTrue($response->isNotModified($request));
}

public function testIsNotModifiedIfModifiedSinceAndEtagWithoutLastModified()
{
$modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
$etag = 'randomly_generated_etag';

$request = new Request();
$request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
$request->headers->set('If-Modified-Since', $modified);

$response = new Response();

$response->headers->set('ETag', $etag);
$this->assertTrue($response->isNotModified($request));

$response->headers->set('ETag', 'non-existent-etag');
$this->assertFalse($response->isNotModified($request));
}

public function testIsValidateable()
{
$response = new Response('', 200, array('Last-Modified' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
Expand Down

0 comments on commit 42ec76e

Please sign in to comment.