Skip to content

Commit

Permalink
Upstream service worker "XHR" test to WPT
Browse files Browse the repository at this point in the history
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}
  • Loading branch information
jugglinmike authored and Commit Bot committed Jun 1, 2017
1 parent 1704aff commit 7fdac9f
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down Expand Up @@ -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 ]
Expand Down Expand Up @@ -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 ]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

self.onfetch = function(event) {
if (event.request.url.indexOf('non-existent-file.txt') !== -1) {
event.respondWith(new Response('Response from service worker'));
}
};

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 7fdac9f

Please sign in to comment.