diff --git a/core/core.services.yml b/core/core.services.yml index 0b09663..3fdbe8c 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -575,10 +575,6 @@ services: http_middleware.negotiation: class: Drupal\Core\StackMiddleware\NegotiationMiddleware arguments: ['@http_negotiation.format_negotiator'] - calls: - - [registerFormat, ['drupal_ajax', ['application/vnd.drupal-ajax']]] - - [registerFormat, ['drupal_dialog', ['application/vnd.drupal-dialog']]] - - [registerFormat, ['drupal_modal', ['application/vnd.drupal-modal']]] tags: - { name: http_middleware, priority: 400 } http_middleware.reverse_proxy: diff --git a/core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php b/core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php index 0560010..4d5189a 100644 --- a/core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php +++ b/core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php @@ -32,8 +32,8 @@ public function filter(RouteCollection $collection, Request $request) { $format = $request->getRequestFormat('html'); /** @var \Symfony\Component\Routing\Route $route */ foreach ($collection as $name => $route) { - // If it is not a match, we move the route to the end, so we effectively - // move the matching route to the front. + // If the route has no _format specification, we move it to the end. If it + // does, then no match means the route is removed entirely. if ($supported_formats = array_filter(explode('|', $route->getRequirement('_format')))) { if (!in_array($format, $supported_formats)) { $collection->remove($name); diff --git a/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php b/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php index 295857d..ca99425 100644 --- a/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php +++ b/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php @@ -34,13 +34,6 @@ class NegotiationMiddleware implements HttpKernelInterface { protected $negotiator; /** - * Contains a hashmap of format as key and mimetype as value. - * - * @var array - */ - protected $formats; - - /** * Constructs a new NegotiationMiddleware. * * @param \Symfony\Component\HttpKernel\HttpKernelInterface $app @@ -57,27 +50,9 @@ public function __construct(HttpKernelInterface $app, ContentNegotiation $negoti * {@inheritdoc} */ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) { - foreach ($this->formats as $format => $mime_type) { - $request->setFormat($format, $mime_type); - } - $request->setRequestFormat($this->negotiator->getContentType($request)); return $this->app->handle($request, $type, $catch); } - /** - * Registers a format for a given MIME type. - * - * @param string $format - * The format. - * @param string $mime_type - * The MIME type. - * - * @return $this - */ - public function registerFormat($format, $mime_type) { - $this->formats[$format] = $mime_type; - return $this; - } } diff --git a/core/modules/rest/src/Plugin/views/display/RestExport.php b/core/modules/rest/src/Plugin/views/display/RestExport.php index 5226cc9..2efdbc7 100644 --- a/core/modules/rest/src/Plugin/views/display/RestExport.php +++ b/core/modules/rest/src/Plugin/views/display/RestExport.php @@ -266,10 +266,7 @@ public function collectRoutes(RouteCollection $collection) { $requirements = array('_method' => 'GET'); // Format as a string using pipes as a delimiter. - /** @var \Drupal\rest\Plugin\views\style\Serializer $style_plugin */ - if ($formats = $style_plugin->getFormats()) { - $requirements['_format'] = implode('|', $formats + ['html']); - } + $requirements['_format'] = implode('|', $style_plugin->getFormats()); // Add the new requirements to the route. $route->addRequirements($requirements); diff --git a/core/modules/rest/src/Plugin/views/style/Serializer.php b/core/modules/rest/src/Plugin/views/style/Serializer.php index c7c700f..ba3ca6f 100644 --- a/core/modules/rest/src/Plugin/views/style/Serializer.php +++ b/core/modules/rest/src/Plugin/views/style/Serializer.php @@ -139,13 +139,18 @@ public function render() { /** * Gets a list of all available formats that can be requested. * - * This will return the configured formats. + * This will return the configured formats, or all formats if none have been + * selected. * * @return array * An array of formats. */ public function getFormats() { - return $this->options['formats']; + if (!empty($this->options['formats'])) { + return $this->options['formats']; + } + + return $this->formats; } } diff --git a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php index 181b710..beb4b50 100644 --- a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php +++ b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php @@ -76,7 +76,7 @@ public function testSerializerResponses() { $view->initDisplay(); $this->executeView($view); - $actual_json = $this->drupalGet('test/serialize/field', array('query' => ['_format' => 'json'])); + $actual_json = $this->drupalGet('test/serialize/field', ['query' => ['_format' => 'json']]); $this->assertResponse(200); $this->assertCacheTags($view->getCacheTags()); // @todo Due to https://www.drupal.org/node/2352009 we can't yet test the @@ -125,7 +125,7 @@ public function testSerializerResponses() { $expected = $serializer->serialize($entities, 'json'); - $actual_json = $this->drupalGet('test/serialize/entity', array('query' => ['_format' => 'json'])); + $actual_json = $this->drupalGet('test/serialize/entity', ['query' => ['_format' => 'json']]); $this->assertResponse(200); $this->assertIdentical($actual_json, $expected, 'The expected JSON output was found.'); $expected_cache_tags = $view->getCacheTags(); @@ -137,7 +137,7 @@ public function testSerializerResponses() { $this->assertCacheTags($expected_cache_tags); $expected = $serializer->serialize($entities, 'hal_json'); - $actual_json = $this->drupalGet('test/serialize/entity', array('query' => ['_format' => 'hal_json'])); + $actual_json = $this->drupalGet('test/serialize/entity', ['query' => ['_format' => 'hal_json']]); $this->assertIdentical($actual_json, $expected, 'The expected HAL output was found.'); $this->assertCacheTags($expected_cache_tags); @@ -171,10 +171,10 @@ public function testSerializerResponses() { )); $view->save(); $expected = $serializer->serialize($entities, 'json'); - $actual_json = $this->drupalGet('test/serialize/entity', array('query' => array('_format' => 'json'))); + $actual_json = $this->drupalGet('test/serialize/entity', ['query' => ['_format' => 'json']]); $this->assertIdentical($actual_json, $expected, 'The expected JSON output was found.'); $expected = $serializer->serialize($entities, 'xml'); - $actual_xml = $this->drupalGet('test/serialize/entity', array('query' => array('_format' => 'xml'))); + $actual_xml = $this->drupalGet('test/serialize/entity', ['query' => ['_format' => 'xml']]); $this->assertIdentical($actual_xml, $expected, 'The expected XML output was found.'); } @@ -191,10 +191,10 @@ public function testResponseFormatConfiguration() { $this->drupalPostForm(NULL, array(), t('Save')); // Should return a 406. - $this->drupalGet('test/serialize/field', array('query' => array('_format' => 'json'))); + $this->drupalGet('test/serialize/field', ['query' => ['_format' => 'json']]); $this->assertResponse(406, 'A 406 response was returned when JSON was requested.'); // Should return a 200. - $this->drupalGet('test/serialize/field', array('query' => array('_format' => 'xml'))); + $this->drupalGet('test/serialize/field', ['query' => ['_format' => 'xml']]); $this->assertResponse(200, 'A 200 response was returned when XML was requested.'); // Add 'json' as an accepted format, so we have multiple. @@ -211,13 +211,13 @@ public function testResponseFormatConfiguration() { $this->assertResponse(200, 'A 200 response was returned when a browser accept header was requested.'); // Should return a 200. - $this->drupalGet('test/serialize/field', array('query' => ['_format' => 'json'])); + $this->drupalGet('test/serialize/field', ['query' => ['_format' => 'json']]); $this->assertResponse(200, 'A 200 response was returned when JSON was requested.'); // Should return a 200. - $this->drupalGet('test/serialize/field', array('query' => ['_format' => 'xml'])); + $this->drupalGet('test/serialize/field', ['query' => ['_format' => 'xml']]); $this->assertResponse(200, 'A 200 response was returned when XML was requested'); // Should return a 406. - $this->drupalGet('test/serialize/field', array('query' => ['_format' => 'html'])); + $this->drupalGet('test/serialize/field', ['query' => ['_format' => 'html']]); $this->assertResponse(200, 'A 200 response was returned when HTML was requested.'); } diff --git a/core/modules/system/src/Tests/Routing/ContentNegotiationRoutingTest.php b/core/modules/system/src/Tests/Routing/ContentNegotiationRoutingTest.php index ccd44d1..3cddfbd 100644 --- a/core/modules/system/src/Tests/Routing/ContentNegotiationRoutingTest.php +++ b/core/modules/system/src/Tests/Routing/ContentNegotiationRoutingTest.php @@ -62,31 +62,26 @@ function testContentRouting() { // ['path', 'accept', 'content-type', 'vary'], // Extension is part of the route path. Constant Content-type. - ['conneg/simple.json', '', 'application/json', FALSE], - ['conneg/simple.json', 'application/xml', 'application/json', FALSE], - ['conneg/simple.json', 'application/json', 'application/json', FALSE], + ['conneg/simple.json', '', 'application/json'], + ['conneg/simple.json', 'application/xml', 'application/json'], + ['conneg/simple.json', 'application/json', 'application/json'], // No extension. Constant Content-type. - ['conneg/html', '', 'text/html', FALSE], - ['conneg/html', '*/*', 'text/html', FALSE], - // @todo We have to turn of full content negotiation to support this. - ['conneg/html', 'application/xml', 'text/html', FALSE], - ['conneg/html', 'text/xml', 'text/html', FALSE], - ['conneg/html', 'text/html', 'text/html', FALSE], + ['conneg/html', '', 'text/html'], + ['conneg/html', '*/*', 'text/html'], + ['conneg/html', 'application/xml', 'text/html'], + ['conneg/html', 'text/xml', 'text/html'], + ['conneg/html', 'text/html', 'text/html'], // Dynamic extension. Linked Content-type. - // @todo We have to turn of full content negotiation to support this. - ['conneg/html?_format=json', '', 'application/json', FALSE], - ['conneg/html?_format=json', '*/*', 'application/json', FALSE], - ['conneg/html?_format=json', 'application/xml', 'application/json', FALSE], - ['conneg/html?_format=json', 'application/json', 'application/json', FALSE], - // @todo We have to turn of full content negotiation to support this. - ['conneg/html?_format=xml', '', 'application/xml', FALSE], - ['conneg/html?_format=xml', '*/*', 'application/xml', FALSE], - ['conneg/html?_format=xml', 'application/json', 'application/xml', FALSE], - ['conneg/html?_format=xml', 'application/xml', 'application/xml', FALSE], - // @todo a Drupal vendor protocol? + ['conneg/html?_format=json', '', 'application/json'], + ['conneg/html?_format=json', '*/*', 'application/json'], + ['conneg/html?_format=json', 'application/xml', 'application/json'], + ['conneg/html?_format=json', 'application/json', 'application/json'], + ['conneg/html?_format=xml', '', 'application/xml'], + ['conneg/html?_format=xml', '*/*', 'application/xml'], + ['conneg/html?_format=xml', 'application/json', 'application/xml'], + ['conneg/html?_format=xml', 'application/xml', 'application/xml'], // Path with a variable. Variable contains a period. - // @todo We have to turn of full content negotiation to support these. ['conneg/plugin/plugin.id', '', 'text/html', FALSE], ['conneg/plugin/plugin.id', '*/*', 'text/html', FALSE], ['conneg/plugin/plugin.id', 'text/xml', 'text/html', FALSE], @@ -111,10 +106,13 @@ function testContentRouting() { ]; foreach ($tests as $test) { - $message = "Testing path:$test[0] Accept:$test[1] Content-type:$test[2]"; - $request = Request::create($test[0]); - if ($test[1]) { - $request->headers->set('Accept', $test[1]); + $path = $test[0]; + $accept_header = $test[1]; + $content_type = $test[2]; + $message = "Testing path:$path Accept:$accept_header Content-type:$content_type"; + $request = Request::create($path); + if ($accept_header) { + $request->headers->set('Accept', $accept_header); } /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */ @@ -124,12 +122,7 @@ function testContentRouting() { // see the error. $this->assertTrue(TRUE, $message); $this->assertEqual($response->getStatusCode(), Response::HTTP_OK); - $this->assertTrue(strpos($response->headers->get('Content-type'), $test[2]) !== FALSE); - - if ($test[3]) { - $this->assertEqual($response->headers->get('Cache-Control'), 'public'); - $this->assertEqual($response->headers->get('Vary'), 'Cookie'); - } + $this->assertTrue(strpos($response->headers->get('Content-type'), $content_type) !== FALSE); } } @@ -152,16 +145,19 @@ public function testFullNegotiation() { ]; foreach ($tests as $test) { - $message = "Testing path:$test[0] Accept:$test[1] Content-type:$test[2]"; - $request = Request::create($test[0]); - $request->headers->set('Accept', $test[1]); + $path = $test[0]; + $accept_header = $test[1]; + $content_type = $test[2]; + $message = "Testing path:$path Accept:$accept_header Content-type:$content_type"; + $request = Request::create($path); + $request->headers->set('Accept', $accept_header); /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */ $kernel = \Drupal::getContainer()->get('http_kernel'); $response = $kernel->handle($request); // Verbose message since simpletest doesn't let us provide a message and // see the error. - $this->assertTrue(TRUE, $message); + $this->pass($message); $this->assertEqual($response->getStatusCode(), Response::HTTP_OK); } } diff --git a/core/modules/system/tests/modules/conneg_test/conneg_test.routing.yml b/core/modules/system/tests/modules/conneg_test/conneg_test.routing.yml index a660d09..bedd52e 100644 --- a/core/modules/system/tests/modules/conneg_test/conneg_test.routing.yml +++ b/core/modules/system/tests/modules/conneg_test/conneg_test.routing.yml @@ -2,31 +2,31 @@ conneg.simpletest: path: conneg/simple.json defaults: - _controller: '\Drupal\conneg_test\Controller\Test::simple' + _controller: '\Drupal\conneg_test\Controller\TestController::simple' requirements: _access: 'TRUE' conneg.html: path: conneg/html defaults: - _controller: '\Drupal\conneg_test\Controller\Test::html' + _controller: '\Drupal\conneg_test\Controller\TestController::html' requirements: _access: 'TRUE' conneg.simple_conneg: path: conneg/html defaults: - _controller: '\Drupal\conneg_test\Controller\Test::format' + _controller: '\Drupal\conneg_test\Controller\TestController::format' requirements: _access: 'TRUE' _format: 'json|xml' conneg.variable_with_period: path: conneg/plugin/{plugin_id} defaults: - _controller: '\Drupal\conneg_test\Controller\Test::variable' + _controller: '\Drupal\conneg_test\Controller\TestController::variable' requirements: _access: 'TRUE' conneg.full_content_negotiation: path: conneg/negotiate defaults: - _controller: '\Drupal\conneg_test\Controller\Test::format' + _controller: '\Drupal\conneg_test\Controller\TestController::format' requirements: _access: 'TRUE' diff --git a/core/modules/system/tests/modules/conneg_test/src/Controller/TestController.php b/core/modules/system/tests/modules/conneg_test/src/Controller/TestController.php index 445f820..7d52480 100644 --- a/core/modules/system/tests/modules/conneg_test/src/Controller/TestController.php +++ b/core/modules/system/tests/modules/conneg_test/src/Controller/TestController.php @@ -11,7 +11,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -class Test { +class TestController { public function simple() { return new JsonResponse(['some' => 'data']);