.../EntityResource/EntityResourceTestBase.php | 36 +++++++++++++++++++--- .../rest/tests/src/Functional/ResourceTestBase.php | 4 +-- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index eea476f..15690d6 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -414,15 +414,14 @@ public function testGet() { // DX: 406 when requesting unsupported format. $response = $this->request('GET', $url, $request_options); $this->assert406Response($response); - // @todo this works fine locally, but on testbot it comes back with - // 'text/plain; charset=UTF-8'. WTF. -// $this->assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type')); + $this->assertNotSame([static::$expectedErrorMimeType], $response->getHeader('Content-Type')); $request_options[RequestOptions::HEADERS]['Accept'] = static::$mimeType; // DX: 406 when requesting unsupported format but specifying Accept header. + // @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')); @@ -460,7 +459,7 @@ public function testPost() { $parseable_invalid_request_body = $this->serializer->encode($this->makeNormalizationInvalid($this->getNormalizedPostEntity()), static::$format); // @todo Change to ['uuid' => UUID] in https://www.drupal.org/node/2820743. $parseable_invalid_request_body_2 = $this->serializer->encode($this->getNormalizedPostEntity() + ['uuid' => [['value' => $this->randomMachineName(129)]]], static::$format); - $parseable_invalid_request_body_3 = $this->serializer->encode($this->getNormalizedPostEntity() + ['field_rest_test' => [['value' => 'no access value']]], static::$format); + $parseable_invalid_request_body_3 = $this->serializer->encode($this->getNormalizedPostEntity() + ['field_rest_test' => [['value' => $this->randomString()]]], static::$format); // The URL and Guzzle request options that will be used in this test. The // request options will be modified/expanded throughout this test: @@ -599,6 +598,21 @@ public function testPost() { $this->assertNormalizationEdgeCases('POST', $url, $request_options); $this->assertAuthenticationEdgeCases('POST', $url, $request_options); + + $request_options[RequestOptions::HEADERS]['Content-Type'] = 'text/xml'; + + + // DX: 415 when request body in existing but not allowed format. + $response = $this->request('POST', $url, $request_options); + // @todo Update this in https://www.drupal.org/node/2826407. Also move it + // higher, before the "no request body" test. That's impossible right now, + // because the format validation happens too late. + $this->assertResourceErrorResponse(415, '', $response); + + + $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType; + + // 201 for well-formed request. $response = $this->request('POST', $url, $request_options); $this->assertResourceResponse(201, FALSE, $response); @@ -793,6 +807,20 @@ public function testPatch() { $this->assertAuthenticationEdgeCases('PATCH', $url, $request_options); + $request_options[RequestOptions::HEADERS]['Content-Type'] = 'text/xml'; + + + // DX: 415 when request body in existing but not allowed format. + $response = $this->request('PATCH', $url, $request_options); + // @todo Update this in https://www.drupal.org/node/2826407. Also move it + // higher, before the "no request body" test. That's impossible right now, + // because the format validation happens too late. + $this->assertResourceErrorResponse(415, '', $response); + + + $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType; + + // 200 for well-formed request. $response = $this->request('PATCH', $url, $request_options); $this->assertResourceResponse(200, FALSE, $response); diff --git a/core/modules/rest/tests/src/Functional/ResourceTestBase.php b/core/modules/rest/tests/src/Functional/ResourceTestBase.php index 6fb0fa5..4822fa1 100644 --- a/core/modules/rest/tests/src/Functional/ResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/ResourceTestBase.php @@ -344,9 +344,7 @@ protected function assertResourceResponse($expected_status_code, $expected_body, * The error response to assert. */ protected function assertResourceErrorResponse($expected_status_code, $expected_message, ResponseInterface $response) { - // @todo Either add this to \Drupal\serialization\Encoder\JsonEncoder, or - // figure out how to let tests specify encoder options, and figure out - // whether they should apply to just error responses or to everything. + // @todo Fix this in https://www.drupal.org/node/2813755. $encode_options = ['json_encode_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT]; $expected_body = $this->serializer->encode(['message' => $expected_message], static::$format, $encode_options); $this->assertResourceResponse($expected_status_code, $expected_body, $response);