-
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.
Upstream service worker navigation tests to WPT
**navigation-redirect** Update existing assertions to adhere to the latest version of the Fetch specification. The relevant change from that document [1] is explained as follows: > [...] > * Only when redirects are automatically followed should we set the > skip-service-worker flag, otherwise we negatively affect navigations. > [...] (Note that the Request's "skip-service-worker flag" was subsequently re-implemented as "service-workers mode" in [2].) Insert two additional tests introduced by Chromium project commit cb6838f5badc8c2df03f387f4aa726629214179a, whose message reads: > Make no-location redirect response to be "opaque redirect" when > redirect mode is manual. > > According to the spec, even if location header is not set, we should > treat the redirect response as "opaqueredirect" if the redirect mode > of the fetch request is "manual". > > This behavior was changed by this commit on the spec. > whatwg/fetch@3e501f2 Remove the "-expectations.txt" file for this test since Chromium passes the corrected version. Remove the Chromium-specific version of the test. **navigation-redirect-body** Re-locate test file to Web Platform Test directory for eventual automated upstreaming. Simplify test body by constructing necessary DOM declaratively with HTML. Schedule frame removal to occur following test completion. **navigation-redirect-to-http** Re-locate test file to Web Platform Test directory for eventual automated upstreaming. Prefer the generalized `redirect.py` script over a test-specific script defining equivalent functionality. Correct typo in test title. [1] whatwg/fetch@ec6f5ef [2] whatwg/fetch@d41c238 BUG=688116 [email protected] Review-Url: https://codereview.chromium.org/2872363002 Cr-Commit-Position: refs/heads/master@{#472332}
- Loading branch information
1 parent
891e76d
commit d9e8cd5
Showing
10 changed files
with
203 additions
and
6 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
service-workers/service-worker/navigation-redirect-body.https.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,53 @@ | ||
<!DOCTYPE html> | ||
<title>Service Worker: Navigation redirection must clear body</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/common/get-host-info.sub.js"></script> | ||
<script src="resources/test-helpers.sub.js"></script> | ||
<meta charset="utf-8"> | ||
<body> | ||
<form id="test-form" method="POST" style="display: none;"> | ||
<input type="submit" id="submit-button" /> | ||
</form> | ||
<script> | ||
promise_test(function(t) { | ||
var scope = 'resources/navigation-redirect-body.py'; | ||
var script = 'resources/navigation-redirect-body-worker.js'; | ||
var registration; | ||
var frame = document.createElement('frame'); | ||
var form = document.getElementById('test-form'); | ||
var submit_button = document.getElementById('submit-button'); | ||
|
||
frame.src = 'about:blank'; | ||
frame.name = 'target_frame'; | ||
frame.id = 'frame'; | ||
document.body.appendChild(frame); | ||
t.add_cleanup(function() { document.body.removeChild(frame); }); | ||
|
||
form.action = scope; | ||
form.target = 'target_frame'; | ||
|
||
return service_worker_unregister_and_register(t, script, scope) | ||
.then(function(r) { | ||
registration = r; | ||
return wait_for_state(t, registration.installing, 'activated'); | ||
}) | ||
.then(function() { | ||
var frame_load_promise = new Promise(function(resolve) { | ||
frame.addEventListener('load', function() { | ||
resolve(frame.contentWindow.document.body.innerText); | ||
}, false); | ||
}); | ||
submit_button.click(); | ||
return frame_load_promise; | ||
}) | ||
.then(function(text) { | ||
var request_uri = decodeURIComponent(text); | ||
assert_equals( | ||
request_uri, | ||
'/service-workers/service-worker/resources/navigation-redirect-body.py?redirect'); | ||
return registration.unregister(); | ||
}); | ||
}, 'Navigation redirection must clear body'); | ||
</script> | ||
</body> |
25 changes: 25 additions & 0 deletions
25
service-workers/service-worker/navigation-redirect-to-http.https.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,25 @@ | ||
<!DOCTYPE html> | ||
<title>Service Worker: Service Worker can receive HTTP opaqueredirect response.</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/common/get-host-info.sub.js"></script> | ||
<script src="resources/test-helpers.sub.js"></script> | ||
<meta charset="utf-8"> | ||
<body></body> | ||
<script> | ||
async_test(function(t) { | ||
var frame_src = get_host_info()['HTTPS_ORIGIN'] + base_path() + | ||
'resources/navigation-redirect-to-http-iframe.html'; | ||
function on_message(e) { | ||
assert_equals(e.data.results, 'OK'); | ||
t.done(); | ||
} | ||
|
||
window.addEventListener('message', t.step_func(on_message), false); | ||
|
||
with_iframe(frame_src) | ||
.then(function(frame) { | ||
t.add_cleanup(function() { frame.remove(); }); | ||
}); | ||
}, 'Verify Service Worker can receive HTTP opaqueredirect response.'); | ||
</script> |
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
11 changes: 11 additions & 0 deletions
11
service-workers/service-worker/resources/navigation-redirect-body-worker.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,11 @@ | ||
self.addEventListener('fetch', function(event) { | ||
event.respondWith( | ||
fetch(event.request) | ||
.then( | ||
function(response) { | ||
return response; | ||
}, | ||
function(error) { | ||
return new Response('Error:' + error); | ||
})); | ||
}); |
9 changes: 9 additions & 0 deletions
9
service-workers/service-worker/resources/navigation-redirect-body.py
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,9 @@ | ||
import os | ||
|
||
filename = os.path.basename(__file__) | ||
|
||
def main(request, response): | ||
if request.method == 'POST': | ||
return 302, [('Location', './%s?redirect' % filename)], '' | ||
|
||
return [('Content-Type', 'text/plain')], request.request_path |
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
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
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
42 changes: 42 additions & 0 deletions
42
service-workers/service-worker/resources/navigation-redirect-to-http-iframe.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,42 @@ | ||
<!DOCTYPE html> | ||
<script src="/common/get-host-info.sub.js"></script> | ||
<script src="test-helpers.sub.js"></script> | ||
<script> | ||
var SCOPE = './redirect.py?Redirect=' + encodeURI('http://example.com'); | ||
var SCRIPT = 'navigation-redirect-to-http-worker.js'; | ||
var host_info = get_host_info(); | ||
|
||
navigator.serviceWorker.getRegistration(SCOPE) | ||
.then(function(registration) { | ||
if (registration) | ||
return registration.unregister(); | ||
}) | ||
.then(function() { | ||
return navigator.serviceWorker.register(SCRIPT, {scope: SCOPE}); | ||
}) | ||
.then(function(registration) { | ||
return new Promise(function(resolve) { | ||
registration.addEventListener('updatefound', function() { | ||
resolve(registration.installing); | ||
}); | ||
}); | ||
}) | ||
.then(function(worker) { | ||
worker.addEventListener('statechange', on_state_change); | ||
}) | ||
.catch(function(reason) { | ||
window.parent.postMessage({results: 'FAILURE: ' + reason.message}, | ||
host_info['HTTPS_ORIGIN']); | ||
}); | ||
|
||
function on_state_change(event) { | ||
if (event.target.state != 'activated') | ||
return; | ||
with_iframe(SCOPE, {auto_remove: false}) | ||
.then(function(frame) { | ||
window.parent.postMessage( | ||
{results: frame.contentDocument.body.textContent}, | ||
host_info['HTTPS_ORIGIN']); | ||
}); | ||
} | ||
</script> |
22 changes: 22 additions & 0 deletions
22
service-workers/service-worker/resources/navigation-redirect-to-http-worker.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,22 @@ | ||
importScripts('/resources/testharness.js'); | ||
|
||
self.addEventListener('fetch', function(event) { | ||
event.respondWith(new Promise(function(resolve) { | ||
Promise.resolve() | ||
.then(function() { | ||
assert_equals( | ||
event.request.redirect, 'manual', | ||
'The redirect mode of navigation request must be manual.'); | ||
return fetch(event.request); | ||
}) | ||
.then(function(response) { | ||
assert_equals( | ||
response.type, 'opaqueredirect', | ||
'The response type of 302 response must be opaqueredirect.'); | ||
resolve(new Response('OK')); | ||
}) | ||
.catch(function(error) { | ||
resolve(new Response('Failed in SW: ' + error)); | ||
}); | ||
})); | ||
}); |