You are here

public function ContentNegotiationRoutingTest::testFullNegotiation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Routing/ContentNegotiationRoutingTest.php \Drupal\system\Tests\Routing\ContentNegotiationRoutingTest::testFullNegotiation()

Full negotiation by header only.

File

core/modules/system/src/Tests/Routing/ContentNegotiationRoutingTest.php, line 124
Contains \Drupal\system\Tests\Routing\ContentNegotiationRoutingTest.

Class

ContentNegotiationRoutingTest
Tests content negotiation routing variations.

Namespace

Drupal\system\Tests\Routing

Code

public function testFullNegotiation() {
  $this
    ->enableModules([
    'accept_header_routing_test',
  ]);
  \Drupal::service('router.builder')
    ->rebuild();
  $tests = [
    // ['path', 'accept', 'content-type'],
    [
      'conneg/negotiate',
      '',
      'text/html',
    ],
    // 406?
    [
      'conneg/negotiate',
      '',
      'text/html',
    ],
    // 406?
    // ['conneg/negotiate', '*/*', '??'],
    [
      'conneg/negotiate',
      'application/json',
      'application/json',
    ],
    [
      'conneg/negotiate',
      'application/xml',
      'application/xml',
    ],
    [
      'conneg/negotiate',
      'application/json',
      'application/json',
    ],
    [
      'conneg/negotiate',
      'application/xml',
      'application/xml',
    ],
  ];
  foreach ($tests as $test) {
    $path = $test[0];
    $accept_header = $test[1];
    $content_type = $test[2];
    $message = "Testing path:{$path} Accept:{$accept_header} Content-type:{$content_type}";
    $request = Request::create('/' . $path);
    $request->headers
      ->set('Accept', $accept_header);

    /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
    $kernel = \Drupal::getContainer()
      ->get('http_kernel');
    $response = $kernel
      ->handle($request);

    // Verbose message since simpletest doesn't let us provide a message and
    // see the error.
    $this
      ->pass($message);
    $this
      ->assertEqual($response
      ->getStatusCode(), Response::HTTP_OK);
  }
}