Use dumpJSConsoleLogInStdErr=true webkit-test-runner option for non-imported tests...
[WebKit-https.git] / LayoutTests / http / wpt / beacon / cors / cors-redirect-failure.html
1 <!doctype html><!-- webkit-test-runner [ dumpJSConsoleLogInStdErr=true ] -->
2 <html>
3   <head>
4     <meta charset="utf-8">
5     <title>SendBeacon CORS checks on redirect</title>
6     <script src=/resources/testharness.js></script>
7     <script src=/resources/testharnessreport.js></script>
8   </head>
9   <body>
10     <script src="/common/utils.js"></script>
11     <script src="/common/get-host-info.sub.js"></script>
12     <script>
13 var RESOURCES_DIR = "/WebKit/beacon/resources/";
14
15 function pollResult(test, id) {
16   var checkUrl = RESOURCES_DIR + "beacon-preflight.py?cmd=get&id=" + id;
17
18   return new Promise(resolve => {
19     step_timeout(test.step_func(() => {
20       fetch(checkUrl).then(response => {
21         response.json().then(body => {
22           resolve(body);
23         });
24       });
25     }), 1000);
26   });
27 }
28
29 function testCORSRedirectFailure(what) {
30   var testBase = get_host_info().HTTP_REMOTE_ORIGIN + RESOURCES_DIR;
31   var id = self.token();
32   var target = encodeURIComponent(testBase + "beacon-preflight.py?allowCors=1&cmd=put&id=" + id);
33
34   var testUrl = testBase + "redirect.py?redirect_status=307&disallowCorsOnResponseNotPreflight&location=" + target;
35
36   promise_test(function(test) {
37     assert_true(navigator.sendBeacon(testUrl, what), "SendBeacon Succeeded");
38     return pollResult(test, id) .then(result => {
39       assert_equals(result['preflight'], 0, "Received preflight")
40       assert_equals(result['beacon'], 0, "Did not receive beacon")
41     });
42   }, "CORS redirection failure test");
43 }
44
45 function testCORSRedirectSuccess(what) {
46   var testBase = get_host_info().HTTP_REMOTE_ORIGIN + RESOURCES_DIR;
47   var id = self.token();
48   var target = encodeURIComponent(testBase + "beacon-preflight.py?allowCors=1&cmd=put&id=" + id);
49
50   var testUrl = testBase + "redirect.py?redirect_status=307&location=" + target;
51
52   promise_test(function(test) {
53     assert_true(navigator.sendBeacon(testUrl, what), "SendBeacon Succeeded");
54     return pollResult(test, id) .then(result => {
55       assert_equals(result['preflight'], 1, "Received preflight")
56       assert_equals(result['beacon'], 1, "Did not receive beacon")
57     });
58   }, "CORS redirection success test");
59 }
60
61 let blob = new Blob(["123"], {type: "text/plain-specific"});
62 testCORSRedirectFailure(blob);
63 testCORSRedirectSuccess(blob);
64     </script>
65   </body>
66 </html>