Commit a0fb9fe7 authored by horo@chromium.org's avatar horo@chromium.org

[ServiceWorker] Add LayoutTests for the fallback behavior of FetchEvent.

Currently the fallback behavior for redirect responses is not implemented correctly.
https://codereview.chromium.org/705273004/ will fix this problem.
This patch adds LayoutTests for this bug.

BUG=431985

Review URL: https://codereview.chromium.org/712303002

git-svn-id: svn://svn.chromium.org/blink/trunk@185185 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent df798212
<!DOCTYPE html>
<title>Service Worker: the fallback behavior of FetchEvent</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js?pipe=sub"></script>
<script>
var expected_urls = [];
function xhr_fail_test(frame, url) {
expected_urls.push(url);
return new Promise(function(resolve, reject) {
frame.contentWindow.xhr(url)
.then(function(){
reject(msg + ' should fail.');
})
.catch(function(){
resolve();
});
});
}
function xhr_succeed_test(frame, url) {
expected_urls.push(url);
return new Promise(function(resolve, reject) {
frame.contentWindow.xhr(url)
.then(function(){
resolve();
})
.catch(function(){
reject(msg + ' should succeed.');
});
});
}
async_test(function(t) {
var SCOPE = 'resources/fetch-request-fallback-iframe.html';
var SCRIPT = 'resources/fetch-request-fallback-worker.js';
var host_info = get_host_info();
var BASE_URL = host_info['HTTP_ORIGIN'] +
'/serviceworker/resources/fetch-access-control.php?';
var OTHER_BASE_URL = host_info['HTTP_REMOTE_ORIGIN'] +
'/serviceworker/resources/fetch-access-control.php?';
var REDIRECT_URL = host_info['HTTP_ORIGIN'] +
'/serviceworker/resources/redirect.php?Redirect=';
var frame;
var worker;
service_worker_unregister_and_register(t, SCRIPT, SCOPE)
.then(function(registration) {
return wait_for_update(t, registration);
})
.then(function(sw) {
worker = sw;
return wait_for_state(t, sw, 'activated');
})
.then(function() { return with_iframe(SCOPE); })
.then(function(f) {
frame = f;
return xhr_succeed_test(frame, BASE_URL);
})
.then(function(f) {
return xhr_fail_test(frame, OTHER_BASE_URL);
})
.then(function(f) {
return xhr_succeed_test(frame, OTHER_BASE_URL + 'ACAOrigin=*');
})
.then(function(f) {
return xhr_succeed_test(frame,
REDIRECT_URL + encodeURIComponent(BASE_URL));
})
.then(function() {
return xhr_fail_test(
frame,
REDIRECT_URL + encodeURIComponent(OTHER_BASE_URL));
})
.then(function() {
return xhr_succeed_test(
frame,
REDIRECT_URL +
encodeURIComponent(OTHER_BASE_URL + 'ACAOrigin=*'));
})
.then(function() {
return new Promise(function(resolve) {
var channel = new MessageChannel();
channel.port1.onmessage = t.step_func(function(msg) {
unload_iframe(frame);
resolve(msg);
});
worker.postMessage({port: channel.port2}, [channel.port2]);
});
})
.then(function(msg) {
var requests = msg.data.requests;
assert_equals(requests.length, expected_urls.length + 1,
'The count of the requests which are passed to the ' +
'ServiceWorker must be correct.');
assert_equals(requests[0].url, new URL(SCOPE, location).toString(),
'The first request to the SW must be the request for ' +
'the page.');
assert_equals(requests[0].mode, 'no-cors',
'The mode of the first request to the SW must be ' +
'no-cors.');
for (var i = 0; i < expected_urls.length; ++i) {
assert_equals(requests[i + 1].url, expected_urls[i],
'The URL of the request which was passed from XHR ' +
'to the ServiceWorker must be correct.');
assert_equals(requests[i + 1].mode, 'cors',
'The mode of the request which was passed from XHR ' +
'to the ServiceWorker must be cors.');
}
service_worker_unregister_and_done(t, SCOPE);
})
.catch(unreached_rejection(t));
}, 'Verify the fallback behavior of FetchEvent');
</script>
<script>
function xhr(url) {
return new Promise(function(resolve, reject) {
var request = new XMLHttpRequest();
request.addEventListener(
'error',
function(event) { reject(event); });
request.addEventListener(
'load',
function(event) { resolve(request.response); });
request.open('GET', url);
request.send();
});
}
</script>
var requests = [];
self.addEventListener('message', function(event) {
event.data.port.postMessage({requests: requests});
});
self.addEventListener('fetch', function(event) {
requests.push({
url: event.request.url,
mode: event.request.mode
});
});
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