Unreviewed, rebaseline a few tests after r220779.
[WebKit-https.git] / LayoutTests / http / wpt / beacon / cors / cors-preflight-arraybufferview-failure.html
1 <!doctype html>
2 <html>
3   <head>
4     <meta charset="utf-8">
5     <title>SendBeacon CORS preflight test</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 testCORSPreflightFailure(what) {
30   var testBase = get_host_info().HTTP_REMOTE_ORIGIN + RESOURCES_DIR;
31   var id = self.token();
32   var testUrl = testBase + "beacon-preflight.py?allowCors=0&cmd=put&id=" + id;
33
34   promise_test(function(test) {
35     assert_true(navigator.sendBeacon(testUrl, what), "SendBeacon Succeeded");
36     return pollResult(test, id) .then(result => {
37       assert_equals(result['preflight'], 1, "Received preflight")
38       assert_equals(result['preflight_referer'], document.URL, "Preflight referer header")
39       assert_equals(result['preflight_requested_method'], "POST", "Preflight requested method")
40       let requested_headers = result['preflight_requested_headers'].toLowerCase()
41       assert_true(requested_headers.includes("content-type"), "Content-Type header is requested")
42       assert_true(requested_headers.includes("referer"), "Referer header is requested")
43       assert_true(requested_headers.includes("origin"), "Origin header is requested")
44       assert_equals(result['beacon'], 0, "Did not receive beacon")
45     });
46   }, "CORS preflight failure test");
47 }
48
49 function stringToArrayBufferView(input) {
50   var buffer = new ArrayBuffer(input.length * 2);
51   var view = new Uint16Array(buffer);
52
53   // dumbly copy over the bytes
54   for (var i = 0, len = input.length; i < len; i++) {
55     view[i] = input.charCodeAt(i);
56   }
57   return view;
58 }
59
60 testCORSPreflightFailure(stringToArrayBufferView("123"));
61     </script>
62   </body>
63 </html>