summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/redirect/redirect-count.any.js
blob: 420f9c0dfcb4065e055f5d519e9675540e95851e (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
// META: global=window,worker
// META: script=../resources/utils.js
// META: script=/common/utils.js
// META: timeout=long

/**
 * Fetches a target that returns response with HTTP status code `statusCode` to
 * redirect `maxCount` times.
 */
function redirectCountTest(maxCount, {statusCode, shouldPass = true} = {}) {
  const desc = `Redirect ${statusCode} ${maxCount} times`;

  const fromUrl = `${RESOURCES_DIR}redirect.py`;
  const toUrl = fromUrl;
  const token1 = token();
  const url = `${fromUrl}?token=${token1}` +
      `&max_age=0` +
      `&redirect_status=${statusCode}` +
      `&max_count=${maxCount}` +
      `&location=${encodeURIComponent(toUrl)}`;

  const requestInit = {'redirect': 'follow'};

  promise_test((test) => {
    return fetch(`${RESOURCES_DIR}clean-stash.py?token=${token1}`)
        .then((resp) => {
          assert_equals(
              resp.status, 200, 'Clean stash response\'s status is 200');

          if (!shouldPass)
            return promise_rejects_js(test, TypeError, fetch(url, requestInit));

          return fetch(url, requestInit)
              .then((resp) => {
                assert_equals(resp.status, 200, 'Response\'s status is 200');
                return resp.text();
              })
              .then((body) => {
                assert_equals(
                    body, maxCount.toString(), `Redirected ${maxCount} times`);
              });
        });
  }, desc);
}

for (const statusCode of [301, 302, 303, 307, 308]) {
  redirectCountTest(20, {statusCode});
  redirectCountTest(21, {statusCode, shouldPass: false});
}

done();