|
10 | 10 | <div id="log"></div>
|
11 | 11 | <script>
|
12 | 12 | // https://fetch.spec.whatwg.org/#statuses
|
13 |
| - var redirect_codes = new Set([301, 302, 303, 307, 308]); |
14 |
| - function redirect(code) { |
15 |
| - var test = async_test(`${document.title} (${code} does ${redirect_codes.has(code)? "redirect": "not redirect"})`); |
16 |
| - test.step(function() { |
17 |
| - var client = new XMLHttpRequest(); |
18 |
| - client.onreadystatechange = function() { |
19 |
| - test.step(function() { |
20 |
| - if(client.readyState == 4) { |
21 |
| - if (redirect_codes.has(code)) { |
22 |
| - assert_equals(client.getResponseHeader("x-request-method"), "GET"); |
23 |
| - assert_equals(client.getResponseHeader("x-request-content-type"), "application/x-pony"); |
24 |
| - assert_equals(client.status, 200); |
25 |
| - } else { |
26 |
| - assert_equals(client.getResponseHeader("x-request-method"), null); |
27 |
| - assert_equals(client.getResponseHeader("x-request-content-type"), null); |
28 |
| - assert_equals(client.status, code); |
| 13 | + const redirect_codes = new Set([301, 302, 303, 307, 308]); |
| 14 | + function maybeRedirect(code, method="GET", redirectLocation="content.py") { |
| 15 | + async_test(t => { |
| 16 | + const client = new XMLHttpRequest(); |
| 17 | + client.onreadystatechange = t.step_func(() => { |
| 18 | + if(client.readyState == 4) { |
| 19 | + if (redirect_codes.has(code)) { |
| 20 | + let expected_method = method; |
| 21 | + let expected_content_type = "application/x-pony"; |
| 22 | + if ((method == "POST" && (code == "301" || code == "302")) || |
| 23 | + (code == "303" && method != "GET" && method != "HEAD")) { |
| 24 | + expected_method = "GET"; |
| 25 | + expected_content_type = "NO"; |
29 | 26 | }
|
30 |
| - test.done(); |
| 27 | + assert_equals(client.getResponseHeader("x-request-method"), expected_method); |
| 28 | + assert_equals(client.getResponseHeader("x-request-content-type"), expected_content_type); |
| 29 | + assert_equals(client.status, 200); |
| 30 | + } else { |
| 31 | + assert_equals(client.getResponseHeader("x-request-method"), null); |
| 32 | + assert_equals(client.getResponseHeader("x-request-content-type"), null); |
| 33 | + assert_equals(client.status, code); |
31 | 34 | }
|
32 |
| - }) |
33 |
| - } |
34 |
| - client.open("GET", "resources/redirect.py?location=content.py&code=" + code); |
| 35 | + t.done(); |
| 36 | + } |
| 37 | + }); |
| 38 | + client.open(method, `resources/redirect.py?location=${redirectLocation}&code=${code}`); |
35 | 39 | client.setRequestHeader("Content-Type", "application/x-pony");
|
36 | 40 | client.send(null);
|
37 |
| - }) |
| 41 | + }, `${document.title} (${code}, ${method}, ${redirectLocation})`); |
38 | 42 | }
|
39 | 43 |
|
40 |
| - for (var number of [300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 350, 399]) { |
41 |
| - redirect(number); |
42 |
| - } |
| 44 | + maybeRedirect(300); |
| 45 | + maybeRedirect(301); |
| 46 | + maybeRedirect(302); |
| 47 | + maybeRedirect(303); |
| 48 | + maybeRedirect(304); |
| 49 | + maybeRedirect(305); |
| 50 | + maybeRedirect(306); |
| 51 | + maybeRedirect(307); |
| 52 | + maybeRedirect(308); |
| 53 | + maybeRedirect(309); |
| 54 | + maybeRedirect(310); |
| 55 | + maybeRedirect(350); |
| 56 | + maybeRedirect(399); |
| 57 | + maybeRedirect(301, "POST"); |
| 58 | + maybeRedirect(302, "POST"); |
| 59 | + maybeRedirect(303, "POST"); |
| 60 | + maybeRedirect(307, "POST"); |
| 61 | + maybeRedirect(301, "HEAD"); |
| 62 | + maybeRedirect(302, "HEAD"); |
| 63 | + maybeRedirect(303, "HEAD"); |
| 64 | + maybeRedirect(307, "HEAD"); |
| 65 | + |
| 66 | + const redirectedLocation = encodeURIComponent("redirect.py?location=content.py"); |
| 67 | + maybeRedirect(301, "GET", redirectedLocation); |
| 68 | + maybeRedirect(302, "GET", redirectedLocation); |
| 69 | + maybeRedirect(303, "GET", redirectedLocation); |
| 70 | + maybeRedirect(307, "GET", redirectedLocation); |
| 71 | + maybeRedirect(301, "POST", redirectedLocation); |
| 72 | + maybeRedirect(302, "POST", redirectedLocation); |
| 73 | + maybeRedirect(303, "POST", redirectedLocation); |
| 74 | + maybeRedirect(307, "POST", redirectedLocation); |
| 75 | + maybeRedirect(303, "CHICKEN", redirectedLocation); |
| 76 | + maybeRedirect(301, "HEAD", redirectedLocation); |
| 77 | + maybeRedirect(302, "HEAD", redirectedLocation); |
| 78 | + maybeRedirect(303, "HEAD", redirectedLocation); |
| 79 | + maybeRedirect(307, "HEAD", redirectedLocation); |
43 | 80 | </script>
|
44 | 81 | </body>
|
45 | 82 | </html>
|
0 commit comments