function ResourceTestBase::assertResourceResponse

Same name in this branch
  1. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::assertResourceResponse()
Same name in other branches
  1. 8.9.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::assertResourceResponse()
  2. 8.9.x core/modules/rest/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\rest\Functional\ResourceTestBase::assertResourceResponse()
  3. 10 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::assertResourceResponse()
  4. 10 core/modules/rest/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\rest\Functional\ResourceTestBase::assertResourceResponse()
  5. 11.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::assertResourceResponse()
  6. 11.x core/modules/rest/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\rest\Functional\ResourceTestBase::assertResourceResponse()

Asserts that a resource response has the given status code and body.

Parameters

int $expected_status_code: The expected response status.

string|false $expected_body: The expected response body. FALSE in case this should not be asserted.

\Psr\Http\Message\ResponseInterface $response: The response to assert.

string[]|false $expected_cache_tags: (optional) The expected cache tags in the X-Drupal-Cache-Tags response header, or FALSE if that header should be absent. Defaults to FALSE.

string[]|false $expected_cache_contexts: (optional) The expected cache contexts in the X-Drupal-Cache-Contexts response header, or FALSE if that header should be absent. Defaults to FALSE.

string|false $expected_page_cache_header_value: (optional) The expected X-Drupal-Cache response header value, or FALSE if that header should be absent. Possible strings: 'MISS', 'HIT'. Defaults to FALSE.

string|false $expected_dynamic_page_cache_header_value: (optional) The expected X-Drupal-Dynamic-Cache response header value, or FALSE if that header should be absent. Possible strings: 'MISS', 'HIT'. Defaults to FALSE.

14 calls to ResourceTestBase::assertResourceResponse()
CommentResourceTestBase::testPostSkipCommentApproval in core/modules/comment/tests/src/Functional/Rest/CommentResourceTestBase.php
Tests POSTing a comment with and without 'skip comment approval'.
DbLogResourceTest::testWatchdog in core/modules/hal/tests/src/Functional/dblog/DbLogResourceTest.php
Writes a log messages and retrieves it via the REST API.
EntityResourceTestBase::testDelete in core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
Tests a DELETE request for an entity, plus edge cases to ensure good DX.
EntityResourceTestBase::testGet in core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
Tests a GET request for an entity, plus edge cases to ensure good DX.
EntityResourceTestBase::testPatch in core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
Tests a PATCH request for an entity, plus edge cases to ensure good DX.

... See full list

File

core/modules/rest/tests/src/Functional/ResourceTestBase.php, line 381

Class

ResourceTestBase
Subclass this for every REST resource, every format and every auth provider.

Namespace

Drupal\Tests\rest\Functional

Code

protected function assertResourceResponse($expected_status_code, $expected_body, ResponseInterface $response, $expected_cache_tags = FALSE, $expected_cache_contexts = FALSE, $expected_page_cache_header_value = FALSE, $expected_dynamic_page_cache_header_value = FALSE) {
    $this->assertSame($expected_status_code, $response->getStatusCode());
    if ($expected_status_code === 204) {
        // DELETE responses should not include a Content-Type header. But Apache
        // sets it to 'text/html' by default. We also cannot detect the presence
        // of Apache either here in the CLI. For now having this documented here
        // is all we can do.
        // $this->assertFalse($response->hasHeader('Content-Type'));
        $this->assertSame('', (string) $response->getBody());
    }
    else {
        $this->assertSame([
            static::$mimeType,
        ], $response->getHeader('Content-Type'));
        if ($expected_body !== FALSE) {
            $this->assertSame($expected_body, (string) $response->getBody());
        }
    }
    // Expected cache tags: X-Drupal-Cache-Tags header.
    $this->assertSame($expected_cache_tags !== FALSE, $response->hasHeader('X-Drupal-Cache-Tags'));
    if (is_array($expected_cache_tags)) {
        $this->assertEqualsCanonicalizing($expected_cache_tags, explode(' ', $response->getHeader('X-Drupal-Cache-Tags')[0]));
    }
    // Expected cache contexts: X-Drupal-Cache-Contexts header.
    $this->assertSame($expected_cache_contexts !== FALSE, $response->hasHeader('X-Drupal-Cache-Contexts'));
    if (is_array($expected_cache_contexts)) {
        $this->assertEqualsCanonicalizing($expected_cache_contexts, explode(' ', $response->getHeader('X-Drupal-Cache-Contexts')[0]));
    }
    // Expected Page Cache header value: X-Drupal-Cache header.
    if ($expected_page_cache_header_value !== FALSE) {
        $this->assertTrue($response->hasHeader('X-Drupal-Cache'));
        $this->assertSame($expected_page_cache_header_value, $response->getHeader('X-Drupal-Cache')[0]);
    }
    else {
        $this->assertFalse($response->hasHeader('X-Drupal-Cache'));
    }
    // Expected Dynamic Page Cache header value: X-Drupal-Dynamic-Cache header.
    if ($expected_dynamic_page_cache_header_value !== FALSE) {
        $this->assertTrue($response->hasHeader('X-Drupal-Dynamic-Cache'));
        $this->assertSame($expected_dynamic_page_cache_header_value, $response->getHeader('X-Drupal-Dynamic-Cache')[0]);
    }
    else {
        $this->assertFalse($response->hasHeader('X-Drupal-Dynamic-Cache'));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.