summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/xhr-response-url.https.html
blob: 673ca52cc67450f8b296ea50f7ac4c4fee310602 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!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/xhr-response-url-worker.js';
let iframe;

function build_url(options) {
  const url = new URL('test', window.location);
  const opts = options ? options : {};
  if (opts.respondWith)
    url.searchParams.set('respondWith', opts.respondWith);
  if (opts.url)
    url.searchParams.set('url', opts.url.href);
  return url.href;
}

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 a URL that tells the service worker to respondWith(fetch(|target|)).
  const target = new URL('resources/sample.txt', window.location);
  const url = build_url({
    respondWith: 'fetch',
    url: 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');

// Same as above with a generated response.
promise_test(async (t) => {
  // Build a URL that tells the service worker to respondWith(new Response()).
  const url = build_url({respondWith: 'string'});

  // Perform the XHR.
  const xhr = await iframe.contentWindow.xhr(url);
  assert_equals(xhr.responseURL, url, 'responseURL');
}, 'XHR responseURL should be the response URL (generated response)');

// Test that XMLHttpRequest.responseXML is a Document whose URL is the
// response URL from the service worker.
promise_test(async (t) => {
  // Build a URL that tells the service worker to respondWith(fetch(|target|)).
  const target = new URL('resources/blank.html', window.location);
  const url = build_url({
    respondWith: 'fetch',
    url: 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');

// Same as above with a generated response from the service worker.
promise_test(async (t) => {
  // Build a URL that tells the service worker to
  // respondWith(new Response()) with a document response.
  const url = build_url({respondWith: 'document'});

  // Perform the XHR.
  const xhr = await iframe.contentWindow.xhr(url, {responseType: 'document'});
  assert_equals(xhr.responseURL, url, 'responseURL');

  // The document's URL uses the response URL, which is the request URL:
  // "Set |document|’s URL to |response|’s url."
  // https://xhr.spec.whatwg.org/#document-response
  assert_equals(xhr.responseXML.URL, url, '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, url, 'responseXML.baseURI');
}, 'XHR Document should use the response URL (generated response)');

promise_test(async (t) => {
  if (iframe)
    iframe.remove();
  await service_worker_unregister(t, scope);
}, 'global cleanup');
</script>