function DrupalKernelTest::testTrustedHosts

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php \Drupal\Tests\Core\DrupalKernel\DrupalKernelTest::testTrustedHosts()
  2. 10 core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php \Drupal\Tests\Core\DrupalKernel\DrupalKernelTest::testTrustedHosts()
  3. 11.x core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php \Drupal\Tests\Core\DrupalKernel\DrupalKernelTest::testTrustedHosts()

Tests hostname validation with settings.

@covers ::setupTrustedHosts @dataProvider providerTestTrustedHosts

File

core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php, line 24

Class

DrupalKernelTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21DrupalKernel.php/class/DrupalKernel/9" title="The DrupalKernel class is the core of Drupal itself." class="local">\Drupal\Core\DrupalKernel</a> @group DrupalKernel

Namespace

Drupal\Tests\Core\DrupalKernel

Code

public function testTrustedHosts($host, $server_name, $message, $expected = FALSE) {
    $request = new Request();
    $trusted_host_patterns = [
        '^example\\.com$',
        '^.+\\.example\\.com$',
        '^example\\.org',
        '^.+\\.example\\.org',
    ];
    if (!empty($host)) {
        $request->headers
            ->set('HOST', $host);
    }
    $request->server
        ->set('SERVER_NAME', $server_name);
    $method = new \ReflectionMethod('Drupal\\Core\\DrupalKernel', 'setupTrustedHosts');
    $method->setAccessible(TRUE);
    $valid_host = $method->invoke(NULL, $request, $trusted_host_patterns);
    $this->assertSame($expected, $valid_host, $message);
    // Reset the trusted hosts because it is statically stored on the request.
    $method->invoke(NULL, $request, []);
    // Reset the request factory because it is statically stored on the request.
    Request::setFactory(NULL);
}

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