Skip to content

Commit 6b0ff18

Browse files
authored
Merge pull request #318 from binaryfire/fix-cors-vary-header-bug
fix: `Cors::varyHeader` bug with multiple Vary headers
2 parents fb5a7cd + d28c3e1 commit 6b0ff18

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/http/src/Cors.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,7 @@ public function varyHeader(ResponseInterface $response, string $header): Respons
290290
} else {
291291
$varyHeaders = $response->getHeader('Vary');
292292
if (! in_array($header, $varyHeaders, true)) {
293-
if (count($varyHeaders) === 1) {
294-
$response = $response->withHeader('Vary', ((string) $varyHeaders[0]) . ', ' . $header);
295-
} else {
296-
$response->withHeader($header, false);
297-
}
293+
$response = $response->withHeader('Vary', implode(', ', $varyHeaders) . ', ' . $header);
298294
}
299295
}
300296

tests/Http/CorsTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Hypervel\Tests\Http;
66

7+
use Hyperf\HttpMessage\Base\Response;
78
use Hypervel\Http\Cors;
89
use PHPUnit\Framework\TestCase;
910
use ReflectionClass;
@@ -247,4 +248,60 @@ private function getOptionsFromService(Cors $service): array
247248
/** @var CorsNormalizedOptions $options */
248249
return $options;
249250
}
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+
}
250307
}

0 commit comments

Comments
 (0)