|
8 | 8 | use GuzzleHttp\Psr7\Request; |
9 | 9 | use GuzzleHttp\Psr7\Response; |
10 | 10 | use Kevinrob\GuzzleCache\CacheMiddleware; |
| 11 | +use Kevinrob\GuzzleCache\KeyValueHttpHeader; |
11 | 12 | use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy; |
12 | 13 | use Kevinrob\GuzzleCache\Storage\VolatileRuntimeStorage; |
13 | 14 | use Psr\Http\Message\RequestInterface; |
@@ -73,4 +74,51 @@ public function testHeaderTtl() |
73 | 74 | $response = $this->client->send($request); |
74 | 75 | $this->assertEquals(CacheMiddleware::HEADER_CACHE_MISS, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO)); |
75 | 76 | } |
| 77 | + |
| 78 | + public function testGreedyCacheStrategyDeletion() |
| 79 | + { |
| 80 | + $storage = new VolatileRuntimeStorage(); |
| 81 | + $strategy = new GreedyCacheStrategy($storage, 60); |
| 82 | + |
| 83 | + $request = new Request('GET', 'http://test.com/'); |
| 84 | + $response = new Response(200, [], 'Test content'); |
| 85 | + |
| 86 | + $result = $strategy->cache($request, $response); |
| 87 | + $this->assertTrue($result, 'Response should be cached'); |
| 88 | + |
| 89 | + $cached = $strategy->fetch($request); |
| 90 | + $this->assertNotNull($cached, 'Cache entry should exist'); |
| 91 | + $this->assertEquals('Test content', (string) $cached->getResponse()->getBody()); |
| 92 | + |
| 93 | + $deleted = $strategy->delete($request); |
| 94 | + $this->assertTrue($deleted, 'Cache entry should be deleted successfully'); |
| 95 | + |
| 96 | + $cached = $strategy->fetch($request); |
| 97 | + $this->assertNull($cached, 'Cache entry should no longer exist after deletion'); |
| 98 | + } |
| 99 | + |
| 100 | + public function testGreedyDeletionWithVaryHeaders() |
| 101 | + { |
| 102 | + $storage = new VolatileRuntimeStorage(); |
| 103 | + $varyHeaders = new KeyValueHttpHeader(['Authorization']); |
| 104 | + $strategy = new GreedyCacheStrategy($storage, 60, $varyHeaders); |
| 105 | + |
| 106 | + $request = new Request('GET', 'http://test.com/', [ |
| 107 | + 'Authorization' => 'Bearer token123' |
| 108 | + ]); |
| 109 | + $response = new Response(200, [], 'Authorized content'); |
| 110 | + |
| 111 | + $result = $strategy->cache($request, $response); |
| 112 | + $this->assertTrue($result, 'Response should be cached with vary headers'); |
| 113 | + |
| 114 | + $cached = $strategy->fetch($request); |
| 115 | + $this->assertNotNull($cached, 'Cache entry should exist with vary headers'); |
| 116 | + $this->assertEquals('Authorized content', (string) $cached->getResponse()->getBody()); |
| 117 | + |
| 118 | + $deleted = $strategy->delete($request); |
| 119 | + $this->assertTrue($deleted, 'Cache entry should be deleted successfully with vary headers'); |
| 120 | + |
| 121 | + $cached = $strategy->fetch($request); |
| 122 | + $this->assertNull($cached, 'Cache entry should no longer exist after deletion with vary headers'); |
| 123 | + } |
76 | 124 | } |
0 commit comments