CorsIntegrationTest.php 3.5 KB

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