From e009f37f208f0f5aade8511ffcf504c13c68076c Mon Sep 17 00:00:00 2001 From: Jisse Reitsma Date: Mon, 4 Mar 2024 11:14:57 +0100 Subject: [PATCH] Make sure to include 'as' in header --- Link/LinkParser.php | 2 +- Test/Integration/HeaderTest.php | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Link/LinkParser.php b/Link/LinkParser.php index b800bc7..07c8695 100644 --- a/Link/LinkParser.php +++ b/Link/LinkParser.php @@ -83,7 +83,7 @@ private function processHeaders(HttpResponse $response) $links = []; foreach ($this->links as $link) { - $links[] = '<'.$link->getUrl().'>; rel="preload"'; + $links[] = '<'.$link->getUrl().'>; rel="preload"; as="'.$link->getType().'"'; } $response->setHeader('Link', implode(', ', $links)); diff --git a/Test/Integration/HeaderTest.php b/Test/Integration/HeaderTest.php index e72a4b6..82876ba 100644 --- a/Test/Integration/HeaderTest.php +++ b/Test/Integration/HeaderTest.php @@ -91,8 +91,17 @@ private function assertValidLink(string $link): void foreach ($linkParams as $linkParam) { $linkParam = explode('=', $linkParam); $this->assertEquals(2, count($linkParam)); - $this->assertEquals('rel', trim($linkParam[0])); - $this->assertEquals('preload', str_replace('"', '', trim($linkParam[1]))); + $linkParamName = trim($linkParam[0]); + $linkParamValue = str_replace('"', '', trim($linkParam[1])); + $this->assertContains($linkParamName, ['rel', 'as']); + + if ($linkParamName === 'rel') { + $this->assertEquals('preload', $linkParamValue); + } + + if ($linkParamName === 'as') { + $this->assertContains($linkParamValue, ['style', 'script', 'font']); + } } } }