Skip to content

Commit 215f7ed

Browse files
committed
Merge pull request #6 from designermonkey/fixes
Fixes
2 parents 0aeae78 + f33e7ec commit 215f7ed

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

src/Cache.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,27 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
5757
}
5858

5959
// Last-Modified header and conditional GET check
60-
$lastModified = $response->getHeader('Last-Modified');
60+
$lastModified = $response->getHeaderLine('Last-Modified');
61+
6162
if ($lastModified) {
6263
if (!is_integer($lastModified)) {
6364
$lastModified = strtotime($lastModified);
6465
}
65-
$ifModifiedSince = $request->getHeader('If-Modified-Since');
66+
67+
$ifModifiedSince = $request->getHeaderLine('If-Modified-Since');
68+
6669
if ($ifModifiedSince && $lastModified <= strtotime($ifModifiedSince)) {
6770
return $response->withStatus(304);
6871
}
6972
}
7073

7174
// ETag header and conditional GET check
7275
$etag = $response->getHeader('ETag');
76+
$etag = reset($etag);
77+
7378
if ($etag) {
74-
$ifNoneMatch = $request->getHeader('If-None-Match');
79+
$ifNoneMatch = $request->getHeaderLine('If-None-Match');
80+
7581
if ($ifNoneMatch) {
7682
$etagList = preg_split('@\s*,\s*@', $ifNoneMatch);
7783
if (in_array($etag, $etagList) || in_array('*', $etagList)) {

src/CacheProvider.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Slim\HttpCache;
33

4+
use InvalidArgumentException;
45
use Pimple\Container;
56
use Pimple\ServiceProviderInterface;
67
use Psr\Http\Message\ResponseInterface;
@@ -25,11 +26,12 @@ public function register(Container $container)
2526
* @param null|int|string $maxAge Maximum cache age (integer timestamp or datetime string)
2627
*
2728
* @return ResponseInterface A new PSR7 response object with `Cache-Control` header
29+
* @throws InvalidArgumentException if the cache-control type is invalid
2830
*/
2931
public function allowCache(ResponseInterface $response, $type = 'private', $maxAge = null)
3032
{
3133
if (!in_array($type, ['private', 'public'])) {
32-
throw new \InvalidArgumentException('Invalid Cache-Control type. Must be "public" or "private".');
34+
throw new InvalidArgumentException('Invalid Cache-Control type. Must be "public" or "private".');
3335
}
3436
$headerValue = $type;
3537
if ($maxAge) {
@@ -61,13 +63,14 @@ public function denyCache(ResponseInterface $response)
6163
* @param int|string $time A UNIX timestamp or a valid `strtotime()` string
6264
*
6365
* @return ResponseInterface A new PSR7 response object with `Expires` header
66+
* @throws InvalidArgumentException if the expiration date cannot be parsed
6467
*/
6568
public function withExpires(ResponseInterface $response, $time)
6669
{
6770
if (!is_integer($time)) {
6871
$time = strtotime($time);
6972
if ($time === false) {
70-
throw new \InvalidArgumentException('Expiration value could not be parsed with `strtotime()`.');
73+
throw new InvalidArgumentException('Expiration value could not be parsed with `strtotime()`.');
7174
}
7275
}
7376

@@ -82,11 +85,12 @@ public function withExpires(ResponseInterface $response, $time)
8285
* @param string $type ETag type: "strong" or "weak"
8386
*
8487
* @return ResponseInterface A new PSR7 response object with `ETag` header
88+
* @throws InvalidArgumentException if the etag type is invalid
8589
*/
8690
public function withEtag(ResponseInterface $response, $value, $type = 'strong')
8791
{
8892
if (!in_array($type, ['strong', 'weak'])) {
89-
throw new \InvalidArgumentException('Invalid etag type. Must be "strong" or "weak".');
93+
throw new InvalidArgumentException('Invalid etag type. Must be "strong" or "weak".');
9094
}
9195
$value = '"' . $value . '"';
9296
if ($type === 'weak') {
@@ -103,13 +107,14 @@ public function withEtag(ResponseInterface $response, $value, $type = 'strong')
103107
* @param int|string $time A UNIX timestamp or a valid `strtotime()` string
104108
*
105109
* @return ResponseInterface A new PSR7 response object with `Last-Modified` header
110+
* @throws InvalidArgumentException if the last modified date cannot be parsed
106111
*/
107112
public function withLastModified(ResponseInterface $response, $time)
108113
{
109114
if (!is_integer($time)) {
110115
$time = strtotime($time);
111116
if ($time === false) {
112-
throw new \InvalidArgumentException('Last Modified value could not be parsed with `strtotime()`.');
117+
throw new InvalidArgumentException('Last Modified value could not be parsed with `strtotime()`.');
113118
}
114119
}
115120

tests/CacheProviderTest.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ public function testAllowCache()
1111
$cacheProvider = new CacheProvider();
1212
$res = $cacheProvider->allowCache(new Response(), 'private', 43200);
1313

14-
$this->assertEquals('private, max-age=43200', $res->getHeader('Cache-Control'));
14+
$cacheControl = $res->getHeaderLine('Cache-Control');
15+
16+
$this->assertEquals('private, max-age=43200', $cacheControl);
1517
}
1618

1719
public function testDenyCache()
1820
{
1921
$cacheProvider = new CacheProvider();
2022
$res = $cacheProvider->denyCache(new Response());
2123

22-
$this->assertEquals('no-store,no-cache', $res->getHeader('Cache-Control'));
24+
$cacheControl = $res->getHeaderLine('Cache-Control');
25+
26+
$this->assertEquals('no-store,no-cache', $cacheControl);
2327
}
2428

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

31-
$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $res->getHeader('Expires'));
35+
$expires = $res->getHeaderLine('Expires');
36+
37+
$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $expires);
3238
}
3339

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

40-
$this->assertEquals('"' . $etag . '"', $res->getHeader('ETag'));
46+
$etagHeader = $res->getHeaderLine('ETag');
47+
48+
$this->assertEquals('"' . $etag . '"', $etagHeader);
4149
}
4250

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

49-
$this->assertEquals('W/"' . $etag . '"', $res->getHeader('ETag'));
57+
$etagHeader = $res->getHeaderLine('ETag');
58+
59+
$this->assertEquals('W/"' . $etag . '"', $etagHeader);
5060
}
5161

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

68-
$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $res->getHeader('Last-Modified'));
78+
$lastModified = $res->getHeaderLine('Last-Modified');
79+
80+
$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $lastModified);
6981
}
7082
}

tests/CacheTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
use Slim\Http\Uri;
88
use Slim\Http\Headers;
99
use Slim\Http\Body;
10-
use Slim\Collection;
10+
use Slim\Http\Collection;
1111

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

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

35-
$this->assertEquals('public, max-age=86400', $res->getHeader('Cache-Control'));
35+
$cacheControl = $res->getHeaderLine('Cache-Control');
36+
37+
$this->assertEquals('public, max-age=86400', $cacheControl);
3638
}
3739

3840
public function testCacheControlHeaderDoesNotOverrideExistingHeader()
@@ -45,7 +47,9 @@ public function testCacheControlHeaderDoesNotOverrideExistingHeader()
4547
};
4648
$res = $cache($req, $res, $next);
4749

48-
$this->assertEquals('no-cache,no-store', $res->getHeader('Cache-Control'));
50+
$cacheControl = $res->getHeaderLine('Cache-Control');
51+
52+
$this->assertEquals('no-cache,no-store', $cacheControl);
4953
}
5054

5155
public function testLastModifiedWithCacheHit()

0 commit comments

Comments
 (0)