xhr-response-url.https.html (3948B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Service Worker: XHR responseURL uses the response url</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="resources/test-helpers.sub.js"></script> 7 <script> 8 const scope = 'resources/xhr-iframe.html'; 9 const script = 'resources/xhr-response-url-worker.js'; 10 let iframe; 11 12 function build_url(options) { 13 const url = new URL('test', window.location); 14 const opts = options ? options : {}; 15 if (opts.respondWith) 16 url.searchParams.set('respondWith', opts.respondWith); 17 if (opts.url) 18 url.searchParams.set('url', opts.url.href); 19 return url.href; 20 } 21 22 promise_test(async (t) => { 23 const registration = 24 await service_worker_unregister_and_register(t, script, scope); 25 await wait_for_state(t, registration.installing, 'activated'); 26 iframe = await with_iframe(scope); 27 }, 'global setup'); 28 29 // Test that XMLHttpRequest.responseURL uses the response URL from the service 30 // worker. 31 promise_test(async (t) => { 32 // Build a URL that tells the service worker to respondWith(fetch(|target|)). 33 const target = new URL('resources/sample.txt', window.location); 34 const url = build_url({ 35 respondWith: 'fetch', 36 url: target 37 }); 38 39 // Perform the XHR. 40 const xhr = await iframe.contentWindow.xhr(url); 41 assert_equals(xhr.responseURL, target.href, 'responseURL'); 42 }, 'XHR responseURL should be the response URL'); 43 44 // Same as above with a generated response. 45 promise_test(async (t) => { 46 // Build a URL that tells the service worker to respondWith(new Response()). 47 const url = build_url({respondWith: 'string'}); 48 49 // Perform the XHR. 50 const xhr = await iframe.contentWindow.xhr(url); 51 assert_equals(xhr.responseURL, url, 'responseURL'); 52 }, 'XHR responseURL should be the response URL (generated response)'); 53 54 // Test that XMLHttpRequest.responseXML is a Document whose URL is the 55 // response URL from the service worker. 56 promise_test(async (t) => { 57 // Build a URL that tells the service worker to respondWith(fetch(|target|)). 58 const target = new URL('resources/blank.html', window.location); 59 const url = build_url({ 60 respondWith: 'fetch', 61 url: target 62 }); 63 64 // Perform the XHR. 65 const xhr = await iframe.contentWindow.xhr(url, {responseType: 'document'}); 66 assert_equals(xhr.responseURL, target.href, 'responseURL'); 67 68 // The document's URL uses the response URL: 69 // "Set |document|’s URL to |response|’s url." 70 // https://xhr.spec.whatwg.org/#document-response 71 assert_equals(xhr.responseXML.URL, target.href, 'responseXML.URL'); 72 73 // The document's base URL falls back to the document URL: 74 // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#document-base-url 75 assert_equals(xhr.responseXML.baseURI, target.href, 'responseXML.baseURI'); 76 }, 'XHR Document should use the response URL'); 77 78 // Same as above with a generated response from the service worker. 79 promise_test(async (t) => { 80 // Build a URL that tells the service worker to 81 // respondWith(new Response()) with a document response. 82 const url = build_url({respondWith: 'document'}); 83 84 // Perform the XHR. 85 const xhr = await iframe.contentWindow.xhr(url, {responseType: 'document'}); 86 assert_equals(xhr.responseURL, url, 'responseURL'); 87 88 // The document's URL uses the response URL, which is the request URL: 89 // "Set |document|’s URL to |response|’s url." 90 // https://xhr.spec.whatwg.org/#document-response 91 assert_equals(xhr.responseXML.URL, url, 'responseXML.URL'); 92 93 // The document's base URL falls back to the document URL: 94 // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#document-base-url 95 assert_equals(xhr.responseXML.baseURI, url, 'responseXML.baseURI'); 96 }, 'XHR Document should use the response URL (generated response)'); 97 98 promise_test(async (t) => { 99 if (iframe) 100 iframe.remove(); 101 await service_worker_unregister(t, scope); 102 }, 'global cleanup'); 103 </script>