summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/xhr/send-redirect.htm
blob: 7d73f0f64cc4d7506e607f75d3bba37b9d3d74a9 (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
<!doctype html>
<html>
  <head>
    <title>XMLHttpRequest: send() - Redirects (basics)</title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <link rel="help" href="https://xhr.spec.whatwg.org/#infrastructure-for-the-send()-method" data-tested-assertations="following::dl[1]/dt[2] following::dl[1]/dd[2]/ol/li[1] following::dl[1]/dd[2]/ol/li[2]" />
  </head>
  <body>
    <div id="log"></div>
    <script>
      // https://fetch.spec.whatwg.org/#statuses
      var redirect_codes = new Set([301, 302, 303, 307, 308]);
      function redirect(code) {
        var test = async_test(`${document.title} (${code} does ${redirect_codes.has(code)? "redirect": "not redirect"})`);
        test.step(function() {
          var client = new XMLHttpRequest();
          client.onreadystatechange = function() {
            test.step(function() {
              if(client.readyState == 4) {
                if (redirect_codes.has(code)) {
                  assert_equals(client.getResponseHeader("x-request-method"), "GET");
                  assert_equals(client.getResponseHeader("x-request-content-type"), "application/x-pony");
                  assert_equals(client.status, 200);
                } else {
                  assert_equals(client.getResponseHeader("x-request-method"), null);
                  assert_equals(client.getResponseHeader("x-request-content-type"), null);
                  assert_equals(client.status, code);
                }
                test.done();
              }
            })
          }
          client.open("GET", "resources/redirect.py?location=content.py&code=" + code);
          client.setRequestHeader("Content-Type", "application/x-pony");
          client.send(null);
        })
      }

      for (var number of [300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 350, 399]) {
        redirect(number);
      }
    </script>
  </body>
</html>