Skip to content

Commit

Permalink
Add JSON module test for load and error events
Browse files Browse the repository at this point in the history
Add a test validating that the load event fires when appropriate for
a <script> element containing a JSON module import. Also check that
the error event is fired when a JSON module 404s.

These aren't intended to replicate the full suite of corresponding
tests for JavaScript modules. The purpose is to serve as a check that
implementers are reusing the codepaths for these events when
implementing JSON modules.

Bug: 1132413
Change-Id: I4277411c1f1f758c9ea163f87c2a539eacabe082
  • Loading branch information
dandclark authored and chromium-wpt-export-bot committed Apr 23, 2021
1 parent aaf4be7 commit 161110d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>load/error events for JSON modules</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/load-error-events-helpers.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/#execute-the-script-block">
</head>
<script>
"use strict";

var test1_load = event_test('inline, 200, parser-inserted', false, false);
var test1_error = event_test('inline, 404, parser-inserted', false, true);

var test2_load = event_test('src, 200, parser-inserted', true, false);
var test2_error = event_test('src, 404, parser-inserted', false, true);

var test3_dynamic_load = event_test('src, 200, not parser-inserted', true, false);
var test3_dynamic_error = event_test('src, 404, not parser-inserted', false, true);

var test4_dynamic_load = event_test('inline, 200, not parser-inserted', false, false);
var test4_dynamic_error = event_test('inline, 404, not parser-inserted', false, true);

var script3_dynamic_load = document.createElement('script');
script3_dynamic_load.setAttribute('type', 'module');
script3_dynamic_load.onload = () => onLoad(test3_dynamic_load);
script3_dynamic_load.onerror = () => onError(test3_dynamic_load);
script3_dynamic_load.src = "./load-error-events.py?test=test3_dynamic_load";
document.head.appendChild(script3_dynamic_load);

var script3_dynamic_error = document.createElement('script');
script3_dynamic_error.setAttribute('type', 'module');
script3_dynamic_error.onload = () => onLoad(test3_dynamic_error);
script3_dynamic_error.onerror = () => onError(test3_dynamic_error);
script3_dynamic_error.src = "./load-error-events.py?test=test3_dynamic_error";
document.head.appendChild(script3_dynamic_error);

var script4_dynamic_load = document.createElement('script');
script4_dynamic_load.setAttribute('type', 'module');
script4_dynamic_load.onload = () => onLoad(test4_dynamic_load);
script4_dynamic_load.onerror = () => onError(test4_dynamic_load);
script4_dynamic_load.async = true;
script4_dynamic_load.appendChild(document.createTextNode(`
import "./module.json" assert { type: "json" };
onExecute(test4_dynamic_load);`
));
document.head.appendChild(script4_dynamic_load);

var script4_dynamic_error = document.createElement('script');
script4_dynamic_error.setAttribute('type', 'module');
script4_dynamic_error.onload = () => onLoad(test4_dynamic_error);
script4_dynamic_error.onerror = () => onError(test4_dynamic_error);
script4_dynamic_error.async = true;
script4_dynamic_error.appendChild(document.createTextNode(`import "./not_found.json" assert { type: "json" };`));
document.head.appendChild(script4_dynamic_error);
</script>
<script onload="onLoad(test1_load);" onerror="onError(test1_load);" type="module">
import "./module.json" assert { type: "json"};
onExecute(test1_load);
</script>
<script onload="onLoad(test1_error);" onerror="onError(test1_error);" type="module">
import "./not_found.json" assert { type: "json"};
onExecute(test1_error);
</script>
<script src="./load-error-events.py?test=test2_load" onload="onLoad(test2_load);" onerror="onError(test2_load);" type="module"></script>
<script src="./load-error-events.py?test=test2_error" onload="onLoad(test2_error);" onerror="onError(test2_error);" type="module"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import re

def main(request, response):
headers = [(b"Content-Type", b"text/javascript")]
test = request.GET.first(b'test')
assert(re.match(b'^[a-zA-Z0-9_]+$', test))

status = 200
if test.find(b'_load') >= 0:
content = b'import "./module.json" assert { type: "json"}; %s.executed = true;' % test
else:
content = b'import "./not_found.json" assert { type: "json"}; %s.test.step(function() { assert_unreached("404 script should not be executed"); });' % test

return status, headers, content

0 comments on commit 161110d

Please sign in to comment.