Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON modules tests for charset, integrity, and MIME type parameters #28606

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<meta charset="windows-1250">
<title>JSON modules: UTF-8 decoding</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script type="module">
import json from "../serve-with-content-type.py?fn=json-module/utf-8.json" assert { type: "json"};
test(() => {
assert_equals(json.data, "śćążź");
}, "JSON module should be loaded as utf-8 even though document's encoding is windows-1250");
</script>
<script type="module">
import json from "../serve-with-content-type.py?fn=json-module/windows-1250.json&ct=text/json%3Bcharset=windows-1250" assert { type: "json"};
test(() => {
assert_not_equals(json.data, "śćążź",
'Should be decoded as UTF-8');
}, "JSON module should be loaded as utf-8 even if it is encoded in windows-1250 and served with a windows-1250 charset response header, and this document's encoding is windows-1250");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!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 type="module" onerror="unreachable()">
import json from "../serve-with-content-type.py?fn=json-module/utf-8.json&ct=text/json%3Bcharset=utf-8" assert { type: "json"};
test(() => {
assert_equals(json.data, "śćążź");
}, "JSON module should be loaded as utf-8 when charset=utf8 is specified");
</script>
<script type="module" onerror="unreachable()">
import json from "../serve-with-content-type.py?fn=json-module/utf-8.json&ct=text/json%3Bcharset=shift-jis" assert { type: "json"};
test(() => {
assert_equals(json.data, "śćążź");
}, "JSON module should be loaded as utf-8 when charset=shift-jis is specified");
</script>
<script type="module" onerror="unreachable()">
import json from "../serve-with-content-type.py?fn=json-module/utf-8.json&ct=text/json%3Bcharset=windows-1252" assert { type: "json"};
test(() => {
assert_equals(json.data, "śćążź");
}, "JSON module should be loaded as utf-8 when charset=windows-1252 is specified");
</script>
<script type="module" onerror="unreachable()">
import json from "../serve-with-content-type.py?fn=json-module/utf-8.json&ct=text/json%3Bcharset=utf-7" assert { type: "json"};;
test(() => {
assert_equals(json.data, "śćążź");
}, "JSON module should be loaded as utf-8 when charset=utf-7 is specified");
</script>
<script type="module" onerror="unreachable()">
import json from "../serve-with-content-type.py?fn=json-module/windows-1250.json&ct=text/json%3Bcharset=windows-1250" assert { type: "json"};
test(() => {
assert_not_equals(json.data, "śćążź",
'Should be decoded as UTF-8');
}, "JSON module should be loaded as utf-8 even if it is encoded in windows-1250 and served with a windows-1250 charset response header");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import json from "./data.json" assert { type: "json" };
window.matchesLog.push(`integrity-matches,json:${json.answer}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import json "./data.json" assert { type: "json" };
window.mismatchesLog.push(`integrity-mismatches,json:${json.answer}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>&lt;script> integrity=""</title>
<link rel="help" href="https://html.spec.whatwg.org/multipage/#prepare-a-script">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>
window.matchesLog = [];
window.matchesEvents = [];

window.mismatchesLog = [];
window.mismatchesEvents = [];
</script>
<script type="module" src="integrity-matches.js" integrity="sha384-VmQQfGzBiLKdyzw4FA4kL4ohu4tyujV68ddgW1aN/1v3cBZNNBn2gDFdVQxfL7+a" onload="window.matchesEvents.push('load');" onerror="window.matchesEvents.push('error')"></script>
<script type="module" src="integrity-mismatches.js" integrity="sha384-doesnotmatch" onload="window.mismatchesEvents.push('load');" onerror="window.mismatchesEvents.push('error')"></script>

<script type="module">
test(() => {
assert_array_equals(window.matchesLog, ["integrity-matches,json:42"], "The module and its dependency must have executed");
assert_array_equals(window.matchesEvents, ["load"], "The load event must have fired");
}, "The integrity attribute must be verified on the top-level of a module loading a JSON module and allow it to execute when it matches");

test(() => {
assert_array_equals(window.mismatchesLog, [], "The module and its dependency must not have executed");
assert_array_equals(window.mismatchesEvents, ["error"], "The error event must have fired");
}, "The integrity attribute must be verified on the top-level of a module loading a JSON module and not allow it to execute when there's a mismatch");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"data": "śćążź",
"comment": "The data above are five Polish letters, similar to scazz. It can be read correctly only with utf-8 encoding."
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
const t2 = async_test("application/json");
const t3 = async_test("text/html+json");
const t4 = async_test("image/svg+json");
const t5 = async_test("text/json;boundary=something");
const t6 = async_test("text/json;foo=bar");
</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" assert { type: "json"};
Expand All @@ -34,3 +36,11 @@
import v from "../serve-with-content-type.py?fn=json-module/module.json&ct=image/svg+json" assert { type: "json"};
check(t4, v);
</script>
<script type="module" onerror="t5.step(() => assert_unreached(event))">
import v from "../serve-with-content-type.py?fn=json-module/module.json&ct=text/json;boundary=something" assert { type: "json"};
check(t5, v);
</script>
<script type="module" onerror="t6.step(() => assert_unreached(event))">
import v from "../serve-with-content-type.py?fn=json-module/module.json&ct=text/json;foo=bar" assert { type: "json"};
check(t6, v);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"data": "�湿�",
"comment": "The data above are five Polish letters, similar to scazz. It can be read correctly only with windows1250 encoding."
}