You are here

public function ContentTypeHeaderMatcherTest::testJsonRequest in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Routing/ContentTypeHeaderMatcherTest.php \Drupal\Tests\Core\Routing\ContentTypeHeaderMatcherTest::testJsonRequest()
  2. 9 core/tests/Drupal/Tests/Core/Routing/ContentTypeHeaderMatcherTest.php \Drupal\Tests\Core\Routing\ContentTypeHeaderMatcherTest::testJsonRequest()

Tests that XML-restricted routes get filtered out on JSON requests.

File

core/tests/Drupal/Tests/Core/Routing/ContentTypeHeaderMatcherTest.php, line 71

Class

ContentTypeHeaderMatcherTest
Confirm that the content types partial matcher is functioning properly.

Namespace

Drupal\Tests\Core\Routing

Code

public function testJsonRequest() {
  $collection = $this->fixtures
    ->sampleRouteCollection();
  $collection
    ->addCollection($this->fixtures
    ->contentRouteCollection());
  $request = Request::create('path/two', 'POST');
  $request->headers
    ->set('Content-type', 'application/json');
  $routes = $this->matcher
    ->filter($collection, $request);
  $this
    ->assertCount(6, $routes, 'The correct number of routes was found.');
  $this
    ->assertNotNull($routes
    ->get('route_f'), 'The json route was found.');
  $this
    ->assertNull($routes
    ->get('route_g'), 'The xml route was not found.');
  foreach ($routes as $name => $route) {
    $this
      ->assertEquals('route_f', $name, 'The json route is the first one in the collection.');
    break;
  }
}