Skip to content

Commit d969c13

Browse files
Fix code and tests
1 parent 31f4a88 commit d969c13

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

src/Cache.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,29 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
5858

5959
// Last-Modified header and conditional GET check
6060
$lastModified = $response->getHeader('Last-Modified');
61+
$lastModified = reset($lastModified);
62+
6163
if ($lastModified) {
6264
if (!is_integer($lastModified)) {
6365
$lastModified = strtotime($lastModified);
6466
}
67+
6568
$ifModifiedSince = $request->getHeader('If-Modified-Since');
69+
$ifModifiedSince = reset($ifModifiedSince);
70+
6671
if ($ifModifiedSince && $lastModified <= strtotime($ifModifiedSince)) {
6772
return $response->withStatus(304);
6873
}
6974
}
7075

7176
// ETag header and conditional GET check
7277
$etag = $response->getHeader('ETag');
78+
$etag = reset($etag);
79+
7380
if ($etag) {
7481
$ifNoneMatch = $request->getHeader('If-None-Match');
82+
$ifNoneMatch = reset($ifNoneMatch);
83+
7584
if ($ifNoneMatch) {
7685
$etagList = preg_split('@\s*,\s*@', $ifNoneMatch);
7786
if (in_array($etag, $etagList) || in_array('*', $etagList)) {

tests/CacheProviderTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ 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->getHeader('Cache-Control');
15+
$cacheControl = reset($cacheControl);
16+
17+
$this->assertEquals('private, max-age=43200', $cacheControl);
1518
}
1619

1720
public function testDenyCache()
1821
{
1922
$cacheProvider = new CacheProvider();
2023
$res = $cacheProvider->denyCache(new Response());
2124

22-
$this->assertEquals('no-store,no-cache', $res->getHeader('Cache-Control'));
25+
$cacheControl = $res->getHeader('Cache-Control');
26+
$cacheControl = reset($cacheControl);
27+
28+
$this->assertEquals('no-store,no-cache', $cacheControl);
2329
}
2430

2531
public function testWithExpires()
@@ -28,7 +34,10 @@ public function testWithExpires()
2834
$cacheProvider = new CacheProvider();
2935
$res = $cacheProvider->withExpires(new Response(), $now);
3036

31-
$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $res->getHeader('Expires'));
37+
$expires = $res->getHeader('Expires');
38+
$expires = reset($expires);
39+
40+
$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $expires);
3241
}
3342

3443
public function testWithETag()
@@ -37,7 +46,10 @@ public function testWithETag()
3746
$cacheProvider = new CacheProvider();
3847
$res = $cacheProvider->withEtag(new Response(), $etag);
3948

40-
$this->assertEquals('"' . $etag . '"', $res->getHeader('ETag'));
49+
$etagHeader = $res->getHeader('ETag');
50+
$etagHeader = reset($etagHeader);
51+
52+
$this->assertEquals('"' . $etag . '"', $etagHeader);
4153
}
4254

4355
public function testWithETagWeak()
@@ -46,7 +58,10 @@ public function testWithETagWeak()
4658
$cacheProvider = new CacheProvider();
4759
$res = $cacheProvider->withEtag(new Response(), $etag, 'weak');
4860

49-
$this->assertEquals('W/"' . $etag . '"', $res->getHeader('ETag'));
61+
$etagHeader = $res->getHeader('ETag');
62+
$etagHeader = reset($etagHeader);
63+
64+
$this->assertEquals('W/"' . $etag . '"', $etagHeader);
5065
}
5166

5267
/**
@@ -65,6 +80,9 @@ public function testWithLastModified()
6580
$cacheProvider = new CacheProvider();
6681
$res = $cacheProvider->withLastModified(new Response(), $now);
6782

68-
$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $res->getHeader('Last-Modified'));
83+
$lastModified = $res->getHeader('Last-Modified');
84+
$lastModified = reset($lastModified);
85+
86+
$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $lastModified);
6987
}
7088
}

tests/CacheTest.php

Lines changed: 11 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,10 @@ 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->getHeader('Cache-Control');
36+
$cacheControl = reset($cacheControl);
37+
38+
$this->assertEquals('public, max-age=86400', $cacheControl);
3639
}
3740

3841
public function testCacheControlHeaderDoesNotOverrideExistingHeader()
@@ -45,7 +48,10 @@ public function testCacheControlHeaderDoesNotOverrideExistingHeader()
4548
};
4649
$res = $cache($req, $res, $next);
4750

48-
$this->assertEquals('no-cache,no-store', $res->getHeader('Cache-Control'));
51+
$cacheControl = $res->getHeader('Cache-Control');
52+
$cacheControl = reset($cacheControl);
53+
54+
$this->assertEquals('no-cache,no-store', $cacheControl);
4955
}
5056

5157
public function testLastModifiedWithCacheHit()

0 commit comments

Comments
 (0)