*
* @dataProvider providerTestResponseFormat
*/
- public function testResponseFormat($methods, array $supported_formats, $request_format, array $request_headers, $request_body, $expected_response_format, $expected_response_content_type, $expected_response_content) {
+ public function testResponseFormat($methods, array $supported_response_formats, array $supported_request_formats, $request_format, array $request_headers, $request_body, $expected_response_format, $expected_response_content_type, $expected_response_content) {
foreach ($request_headers as $key => $value) {
unset($request_headers[$key]);
$key = strtoupper(str_replace('-', '_', $key));
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)]));
+
+ $route_requirements = $this->generateRouteRequirements($supported_response_formats, $supported_request_formats);
+
+ $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(),
*
* @dataProvider providerTestResponseFormat
*/
- public function testOnResponseWithCacheableResponse($methods, array $supported_formats, $request_format, array $request_headers, $request_body, $expected_response_format, $expected_response_content_type, $expected_response_content) {
- $rest_config_name = $this->randomMachineName();
-
+ public function testOnResponseWithCacheableResponse($methods, array $supported_response_formats, array $supported_request_formats, $request_format, array $request_headers, $request_body, $expected_response_format, $expected_response_content_type, $expected_response_content) {
foreach ($request_headers as $key => $value) {
unset($request_headers[$key]);
$key = strtoupper(str_replace('-', '_', $key));
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)]));
+
+ $route_requirements = $this->generateRouteRequirements($supported_response_formats, $supported_request_formats);
+
+ $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);
*
* @dataProvider providerTestResponseFormat
*/
- public function testOnResponseWithUncacheableResponse($methods, array $supported_formats, $request_format, array $request_headers, $request_body, $expected_response_format, $expected_response_content_type, $expected_response_content) {
- $rest_config_name = $this->randomMachineName();
-
+ public function testOnResponseWithUncacheableResponse($methods, array $supported_response_formats, array $supported_request_formats, $request_format, array $request_headers, $request_body, $expected_response_format, $expected_response_content_type, $expected_response_content) {
foreach ($request_headers as $key => $value) {
unset($request_headers[$key]);
$key = strtoupper(str_replace('-', '_', $key));
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)]));
+
+ $route_requirements = $this->generateRouteRequirements($supported_response_formats, $supported_request_formats);
+
+ $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);
// @todo add 'HEAD' in https://www.drupal.org/node/2752325
['GET'],
['xml', 'json'],
+ [],
'json',
[],
NULL,
// @todo add 'HEAD' in https://www.drupal.org/node/2752325
['GET'],
['xml', 'json'],
+ [],
'xml',
[],
NULL,
// @todo add 'HEAD' in https://www.drupal.org/node/2752325
['GET'],
['json', 'xml'],
+ [],
FALSE,
[],
NULL,
// @todo add 'HEAD' in https://www.drupal.org/node/2752325
['GET'],
['xml', 'json'],
+ [],
FALSE,
[],
NULL,
'unsafe methods with response (POST, PATCH): client requested no format, response should use request body format (JSON)' => [
['POST', 'PATCH'],
['xml', 'json'],
+ ['xml', 'json'],
FALSE,
['Content-Type' => 'application/json'],
$json_encoded,
'unsafe methods with response (POST, PATCH): client requested no format, response should use request body format (XML)' => [
['POST', 'PATCH'],
['xml', 'json'],
+ ['xml', 'json'],
FALSE,
['Content-Type' => 'text/xml'],
$xml_encoded,
'unsafe methods with response (POST, PATCH): client requested format other than request body format (JSON): response format should use requested format (XML)' => [
['POST', 'PATCH'],
['xml', 'json'],
+ ['xml', 'json'],
'xml',
['Content-Type' => 'application/json'],
$json_encoded,
'unsafe methods with response (POST, PATCH): client requested format other than request body format (XML), but is allowed for the request body (JSON)' => [
['POST', 'PATCH'],
['xml', 'json'],
+ ['xml', 'json'],
'json',
['Content-Type' => 'text/xml'],
$xml_encoded,
'application/json',
$json_encoded,
],
+ 'unsafe methods with response (POST, PATCH): client requested format other than request body format when only XML is allowed as a content type format' => [
+ ['POST', 'PATCH'],
+ ['xml'],
+ ['json'],
+ 'json',
+ ['Content-Type' => 'text/xml'],
+ $xml_encoded,
+ 'json',
+ 'application/json',
+ $json_encoded,
+ ],
+ 'unsafe methods with response (POST, PATCH): client requested format other than request body format when only JSON is allowed as a content type format' => [
+ ['POST', 'PATCH'],
+ ['json'],
+ ['xml'],
+ 'xml',
+ ['Content-Type' => 'application/json'],
+ $json_encoded,
+ 'xml',
+ 'text/xml',
+ $xml_encoded,
+ ],
];
$unsafe_method_bodyless_test_cases = [
- 'unsafe methods with response bodies (DELETE): client requested no format, response should have no format' => [
+ 'unsafe methods without response bodies (DELETE): client requested no format, response should have no format' => [
['DELETE'],
['xml', 'json'],
+ ['xml', 'json'],
FALSE,
['Content-Type' => 'application/json'],
NULL,
NULL,
'',
],
- 'unsafe methods with response bodies (DELETE): client requested format (XML), response should have no format' => [
+ 'unsafe methods without response bodies (DELETE): client requested format (XML), response should have no format' => [
['DELETE'],
['xml', 'json'],
+ ['xml', 'json'],
'xml',
['Content-Type' => 'application/json'],
NULL,
NULL,
'',
],
- 'unsafe methods with response bodies (DELETE): client requested format (JSON), response should have no format' => [
+ 'unsafe methods without response bodies (DELETE): client requested format (JSON), response should have no format' => [
['DELETE'],
['xml', 'json'],
+ ['xml', 'json'],
'json',
['Content-Type' => 'application/json'],
NULL,
return $resource_response_subscriber;
}
+ /**
+ * Generates route requirements based on supported formats.
+ *
+ * @param array $supported_response_formats
+ * The supported response formats to add to the route requirements.
+ * @param array $supported_request_formats
+ * The supported request formats to add to the route requirements.
+ *
+ * @return array
+ * An array of route requirements.
+ */
+ protected function generateRouteRequirements(array $supported_response_formats, array $supported_request_formats) {
+ $route_requirements = [
+ '_format' => implode('|', $supported_response_formats),
+ ];
+ if (!empty($supported_request_formats)) {
+ $route_requirements['_content_type_format'] = implode('|', $supported_request_formats);
+ }
+
+ return $route_requirements;
+ }
+
}