diff --git a/core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php b/core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php index d09fdced4c..ea93d78988 100644 --- a/core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php +++ b/core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php @@ -92,8 +92,19 @@ public function testResponseFormat($methods, array $supported_formats, $request_ if ($request_format) { $request->setRequestFormat($request_format); } - $route_requirement_key_format = $request->isMethodCacheable() ? '_format' : '_content_type_format'; - $route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => $this->randomMachineName()], [$route_requirement_key_format => implode('|', $supported_formats)])); + + // Add the supported formats as allowed response formats so requests can + // receive responses in a different format. + $supported_format_string = implode('|', $supported_formats); + $route_requirements = [ + '_format' => $supported_format_string, + ]; + + if (!$request->isMethodCacheable()) { + $route_requirements['_content_type_format'] = $supported_format_string; + } + + $route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => $this->randomMachineName()], $route_requirements)); $resource_response_subscriber = new ResourceResponseSubscriber( $this->prophesize(SerializerInterface::class)->reveal(), @@ -129,8 +140,19 @@ public function testOnResponseWithCacheableResponse($methods, array $supported_f if ($request_format) { $request->setRequestFormat($request_format); } - $route_requirement_key_format = $request->isMethodCacheable() ? '_format' : '_content_type_format'; - $route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => $rest_config_name], [$route_requirement_key_format => implode('|', $supported_formats)])); + + // Add the supported formats as allowed response formats so requests can + // receive responses in a different format. + $supported_format_string = implode('|', $supported_formats); + $route_requirements = [ + '_format' => $supported_format_string, + ]; + + if (!$request->isMethodCacheable()) { + $route_requirements['_content_type_format'] = $supported_format_string; + } + + $route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => $this->randomMachineName()], $route_requirements)); // The RequestHandler must return a ResourceResponseInterface object. $handler_response = new ResourceResponse($method !== 'DELETE' ? ['REST' => 'Drupal'] : NULL); @@ -179,8 +201,19 @@ public function testOnResponseWithUncacheableResponse($methods, array $supported if ($request_format) { $request->setRequestFormat($request_format); } - $route_requirement_key_format = $request->isMethodCacheable() ? '_format' : '_content_type_format'; - $route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => $rest_config_name], [$route_requirement_key_format => implode('|', $supported_formats)])); + + // Add the supported formats as allowed response formats so requests can + // receive responses in a different format. + $supported_format_string = implode('|', $supported_formats); + $route_requirements = [ + '_format' => $supported_format_string, + ]; + + if (!$request->isMethodCacheable()) { + $route_requirements['_content_type_format'] = $supported_format_string; + } + + $route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => $this->randomMachineName()], $route_requirements)); // The RequestHandler must return a ResourceResponseInterface object. $handler_response = new ModifiedResourceResponse($method !== 'DELETE' ? ['REST' => 'Drupal'] : NULL);