-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test that loads twice from the same preloaded URL
This test is meant to support the discussion in whatwg/fetch#590. The test accesses a URL that returns an integer that gets incremented with each request (starting with 0). The test preloads it once, and then loads it twice. In Firefox, the test returns 0,0 In Chrome, the test returns 0,1 (preload is used for one request) In Safari, the test returns 1,2 (preload makes an unused request)
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<!DOCTYPE html> | ||
<title>Test cases for scenarios not defined in the preload spec</title> | ||
<script src="/resources/testharness.js" a></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script> | ||
const serveUrl = './resources/inc.py' | ||
function uuidv4() { | ||
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => | ||
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) | ||
); | ||
} | ||
|
||
async function load(key) { | ||
const response = await fetch(`${serveUrl}?key=${key}`, {cache: 'force-cache'}) | ||
return await response.text() | ||
} | ||
|
||
function preload(key) { | ||
const link = document.createElement('link') | ||
link.rel = "preload" | ||
link.href = `${serveUrl}?key=${key}` | ||
link.as = "fetch" | ||
link.setAttribute("crossorigin", "crossorigin") | ||
document.head.appendChild(link) | ||
return new Promise(resolve => { | ||
link.addEventListener("load", resolve) | ||
}) | ||
} | ||
|
||
promise_test(async() => { | ||
const key = uuidv4() | ||
const p = await preload(key) | ||
const results = [await load(key), await load(key)] | ||
|
||
const expected = { | ||
// Preload result is used for all subsequent requests | ||
gecko: ["0", "0"], | ||
|
||
// Preload is used once | ||
blink: ["0", "1"], | ||
|
||
// Preload is not used in subsequent requests | ||
webkit: ["1", "2"] | ||
} | ||
|
||
assert_array_equals(results, expected.gecko) | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from datetime import datetime | ||
|
||
# datetime object containing current date and time | ||
def main(request, response): | ||
stash = request.server.stash | ||
key = request.GET[b"key"] | ||
prev = stash.take(key) | ||
value = 0 | ||
if prev is not None: | ||
value = prev + 1 | ||
stash.put(key, value) | ||
headers = [(b"Content-Type", b"text/plain"), | ||
(b"Cache-Control", b"cache")] | ||
return headers, str(value) |