Skip to content

Commit dd956e4

Browse files
committed
Testing for empty body on cache hit
Adding tests ETag and Last-Modified to ensure the response body shouldn't send data when their is a cache hit.
1 parent 6a8f44d commit dd956e4

1 file changed

Lines changed: 48 additions & 10 deletions

File tree

tests/CacheTest.php

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616
use Slim\Psr7\Factory\ResponseFactory;
1717
use Slim\Psr7\Factory\ServerRequestFactory;
1818

19+
use Slim\Psr7\Factory\StreamFactory;
1920
use function gmdate;
2021
use function time;
2122

2223
class CacheTest extends TestCase
2324
{
25+
private function createCache(string $type = 'privte', int $maxAge = 86400, bool $mustRevalidate = false): Cache
26+
{
27+
return new Cache(new StreamFactory(), $type, $maxAge, $mustRevalidate);
28+
}
29+
30+
2431
public function requestFactory(): ServerRequestInterface
2532
{
2633
$serverRequestFactory = new ServerRequestFactory();
@@ -64,7 +71,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
6471

6572
public function testCacheControlHeader()
6673
{
67-
$cache = new Cache('public', 86400);
74+
$cache = $this->createCache('public', 86400);
6875
$req = $this->requestFactory();
6976

7077
$res = $cache->process($req, $this->createRequestHandler());
@@ -76,7 +83,7 @@ public function testCacheControlHeader()
7683

7784
public function testCacheControlHeaderWithMustRevalidate()
7885
{
79-
$cache = new Cache('private', 86400, true);
86+
$cache = $this->createCache('private', 86400, true);
8087
$req = $this->requestFactory();
8188

8289
$res = $cache->process($req, $this->createRequestHandler());
@@ -88,7 +95,7 @@ public function testCacheControlHeaderWithMustRevalidate()
8895

8996
public function testCacheControlHeaderWithZeroMaxAge()
9097
{
91-
$cache = new Cache('private', 0, false);
98+
$cache = $this->createCache('private', 0, false);
9299
$req = $this->requestFactory();
93100

94101
$res = $cache->process($req, $this->createRequestHandler());
@@ -100,7 +107,7 @@ public function testCacheControlHeaderWithZeroMaxAge()
100107

101108
public function testCacheControlHeaderDoesNotOverrideExistingHeader()
102109
{
103-
$cache = new Cache('public', 86400);
110+
$cache = $this->createCache('public', 86400);
104111
$req = $this->requestFactory();
105112

106113
$res = $this->createResponse()->withHeader('Cache-Control', 'no-cache,no-store');
@@ -116,7 +123,7 @@ public function testLastModifiedWithCacheHit()
116123
$now = time();
117124
$lastModified = gmdate('D, d M Y H:i:s T', $now + 86400);
118125
$ifModifiedSince = gmdate('D, d M Y H:i:s T', $now + 86400);
119-
$cache = new Cache('public', 86400);
126+
$cache = $this->createCache('public', 86400);
120127

121128
$req = $this->requestFactory()->withHeader('If-Modified-Since', $ifModifiedSince);
122129

@@ -131,7 +138,7 @@ public function testLastModifiedWithCacheHitAndNewerDate()
131138
$now = time();
132139
$lastModified = gmdate('D, d M Y H:i:s T', $now + 86400);
133140
$ifModifiedSince = gmdate('D, d M Y H:i:s T', $now + 172800); // <-- Newer date
134-
$cache = new Cache('public', 86400);
141+
$cache = $this->createCache('public', 86400);
135142
$req = $this->requestFactory()->withHeader('If-Modified-Since', $ifModifiedSince);
136143

137144
$res = $this->createResponse()->withHeader('Last-Modified', $lastModified);
@@ -145,7 +152,7 @@ public function testLastModifiedWithCacheHitAndOlderDate()
145152
$now = time();
146153
$lastModified = gmdate('D, d M Y H:i:s T', $now + 86400);
147154
$ifModifiedSince = gmdate('D, d M Y H:i:s T', $now); // <-- Older date
148-
$cache = new Cache('public', 86400);
155+
$cache = $this->createCache('public', 86400);
149156
$req = $this->requestFactory()->withHeader('If-Modified-Since', $ifModifiedSince);
150157

151158
$res = $this->createResponse()->withHeader('Last-Modified', $lastModified);
@@ -159,7 +166,7 @@ public function testLastModifiedWithCacheMiss()
159166
$now = time();
160167
$lastModified = gmdate('D, d M Y H:i:s T', $now + 86400);
161168
$ifModifiedSince = gmdate('D, d M Y H:i:s T', $now - 86400);
162-
$cache = new Cache('public', 86400);
169+
$cache = $this->createCache('public', 86400);
163170
$req = $this->requestFactory()->withHeader('If-Modified-Since', $ifModifiedSince);
164171

165172
$res = $this->createResponse()->withHeader('Last-Modified', $lastModified);
@@ -172,7 +179,7 @@ public function testETagWithCacheHit()
172179
{
173180
$etag = 'abc';
174181
$ifNoneMatch = 'abc';
175-
$cache = new Cache('public', 86400);
182+
$cache = $this->createCache('public', 86400);
176183
$req = $this->requestFactory()->withHeader('If-None-Match', $ifNoneMatch);
177184

178185
$res = $this->createResponse()->withHeader('Etag', $etag);
@@ -185,12 +192,43 @@ public function testETagWithCacheMiss()
185192
{
186193
$etag = 'abc';
187194
$ifNoneMatch = 'xyz';
188-
$cache = new Cache('public', 86400);
195+
$cache = $this->createCache('public', 86400);
189196
$req = $this->requestFactory()->withHeader('If-None-Match', $ifNoneMatch);
190197

191198
$res = $this->createResponse()->withHeader('Etag', $etag);
192199
$res = $cache->process($req, $this->createRequestHandler($res));
193200

194201
$this->assertEquals(200, $res->getStatusCode());
195202
}
203+
204+
public function testETagReturnsNoBodyOnCacheHit(): void
205+
{
206+
$etag = 'abc';
207+
$cache = $this->createCache();
208+
$req = $this->requestFactory()->withHeader('If-None-Match', $etag);
209+
210+
$res = $this->createResponse()->withHeader('Etag', $etag);
211+
$res->getBody()->write('payload data');
212+
$res = $cache->process($req, $this->createRequestHandler($res));
213+
214+
self::assertSame(304, $res->getStatusCode());
215+
self::assertSame('', (string) $res->getBody());
216+
}
217+
218+
public function testLastModifiedReturnsNoBodyOnCacheHit(): void
219+
{
220+
$now = time() + 86400;
221+
$lastModified = gmdate('D, d M Y H:i:s T', $now);
222+
$ifModifiedSince = gmdate('D, d M Y H:i:s T', $now);
223+
$cache = $this->createCache();
224+
225+
$req = $this->requestFactory()->withHeader('If-Modified-Since', $ifModifiedSince);
226+
$res = $this->createResponse()->withHeader('Last-Modified', $lastModified);
227+
$res->getBody()->write('payload data');
228+
229+
$res = $cache->process($req, $this->createRequestHandler($res));
230+
231+
self::assertEquals(304, $res->getStatusCode());
232+
self::assertSame('', (string) $res->getBody());
233+
}
196234
}

0 commit comments

Comments
 (0)