Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle OPTIONS cacheable #74

Merged
merged 3 commits into from
Apr 18, 2020
Merged
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
12 changes: 10 additions & 2 deletions src/Cors.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ public function __construct(HttpKernelInterface $app, array $options = array())

public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
if ($this->cors->isPreflightRequest($request)) {
return $this->cors->handlePreflightRequest($request);
if ($request->getMethod() === 'OPTIONS') {
if ($this->cors->isPreflightRequest($request)) {
$response = $this->cors->handlePreflightRequest($request);
} else {
$response = $this->app->handle($request, $type, $catch);
}

$this->cors->varyHeader($response, 'Access-Control-Request-Method');

return $response;
}

$response = $this->app->handle($request, $type, $catch);
Expand Down
8 changes: 3 additions & 5 deletions src/CorsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public function isCorsRequest(Request $request)

public function isPreflightRequest(Request $request)
{
return $this->isCorsRequest($request)
&& $request->getMethod() === 'OPTIONS'
&& $request->headers->has('Access-Control-Request-Method');
return $request->getMethod() === 'OPTIONS' && $request->headers->has('Access-Control-Request-Method');
}

public function handlePreflightRequest(Request $request)
Expand Down Expand Up @@ -213,11 +211,11 @@ private function configureMaxAge(Response $response, Request $request)
}
}

private function varyHeader(Response $response, $header)
public function varyHeader(Response $response, $header)
{
if (!$response->headers->has('Vary')) {
$response->headers->set('Vary', $header);
} else {
} elseif (!in_array($header, explode(', ', $response->headers->get('Vary')))) {
$response->headers->set('Vary', $response->headers->get('Vary') . ', ' . $header);
}
}
Expand Down
17 changes: 16 additions & 1 deletion tests/CorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,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', $response->headers->get('Vary'));
$this->assertStringContainsString('Access-Control-Request-Headers', $response->headers->get('Vary'));
}

/**
Expand Down Expand Up @@ -304,6 +304,20 @@ public function it_returns_access_control_headers_on_cors_request_with_pattern_o
$this->assertEquals('Origin', $response->headers->get('Vary'));
}

/**
* @test
*/
public function it_adds_vary_headers_on_preflight_non_preflight_options()
{
$app = $this->createStackedApp();
$request = new Request();
$request->setMethod('OPTIONS');

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

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

/**
* @test
*/
Expand All @@ -316,6 +330,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'));
}

/**
Expand Down