Commit 22d18686 authored by alexpott's avatar alexpott

Issue #2775479 by Wim Leers, klausi, dawehner: Try to remove the "map HEAD to...

Issue #2775479 by Wim Leers, klausi, dawehner: Try to remove the "map HEAD to GET" logic in \Drupal\rest\RequestHandler::handle()
parent 2736deb8
...@@ -61,22 +61,20 @@ public static function create(ContainerInterface $container) { ...@@ -61,22 +61,20 @@ public static function create(ContainerInterface $container) {
* The response object. * The response object.
*/ */
public function handle(RouteMatchInterface $route_match, Request $request) { public function handle(RouteMatchInterface $route_match, Request $request) {
$method = strtolower($request->getMethod());
// Symfony is built to transparently map HEAD requests to a GET request. In // Symfony is built to transparently map HEAD requests to a GET request. In
// the case of the REST module's RequestHandler though, we essentially have // the case of the REST module's RequestHandler though, we essentially have
// our own light-weight routing system on top of the Drupal/symfony routing // our own light-weight routing system on top of the Drupal/symfony routing
// system. So, we have to do the same as what the UrlMatcher does: map HEAD // system. So, we have to respect the decision that the routing system made:
// requests to the logic for GET. This also guarantees response headers for // we look not at the request method, but at the route's method. All REST
// HEAD requests are identical to those for GET requests, because we just // routes are guaranteed to have _method set.
// return a GET response. Response::prepare() will transform it to a HEAD // Response::prepare() will transform it to a HEAD response at the very last
// response at the very last moment. // moment.
// @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 // @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4
// @see \Symfony\Component\Routing\Matcher\UrlMatcher::matchCollection() // @see \Symfony\Component\Routing\Matcher\UrlMatcher::matchCollection()
// @see \Symfony\Component\HttpFoundation\Response::prepare() // @see \Symfony\Component\HttpFoundation\Response::prepare()
if ($method === 'head') { $method = strtolower($route_match->getRouteObject()->getMethods()[0]);
$method = 'get'; assert(count($route_match->getRouteObject()->getMethods()) === 1);
}
$resource_config_id = $route_match->getRouteObject()->getDefault('_rest_resource_config'); $resource_config_id = $route_match->getRouteObject()->getDefault('_rest_resource_config');
/** @var \Drupal\rest\RestResourceConfigInterface $resource_config */ /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */
......
...@@ -49,7 +49,7 @@ public function setUp() { ...@@ -49,7 +49,7 @@ public function setUp() {
*/ */
public function testHandle() { public function testHandle() {
$request = new Request(); $request = new Request();
$route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => 'restplugin'], ['_format' => 'json'])); $route_match = new RouteMatch('test', (new Route('/rest/test', ['_rest_resource_config' => 'restplugin'], ['_format' => 'json']))->setMethods(['GET']));
$resource = $this->prophesize(StubRequestHandlerResourcePlugin::class); $resource = $this->prophesize(StubRequestHandlerResourcePlugin::class);
$resource->get(NULL, $request) $resource->get(NULL, $request)
...@@ -76,7 +76,7 @@ public function testHandle() { ...@@ -76,7 +76,7 @@ public function testHandle() {
$this->assertEquals($response, $handler_response); $this->assertEquals($response, $handler_response);
// We will call the patch method this time. // We will call the patch method this time.
$route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => 'restplugin'], ['_content_type_format' => 'json'])); $route_match = new RouteMatch('test', (new Route('/rest/test', ['_rest_resource_config' => 'restplugin'], ['_content_type_format' => 'json']))->setMethods(['PATCH']));
$request->setMethod('PATCH'); $request->setMethod('PATCH');
$response = new ResourceResponse([]); $response = new ResourceResponse([]);
$resource->patch(NULL, $request) $resource->patch(NULL, $request)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment