Commit 51d3d8a9 authored by alexpott's avatar alexpott

Issue #2805281 by Wim Leers, damiankloip: ?_format=hal_json error responses...

Issue #2805281 by Wim Leers, damiankloip: ?_format=hal_json error responses are application/json, yet should be application/hal+json
parent 0f613741
......@@ -26,7 +26,3 @@ services:
class: Drupal\hal\Encoder\JsonEncoder
tags:
- { name: encoder, priority: 10, format: hal_json }
exception.default_json:
class: Drupal\hal\EventSubscriber\ExceptionHalJsonSubscriber
tags:
- { name: event_subscriber }
<?php
namespace Drupal\hal\EventSubscriber;
use Drupal\Core\EventSubscriber\ExceptionJsonSubscriber;
/**
* Handle HAL JSON exceptions the same as JSON exceptions.
*/
class ExceptionHalJsonSubscriber extends ExceptionJsonSubscriber {
/**
* {@inheritdoc}
*/
protected function getHandledFormats() {
return ['hal_json'];
}
}
......@@ -27,9 +27,4 @@ class BlockHalJsonAnonTest extends BlockResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
}
......@@ -27,11 +27,6 @@ class BlockHalJsonBasicAuthTest extends BlockResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -27,11 +27,6 @@ class BlockHalJsonCookieTest extends BlockResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -27,11 +27,6 @@ abstract class CommentHalJsonTestBase extends CommentResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*
......
......@@ -27,9 +27,4 @@ class ConfigTestHalJsonAnonTest extends ConfigTestResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
}
......@@ -27,11 +27,6 @@ class ConfigTestHalJsonBasicAuthTest extends ConfigTestResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -27,11 +27,6 @@ class ConfigTestHalJsonCookieTest extends ConfigTestResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -31,11 +31,6 @@ class EntityTestHalJsonAnonTest extends EntityTestResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -31,11 +31,6 @@ class NodeHalJsonAnonTest extends NodeResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -27,9 +27,4 @@ class RoleHalJsonAnonTest extends RoleResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
}
......@@ -27,11 +27,6 @@ class RoleHalJsonBasicAuthTest extends RoleResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -27,11 +27,6 @@ class RoleHalJsonCookieTest extends RoleResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -30,11 +30,6 @@ class TermHalJsonAnonTest extends TermResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -30,11 +30,6 @@ class UserHalJsonAnonTest extends UserResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -27,11 +27,6 @@ class VocabularyHalJsonAnonTest extends VocabularyResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* @todo Remove this override in https://www.drupal.org/node/2805281.
*/
......
......@@ -27,11 +27,6 @@ class VocabularyHalJsonBasicAuthTest extends VocabularyResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -27,11 +27,6 @@ class VocabularyHalJsonCookieTest extends VocabularyResourceTestBase {
*/
protected static $mimeType = 'application/hal+json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,9 +21,4 @@ class BlockJsonAnonTest extends BlockResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
}
......@@ -26,11 +26,6 @@ class BlockJsonBasicAuthTest extends BlockResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,11 +21,6 @@ class BlockJsonCookieTest extends BlockResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,11 +21,6 @@ class CommentJsonAnonTest extends CommentResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*
......
......@@ -26,11 +26,6 @@ class CommentJsonBasicAuthTest extends CommentResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,11 +21,6 @@ class CommentJsonCookieTest extends CommentResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -278,8 +278,10 @@ public function testPostDxWithoutCriticalBaseFields() {
// DX: 422 when missing 'entity_type' field.
$request_options[RequestOptions::BODY] = $this->serializer->encode(array_diff_key($this->getNormalizedPostEntity(), ['entity_type' => TRUE]), static::$format);
$response = $this->request('POST', $url, $request_options);
// @todo Uncomment, remove next line in https://www.drupal.org/node/2820364.
$this->assertResourceErrorResponse(500, 'A fatal error occurred: Internal Server Error', $response);
// @todo Uncomment, remove next 3 lines in https://www.drupal.org/node/2820364.
$this->assertSame(500, $response->getStatusCode());
$this->assertSame(['application/json'], $response->getHeader('Content-Type'));
$this->assertSame('{"message":"A fatal error occurred: Internal Server Error"}', (string) $response->getBody());
//$this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nentity_type: This value should not be null.\n", $response);
// DX: 422 when missing 'entity_id' field.
......@@ -301,8 +303,10 @@ public function testPostDxWithoutCriticalBaseFields() {
// DX: 422 when missing 'entity_type' field.
$request_options[RequestOptions::BODY] = $this->serializer->encode(array_diff_key($this->getNormalizedPostEntity(), ['field_name' => TRUE]), static::$format);
$response = $this->request('POST', $url, $request_options);
// @todo Uncomment, remove next line in https://www.drupal.org/node/2820364.
$this->assertResourceErrorResponse(500, 'A fatal error occurred: Field is unknown.', $response);
// @todo Uncomment, remove next 3 lines in https://www.drupal.org/node/2820364.
$this->assertSame(500, $response->getStatusCode());
$this->assertSame(['application/json'], $response->getHeader('Content-Type'));
$this->assertSame('{"message":"A fatal error occurred: Field is unknown."}', (string) $response->getBody());
//$this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nfield_name: This value should not be null.\n", $response);
}
......
......@@ -21,9 +21,4 @@ class ConfigTestJsonAnonTest extends ConfigTestResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
}
......@@ -26,11 +26,6 @@ class ConfigTestJsonBasicAuthTest extends ConfigTestResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,11 +21,6 @@ class ConfigTestJsonCookieTest extends ConfigTestResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -43,10 +43,9 @@
* (permissions or perhaps custom access control handling, such as node
* grants), plus
* 2. a concrete subclass extending the abstract entity type-specific subclass
* that specifies the exact @code $format @endcode, @code $mimeType @endcode,
* @code $expectedErrorMimeType @endcode and @code $auth @endcode for this
* concrete test. Usually that's all that's necessary: most concrete
* subclasses will be very thin.
* that specifies the exact @code $format @endcode, @code $mimeType @endcode
* and @code $auth @endcode for this concrete test. Usually that's all that's
* necessary: most concrete subclasses will be very thin.
*
* For every of these concrete subclasses, a comprehensive test scenario will
* run per HTTP method:
......@@ -420,7 +419,7 @@ public function testGet() {
// DX: 406 when requesting unsupported format.
$response = $this->request('GET', $url, $request_options);
$this->assert406Response($response);
$this->assertNotSame([static::$expectedErrorMimeType], $response->getHeader('Content-Type'));
$this->assertNotSame([static::$mimeType], $response->getHeader('Content-Type'));
$request_options[RequestOptions::HEADERS]['Accept'] = static::$mimeType;
......@@ -430,7 +429,7 @@ public function testGet() {
// @todo Update in https://www.drupal.org/node/2825347.
$response = $this->request('GET', $url, $request_options);
$this->assert406Response($response);
$this->assertSame([static::$expectedErrorMimeType], $response->getHeader('Content-Type'));
$this->assertSame(['application/json'], $response->getHeader('Content-Type'));
$url = Url::fromRoute('rest.entity.' . static::$entityTypeId . '.GET.' . static::$format);
......
......@@ -21,9 +21,4 @@ class EntityTestJsonAnonTest extends EntityTestResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
}
......@@ -26,11 +26,6 @@ class EntityTestJsonBasicAuthTest extends EntityTestResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,11 +21,6 @@ class EntityTestJsonCookieTest extends EntityTestResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,9 +21,4 @@ class NodeJsonAnonTest extends NodeResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
}
......@@ -26,11 +26,6 @@ class NodeJsonBasicAuthTest extends NodeResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,11 +21,6 @@ class NodeJsonCookieTest extends NodeResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,9 +21,4 @@ class RoleJsonAnonTest extends RoleResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
}
......@@ -26,11 +26,6 @@ class RoleJsonBasicAuthTest extends RoleResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,11 +21,6 @@ class RoleJsonCookieTest extends RoleResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,9 +21,4 @@ class TermJsonAnonTest extends TermResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
}
......@@ -26,11 +26,6 @@ class TermJsonBasicAuthTest extends TermResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,11 +21,6 @@ class TermJsonCookieTest extends TermResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,9 +21,4 @@ class UserJsonAnonTest extends UserResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
}
......@@ -26,11 +26,6 @@ class UserJsonBasicAuthTest extends UserResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,11 +21,6 @@ class UserJsonCookieTest extends UserResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,11 +21,6 @@ class VocabularyJsonAnonTest extends VocabularyResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* Disable the GET test coverage due to bug in taxonomy module.
* @todo Fix in https://www.drupal.org/node/2805281: remove this override.
......
......@@ -26,11 +26,6 @@ class VocabularyJsonBasicAuthTest extends VocabularyResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -21,11 +21,6 @@ class VocabularyJsonCookieTest extends VocabularyResourceTestBase {
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* {@inheritdoc}
*/
......
......@@ -46,17 +46,6 @@ abstract class ResourceTestBase extends BrowserTestBase {
*/
protected static $mimeType = 'application/json';
/**
* The expected MIME type in case of 4xx error responses.
*
* (Can be different, when $mimeType for example encodes a particular
* normalization, such as 'application/hal+json': its error response MIME
* type is 'application/json'.)
*
* @var string
*/
protected static $expectedErrorMimeType = 'application/json';
/**
* The authentication mechanism to use in this test.
*
......@@ -301,10 +290,6 @@ protected function request($method, Url $url, array $request_options) {
/**
* Asserts that a resource response has the given status code and body.
*
* (Also asserts that the expected error MIME type is present, but this is
* defined globally for the test via static::$expectedErrorMimeType, because
* all error responses should use the same MIME type.)
*
* @param int $expected_status_code
* The expected response status.
* @param string|false $expected_body
......@@ -318,7 +303,7 @@ protected function assertResourceResponse($expected_status_code, $expected_body,
$this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
}
else {
$this->assertSame([static::$expectedErrorMimeType], $response->getHeader('Content-Type'));
$this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
}
if ($expected_body !== FALSE) {
$this->assertSame($expected_body, (string) $response->getBody());
......@@ -328,10 +313,6 @@ protected function assertResourceResponse($expected_status_code, $expected_body,
/**
* Asserts that a resource error response has the given message.
*
* (Also asserts that the expected error MIME type is present, but this is
* defined globally for the test via static::$expectedErrorMimeType, because
* all error responses should use the same MIME type.)
*
* @param int $expected_status_code
* The expected response status.
* @param string $expected_message
......
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