function ResourceResponseSubscriberTest::providerTestResponseFormat

Same name in other branches
  1. 9 core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php \Drupal\Tests\rest\Unit\EventSubscriber\ResourceResponseSubscriberTest::providerTestResponseFormat()
  2. 8.9.x core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php \Drupal\Tests\rest\Unit\EventSubscriber\ResourceResponseSubscriberTest::providerTestResponseFormat()
  3. 10 core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php \Drupal\Tests\rest\Unit\EventSubscriber\ResourceResponseSubscriberTest::providerTestResponseFormat()

Return value

array 0. methods to test 1. supported formats for route requirements 2. request format 3. request headers 4. request body 5. expected response format 6. expected response content type 7. expected response body

File

core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php, line 222

Class

ResourceResponseSubscriberTest
@coversDefaultClass \Drupal\rest\EventSubscriber\ResourceResponseSubscriber @group rest

Namespace

Drupal\Tests\rest\Unit\EventSubscriber

Code

public static function providerTestResponseFormat() {
    $json_encoded = Json::encode([
        'REST' => 'Drupal',
    ]);
    $xml_encoded = "<?xml version=\"1.0\"?>\n<response><REST>Drupal</REST></response>\n";
    $safe_method_test_cases = [
        'safe methods: client requested format (JSON)' => [
            [
                'GET',
                'HEAD',
            ],
            [
                'xml',
                'json',
            ],
            [],
            'json',
            [],
            NULL,
            'json',
            'application/json',
            $json_encoded,
        ],
        'safe methods: client requested format (XML)' => [
            [
                'GET',
                'HEAD',
            ],
            [
                'xml',
                'json',
            ],
            [],
            'xml',
            [],
            NULL,
            'xml',
            'text/xml',
            $xml_encoded,
        ],
        'safe methods: client requested no format: response should use the first configured format (JSON)' => [
            [
                'GET',
                'HEAD',
            ],
            [
                'json',
                'xml',
            ],
            [],
            FALSE,
            [],
            NULL,
            'json',
            'application/json',
            $json_encoded,
        ],
        'safe methods: client requested no format: response should use the first configured format (XML)' => [
            [
                'GET',
                'HEAD',
            ],
            [
                'xml',
                'json',
            ],
            [],
            FALSE,
            [],
            NULL,
            'xml',
            'text/xml',
            $xml_encoded,
        ],
    ];
    $unsafe_method_bodied_test_cases = [
        '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,
            'json',
            '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,
            'xml',
            '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,
            'xml',
            'text/xml',
            $xml_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,
            'json',
            '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_no_body_test_cases = [
        'unsafe methods without request bodies (DELETE): client requested no format, response should have the first acceptable format' => [
            [
                'DELETE',
            ],
            [
                'xml',
                'json',
            ],
            [
                'xml',
                'json',
            ],
            FALSE,
            [
                'Content-Type' => 'application/json',
            ],
            NULL,
            'xml',
            'text/xml',
            $xml_encoded,
        ],
        'unsafe methods without request bodies (DELETE): client requested format (XML), response should have xml format' => [
            [
                'DELETE',
            ],
            [
                'xml',
                'json',
            ],
            [
                'xml',
                'json',
            ],
            'xml',
            [
                'Content-Type' => 'application/json',
            ],
            NULL,
            'xml',
            'text/xml',
            $xml_encoded,
        ],
        'unsafe methods without request bodies (DELETE): client requested format (JSON), response should have json format' => [
            [
                'DELETE',
            ],
            [
                'xml',
                'json',
            ],
            [
                'xml',
                'json',
            ],
            'json',
            [
                'Content-Type' => 'application/json',
            ],
            NULL,
            'json',
            'application/json',
            $json_encoded,
        ],
    ];
    return $safe_method_test_cases + $unsafe_method_bodied_test_cases + $unsafe_method_no_body_test_cases;
}

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