3 namespace Drupal\Tests\Core\EventSubscriber;
5 use Drupal\Core\EventSubscriber\OptionsRequestSubscriber;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Cmf\Component\Routing\RouteProviderInterface;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10 use Symfony\Component\HttpKernel\HttpKernelInterface;
11 use Symfony\Component\Routing\Route;
12 use Symfony\Component\Routing\RouteCollection;
15 * @coversDefaultClass \Drupal\Core\EventSubscriber\OptionsRequestSubscriber
16 * @group EventSubscriber
18 class OptionsRequestSubscriberTest extends UnitTestCase {
23 public function testWithNonOptionRequest() {
24 $kernel = $this->prophesize(HttpKernelInterface::class);
25 $request = Request::create('/example', 'GET');
27 $route_provider = $this->prophesize(RouteProviderInterface::class);
28 $route_provider->getRouteCollectionForRequest($request)->shouldNotBeCalled();
30 $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
31 $event = new GetResponseEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
32 $subscriber->onRequest($event);
34 $this->assertFalse($event->hasResponse());
40 public function testWithoutMatchingRoutes() {
41 $kernel = $this->prophesize(HttpKernelInterface::class);
42 $request = Request::create('/example', 'OPTIONS');
44 $route_provider = $this->prophesize(RouteProviderInterface::class);
45 $route_provider->getRouteCollectionForRequest($request)->willReturn(new RouteCollection())->shouldBeCalled();
47 $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
48 $event = new GetResponseEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
49 $subscriber->onRequest($event);
51 $this->assertFalse($event->hasResponse());
56 * @dataProvider providerTestOnRequestWithOptionsRequest
58 public function testWithOptionsRequest(RouteCollection $collection, $expected_header) {
59 $kernel = $this->prophesize(HttpKernelInterface::class);
60 $request = Request::create('/example', 'OPTIONS');
62 $route_provider = $this->prophesize(RouteProviderInterface::class);
63 $route_provider->getRouteCollectionForRequest($request)->willReturn($collection)->shouldBeCalled();
65 $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
66 $event = new GetResponseEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
67 $subscriber->onRequest($event);
69 $this->assertTrue($event->hasResponse());
70 $response = $event->getResponse();
71 $this->assertEquals(200, $response->getStatusCode());
72 $this->assertEquals($expected_header, $response->headers->get('Allow'));
75 public function providerTestOnRequestWithOptionsRequest() {
78 foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method) {
79 $collection = new RouteCollection();
80 $collection->add('example.1', new Route('/example', [], [], [], '', [], [$method]));
81 $data['one_route_' . $method] = [$collection, $method];
84 foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_a) {
85 foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_b) {
86 if ($method_a != $method_b) {
87 $collection = new RouteCollection();
88 $collection->add('example.1', new Route('/example', [], [], [], '', [], [$method_a, $method_b]));
89 $data['one_route_' . $method_a . '_' . $method_b] = [$collection, $method_a . ', ' . $method_b];
94 foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_a) {
95 foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_b) {
96 foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_c) {
97 $collection = new RouteCollection();
98 $collection->add('example.1', new Route('/example', [], [], [], '', [], [$method_a]));
99 $collection->add('example.2', new Route('/example', [], [], [], '', [], [$method_a, $method_b]));
100 $collection->add('example.3', new Route('/example', [], [], [], '', [], [$method_b, $method_c]));
101 $methods = array_unique([$method_a, $method_b, $method_c]);
102 $data['multiple_routes_' . $method_a . '_' . $method_b . '_' . $method_c] = [$collection, implode(', ', $methods)];