Skip to content

Commit cca409b

Browse files
authored
Merge pull request #32 from fruitcake/feat-multiple-vary-headers
Check multiple Vary headers when existing
2 parents 6d89006 + 69714b5 commit cca409b

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/CorsService.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,15 @@ public function varyHeader(Response $response, string $header): Response
276276
{
277277
if (!$response->headers->has('Vary')) {
278278
$response->headers->set('Vary', $header);
279-
} elseif (!in_array($header, explode(', ', (string) $response->headers->get('Vary')))) {
280-
$response->headers->set('Vary', ((string) $response->headers->get('Vary')) . ', ' . $header);
279+
} else {
280+
$varyHeaders = $response->getVary();
281+
if (!in_array($header, $varyHeaders, true)) {
282+
if (count($response->headers->all('Vary')) === 1) {
283+
$response->setVary(((string)$response->headers->get('Vary')) . ', ' . $header);
284+
} else {
285+
$response->setVary($header, false);
286+
}
287+
}
281288
}
282289

283290
return $response;

tests/CorsTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,32 @@ public function itAppendsAnExistingVaryHeader(): void
274274
$this->assertEquals('Content-Type, Origin', $response->headers->get('Vary'));
275275
}
276276

277+
/**
278+
* @test
279+
* @see http://www.w3.org/TR/cors/index.html#resource-implementation
280+
*/
281+
public function itAppendsMultipleExistingVaryHeaders(): void
282+
{
283+
$app = $this->createStackedApp(
284+
array(
285+
'allowedOrigins' => ['*'],
286+
'supportsCredentials' => true,
287+
),
288+
array(
289+
'Vary' => [
290+
'Content-Type',
291+
'Referer',
292+
],
293+
)
294+
);
295+
$request = $this->createValidActualRequest();
296+
297+
$response = $app->handle($request);
298+
299+
$this->assertCount(3, $response->headers->all('Vary'));
300+
$this->assertEquals(['Content-Type', 'Referer', 'Origin'], $response->headers->all('Vary'));
301+
}
302+
277303
/**
278304
* @test
279305
*/
@@ -555,7 +581,7 @@ private function createValidPreflightRequest(): Request
555581

556582
/**
557583
* @param CorsInputOptions $options
558-
* @param string[] $responseHeaders
584+
* @param array<array<string>|string> $responseHeaders
559585
* @return MockApp
560586
*/
561587
private function createStackedApp(array $options = array(), array $responseHeaders = array()): MockApp

tests/MockApp.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
class MockApp
2323
{
24-
/** @var string[] */
24+
/** @var array<array<string>|string> */
2525
private $responseHeaders;
2626

2727
/**
@@ -30,7 +30,7 @@ class MockApp
3030
private $cors;
3131

3232
/**
33-
* @param string[] $responseHeaders
33+
* @param array<array<string>|string> $responseHeaders
3434
* @param CorsInputOptions $options
3535
*/
3636
public function __construct(array $responseHeaders, array $options = [])

0 commit comments

Comments
 (0)