summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/cors/cors-preflight-cache.any.js
blob: ce6a169d8146750b183c9210d1b2041fac879248 (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
// META: script=/common/utils.js
// META: script=../resources/utils.js
// META: script=/common/get-host-info.sub.js

var cors_url = get_host_info().HTTP_REMOTE_ORIGIN +
              dirname(location.pathname) +
              RESOURCES_DIR +
              "preflight.py";

promise_test((test) => {
  var uuid_token = token();
  var request_url =
      cors_url + "?token=" + uuid_token + "&max_age=12000&allow_methods=POST" +
      "&allow_headers=x-test-header";
  return fetch(cors_url + "?token=" + uuid_token + "&clear-stash")
    .then(() => {
      return fetch(
        new Request(request_url,
                    {
                      mode: "cors",
                      method: "POST",
                      headers: [["x-test-header", "test1"]]
                    }));
    })
    .then((resp) => {
      assert_equals(resp.status, 200, "Response's status is 200");
      assert_equals(resp.headers.get("x-did-preflight"), "1", "Preflight request has been made");
      return fetch(cors_url + "?token=" + uuid_token + "&clear-stash");
    })
    .then((res) => res.text())
    .then((txt) => {
      assert_equals(txt, "1", "Server stash must be cleared.");
      return fetch(
        new Request(request_url,
                    {
                      mode: "cors",
                      method: "POST",
                      headers: [["x-test-header", "test2"]]
                    }));
    })
    .then((resp) => {
      assert_equals(resp.status, 200, "Response's status is 200");
      assert_equals(resp.headers.get("x-did-preflight"), "0", "Preflight request has not been made");
      return fetch(cors_url + "?token=" + uuid_token + "&clear-stash");
    });
});