summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/xhr/cors-upload.any.js
blob: 3beb0637ccf21f90b62ebc8a2cf7990e227fba9d (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
// META: title=Cross-Origin POST with preflight and FormData body should send body
// META: script=/common/get-host-info.sub.js
"use strict";

function testCorsFormDataUpload(description, path, method, form, headers, withCredentials) {
  const test = async_test(description);
  const client = new XMLHttpRequest();
  const url = corsURL(path);

  client.open(method, url, true);
  client.withCredentials = withCredentials;
  for (const key of Object.keys(headers)) {
    client.setRequestHeader(key, headers[key]);
  }

  client.send(form);

  client.onload = () => {
    test.step(() => {
      assert_equals(client.status, 200);
      assert_regexp_match(client.responseText, /Content-Disposition: form-data/);

      for (const key of form.keys()) {
        assert_regexp_match(client.responseText, new RegExp(key));
        assert_regexp_match(client.responseText, new RegExp(form.get(key)));
      }
    });
    test.done();
  };
}

function corsURL(path) {
  const url = new URL(path, location.href);
  url.hostname = get_host_info().REMOTE_HOST;
  return url.href;
}

const form = new FormData();
form.append("key", "value");

testCorsFormDataUpload(
  "Cross-Origin POST FormData body but no preflight",
  "resources/echo-content-cors.py",
  "POST",
  form,
  {},
  false
);

testCorsFormDataUpload(
  "Cross-Origin POST with preflight and FormData body",
  "resources/echo-content-cors.py",
  "POST",
  form,
  {
    Authorization: "Bearer access-token"
  },
  true
);