1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<body>
<script>
host_info = get_host_info();
function verifyNumberOfDownloads(url, number) {
var numDownloads = 0;
let absoluteURL = new URL(url, location.href).href;
performance.getEntriesByName(absoluteURL).forEach(entry => {
if (entry.transferSize > 0) {
numDownloads++;
}
});
assert_equals(numDownloads, number, url);
}
function attachAndWaitForLoad(element) {
return new Promise((resolve, reject) => {
element.onload = resolve;
element.onerror = reject;
document.body.appendChild(element);
});
}
function attachAndWaitForError(element) {
return new Promise((resolve, reject) => {
element.onload = reject;
element.onerror = resolve;
document.body.appendChild(element);
});
}
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/dummy.js';
return attachAndWaitForLoad(link).then(() => {
verifyNumberOfDownloads('resources/dummy.js', 1);
// Verify that <script> doesn't fetch the module again.
var script = document.createElement('script');
script.type = 'module';
script.src = 'resources/dummy.js';
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads('resources/dummy.js', 1);
});
}, 'link rel=modulepreload');
/**
* Begin tests to ensure crossorigin value behaves the same on
* link rel=modulepreload as it does script elements.
*/
promise_test(function(t) {
document.cookie = 'same=1';
var link = document.createElement('link');
link.rel = 'modulepreload';
link.crossorigin = 'anonymous';
link.href = 'resources/dummy.js?sameOriginAnonymous';
return attachAndWaitForLoad(link).then(() => {
verifyNumberOfDownloads('resources/dummy.js?sameOriginAnonymous', 1);
// Verify that <script> doesn't fetch the module again.
var script = document.createElement('script');
script.type = 'module';
script.crossorigin = 'anonymous';
script.src = 'resources/dummy.js?sameOriginAnonymous';
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads('resources/dummy.js?sameOriginAnonymous', 1);
});
}, 'same-origin link rel=modulepreload crossorigin=anonymous');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.crossorigin = 'use-credentials';
link.href = 'resources/dummy.js?sameOriginUseCredentials';
return attachAndWaitForLoad(link).then(() => {
verifyNumberOfDownloads('resources/dummy.js?sameOriginUseCredentials', 1);
// Verify that <script> doesn't fetch the module again.
var script = document.createElement('script');
script.type = 'module';
script.crossorigin = 'use-credentials';
script.src = 'resources/dummy.js?sameOriginUseCredentials';
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads('resources/dummy.js?sameOriginUseCredentials', 1);
});
}, 'same-origin link rel=modulepreload crossorigin=use-credentials');
promise_test(function(t) {
const setCookiePromise = fetch(
`${host_info.HTTP_REMOTE_ORIGIN}/cookies/resources/set-cookie.py?name=cross&path=/preload/`,
{
mode: 'no-cors',
credentials: 'include',
});
return setCookiePromise.then(() => {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = `${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginNone`;
return attachAndWaitForLoad(link);
}).then(() => {
verifyNumberOfDownloads(`${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginNone`, 1);
// Verify that <script> doesn't fetch the module again.
var script = document.createElement('script');
script.type = 'module';
script.src = `${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginNone`;
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads(`${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginNone`, 1);
});
}, 'cross-origin link rel=modulepreload');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.crossorigin = 'anonymous';
link.href = `${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginAnonymous`;
return attachAndWaitForLoad(link).then(() => {
verifyNumberOfDownloads(`${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginAnonymous`, 1);
// Verify that <script> doesn't fetch the module again.
var script = document.createElement('script');
script.type = 'module';
script.crossorigin = 'anonymous';
script.src = `${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginAnonymous`;
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads(`${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginAnonymous`, 1);
});
}, 'cross-origin link rel=modulepreload crossorigin=anonymous');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.crossorigin = 'use-credentials';
link.href = `${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginUseCredentials`;
return attachAndWaitForLoad(link).then(() => {
verifyNumberOfDownloads(`${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginUseCredentials`, 1);
// Verify that <script> doesn't fetch the module again.
var script = document.createElement('script');
script.type = 'module';
script.crossorigin = 'use-credentials';
script.src = `${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginUseCredentials`;
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads(`${host_info.HTTP_REMOTE_ORIGIN}/preload/resources/cross-origin-module.py?crossOriginUseCredentials`, 1);
});
}, 'cross-origin link rel=modulepreload crossorigin=use-credentials');
/**
* End link rel=modulepreload crossorigin attribute tests.
*/
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.js';
return attachAndWaitForLoad(link).then(() => {
verifyNumberOfDownloads('resources/module1.js', 1);
// The load event fires before (optional) submodules fetch.
verifyNumberOfDownloads('resources/module2.js', 0);
var script = document.createElement('script');
script.type = 'module';
script.src = 'resources/module1.js';
return attachAndWaitForLoad(script);
}).then(() => {
verifyNumberOfDownloads('resources/module1.js', 1);
verifyNumberOfDownloads('resources/module2.js', 1);
});
}, 'link rel=modulepreload with submodules');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/syntax-error.js';
return attachAndWaitForLoad(link);
}, 'link rel=modulepreload for a module with syntax error');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/not-exist.js';
return attachAndWaitForError(link);
}, 'link rel=modulepreload for a module with network error');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = null;
return attachAndWaitForError(link);
}, 'link rel=modulepreload with bad href attribute');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.js?as-script';
link.as = 'script'
return attachAndWaitForLoad(link);
}, 'link rel=modulepreload as=script');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.js?as-image';
link.as = 'image'
return attachAndWaitForError(link);
}, 'link rel=modulepreload with invalid as= value');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.js?integrity-match';
link.integrity = 'sha256-ZPBZ+J9CiHzZXaBBluSeCpjzuTUkT+rSWIdXUV3AtVo='
return attachAndWaitForLoad(link);
}, 'link rel=modulepreload with integrity match');
promise_test(function(t) {
var link = document.createElement('link');
link.rel = 'modulepreload';
link.href = 'resources/module1.js?integrity-doesnotmatch';
link.integrity = 'sha384-doesnotmatch'
return attachAndWaitForError(link);
}, 'link rel=modulepreload with integrity mismatch');
</script>
</body>
|