summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/xhr/access-control-and-redirects-async-same-origin.any.js
blob: 11d38fa451e68dec722cc3aded171c70dd23013f (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
// META: title=Tests that asynchronous XMLHttpRequests handle redirects according to the CORS standard.
// META: script=/common/get-host-info.sub.js

    function runTest(test, path, credentials, expectSuccess) {
      const xhr = new XMLHttpRequest();
      xhr.withCredentials = credentials;
      xhr.open("GET", "resources/redirect.py?location=" + get_host_info().HTTP_REMOTE_ORIGIN + path, true);

      xhr.onload = test.step_func_done(function() {
        assert_true(expectSuccess);
        assert_equals(xhr.responseText, "PASS: Cross-domain access allowed.");
      });
      xhr.onerror = test.step_func_done(function() {
        assert_false(expectSuccess);
        assert_equals(xhr.status, 0);
      });
      xhr.send(null);
    }

    const withoutCredentials = false;
    const withCredentials = true;
    const succeeds = true;
    const fails = false;

    // Test simple same origin requests that receive cross origin redirects.

    // The redirect response passes the access check.
    async_test(t => {
      runTest(t, "/xhr/resources/access-control-basic-allow-star.py",
          withoutCredentials, succeeds)
    }, "Request without credentials is redirected to a cross-origin response with Access-Control-Allow-Origin=* (with star)");

    // The redirect response fails the access check because credentials were sent.
    async_test(t => {
      runTest(t, "/xhr/resources/access-control-basic-allow-star.py",
          withCredentials, fails)
    }, "Request with credentials is redirected to a cross-origin response with Access-Control-Allow-Origin=* (with star)");

    // The redirect response passes the access check.
    async_test(t => {
      runTest(t, "/xhr/resources/access-control-basic-allow.py",
          withoutCredentials, succeeds)
    }, "Request without credentials is redirected to a cross-origin response with a specific Access-Control-Allow-Origin");

    // The redirect response passes the access check.
    async_test(t => {
      runTest(t, "/xhr/resources/access-control-basic-allow.py",
          withCredentials, succeeds)
    }, "Request with credentials is redirected to a cross-origin response with a specific Access-Control-Allow-Origin");

    // forbidding credentials. The redirect response passes the access check.
    async_test(t => {
      runTest(t, "/xhr/resources/access-control-basic-allow-no-credentials.py",
          withoutCredentials, succeeds)
    }, "Request without credentials is redirected to a cross-origin response with a specific Access-Control-Allow-Origin (no credentials)");

    // forbidding credentials. The redirect response fails the access check.
    async_test(t => {
      runTest(t, "/xhr/resources/access-control-basic-allow-no-credentials.py",
          withCredentials, fails)
    }, "Request with credentials is redirected to a cross-origin response with a specific Access-Control-Allow-Origin (no credentials)");