3 namespace Drupal\FunctionalTests\HttpKernel;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests CORS provided by Drupal.
11 * @see sites/default/default.services.yml
12 * @see \Asm89\Stack\Cors
13 * @see \Asm89\Stack\CorsService
17 class CorsIntegrationTest extends BrowserTestBase {
22 public static $modules = ['system', 'test_page_test', 'page_cache'];
24 public function testCrossSiteRequest() {
25 // Test default parameters.
26 $cors_config = $this->container->getParameter('cors.config');
27 $this->assertSame(FALSE, $cors_config['enabled']);
28 $this->assertSame([], $cors_config['allowedHeaders']);
29 $this->assertSame([], $cors_config['allowedMethods']);
30 $this->assertSame(['*'], $cors_config['allowedOrigins']);
32 $this->assertSame(FALSE, $cors_config['exposedHeaders']);
33 $this->assertSame(FALSE, $cors_config['maxAge']);
34 $this->assertSame(FALSE, $cors_config['supportsCredentials']);
36 // Enable CORS with the default options.
37 $cors_config['enabled'] = TRUE;
39 $this->setContainerParameter('cors.config', $cors_config);
40 $this->rebuildContainer();
42 // Fire off a request.
43 $this->drupalGet('/test-page', [], ['Origin' => 'http://example.com']);
44 $this->assertSession()->statusCodeEquals(200);
45 $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
46 $this->assertSession()->responseHeaderEquals('Access-Control-Allow-Origin', 'http://example.com');
48 // Fire the same exact request. This time it should be cached.
49 $this->drupalGet('/test-page', [], ['Origin' => 'http://example.com']);
50 $this->assertSession()->statusCodeEquals(200);
51 $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
52 $this->assertSession()->responseHeaderEquals('Access-Control-Allow-Origin', 'http://example.com');
54 // Fire a request for a different origin. Verify the CORS header.
55 $this->drupalGet('/test-page', [], ['Origin' => 'http://example.org']);
56 $this->assertSession()->statusCodeEquals(200);
57 $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
58 $this->assertSession()->responseHeaderEquals('Access-Control-Allow-Origin', 'http://example.org');
60 // Configure the CORS stack to allow a specific set of origins.
61 $cors_config['allowedOrigins'] = ['http://example.com'];
63 $this->setContainerParameter('cors.config', $cors_config);
64 $this->rebuildContainer();
66 // Fire a request from an origin that isn't allowed.
67 /** @var \Symfony\Component\HttpFoundation\Response $response */
68 $this->drupalGet('/test-page', [], ['Origin' => 'http://non-valid.com']);
69 $this->assertSession()->statusCodeEquals(403);
70 $this->assertSession()->pageTextContains('Not allowed.');
72 // Specify a valid origin.
73 $this->drupalGet('/test-page', [], ['Origin' => 'http://example.com']);
74 $this->assertSession()->statusCodeEquals(200);
75 $this->assertSession()->responseHeaderEquals('Access-Control-Allow-Origin', 'http://example.com');
77 // Verify POST still functions with 'Origin' header set to site's domain.
78 $origin = \Drupal::request()->getSchemeAndHttpHost();
80 /** @var \GuzzleHttp\ClientInterface $httpClient */
81 $httpClient = $this->getSession()->getDriver()->getClient()->getClient();
82 $url = Url::fromUri('base:/test-page');
83 $response = $httpClient->request('POST', $url->setAbsolute()->toString(), [
88 $this->assertEquals(200, $response->getStatusCode());