Skip to content

Commit

Permalink
Merge pull request #6 from designermonkey/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
akrabat committed Jun 8, 2015
2 parents 0aeae78 + f33e7ec commit 215f7ed
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
12 changes: 9 additions & 3 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,27 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
}

// Last-Modified header and conditional GET check
$lastModified = $response->getHeader('Last-Modified');
$lastModified = $response->getHeaderLine('Last-Modified');

if ($lastModified) {
if (!is_integer($lastModified)) {
$lastModified = strtotime($lastModified);
}
$ifModifiedSince = $request->getHeader('If-Modified-Since');

$ifModifiedSince = $request->getHeaderLine('If-Modified-Since');

if ($ifModifiedSince && $lastModified <= strtotime($ifModifiedSince)) {
return $response->withStatus(304);
}
}

// ETag header and conditional GET check
$etag = $response->getHeader('ETag');
$etag = reset($etag);

if ($etag) {
$ifNoneMatch = $request->getHeader('If-None-Match');
$ifNoneMatch = $request->getHeaderLine('If-None-Match');

if ($ifNoneMatch) {
$etagList = preg_split('@\s*,\s*@', $ifNoneMatch);
if (in_array($etag, $etagList) || in_array('*', $etagList)) {
Expand Down
13 changes: 9 additions & 4 deletions src/CacheProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Slim\HttpCache;

use InvalidArgumentException;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -25,11 +26,12 @@ public function register(Container $container)
* @param null|int|string $maxAge Maximum cache age (integer timestamp or datetime string)
*
* @return ResponseInterface A new PSR7 response object with `Cache-Control` header
* @throws InvalidArgumentException if the cache-control type is invalid
*/
public function allowCache(ResponseInterface $response, $type = 'private', $maxAge = null)
{
if (!in_array($type, ['private', 'public'])) {
throw new \InvalidArgumentException('Invalid Cache-Control type. Must be "public" or "private".');
throw new InvalidArgumentException('Invalid Cache-Control type. Must be "public" or "private".');
}
$headerValue = $type;
if ($maxAge) {
Expand Down Expand Up @@ -61,13 +63,14 @@ public function denyCache(ResponseInterface $response)
* @param int|string $time A UNIX timestamp or a valid `strtotime()` string
*
* @return ResponseInterface A new PSR7 response object with `Expires` header
* @throws InvalidArgumentException if the expiration date cannot be parsed
*/
public function withExpires(ResponseInterface $response, $time)
{
if (!is_integer($time)) {
$time = strtotime($time);
if ($time === false) {
throw new \InvalidArgumentException('Expiration value could not be parsed with `strtotime()`.');
throw new InvalidArgumentException('Expiration value could not be parsed with `strtotime()`.');
}
}

Expand All @@ -82,11 +85,12 @@ public function withExpires(ResponseInterface $response, $time)
* @param string $type ETag type: "strong" or "weak"
*
* @return ResponseInterface A new PSR7 response object with `ETag` header
* @throws InvalidArgumentException if the etag type is invalid
*/
public function withEtag(ResponseInterface $response, $value, $type = 'strong')
{
if (!in_array($type, ['strong', 'weak'])) {
throw new \InvalidArgumentException('Invalid etag type. Must be "strong" or "weak".');
throw new InvalidArgumentException('Invalid etag type. Must be "strong" or "weak".');
}
$value = '"' . $value . '"';
if ($type === 'weak') {
Expand All @@ -103,13 +107,14 @@ public function withEtag(ResponseInterface $response, $value, $type = 'strong')
* @param int|string $time A UNIX timestamp or a valid `strtotime()` string
*
* @return ResponseInterface A new PSR7 response object with `Last-Modified` header
* @throws InvalidArgumentException if the last modified date cannot be parsed
*/
public function withLastModified(ResponseInterface $response, $time)
{
if (!is_integer($time)) {
$time = strtotime($time);
if ($time === false) {
throw new \InvalidArgumentException('Last Modified value could not be parsed with `strtotime()`.');
throw new InvalidArgumentException('Last Modified value could not be parsed with `strtotime()`.');
}
}

Expand Down
24 changes: 18 additions & 6 deletions tests/CacheProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ public function testAllowCache()
$cacheProvider = new CacheProvider();
$res = $cacheProvider->allowCache(new Response(), 'private', 43200);

$this->assertEquals('private, max-age=43200', $res->getHeader('Cache-Control'));
$cacheControl = $res->getHeaderLine('Cache-Control');

$this->assertEquals('private, max-age=43200', $cacheControl);
}

public function testDenyCache()
{
$cacheProvider = new CacheProvider();
$res = $cacheProvider->denyCache(new Response());

$this->assertEquals('no-store,no-cache', $res->getHeader('Cache-Control'));
$cacheControl = $res->getHeaderLine('Cache-Control');

$this->assertEquals('no-store,no-cache', $cacheControl);
}

public function testWithExpires()
Expand All @@ -28,7 +32,9 @@ public function testWithExpires()
$cacheProvider = new CacheProvider();
$res = $cacheProvider->withExpires(new Response(), $now);

$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $res->getHeader('Expires'));
$expires = $res->getHeaderLine('Expires');

$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $expires);
}

public function testWithETag()
Expand All @@ -37,7 +43,9 @@ public function testWithETag()
$cacheProvider = new CacheProvider();
$res = $cacheProvider->withEtag(new Response(), $etag);

$this->assertEquals('"' . $etag . '"', $res->getHeader('ETag'));
$etagHeader = $res->getHeaderLine('ETag');

$this->assertEquals('"' . $etag . '"', $etagHeader);
}

public function testWithETagWeak()
Expand All @@ -46,7 +54,9 @@ public function testWithETagWeak()
$cacheProvider = new CacheProvider();
$res = $cacheProvider->withEtag(new Response(), $etag, 'weak');

$this->assertEquals('W/"' . $etag . '"', $res->getHeader('ETag'));
$etagHeader = $res->getHeaderLine('ETag');

$this->assertEquals('W/"' . $etag . '"', $etagHeader);
}

/**
Expand All @@ -65,6 +75,8 @@ public function testWithLastModified()
$cacheProvider = new CacheProvider();
$res = $cacheProvider->withLastModified(new Response(), $now);

$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $res->getHeader('Last-Modified'));
$lastModified = $res->getHeaderLine('Last-Modified');

$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $lastModified);
}
}
14 changes: 9 additions & 5 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
use Slim\Http\Uri;
use Slim\Http\Headers;
use Slim\Http\Body;
use Slim\Collection;
use Slim\Http\Collection;

class CacheTest extends \PHPUnit_Framework_TestCase
{
public function requestFactory()
{
$uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123');
$headers = new Headers();
$cookies = new Collection();
$serverParams = new Collection();
$cookies = [];
$serverParams = [];
$body = new Body(fopen('php://temp', 'r+'));

return new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
Expand All @@ -32,7 +32,9 @@ public function testCacheControlHeader()
};
$res = $cache($req, $res, $next);

$this->assertEquals('public, max-age=86400', $res->getHeader('Cache-Control'));
$cacheControl = $res->getHeaderLine('Cache-Control');

$this->assertEquals('public, max-age=86400', $cacheControl);
}

public function testCacheControlHeaderDoesNotOverrideExistingHeader()
Expand All @@ -45,7 +47,9 @@ public function testCacheControlHeaderDoesNotOverrideExistingHeader()
};
$res = $cache($req, $res, $next);

$this->assertEquals('no-cache,no-store', $res->getHeader('Cache-Control'));
$cacheControl = $res->getHeaderLine('Cache-Control');

$this->assertEquals('no-cache,no-store', $cacheControl);
}

public function testLastModifiedWithCacheHit()
Expand Down

0 comments on commit 215f7ed

Please sign in to comment.