Commit 7fdac9fc authored by mike's avatar mike Committed by Commit Bot

Upstream service worker "XHR" test to WPT

The original version of this test would only pass if the service worker
did *not* intercept the XHR request. That behavior conflicted with the
Fetch and Service Worker specification and with the test's documentation
itself. Update the test to pass only if the service worker intercepts
the request.

In addition:

- Update URLs to suitable values for the Web Platform Tests project
- Add "use strict" directives to script bodies
- Increase precision of "cleanup" logic scheduling
- Re-name files to adhere to precent set by existing WPT tests
- Catch and report errors within Promise chain rather than relying on
  implicit reporting of uncaught errors.

BUG=688116, 602051
R=falken@chromium.org

Review-Url: https://codereview.chromium.org/2907443002
Cr-Commit-Position: refs/heads/master@{#476324}
parent 1704affd
......@@ -2745,7 +2745,6 @@ Bug(none) http/tests/serviceworker/navigation-preload/chromium/use-counter.html
Bug(none) http/tests/serviceworker/ServiceWorkerGlobalScope/registration-attribute.html [ Failure ]
Bug(none) http/tests/serviceworker/ServiceWorkerGlobalScope/unregister.html [ Failure ]
Bug(none) http/tests/serviceworker/ServiceWorkerGlobalScope/update.html [ Failure ]
Bug(none) http/tests/serviceworker/sync-xhr-doesnt-deadlock.html [ Failure ]
Bug(none) http/tests/serviceworker/waiting.html [ Failure ]
Bug(none) http/tests/serviceworker/webexposed/global-interface-listing-service-worker.html [ Failure ]
Bug(none) http/tests/serviceworker/websocket/websocket-in-service-worker.html [ Failure ]
......@@ -4601,7 +4600,6 @@ Bug(none) virtual/mojo-loading/http/tests/serviceworker/navigation-preload/chrom
Bug(none) virtual/mojo-loading/http/tests/serviceworker/ServiceWorkerGlobalScope/registration-attribute.html [ Failure ]
Bug(none) virtual/mojo-loading/http/tests/serviceworker/ServiceWorkerGlobalScope/unregister.html [ Failure ]
Bug(none) virtual/mojo-loading/http/tests/serviceworker/ServiceWorkerGlobalScope/update.html [ Failure ]
Bug(none) virtual/mojo-loading/http/tests/serviceworker/sync-xhr-doesnt-deadlock.html [ Failure ]
Bug(none) virtual/mojo-loading/http/tests/serviceworker/waiting.html [ Failure ]
Bug(none) virtual/mojo-loading/http/tests/serviceworker/webexposed/global-interface-listing-service-worker.html [ Failure ]
Bug(none) virtual/mojo-loading/http/tests/serviceworker/websocket/websocket-in-service-worker.html [ Failure ]
......@@ -5172,7 +5170,6 @@ Bug(none) virtual/off-main-thread-fetch/http/tests/serviceworker/navigation-prel
Bug(none) virtual/off-main-thread-fetch/http/tests/serviceworker/ServiceWorkerGlobalScope/registration-attribute.html [ Failure ]
Bug(none) virtual/off-main-thread-fetch/http/tests/serviceworker/ServiceWorkerGlobalScope/unregister.html [ Failure ]
Bug(none) virtual/off-main-thread-fetch/http/tests/serviceworker/ServiceWorkerGlobalScope/update.html [ Failure ]
Bug(none) virtual/off-main-thread-fetch/http/tests/serviceworker/sync-xhr-doesnt-deadlock.html [ Failure ]
Bug(none) virtual/off-main-thread-fetch/http/tests/serviceworker/waiting.html [ Failure ]
Bug(none) virtual/off-main-thread-fetch/http/tests/serviceworker/webexposed/global-interface-listing-service-worker.html [ Failure ]
Bug(none) virtual/off-main-thread-fetch/http/tests/serviceworker/websocket/websocket-in-service-worker.html [ Failure ]
......
This is a testharness.js-based test.
FAIL Verify SyncXHR is intercepted assert_equals: HTTP response status code for intercepted request expected 200 but got 404
Harness: the test ran to completion.
<!DOCTYPE html>
<title>Service Worker: Synchronous XHR is intercepted</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
'use strict';
promise_test(function(t) {
var url = 'resources/fetch-request-xhr-sync-worker.js';
var scope = 'resources/fetch-request-xhr-sync-iframe.html';
var non_existent_file = 'non-existent-file.txt';
return service_worker_unregister_and_register(t, url, scope)
.then(function(registration) {
t.add_cleanup(function() {
registration.unregister();
});
return wait_for_state(t, registration.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(frame) {
t.add_cleanup(function() {
frame.remove();
});
return new Promise(function(resolve, reject) {
setTimeout(function() {
var xhr;
try {
xhr = frame.contentWindow.performSyncXHR(non_existent_file);
resolve(xhr);
} catch (err) {
reject(err);
}
}, 0);
})
})
.then(function(xhr) {
assert_equals(
xhr.status,
200,
'HTTP response status code for intercepted request'
);
assert_equals(
xhr.responseText,
'Response from service worker',
'HTTP response text for intercepted request'
);
});
}, 'Verify SyncXHR is intercepted');
</script>
<!DOCTYPE html>
<title>Service Worker: Synchronous XHR is intercepted iframe</title>
<script>
'use strict';
function performSyncXHR(url) {
var syncXhr = new XMLHttpRequest();
syncXhr.open('GET', url, false);
syncXhr.send();
return syncXhr;
}
</script>
'use strict';
self.onfetch = function(event) {
if (event.request.url.indexOf('non-existent-file.txt') !== -1) {
event.respondWith(new Response('Response from service worker'));
}
};
<!DOCTYPE html>
<title>Service Worker: SyncXHR doesn't deadlock iframe</title>
<script>
function performSyncXHR() {
var url = 'sync-xhr-doesnt-deadlock.data?bustcache=' + Date.now();
var syncXhr = new XMLHttpRequest();
syncXhr.open("GET", url, false);
syncXhr.send();
if (syncXhr.responseText != 'hello')
throw 'FAIL';
}
</script>
self.onfetch = function(event) {
if (event.request.url.indexOf('sync-xhr-doesnt-deadlock.data') == -1)
return;
event.respondWith(fetch('404resource?bustcache=' + Date.now()));
};
CONSOLE WARNING: line 7: Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
This is a testharness.js-based test.
PASS Verify SyncXHR does not deadlock
Harness: the test ran to completion.
<!DOCTYPE html>
<title>Service Worker: SyncXHR doesn't deadlock</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<script>
async_test(function(t) {
var url = 'resources/sync-xhr-doesnt-deadlock.js';
var scope = 'resources/sync-xhr-doesnt-deadlock-iframe.html';
service_worker_unregister_and_register(t, url, scope)
.then(function(registration) {
return wait_for_state(t, registration.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(frame) {
setTimeout(function() {
frame.contentWindow.performSyncXHR();
service_worker_unregister_and_done(t, scope);
}, 0);
})
.catch(unreached_rejection(t));
}, 'Verify SyncXHR does not deadlock');
</script>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment