Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[6.x] Fix last_modified option in SetCacheHeader (#30335)
* fix last_modified option in SetCacheHeader

* styling issues

* last styling issues

* style
  • Loading branch information
aponscat authored and taylorotwell committed Oct 21, 2019
1 parent 7f9ec5c commit 737427f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Illuminate/Http/Middleware/SetCacheHeaders.php
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
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());
}
}

0 comments on commit 737427f

Please sign in to comment.