send-redirect.htm (3550B)
1 <!doctype html> 2 <html> 3 <head> 4 <title>XMLHttpRequest: send() - Redirects (basics)</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <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]" /> 8 </head> 9 <body> 10 <div id="log"></div> 11 <script> 12 // https://fetch.spec.whatwg.org/#statuses 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"; 26 } 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); 34 } 35 t.done(); 36 } 37 }); 38 client.open(method, `resources/redirect.py?location=${redirectLocation}&code=${code}`); 39 client.setRequestHeader("Content-Type", "application/x-pony"); 40 client.send(null); 41 }, `${document.title} (${code}, ${method}, ${redirectLocation})`); 42 } 43 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); 80 </script> 81 </body> 82 </html>