summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/metadata/resources/helper.js
blob: 725f9a7e43b2f4a852deb8a0d2f6394889434762 (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
function validate_expectations(key, expected, tag) {
  return fetch("/fetch/metadata/resources/record-header.py?retrieve=true&file=" + key)
    .then(response => response.text())
    .then(text => {
      assert_not_equals(text, "No header has been recorded");
      let value = JSON.parse(text);
      test(t => assert_equals(value.dest, expected.dest), `${tag}: sec-fetch-dest`);
      test(t => assert_equals(value.mode, expected.mode), `${tag}: sec-fetch-mode`);
      test(t => assert_equals(value.site, expected.site), `${tag}: sec-fetch-site`);
      test(t => assert_equals(value.user, expected.user), `${tag}: sec-fetch-user`);
    });
}

function validate_expectations_custom_url(url, header, expected, tag) {
  return fetch(url, header)
    .then(response => response.text())
    .then(text => {
      assert_not_equals(text, "No header has been recorded");
      let value = JSON.parse(text);
      test(t => assert_equals(value.dest, expected.dest), `${tag}: sec-fetch-dest`);
      test(t => assert_equals(value.mode, expected.mode), `${tag}: sec-fetch-mode`);
      test(t => assert_equals(value.site, expected.site), `${tag}: sec-fetch-site`);
      test(t => assert_equals(value.user, expected.user), `${tag}: sec-fetch-user`);
    });
}

/**
 * @param {object} value
 * @param {object} expected
 * @param {string} tag
 **/
function assert_header_equals(value, expected, tag) {
  if (typeof(value) === "string"){
    assert_not_equals(value, "No header has been recorded");
    value = JSON.parse(value);
  }

  test(t => assert_equals(value.dest, expected.dest), `${tag}: sec-fetch-dest`);
  test(t => assert_equals(value.mode, expected.mode), `${tag}: sec-fetch-mode`);
  test(t => assert_equals(value.site, expected.site), `${tag}: sec-fetch-site`);
  test(t => assert_equals(value.user, expected.user), `${tag}: sec-fetch-user`);
}