Skip to content

Commit 7355e4c

Browse files
committed
Add tests for crossorigin
1 parent d176275 commit 7355e4c

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

src/Listeners/AddFromBody.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function handle(GenerateEarlyHints $event)
2222
[$src, $href, $data, $rel, $type, $crossorigin, $as, $fetchpriority, $integrity, $referrerpolicy, $imagesizes, $imagesrcset] = $element;
2323
$rel = $type === 'module' ? 'modulepreload' : $rel;
2424

25-
if ($rel === 'modulepreload' && ! $crossorigin) {
25+
if ($rel === 'modulepreload' && empty($crossorigin)) {
2626
// On module or modulepreload the crossorigin is REQUIRED https://github.com/whatwg/html/issues/1888
2727
$crossorigin = 'anonymous';
2828
}

tests/AddHttp3EarlyHintsTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,30 @@ public function it_will_return_a_js_link_header_for_js()
7878
$this->assertStringEndsWith('as="script"', $response->headers->get('link'));
7979
}
8080

81+
/** @test */
82+
public function it_will_return_a_js_link_header_with_crossorigin_for_js_module()
83+
{
84+
$request = $this->getNewRequest();
85+
86+
$response = $this->middleware->handle($request, $this->getNext('pageWithJsModule'));
87+
88+
$this->assertTrue($this->isServerPushResponse($response));
89+
$this->assertStringEndsWith('as="script"', $response->headers->get('link'));
90+
$this->assertStringContainsString('crossorigin="anonymous"', $response->headers->get('link'));
91+
}
92+
93+
/** @test */
94+
public function it_will_return_a_js_link_header_with_use_credentials_crossorigin_for_js_module()
95+
{
96+
$request = $this->getNewRequest();
97+
98+
$response = $this->middleware->handle($request, $this->getNext('pageWithJsCrossoriginModule'));
99+
100+
$this->assertTrue($this->isServerPushResponse($response));
101+
$this->assertStringEndsWith('as="script"', $response->headers->get('link'));
102+
$this->assertStringContainsString('crossorigin="use-credentials"', $response->headers->get('link'));
103+
}
104+
81105
/** @test */
82106
public function it_will_return_an_image_link_header_for_images()
83107
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html>
2+
3+
<head>
4+
<title>Page title</title>
5+
</head>
6+
7+
<body>
8+
<script type="module" src="js/module.js" crossorigin="use-credentials"></script>
9+
</body>
10+
11+
</html>

tests/fixtures/pageWithJsModule.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html>
2+
3+
<head>
4+
<title>Page title</title>
5+
</head>
6+
7+
<body>
8+
<script type="module" src="js/module.js"></script>
9+
</body>
10+
11+
</html>

0 commit comments

Comments
 (0)