-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some initial tests for JSON modules.
See <whatwg/html#4407>.
- Loading branch information
Showing
14 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
html/semantics/scripting-1/the-script-element/json-module/array.json
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 @@ | ||
["en", "try"] |
1 change: 1 addition & 0 deletions
1
html/semantics/scripting-1/the-script-element/json-module/false.json
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 @@ | ||
false |
26 changes: 26 additions & 0 deletions
26
html/semantics/scripting-1/the-script-element/json-module/invalid-content-type.html
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,26 @@ | ||
<!DOCTYPE html> | ||
<meta charset=utf-8> | ||
<title>JSON modules: invalid Content-Type</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<div id=log></div> | ||
<script> | ||
const content_types = [ | ||
"application/json+protobuf", | ||
"application/json+blah", | ||
"text/x-json", | ||
"text/json+blah", | ||
"application/blahjson", | ||
"image/json", | ||
]; | ||
for (const content_type of content_types) { | ||
async_test(t => { | ||
const script = document.createElement("script"); | ||
script.onerror = t.step_func_done(); | ||
script.onload = t.unreached_func("Should not load"); | ||
script.type = "module"; | ||
script.src = `module.json?pipe=header(Content-Type,${content_type})`; | ||
document.body.appendChild(script); | ||
}, content_type); | ||
} | ||
</script> |
18 changes: 18 additions & 0 deletions
18
html/semantics/scripting-1/the-script-element/json-module/module.html
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,18 @@ | ||
<!DOCTYPE html> | ||
<meta charset=utf-8> | ||
<title>JSON modules</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<div id=log></div> | ||
<script> | ||
const t = async_test(); | ||
</script> | ||
<script type="module" onerror="t.step(() => assert_unreached(event))"> | ||
import v from "./module.json"; | ||
t.step(() => { | ||
assert_equals(typeof v, "object"); | ||
assert_array_equals(Object.keys(v), ["test"]); | ||
assert_equals(v.test, true); | ||
t.done(); | ||
}); | ||
</script> |
3 changes: 3 additions & 0 deletions
3
html/semantics/scripting-1/the-script-element/json-module/module.json
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,3 @@ | ||
{ | ||
"test": true | ||
} |
14 changes: 14 additions & 0 deletions
14
html/semantics/scripting-1/the-script-element/json-module/non-object.any.js
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 @@ | ||
// META: global=window,worker | ||
|
||
for (const value of [null, true, false, "string"]) { | ||
promise_test(async t => { | ||
const result = await import(`./${value}.json`); | ||
assert_equals(result, value); | ||
}, `Non-object: ${value}`); | ||
} | ||
|
||
promise_test(async t => { | ||
const result = await import("./array.json"); | ||
assert_array_equals(result, ["en", "try"]); | ||
}, "Non-object: array"); | ||
|
1 change: 1 addition & 0 deletions
1
html/semantics/scripting-1/the-script-element/json-module/null.json
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 @@ | ||
null |
21 changes: 21 additions & 0 deletions
21
html/semantics/scripting-1/the-script-element/json-module/parse-error.html
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,21 @@ | ||
<!DOCTYPE html> | ||
<meta charset=utf-8> | ||
<title>JSON modules: parse error</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<div id=log></div> | ||
<script> | ||
setup({ | ||
allow_uncaught_exception: true, | ||
}); | ||
async_test(t => { | ||
window.addEventListener("error", t.step_func_done(e => { | ||
assert_true(e instanceof ErrorEvent, "ErrorEvent"); | ||
assert_equals(e.filename, new URL("parse-error.json", location).href); | ||
assert_true(e.error instanceof SyntaxError, "SyntaxError"); | ||
})); | ||
}); | ||
</script> | ||
<script type="module"> | ||
import v from "./parse-error.json"; | ||
</script> |
1 change: 1 addition & 0 deletions
1
html/semantics/scripting-1/the-script-element/json-module/parse-error.json
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 @@ | ||
{ |
1 change: 1 addition & 0 deletions
1
html/semantics/scripting-1/the-script-element/json-module/string.json
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 @@ | ||
"string" |
1 change: 1 addition & 0 deletions
1
html/semantics/scripting-1/the-script-element/json-module/true.json
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 @@ | ||
true |
36 changes: 36 additions & 0 deletions
36
html/semantics/scripting-1/the-script-element/json-module/utf8.html
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,36 @@ | ||
<!DOCTYPE html> | ||
<meta charset=utf-8> | ||
<title>JSON modules: UTF-8 decoding</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<div id=log></div> | ||
<script> | ||
function check(t, v) { | ||
t.step(() => { | ||
assert_equals(typeof v, "object"); | ||
assert_array_equals(Object.keys(v), ["test"]); | ||
assert_equals(v.test, "\u2026"); | ||
t.done(); | ||
}); | ||
} | ||
const t1 = async_test("utf-8"); | ||
const t2 = async_test("shift-jis"); | ||
const t3 = async_test("windows-1252"); | ||
const t4 = async_test("utf-7"); | ||
</script> | ||
<script type="module" onerror="t1.step(() => assert_unreached(event))"> | ||
import v from "../serve-with-content-type.py?fn=json-module/utf8.json&ct=text/json%3Bcharset=utf-8"; | ||
check(t1, v); | ||
</script> | ||
<script type="module" onerror="t2.step(() => assert_unreached(event))"> | ||
import v from "../serve-with-content-type.py?fn=json-module/utf8.json&ct=text/json%3Bcharset=shift-jis"; | ||
check(t2, v); | ||
</script> | ||
<script type="module" onerror="t3.step(() => assert_unreached(event))"> | ||
import v from "../serve-with-content-type.py?fn=json-module/utf8.json&ct=text/json%3Bcharset=windows-1252"; | ||
check(t3, v); | ||
</script> | ||
<script type="module" onerror="t4.step(() => assert_unreached(event))"> | ||
import v from "../serve-with-content-type.py?fn=json-module/utf8.json&ct=text/json%3Bcharset=utf-7"; | ||
check(t4, v); | ||
</script> |
3 changes: 3 additions & 0 deletions
3
html/semantics/scripting-1/the-script-element/json-module/utf8.json
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,3 @@ | ||
{ | ||
"test": "…" | ||
} |
36 changes: 36 additions & 0 deletions
36
html/semantics/scripting-1/the-script-element/json-module/valid-content-type.html
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,36 @@ | ||
<!DOCTYPE html> | ||
<meta charset=utf-8> | ||
<title>JSON modules: Content-Type</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<div id=log></div> | ||
<script> | ||
function check(t, v) { | ||
t.step(() => { | ||
assert_equals(typeof v, "object"); | ||
assert_array_equals(Object.keys(v), ["test"]); | ||
assert_equals(v.test, true); | ||
t.done(); | ||
}); | ||
} | ||
const t1 = async_test("text/json"); | ||
const t2 = async_test("application/json"); | ||
const t3 = async_test("text/html+json"); | ||
const t4 = async_test("image/svg+json"); | ||
</script> | ||
<script type="module" onerror="t1.step(() => assert_unreached(event))"> | ||
import v from "../serve-with-content-type.py?fn=json-module/module.json&ct=text/json"; | ||
check(t1, v); | ||
</script> | ||
<script type="module" onerror="t2.step(() => assert_unreached(event))"> | ||
import v from "../serve-with-content-type.py?fn=json-module/module.json&ct=application/json"; | ||
check(t2, v); | ||
</script> | ||
<script type="module" onerror="t3.step(() => assert_unreached(event))"> | ||
import v from "../serve-with-content-type.py?fn=json-module/module.json&ct=text/html+json"; | ||
check(t3, v); | ||
</script> | ||
<script type="module" onerror="t4.step(() => assert_unreached(event))"> | ||
import v from "../serve-with-content-type.py?fn=json-module/module.json&ct=image/svg+json"; | ||
check(t4, v); | ||
</script> |