You are here

public function CorsResponseEventSubscriberTest::testAddCorsHeaders in CORS 8

Tests adding CORS headers to the response.

File

tests/src/Unit/EventSubscriber/CorsResponseEventSubscriberTest.php, line 36
Contains Drupal\Tests\cors\Unit\EventSubscriber\CorsResponseEventSubscriberTest.

Class

CorsResponseEventSubscriberTest

Namespace

Drupal\Tests\cors\Unit\EventSubscriber

Code

public function testAddCorsHeaders() {
  $config_factory = $this
    ->getConfigFactoryStub(array(
    "cors.settings" => array(
      "domains" => array(
        "* | http://example.com",
      ),
    ),
  ));
  $alias_manager = $this
    ->getMock('Drupal\\Core\\Path\\AliasManagerInterface');
  $path_matcher = $this
    ->getMock('Drupal\\Core\\Path\\PathMatcherInterface');
  $path_matcher
    ->expects($this
    ->any())
    ->method('matchPath')
    ->withAnyParameters()
    ->will($this
    ->returnValue(TRUE));

  // Create the response event subscriber.
  $subscriber = new CorsResponseEventSubscriber($config_factory, $alias_manager, $path_matcher);

  // Create the response event.
  $http_kernel = $this
    ->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
  $request = new Request();
  $response = new Response();
  $event = new FilterResponseEvent($http_kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response);

  // Call the event handler.
  $subscriber
    ->addCorsHeaders($event);
  $this
    ->assertEquals('*', $response->headers
    ->get('access-control-allow-origin'), "The access-control-allow-origin header was set");
}