3 namespace Drupal\Tests\http2_server_push\Functional;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\user\Entity\Role;
8 use Drupal\user\RoleInterface;
11 * @group http2_server_push
13 class LinkHeaderTest extends BrowserTestBase {
20 public static $modules = ['toolbar', 'http2_server_push'];
25 protected function setUp() {
28 // Grant anonymous users access to the toolbar, so the anonymous user gets
30 $this->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), ['access toolbar']);
32 // \Drupal\Tests\BrowserTestBase::installDrupal() overrides some of the
33 // defaults for easier test debugging. But for a CDN integration test, we do
34 // want the defaults to be applied, because that is what we want to test.
35 $this->config('system.performance')
36 ->set('css.preprocess', TRUE)
37 ->set('js.preprocess', TRUE)
41 public function testCssJsLinkHeaders() {
42 $session = $this->getSession();
44 $html = $this->drupalGet('<front>');
46 // Determine the expected link response headers.
47 $expected_link_response_headers = [];
48 $document = Html::load($html);
49 $xpath = new \DOMXPath($document);
50 $dom_nodes = $xpath->query('//link[@rel="stylesheet"]');
51 foreach ($dom_nodes as $dom_node) {
52 $expected_link_response_headers[] = '<' . $dom_node->getAttribute('href') . '>; rel=preload; as=style';
54 $dom_nodes = $xpath->query('//script[@src]');
55 foreach ($dom_nodes as $dom_node) {
56 $expected_link_response_headers[] = '<' . $dom_node->getAttribute('src') . '>; rel=preload; as=script';
59 // Assert that the expected Server Push Link response headers are present.
60 $this->assertSame($expected_link_response_headers, $session->getResponseHeaders()['Link']);
62 // @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.
63 $conditional_script_html = <<<HTML
68 $this->assertSession()->responseContains($conditional_script_html);