3 namespace Drupal\Tests\Core\EventSubscriber;
5 use Drupal\Core\EventSubscriber\OptionsRequestSubscriber;
6 use Symfony\Cmf\Component\Routing\RouteProviderInterface;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
9 use Symfony\Component\HttpKernel\HttpKernelInterface;
10 use Symfony\Component\Routing\Route;
11 use Symfony\Component\Routing\RouteCollection;
14 * @coversDefaultClass \Drupal\Core\EventSubscriber\OptionsRequestSubscriber
15 * @group EventSubscriber
17 class OptionsRequestSubscriberTest extends \PHPUnit_Framework_TestCase {
22 public function testWithNonOptionRequest() {
23 $kernel = $this->prophesize(HttpKernelInterface::class);
24 $request = Request::create('/example', 'GET');
26 $route_provider = $this->prophesize(RouteProviderInterface::class);
27 $route_provider->getRouteCollectionForRequest($request)->shouldNotBeCalled();
29 $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
30 $event = new GetResponseEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
31 $subscriber->onRequest($event);
33 $this->assertFalse($event->hasResponse());
39 public function testWithoutMatchingRoutes() {
40 $kernel = $this->prophesize(HttpKernelInterface::class);
41 $request = Request::create('/example', 'OPTIONS');
43 $route_provider = $this->prophesize(RouteProviderInterface::class);
44 $route_provider->getRouteCollectionForRequest($request)->willReturn(new RouteCollection())->shouldBeCalled();
46 $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
47 $event = new GetResponseEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
48 $subscriber->onRequest($event);
50 $this->assertFalse($event->hasResponse());
55 * @dataProvider providerTestOnRequestWithOptionsRequest
57 public function testWithOptionsRequest(RouteCollection $collection, $expected_header) {
58 $kernel = $this->prophesize(HttpKernelInterface::class);
59 $request = Request::create('/example', 'OPTIONS');
61 $route_provider = $this->prophesize(RouteProviderInterface::class);
62 $route_provider->getRouteCollectionForRequest($request)->willReturn($collection)->shouldBeCalled();
64 $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
65 $event = new GetResponseEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
66 $subscriber->onRequest($event);
68 $this->assertTrue($event->hasResponse());
69 $response = $event->getResponse();
70 $this->assertEquals(200, $response->getStatusCode());
71 $this->assertEquals($expected_header, $response->headers->get('Allow'));
74 public function providerTestOnRequestWithOptionsRequest() {
77 foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method) {
78 $collection = new RouteCollection();
79 $collection->add('example.1', new Route('/example', [], [], [], '', [], [$method]));
80 $data['one_route_' . $method] = [$collection, $method];
83 foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_a) {
84 foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_b) {
85 if ($method_a != $method_b) {
86 $collection = new RouteCollection();
87 $collection->add('example.1', new Route('/example', [], [], [], '', [], [$method_a, $method_b]));
88 $data['one_route_' . $method_a . '_' . $method_b] = [$collection, $method_a . ', ' . $method_b];
93 foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_a) {
94 foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_b) {
95 foreach (['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] as $method_c) {
96 $collection = new RouteCollection();
97 $collection->add('example.1', new Route('/example', [], [], [], '', [], [$method_a]));
98 $collection->add('example.2', new Route('/example', [], [], [], '', [], [$method_a, $method_b]));
99 $collection->add('example.3', new Route('/example', [], [], [], '', [], [$method_b, $method_c]));
100 $methods = array_unique([$method_a, $method_b, $method_c]);
101 $data['multiple_routes_' . $method_a . '_' . $method_b . '_' . $method_c] = [$collection, implode(', ', $methods)];