LinkHeaderTest.php in HTTP/2 Server Push 8
File
tests/src/Functional/LinkHeaderTest.php
View source
<?php
namespace Drupal\Tests\http2_server_push\Functional;
use Drupal\Component\Utility\Html;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class LinkHeaderTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'toolbar',
'http2_server_push',
];
protected function setUp() {
parent::setUp();
$this
->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), [
'access toolbar',
]);
}
public function testCssJsLinkHeaders() {
$session = $this
->getSession();
$this
->drupalGet('<front>');
$this
->assertNull($session
->getResponseHeader('Link'));
$this
->config('system.performance')
->set('css.preprocess', TRUE)
->set('js.preprocess', TRUE)
->save();
$html = $this
->drupalGet('<front>');
$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';
}
$this
->assertSame($expected_link_response_headers, $session
->getResponseHeaders()['Link']);
$conditional_script_html = <<<HTML
<!--[if lte IE 8]>
<script
HTML;
$this
->assertSession()
->responseContains($conditional_script_html);
}
}