OLD | NEW |
1 <p>Tests that asynchronous XMLHttpRequests handle redirects according to the COR
S standard.</p> | 1 <p>Tests that asynchronous XMLHttpRequests handle redirects according to the COR
S standard.</p> |
2 | 2 |
3 <pre id="console"></pre> | 3 <pre id="console"></pre> |
4 <script> | 4 <script> |
5 if (window.layoutTestController) { | 5 if (window.layoutTestController) { |
6 layoutTestController.dumpAsText(); | 6 layoutTestController.dumpAsText(); |
7 layoutTestController.waitUntilDone(); | 7 layoutTestController.waitUntilDone(); |
8 } | 8 } |
9 | 9 |
10 function log(message) | 10 function log(message) |
11 { | 11 { |
12 document.getElementById('console').appendChild(document.createTextNode(messa
ge + '\n')); | 12 document.getElementById('console').appendChild(document.createTextNode(messa
ge + '\n')); |
13 } | 13 } |
14 | 14 |
15 function runTestAsync(url, forcePreflight, expectSuccess) { | 15 function runTestAsync(url, addCustomHeader, expectSuccess) { |
16 log("Testing " + url); | 16 log("Testing " + url); |
17 log("Expecting success: " + expectSuccess); | 17 log("Expecting success: " + expectSuccess); |
18 | 18 |
19 xhr = new XMLHttpRequest(); | 19 xhr = new XMLHttpRequest(); |
20 xhr.open("GET", url, true); | 20 xhr.open("GET", url, true); |
21 if (forcePreflight) | 21 if (addCustomHeader) |
22 xhr.setRequestHeader("x-webkit", "foo"); | 22 xhr.setRequestHeader("x-webkit", "foo"); |
23 | 23 |
24 xhr.onload = function() { | 24 xhr.onload = function() { |
25 log((expectSuccess ? "PASS" : "FAIL") + ": " + xhr.responseText); | 25 log((expectSuccess ? "PASS" : "FAIL") + ": " + xhr.responseText); |
26 nextTest(); | 26 nextTest(); |
27 } | 27 } |
28 xhr.onerror = function() { | 28 xhr.onerror = function() { |
29 log((expectSuccess ? "FAIL" : "PASS") + ": " + xhr.status); | 29 log((expectSuccess ? "FAIL" : "PASS") + ": " + xhr.status); |
30 nextTest(); | 30 nextTest(); |
31 } | 31 } |
32 xhr.send(null); | 32 xhr.send(null); |
33 } | 33 } |
34 | 34 |
35 var simple = false; | 35 var noCustomHeader = false; |
36 var preflight = true; | 36 var addCustomHeader = true; |
37 var succeeds = true; | 37 var succeeds = true; |
38 var fails = false; | 38 var fails = false; |
39 | 39 |
40 var tests = [ | 40 var tests = [ |
41 // 1) Test simple same origin requests that receive cross origin redirects. | 41 // 1) Test simple same origin requests that receive cross origin redirects. |
42 | 42 |
43 // Request receives a cross-origin redirect response without CORS headers. The r
edirect response fails the access check. | 43 // Request receives a cross-origin redirect response without CORS headers. The r
edirect response fails the access check. |
44 ["resources/redirect-cors.php?url=http://127.0.0.1:8000/xmlhttprequest/resources
/access-control-basic-allow-star.cgi", | 44 ["resources/redirect-cors.php?url=http://localhost:8000/xmlhttprequest/resources
/access-control-basic-allow-star.cgi", |
45 simple, fails], | 45 noCustomHeader, fails], |
46 | 46 |
47 // Request receives a cross-origin redirect response with CORS headers. The redi
rect response passes the access check, | 47 // Request receives a cross-origin redirect response with CORS headers. The redi
rect response passes the access check, |
48 // but the resource response fails its access check because the security origin
is a globally unique identifier after | 48 // but the resource response fails its access check because the security origin
is a globally unique identifier after |
49 // the redirect and the same origin XHR has 'allowCredentials' true. | 49 // the redirect and the same origin XHR has 'allowCredentials' true. |
50 ["resources/redirect-cors.php?url=http://127.0.0.1:8000/xmlhttprequest/resources
/access-control-basic-allow-star.cgi&\ | 50 ["resources/redirect-cors.php?url=http://localhost:8000/xmlhttprequest/resources
/access-control-basic-allow-star.cgi&\ |
51 access-control-allow-origin=http://localhost:8000&\ | 51 access-control-allow-origin=http://localhost:8000&\ |
52 access-control-allow-credentials=true", | 52 access-control-allow-credentials=true", |
53 simple, fails], | 53 noCustomHeader, fails], |
54 | 54 |
55 // Same as above, but to a less permissive resource that only allows the request
ing origin. | 55 // Same as above, but to a less permissive resource that only allows the request
ing origin. |
56 ["resources/redirect-cors.php?url=http://127.0.0.1:8000/xmlhttprequest/resources
/access-control-basic-allow.cgi&\ | 56 ["resources/redirect-cors.php?url=http://localhost:8000/xmlhttprequest/resources
/access-control-basic-allow.cgi&\ |
57 access-control-allow-origin=http://localhost:8000&\ | 57 access-control-allow-origin=http://localhost:8000&\ |
58 access-control-allow-credentials=true", | 58 access-control-allow-credentials=true", |
59 simple, fails], | 59 noCustomHeader, fails], |
60 | 60 |
61 // 2) Test simple cross origin requests that receive redirects. | 61 // 2) Test simple cross origin requests that receive redirects. |
62 | 62 |
63 // Receives a redirect response without CORS headers. The redirect response fail
s the access check. | 63 // Receives a redirect response without CORS headers. The redirect response fail
s the access check. |
64 ["http://127.0.0.1:8000/xmlhttprequest/resources/redirect-cors.php?url=http://12
7.0.0.1:8000/xmlhttprequest/resources/access-control-basic-allow-star.cgi", | 64 ["http://localhost:8000/xmlhttprequest/resources/redirect-cors.php?url=http://lo
calhost:8000/xmlhttprequest/resources/access-control-basic-allow-star.cgi", |
65 simple, fails], | 65 noCustomHeader, fails], |
66 | 66 |
67 // Receives a redirect response with CORS headers. The redirect response passes
the access check and the resource response | 67 // Receives a redirect response with CORS headers. The redirect response passes
the access check and the resource response |
68 // passes the access check. | 68 // passes the access check. |
69 ["http://127.0.0.1:8000/xmlhttprequest/resources/redirect-cors.php?url=http://12
7.0.0.1:8000/xmlhttprequest/resources/access-control-basic-allow-star.cgi&\ | 69 ["http://localhost:8000/xmlhttprequest/resources/redirect-cors.php?url=http://lo
calhost:8000/xmlhttprequest/resources/access-control-basic-allow-star.cgi&\ |
70 access-control-allow-origin=http://localhost:8000", | 70 access-control-allow-origin=http://localhost:8000", |
71 simple, succeeds], | 71 noCustomHeader, succeeds], |
72 | 72 |
73 // Receives a redirect response with a URL containing the userinfo production. | 73 // Receives a redirect response with a URL containing the userinfo production. |
74 ["http://127.0.0.1:8000/xmlhttprequest/resources/redirect-cors.php?url=http://us
ername:password@127.0.0.1:8000/xmlhttprequest/resources/access-control-basic-all
ow-star.cgi&\ | 74 ["http://localhost:8000/xmlhttprequest/resources/redirect-cors.php?url=http://us
ername:password@localhost:8000/xmlhttprequest/resources/access-control-basic-all
ow-star.cgi&\ |
75 access-control-allow-origin=http://localhost:8000", | 75 access-control-allow-origin=http://localhost:8000", |
76 simple, fails], | 76 noCustomHeader, fails], |
77 | 77 |
78 // Receives a redirect response with a URL with an unsupported scheme. | 78 // Receives a redirect response with a URL with an unsupported scheme. |
79 ["http://127.0.0.1:8000/xmlhttprequest/resources/redirect-cors.php?url=foo://bar
.cgi&\ | 79 ["http://localhost:8000/xmlhttprequest/resources/redirect-cors.php?url=foo://bar
.cgi&\ |
80 access-control-allow-origin=http://localhost:8000", | 80 access-control-allow-origin=http://localhost:8000", |
81 simple, fails], | 81 noCustomHeader, fails], |
82 | 82 |
83 // 3) Test preflighted cross origin requests that receive redirects. | 83 // 3) Test preflighted cross origin requests that receive redirects. |
84 | 84 |
85 // Receives a redirect response to the preflight request and fails. | 85 // Receives a redirect response to the preflight request and fails. |
86 ["http://127.0.0.1:8000/xmlhttprequest/resources/redirect-cors.php?redirect-pref
light=true&\ | 86 ["http://localhost:8000/xmlhttprequest/resources/redirect-cors.php?redirect-pref
light=true&\ |
87 url=http://127.0.0.1:8000/xmlhttprequest/resources/access-control-basic-allow-
star.cgi&\ | 87 url=http://localhost:8000/xmlhttprequest/resources/access-control-basic-allow-
star.cgi&\ |
88 access-control-allow-origin=*", | 88 access-control-allow-origin=*", |
89 preflight, fails], | 89 addCustomHeader, fails], |
90 | 90 |
91 // Successful preflight and receives a redirect response to the actual request a
nd fails. | 91 // Successful preflight and receives a redirect response to the actual request a
nd fails. |
92 ["http://127.0.0.1:8000/xmlhttprequest/resources/redirect-cors.php?redirect-pref
light=false&\ | 92 ["http://localhost:8000/xmlhttprequest/resources/redirect-cors.php?redirect-pref
light=false&\ |
93 url=http://127.0.0.1:8000/xmlhttprequest/resources/access-control-basic-allow-
star.cgi&\ | 93 url=http://localhost:8000/xmlhttprequest/resources/access-control-basic-allow-
star.cgi&\ |
94 access-control-allow-origin=*&\ | 94 access-control-allow-origin=*&\ |
95 access-control-allow-headers=x-webkit", | 95 access-control-allow-headers=x-webkit", |
96 preflight, fails], | 96 addCustomHeader, fails], |
| 97 |
| 98 // 4) Test same origin requests with a custom header that receive a same origin
redirect. |
| 99 ["resources/redirect-cors.php?url=http://127.0.0.1:8000/xmlhttprequest/resources
/get.txt", |
| 100 addCustomHeader, succeeds], |
97 ] | 101 ] |
98 | 102 |
99 var currentTest = 0; | 103 var currentTest = 0; |
100 | 104 |
101 function nextTest() { | 105 function nextTest() { |
102 if (currentTest < tests.length) | 106 if (currentTest < tests.length) |
103 runTestAsync.apply(null, tests[currentTest++]); | 107 runTestAsync.apply(null, tests[currentTest++]); |
104 else if (window.layoutTestController) | 108 else if (window.layoutTestController) |
105 layoutTestController.notifyDone(); | 109 layoutTestController.notifyDone(); |
106 } | 110 } |
107 | 111 |
108 nextTest(); | 112 nextTest(); |
109 </script> | 113 </script> |
OLD | NEW |