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

[5.6] Add a HTTP cache middleware #22389

Merged
merged 3 commits into from
Dec 13, 2017
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
47 changes: 47 additions & 0 deletions src/Illuminate/Http/Middleware/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Illuminate\Http\Middleware;

use Closure;

class Cache
{
/**
* Add cache related HTTP headers.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|array $options
* @return \Symfony\Component\HttpFoundation\Response
* @throws \InvalidArgumentException
*/
public function handle($request, Closure $next, $options = [])
{
/**
* @var $response \Symfony\Component\HttpFoundation\Response
*/
$response = $next($request);
if (! $request->isMethodCacheable() || ! $response->getContent()) {
return $response;
}

if (\is_string($options)) {
$parsedOptions = [];
foreach (explode(';', $options) as $opt) {
$data = explode('=', $opt, 2);
$parsedOptions[$data[0]] = $data[1] ?? true;
}

$options = $parsedOptions;
}

if (isset($options['etag']) && true === $options['etag']) {
$options['etag'] = md5($response->getContent());
}

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

return $response;
}
}
85 changes: 85 additions & 0 deletions tests/Http/Middleware/CacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Illuminate\Tests\Http\Middleware;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use PHPUnit\Framework\TestCase;
use Illuminate\Http\Middleware\Cache;

class CacheTest extends TestCase
{
public function testDoNotSetHeaderWhenMethodNotCacheable()
{
$request = new Request();
$request->setMethod('PUT');

$response = (new Cache())->handle($request, function () {
return new Response('Hello Laravel');
}, 'max_age=120;s_maxage=60');

$this->assertNull($response->getMaxAge());
}

public function testDoNotSetHeaderWhenNoContent()
{
$response = (new Cache())->handle(new Request(), function () {
return new Response();
}, 'max_age=120;s_maxage=60');

$this->assertNull($response->getMaxAge());
$this->assertNull($response->getEtag());
}

public function testAddHeaders()
{
$response = (new Cache())->handle(new Request(), function () {
return new Response('some content');
}, 'max_age=100;s_maxage=200;etag=ABC');

$this->assertSame('"ABC"', $response->getEtag());
$this->assertSame('max-age=100, public, s-maxage=200', $response->headers->get('Cache-Control'));
}

public function testAddHeadersUsingArray()
{
$response = (new Cache())->handle(new Request(), function () {
return new Response('some content');
}, ['max_age' => 100, 's_maxage' => 200, 'etag' => 'ABC']);

$this->assertSame('"ABC"', $response->getEtag());
$this->assertSame('max-age=100, public, s-maxage=200', $response->headers->get('Cache-Control'));
}

public function testGenerateEtag()
{
$response = (new Cache())->handle(new Request(), function () {
return new Response('some content');
}, 'etag;max_age=100;s_maxage=200');

$this->assertSame('"9893532233caff98cd083a116b013c0b"', $response->getEtag());
$this->assertSame('max-age=100, public, s-maxage=200', $response->headers->get('Cache-Control'));
}

public function testIsNotModified()
{
$request = new Request();
$request->headers->set('If-None-Match', '"9893532233caff98cd083a116b013c0b"');

$response = (new Cache())->handle($request, function () {
return new Response('some content');
}, 'etag;max_age=100;s_maxage=200');

$this->assertSame(304, $response->getStatusCode());
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidOption()
{
(new Cache())->handle(new Request(), function () {
return new Response('some content');
}, 'invalid');
}
}