Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/CorsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ public function varyHeader(Response $response, $header): Response
{
if (!$response->headers->has('Vary')) {
$response->headers->set('Vary', $header);
} elseif (!in_array($header, explode(', ', $response->headers->get('Vary')))) {
$response->headers->set('Vary', $response->headers->get('Vary') . ', ' . $header);
} elseif (!in_array($header, $response->headers->all('Vary'))) {
$response->headers->set('Vary', $header, false);
}

return $response;
Expand Down
44 changes: 33 additions & 11 deletions tests/CorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function it_returns_allow_headers_header_on_allow_all_headers_request()

$this->assertEquals(204, $response->getStatusCode());
$this->assertEquals('Foo, BAR', $response->headers->get('Access-Control-Allow-Headers'));
$this->assertEquals('Access-Control-Request-Headers, Access-Control-Request-Method', $response->headers->get('Vary'));
$this->assertEquals(['Access-Control-Request-Headers', 'Access-Control-Request-Method'], $response->headers->all('Vary'));
}

/**
Expand All @@ -106,7 +106,7 @@ public function it_returns_allow_headers_header_on_allow_all_headers_request_cre

$this->assertEquals(204, $response->getStatusCode());
$this->assertEquals('Foo, BAR', $response->headers->get('Access-Control-Allow-Headers'));
$this->assertEquals('Access-Control-Request-Headers, Access-Control-Request-Method', $response->headers->get('Vary'));
$this->assertEquals(['Access-Control-Request-Headers', 'Access-Control-Request-Method'], $response->headers->all('Vary'));
}

/**
Expand Down Expand Up @@ -164,7 +164,7 @@ public function it_adds_a_vary_header_when_wildcard_and_supports_credentials()
$response = $app->handle($request);

$this->assertTrue($response->headers->has('Vary'));
$this->assertEquals('Origin', $response->headers->get('Vary'));
$this->assertEquals(['Origin'], $response->headers->all('Vary'));
}

/**
Expand All @@ -182,7 +182,7 @@ public function it_adds_multiple_vary_header_when_wildcard_and_supports_credenti
$response = $app->handle($request);

$this->assertTrue($response->headers->has('Vary'));
$this->assertEquals('Origin, Access-Control-Request-Method', $response->headers->get('Vary'));
$this->assertEquals(['Origin', 'Access-Control-Request-Method'], $response->headers->all('Vary'));
}

/**
Expand All @@ -198,7 +198,7 @@ public function it_adds_a_vary_header_when_has_origin_patterns()
$response = $app->handle($request);

$this->assertTrue($response->headers->has('Vary'));
$this->assertEquals('Origin', $response->headers->get('Vary'));
$this->assertEquals(['Origin'], $response->headers->all('Vary'));
}

/**
Expand Down Expand Up @@ -268,7 +268,7 @@ public function it_appends_an_existing_vary_header()
$response = $app->handle($request);

$this->assertTrue($response->headers->has('Vary'));
$this->assertEquals('Content-Type, Origin', $response->headers->get('Vary'));
$this->assertEquals(['Content-Type', 'Origin'], $response->headers->all('Vary'));
}

/**
Expand Down Expand Up @@ -302,7 +302,7 @@ public function it_returns_access_control_headers_on_cors_request_with_pattern_o
$this->assertTrue($response->headers->has('Access-Control-Allow-Origin'));
$this->assertEquals('http://localhost', $response->headers->get('Access-Control-Allow-Origin'));
$this->assertTrue($response->headers->has('Vary'));
$this->assertEquals('Origin', $response->headers->get('Vary'));
$this->assertEquals(['Origin'], $response->headers->all('Vary'));
}

/**
Expand All @@ -316,7 +316,7 @@ public function it_adds_vary_headers_on_preflight_non_preflight_options()

$response = $app->handle($request);

$this->assertEquals('Access-Control-Request-Method', $response->headers->get('Vary'));
$this->assertEquals(['Access-Control-Request-Method'], $response->headers->all('Vary'));
}

/**
Expand All @@ -331,7 +331,7 @@ public function it_returns_access_control_headers_on_valid_preflight_request()

$this->assertTrue($response->headers->has('Access-Control-Allow-Origin'));
$this->assertEquals('http://localhost', $response->headers->get('Access-Control-Allow-Origin'));
$this->assertEquals('Access-Control-Request-Method', $response->headers->get('Vary'));
$this->assertEquals(['Access-Control-Request-Method'], $response->headers->all('Vary'));
}

/**
Expand Down Expand Up @@ -397,7 +397,7 @@ public function it_returns_valid_preflight_request_with_allow_methods_all()
$this->assertTrue($response->headers->has('Access-Control-Allow-Methods'));
// it will return the Access-Control-Request-Method pass in the request
$this->assertEquals('GET', $response->headers->get('Access-Control-Allow-Methods'));
$this->assertEquals('Access-Control-Request-Method', $response->headers->get('Vary'));
$this->assertEquals(['Access-Control-Request-Method'], $response->headers->all('Vary'));

}

Expand All @@ -415,7 +415,7 @@ public function it_returns_valid_preflight_request_with_allow_methods_all_creden
// it will return the Access-Control-Request-Method pass in the request
$this->assertEquals('GET', $response->headers->get('Access-Control-Allow-Methods'));
// it should vary this header
$this->assertEquals('Access-Control-Request-Method', $response->headers->get('Vary'));
$this->assertEquals(['Access-Control-Request-Method'], $response->headers->all('Vary'));
}

/**
Expand Down Expand Up @@ -533,6 +533,28 @@ public function it_doesnt_set_access_control_allow_origin_without_origin()
$this->assertFalse($response->headers->has('Access-Control-Allow-Origin'));
}

/**
* @test
*/
public function it_doesnt_lose_preexisting_vary_header_values()
{
$app = $this->createStackedApp([
'allowedOrigins' => ['*'],
'supportsCredentials' => true,
], [
'Vary' => [
'X-Custom-Header-1',
'X-Custom-Header-2'
]
]);
$request = $this->createValidActualRequest();

$response = $app->handle($request);

$this->assertTrue($response->headers->has('Vary'));
$this->assertEquals(['X-Custom-Header-1', 'X-Custom-Header-2', 'Origin'], $response->headers->all('Vary'));
}

private function createValidActualRequest()
{
$request = new Request();
Expand Down