blob: 583967390b41318d322a9cc42aa27507d3847fb7 [file] [log] [blame]
<!DOCTYPE html>
<title>Service Worker: Redirected response</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/get-host-info.js?pipe=sub"></script>
<script src="resources/test-helpers.js"></script>
<script>
function redirected_test(url,
fetch_option,
fetch_method,
cache,
expected_redirected,
expected_url_list) {
return fetch_method(url, fetch_option).then(response => {
var cloned_response = response.clone();
assert_equals(
response.redirected, expected_redirected,
'The redirected flag of response must match. URL: ' + url);
assert_equals(
cloned_response.redirected, expected_redirected,
'The redirected flag of cloned response must match. URL: ' + url);
if (self.internals) {
assert_array_equals(
self.internals.getInternalResponseURLList(response),
expected_url_list,
'The URL list of response must match. URL: ' + url);
assert_array_equals(
self.internals.getInternalResponseURLList(cloned_response),
expected_url_list,
'The URL list of cloned response must match. URL: ' + url);
}
return cache.put(url, response);
})
.then(_ => cache.match(url))
.then(response => {
assert_equals(response.redirected, expected_redirected,
'The redirected flag of response in CacheStorage must match. URL: ' +
url);
if (self.internals) {
assert_array_equals(
self.internals.getInternalResponseURLList(response),
expected_url_list,
'The URL list of response in CacheStorage must match. URL: ' +
url);
}
});
}
promise_test(t => {
var SCOPE = 'resources/blank.html?redirected-response';
var SCRIPT = 'resources/fetch-rewrite-worker.js';
var host_info = get_host_info();
var REDIRECT_URL = host_info['HTTP_ORIGIN'] +
'/serviceworker/resources/redirect.php?Redirect=';
var TARGET_URL = host_info['HTTP_ORIGIN'] +
'/serviceworker/resources/simple.txt';
var REDIRECT_TO_TARGET_URL = REDIRECT_URL + encodeURIComponent(TARGET_URL);
var CACHE_NAME = 'serviceworker/redirected-response';
var frame;
var cache;
return service_worker_unregister_and_register(t, SCRIPT, SCOPE)
.then(registration => {
return wait_for_state(t, registration.installing, 'activated');
})
.then(_ => self.caches.open(CACHE_NAME))
.then(c => {
cache = c;
return with_iframe(SCOPE);
})
.then(f => {
frame = f;
return Promise.all([
// Tests without service workers.
redirected_test(TARGET_URL, {}, self.fetch, cache,
false /* expected_redirected */,
[TARGET_URL]),
redirected_test(REDIRECT_TO_TARGET_URL, {}, self.fetch, cache,
true /* expected_redirected */,
[REDIRECT_TO_TARGET_URL, TARGET_URL]),
redirected_test(REDIRECT_TO_TARGET_URL + '&manual',
{redirect: 'manual'}, self.fetch, cache,
false /* expected_redirected */,
[REDIRECT_TO_TARGET_URL + '&manual']),
promise_rejects(
t, new TypeError(),
self.fetch(REDIRECT_TO_TARGET_URL + '&error',
{redirect:'error'}),
'The redirect response from the server should be treated as' +
' an error when the redirect flag of request was \'error\'.'),
// Tests without redirects with service worker.
redirected_test('./?url=' + encodeURIComponent(TARGET_URL),
{},
frame.contentWindow.fetch,
cache,
false /* expected_redirected */,
[TARGET_URL]),
// The service worker returns a redirected response.
redirected_test(
'./?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) +
'&original-redirect-mode=follow',
{redirect: 'follow'},
frame.contentWindow.fetch,
cache,
true /* expected_redirected */,
[REDIRECT_TO_TARGET_URL, TARGET_URL]),
promise_rejects(
t, new TypeError(),
frame.contentWindow.fetch(
'./?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) +
'&original-redirect-mode=error',
{redirect: 'error'}),
'The redirected response from the service worker should be ' +
'treated as an error when the redirect flag of request was ' +
'\'error\'.'),
promise_rejects(
t, new TypeError(),
frame.contentWindow.fetch(
'./?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) +
'&original-redirect-mode=manual',
{redirect: 'manual'}),
'The redirected response from the service worker should be ' +
'treated as an error when the redirect flag of request was ' +
'\'manual\'.'),
// The service worker returns an opaqueredirect response.
promise_rejects(
t, new TypeError(),
frame.contentWindow.fetch(
'./?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) +
'&original-redirect-mode=follow&redirect-mode=manual',
{redirect: 'follow'}),
'The opaqueredirect response from the service worker should ' +
'be treated as an error when the redirect flag of request was' +
' \'follow\'.'),
promise_rejects(
t, new TypeError(),
frame.contentWindow.fetch(
'./?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) +
'&original-redirect-mode=error&redirect-mode=manual',
{redirect: 'error'}),
'The opaqueredirect response from the service worker should ' +
'be treated as an error when the redirect flag of request was' +
' \'error\'.'),
redirected_test(
'./?url=' + encodeURIComponent(REDIRECT_TO_TARGET_URL) +
'&original-redirect-mode=manual&redirect-mode=manual',
{redirect: 'manual'},
frame.contentWindow.fetch,
cache,
false /* expected_redirected */,
[REDIRECT_TO_TARGET_URL]),
]);
})
.then(_ => self.caches.delete(CACHE_NAME))
.then(_ => {
frame.remove();
return service_worker_unregister(t, SCOPE);
});
}, 'Verify redirected flag of responses.');
</script>