Version 1
[yaffs-website] / web / core / tests / Drupal / FunctionalTests / HttpKernel / CorsIntegrationTest.php
1 <?php
2
3 namespace Drupal\FunctionalTests\HttpKernel;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests CORS provided by Drupal.
9  *
10  * @see sites/default/default.services.yml
11  * @see \Asm89\Stack\Cors
12  * @see \Asm89\Stack\CorsService
13  *
14  * @group Http
15  */
16 class CorsIntegrationTest extends BrowserTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = ['system', 'test_page_test', 'page_cache'];
22
23   public function testCrossSiteRequest() {
24     // Test default parameters.
25     $cors_config = $this->container->getParameter('cors.config');
26     $this->assertSame(FALSE, $cors_config['enabled']);
27     $this->assertSame([], $cors_config['allowedHeaders']);
28     $this->assertSame([], $cors_config['allowedMethods']);
29     $this->assertSame(['*'], $cors_config['allowedOrigins']);
30
31     $this->assertSame(FALSE, $cors_config['exposedHeaders']);
32     $this->assertSame(FALSE, $cors_config['maxAge']);
33     $this->assertSame(FALSE, $cors_config['supportsCredentials']);
34
35     // Enable CORS with the default options.
36     $cors_config['enabled'] = TRUE;
37
38     $this->setContainerParameter('cors.config', $cors_config);
39     $this->rebuildContainer();
40
41     // Fire off a request.
42     $this->drupalGet('/test-page', [], ['Origin' => 'http://example.com']);
43     $this->assertSession()->statusCodeEquals(200);
44     $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
45     $this->assertSession()->responseHeaderEquals('Access-Control-Allow-Origin', 'http://example.com');
46
47     // Fire the same exact request. This time it should be cached.
48     $this->drupalGet('/test-page', [], ['Origin' => 'http://example.com']);
49     $this->assertSession()->statusCodeEquals(200);
50     $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
51     $this->assertSession()->responseHeaderEquals('Access-Control-Allow-Origin', 'http://example.com');
52
53     // Fire a request for a different origin. Verify the CORS header.
54     $this->drupalGet('/test-page', [], ['Origin' => 'http://example.org']);
55     $this->assertSession()->statusCodeEquals(200);
56     $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
57     $this->assertSession()->responseHeaderEquals('Access-Control-Allow-Origin', 'http://example.org');
58
59     // Configure the CORS stack to allow a specific set of origins.
60     $cors_config['allowedOrigins'] = ['http://example.com'];
61
62     $this->setContainerParameter('cors.config', $cors_config);
63     $this->rebuildContainer();
64
65     // Fire a request from an origin that isn't allowed.
66     /** @var \Symfony\Component\HttpFoundation\Response $response */
67     $this->drupalGet('/test-page', [], ['Origin' => 'http://non-valid.com']);
68     $this->assertSession()->statusCodeEquals(403);
69     $this->assertSession()->pageTextContains('Not allowed.');
70
71     // Specify a valid origin.
72     $this->drupalGet('/test-page', [], ['Origin' => 'http://example.com']);
73     $this->assertSession()->statusCodeEquals(200);
74     $this->assertSession()->responseHeaderEquals('Access-Control-Allow-Origin', 'http://example.com');
75   }
76
77 }