summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/xhr-content-length.https.window.js
blob: 1ae320e9c3c5713a2d47291d83ba7d72dc03c152 (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
// META: script=resources/test-helpers.sub.js

let frame;

promise_test(async (t) => {
  const scope = "resources/empty.html";
  const script = "resources/xhr-content-length-worker.js";
  const registration = await service_worker_unregister_and_register(t, script, scope);
  await wait_for_state(t, registration.installing, "activated");
  frame = await with_iframe(scope);
}, "Setup");

promise_test(async t => {
  const xhr = new frame.contentWindow.XMLHttpRequest();
  xhr.open("GET", "test?type=no-content-length");
  xhr.send();
  const event = await new Promise(resolve => xhr.onload = resolve);
  assert_equals(xhr.getResponseHeader("content-length"), null);
  assert_false(event.lengthComputable);
  assert_equals(event.total, 0);
  assert_equals(event.loaded, xhr.responseText.length);
}, `Synthetic response without Content-Length header`);

promise_test(async t => {
  const xhr = new frame.contentWindow.XMLHttpRequest();
  xhr.open("GET", "test?type=larger-content-length");
  xhr.send();
  const event = await new Promise(resolve => xhr.onload = resolve);
  assert_equals(xhr.getResponseHeader("content-length"), "10000");
  assert_true(event.lengthComputable);
  assert_equals(event.total, 10000);
  assert_equals(event.loaded, xhr.responseText.length);
}, `Synthetic response with Content-Length header with value larger than response body length`);

promise_test(async t => {
  const xhr = new frame.contentWindow.XMLHttpRequest();
  xhr.open("GET", "test?type=double-content-length");
  xhr.send();
  const event = await new Promise(resolve => xhr.onload = resolve);
  assert_equals(xhr.getResponseHeader("content-length"), "10000, 10000");
  assert_true(event.lengthComputable);
  assert_equals(event.total, 10000);
  assert_equals(event.loaded, xhr.responseText.length);
}, `Synthetic response with two Content-Length headers value larger than response body length`);

promise_test(async t => {
  const xhr = new frame.contentWindow.XMLHttpRequest();
  xhr.open("GET", "test?type=bogus-content-length");
  xhr.send();
  const event = await new Promise(resolve => xhr.onload = resolve);
  assert_equals(xhr.getResponseHeader("content-length"), "test");
  assert_false(event.lengthComputable);
  assert_equals(event.total, 0);
  assert_equals(event.loaded, xhr.responseText.length);
}, `Synthetic response with bogus Content-Length header`);