You are here

public function LinkHeaderTest::testCssJsLinkHeaders in HTTP/2 Server Push 8

File

tests/src/Functional/LinkHeaderTest.php, line 36

Class

LinkHeaderTest
@group http2_server_push

Namespace

Drupal\Tests\http2_server_push\Functional

Code

public function testCssJsLinkHeaders() {
  $session = $this
    ->getSession();

  // No Server Push Link response headers should be present when CSS and JS
  // aggregation are disabled.
  $this
    ->drupalGet('<front>');
  $this
    ->assertNull($session
    ->getResponseHeader('Link'));

  // \Drupal\Tests\BrowserTestBase::installDrupal() overrides some of the
  // defaults for easier test debugging. But for a CDN integration test, we do
  // want the defaults to be applied, because that is what we want to test.
  $this
    ->config('system.performance')
    ->set('css.preprocess', TRUE)
    ->set('js.preprocess', TRUE)
    ->save();
  $html = $this
    ->drupalGet('<front>');

  // Determine the expected link response headers.
  $expected_link_response_headers = [];
  $document = Html::load($html);
  $xpath = new \DOMXPath($document);
  $dom_nodes = $xpath
    ->query('//link[@rel="stylesheet"]');
  foreach ($dom_nodes as $dom_node) {
    $expected_link_response_headers[] = '<' . $dom_node
      ->getAttribute('href') . '>; rel=preload; as=style';
  }
  $dom_nodes = $xpath
    ->query('//script[@src]');
  foreach ($dom_nodes as $dom_node) {
    $expected_link_response_headers[] = '<' . $dom_node
      ->getAttribute('src') . '>; rel=preload; as=script';
  }

  // Assert that the expected Server Push Link response headers are present.
  $this
    ->assertSame($expected_link_response_headers, $session
    ->getResponseHeaders()['Link']);

  // @todo test more explicitly that browser-conditional CSS and JS does not get a Server Push Link header. This is a bit difficult because DOMXPath automatically ignores this HTML.
  $conditional_script_html = <<<HTML
<!--[if lte IE 8]>
<script
HTML;
  $this
    ->assertSession()
    ->responseContains($conditional_script_html);
}