|
15 | 15 |
|
16 | 16 | use CodeIgniter\HTTP\DownloadResponse; |
17 | 17 | use CodeIgniter\HTTP\IncomingRequest; |
| 18 | +use CodeIgniter\HTTP\Method; |
18 | 19 | use CodeIgniter\HTTP\RedirectResponse; |
19 | 20 | use CodeIgniter\HTTP\Response; |
20 | 21 | use CodeIgniter\HTTP\ResponseInterface; |
|
31 | 32 | #[Group('Others')] |
32 | 33 | final class PageCacheTest extends CIUnitTestCase |
33 | 34 | { |
34 | | - private function createRequest(): IncomingRequest |
| 35 | + private function createRequest(string $method = Method::GET): IncomingRequest |
35 | 36 | { |
36 | 37 | $superglobals = service('superglobals'); |
37 | 38 | $superglobals->setServer('REQUEST_URI', '/'); |
38 | 39 |
|
39 | 40 | $siteUri = new SiteURI(new App()); |
40 | 41 |
|
41 | | - return new IncomingRequest(new App(), $siteUri, null, new UserAgent()); |
| 42 | + return (new IncomingRequest(new App(), $siteUri, null, new UserAgent())) |
| 43 | + ->withMethod($method); |
42 | 44 | } |
43 | 45 |
|
44 | 46 | public function testDefaultConfigCachesAllStatusCodes(): void |
@@ -141,6 +143,39 @@ public function testCustomCacheStatusCodes(): void |
141 | 143 | $this->assertNotInstanceOf(ResponseInterface::class, $result); |
142 | 144 | } |
143 | 145 |
|
| 146 | + public function testQueryRequestIsNotCached(): void |
| 147 | + { |
| 148 | + $config = new Cache(); |
| 149 | + $filter = new PageCache($config); |
| 150 | + |
| 151 | + $request = $this->createRequest(Method::QUERY); |
| 152 | + $response = new Response(); |
| 153 | + $response->setStatusCode(200); |
| 154 | + $response->setBody('Success'); |
| 155 | + |
| 156 | + $result = $filter->after($request, $response); |
| 157 | + $this->assertNotInstanceOf(ResponseInterface::class, $result); |
| 158 | + } |
| 159 | + |
| 160 | + public function testQueryRequestDoesNotReturnCachedResponse(): void |
| 161 | + { |
| 162 | + $filter = new PageCache(new Cache()); |
| 163 | + $request = $this->createRequest(Method::QUERY); |
| 164 | + $key = service('responsecache')->generateCacheKey($request); |
| 165 | + |
| 166 | + service('cache')->save($key, serialize([ |
| 167 | + 'headers' => [], |
| 168 | + 'output' => 'Cached', |
| 169 | + 'status' => 200, |
| 170 | + 'reason' => 'OK', |
| 171 | + ]), 60); |
| 172 | + |
| 173 | + $result = $filter->before($request); |
| 174 | + $this->assertNull($result); |
| 175 | + |
| 176 | + service('cache')->delete($key); |
| 177 | + } |
| 178 | + |
144 | 179 | public function testDownloadResponseNotCached(): void |
145 | 180 | { |
146 | 181 | $config = new Cache(); |
|
0 commit comments