You are here

public function ResourceResponseSubscriberTest::providerTestResponseFormat in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php \Drupal\Tests\rest\Unit\EventSubscriber\ResourceResponseSubscriberTest::providerTestResponseFormat()
  2. 9 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 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)' => [
      // @todo add 'HEAD' in https://www.drupal.org/node/2752325
      [
        'GET',
      ],
      [
        'xml',
        'json',
      ],
      [],
      'json',
      [],
      NULL,
      'json',
      'application/json',
      $json_encoded,
    ],
    'safe methods: client requested format (XML)' => [
      // @todo add 'HEAD' in https://www.drupal.org/node/2752325
      [
        'GET',
      ],
      [
        'xml',
        'json',
      ],
      [],
      'xml',
      [],
      NULL,
      'xml',
      'text/xml',
      $xml_encoded,
    ],
    'safe methods: client requested no format: response should use the first configured format (JSON)' => [
      // @todo add 'HEAD' in https://www.drupal.org/node/2752325
      [
        'GET',
      ],
      [
        'json',
        'xml',
      ],
      [],
      FALSE,
      [],
      NULL,
      'json',
      'application/json',
      $json_encoded,
    ],
    'safe methods: client requested no format: response should use the first configured format (XML)' => [
      // @todo add 'HEAD' in https://www.drupal.org/node/2752325
      [
        'GET',
      ],
      [
        '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_bodyless_test_cases = [
    '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,
      'xml',
      NULL,
      '',
    ],
    '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,
      'xml',
      NULL,
      '',
    ],
    '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,
      'json',
      NULL,
      '',
    ],
  ];
  return $safe_method_test_cases + $unsafe_method_bodied_test_cases + $unsafe_method_bodyless_test_cases;
}