|
4 | 4 |
|
5 | 5 | namespace Hypervel\Tests\Http; |
6 | 6 |
|
| 7 | +use Hyperf\HttpMessage\Base\Response; |
7 | 8 | use Hypervel\Http\Cors; |
8 | 9 | use PHPUnit\Framework\TestCase; |
9 | 10 | use ReflectionClass; |
@@ -247,4 +248,60 @@ private function getOptionsFromService(Cors $service): array |
247 | 248 | /** @var CorsNormalizedOptions $options */ |
248 | 249 | return $options; |
249 | 250 | } |
| 251 | + |
| 252 | + public function testVaryHeaderAddsHeaderWhenNoneExists(): void |
| 253 | + { |
| 254 | + $cors = new Cors(); |
| 255 | + $response = new Response(); |
| 256 | + |
| 257 | + $result = $cors->varyHeader($response, 'Origin'); |
| 258 | + |
| 259 | + $this->assertSame(['Origin'], $result->getHeader('Vary')); |
| 260 | + } |
| 261 | + |
| 262 | + public function testVaryHeaderAppendsToExistingSingleHeader(): void |
| 263 | + { |
| 264 | + $cors = new Cors(); |
| 265 | + $response = (new Response())->withHeader('Vary', 'Accept-Encoding'); |
| 266 | + |
| 267 | + $result = $cors->varyHeader($response, 'Origin'); |
| 268 | + |
| 269 | + $this->assertSame(['Accept-Encoding, Origin'], $result->getHeader('Vary')); |
| 270 | + } |
| 271 | + |
| 272 | + public function testVaryHeaderDoesNotDuplicateExistingHeader(): void |
| 273 | + { |
| 274 | + $cors = new Cors(); |
| 275 | + $response = (new Response())->withHeader('Vary', 'Origin'); |
| 276 | + |
| 277 | + $result = $cors->varyHeader($response, 'Origin'); |
| 278 | + |
| 279 | + $this->assertSame(['Origin'], $result->getHeader('Vary')); |
| 280 | + } |
| 281 | + |
| 282 | + public function testVaryHeaderHandlesMultipleExistingHeaders(): void |
| 283 | + { |
| 284 | + $cors = new Cors(); |
| 285 | + // PSR-7 allows multiple header values as array elements |
| 286 | + $response = (new Response()) |
| 287 | + ->withHeader('Vary', 'Accept-Encoding') |
| 288 | + ->withAddedHeader('Vary', 'Accept-Language'); |
| 289 | + |
| 290 | + $result = $cors->varyHeader($response, 'Origin'); |
| 291 | + |
| 292 | + $this->assertSame(['Accept-Encoding, Accept-Language, Origin'], $result->getHeader('Vary')); |
| 293 | + } |
| 294 | + |
| 295 | + public function testVaryHeaderDoesNotDuplicateWhenInMultipleHeaders(): void |
| 296 | + { |
| 297 | + $cors = new Cors(); |
| 298 | + $response = (new Response()) |
| 299 | + ->withHeader('Vary', 'Accept-Encoding') |
| 300 | + ->withAddedHeader('Vary', 'Origin'); |
| 301 | + |
| 302 | + $result = $cors->varyHeader($response, 'Origin'); |
| 303 | + |
| 304 | + // Should not add Origin again since it's already present |
| 305 | + $this->assertSame(['Accept-Encoding', 'Origin'], $result->getHeader('Vary')); |
| 306 | + } |
250 | 307 | } |
0 commit comments