function ContentNegotiationRoutingTest::testFullNegotiation

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Routing/ContentNegotiationRoutingTest.php \Drupal\KernelTests\Core\Routing\ContentNegotiationRoutingTest::testFullNegotiation()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Routing/ContentNegotiationRoutingTest.php \Drupal\KernelTests\Core\Routing\ContentNegotiationRoutingTest::testFullNegotiation()
  3. 11.x core/tests/Drupal/KernelTests/Core/Routing/ContentNegotiationRoutingTest.php \Drupal\KernelTests\Core\Routing\ContentNegotiationRoutingTest::testFullNegotiation()

Full negotiation by header only.

File

core/tests/Drupal/KernelTests/Core/Routing/ContentNegotiationRoutingTest.php, line 102

Class

ContentNegotiationRoutingTest
Tests content negotiation routing variations.

Namespace

Drupal\KernelTests\Core\Routing

Code

public function testFullNegotiation() : void {
    $this->enableModules([
        'accept_header_routing_test',
    ]);
    $tests = [
        // ['path', 'accept', 'content-type'],
        // 406?
[
            'content_negotiation/negotiate',
            '',
            'text/html',
        ],
        // 406?
[
            'content_negotiation/negotiate',
            '',
            'text/html',
        ],
        // ['content_negotiation/negotiate', '*/*', '??'],
[
            'content_negotiation/negotiate',
            'application/json',
            'application/json',
        ],
        [
            'content_negotiation/negotiate',
            'application/xml',
            'application/xml',
        ],
        [
            'content_negotiation/negotiate',
            'application/json',
            'application/json',
        ],
        [
            'content_negotiation/negotiate',
            'application/xml',
            'application/xml',
        ],
    ];
    foreach ($tests as $test) {
        $path = $test[0];
        $accept_header = $test[1];
        $content_type = $test[2];
        $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);
        $this->assertEquals(Response::HTTP_OK, $response->getStatusCode(), "Testing path:{$path} Accept:{$accept_header} Content-type:{$content_type}");
    }
}

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