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

[6.x] Fix last_modified option in SetCacheHeader #30335

Merged
merged 4 commits into from
Oct 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Illuminate/Http/Middleware/SetCacheHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Http\Middleware;

use Closure;
use Illuminate\Support\Carbon;

class SetCacheHeaders
{
Expand Down Expand Up @@ -32,6 +33,14 @@ public function handle($request, Closure $next, $options = [])
$options['etag'] = md5($response->getContent());
}

if (isset($options['last_modified'])) {
if (is_numeric($options['last_modified'])) {
$options['last_modified'] = Carbon::createFromTimestamp($options['last_modified']);
} else {
$options['last_modified'] = Carbon::parse($options['last_modified']);
}
}

$response->setCache($options);
$response->isNotModified($request);

Expand Down
22 changes: 22 additions & 0 deletions tests/Http/Middleware/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Middleware\SetCacheHeaders as Cache;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Carbon;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -82,4 +83,25 @@ public function testInvalidOption()
return new Response('some content');
}, 'invalid');
}

public function testLastModifiedUnixTime()
{
$time = time();

$response = (new Cache)->handle(new Request, function () {
return new Response('some content');
}, "last_modified=$time");

$this->assertSame($time, $response->getLastModified()->getTimestamp());
}

public function testLastModifiedStringDate()
{
$birthdate = '1973-04-09 10:10:10';
$response = (new Cache)->handle(new Request, function () {
return new Response('some content');
}, "last_modified=$birthdate");

$this->assertSame(Carbon::parse($birthdate)->timestamp, $response->getLastModified()->getTimestamp());
}
}