blob: 6c2cf67239b5992c311411db06af2c387c0f0673 [file] [log] [blame]
<!DOCTYPE html>
<meta charset="utf-8">
<title>Service Worker: XHR responseURL uses the response url</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
const scope = 'resources/xhr-iframe.html';
const script = 'resources/fetch-rewrite-worker.js';
let iframe;
promise_test(async (t) => {
const registration =
await service_worker_unregister_and_register(t, script, scope);
await wait_for_state(t, registration.installing, 'activated');
iframe = await with_iframe(scope);
}, 'global setup');
// Test that XMLHttpRequest.responseURL uses the response URL from the service
// worker.
promise_test(async (t) => {
// Build the XHR URL. Set the |url| param to tell the service worker
// to respondWith(fetch(|url|)).
const target = new URL('resources/dummy.txt', window.location);
const url = `test?url=${encodeURIComponent(target)}`;
// Perform the XHR.
const xhr = await iframe.contentWindow.xhr(url);
assert_equals(xhr.responseURL, target.href, 'responseURL');
}, 'XHR responseURL should be the response URL');
// Test that XMLHttpRequest.responseXML is a Document whose URL is the
// response URL from the service worker.
promise_test(async (t) => {
// Build the XHR URL. Set the |url| param to tell the service worker
// to respondWith(fetch(|url|)).
const target = new URL('resources/blank.html', window.location);
const url = `test?url=${encodeURIComponent(target)}`;
// Perform the XHR.
const xhr = await iframe.contentWindow.xhr(url, {responseType: 'document'});
assert_equals(xhr.responseURL, target.href, 'responseURL');
// The document's URL uses the response URL:
// "Set |document|’s URL to |response|’s url."
// https://xhr.spec.whatwg.org/#document-response
assert_equals(xhr.responseXML.URL, target.href, 'responseXML.URL');
// The document's base URL falls back to the document URL:
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#document-base-url
assert_equals(xhr.responseXML.baseURI, target.href, 'responseXML.baseURI');
}, 'XHR Document should use the response URL');
promise_test(async (t) => {
if (iframe)
iframe.remove();
await service_worker_unregister(t, scope);
}, 'global cleanup');
</script>