| <!doctype html> |
| <html> |
| <head> |
| <meta charset="utf-8"> |
| <title>SendBeacon Content-Type header</title> |
| <script src=/resources/testharness.js></script> |
| <script src=/resources/testharnessreport.js></script> |
| </head> |
| <body> |
| <script src="/common/utils.js"></script> |
| <script src="/common/get-host-info.sub.js"></script> |
| <script> |
| var RESOURCES_DIR = "/beacon/resources/"; |
| |
| function pollResult(test, id) { |
| var checkUrl = RESOURCES_DIR + "content-type.py?cmd=get&id=" + id; |
| |
| return new Promise(resolve => { |
| step_timeout(test.step_func(() => { |
| fetch(checkUrl).then(response => { |
| response.text().then(body => { |
| resolve(body); |
| }); |
| }); |
| }), 1000); |
| }); |
| } |
| |
| function testContentTypeHeader(what, contentType, title) { |
| var testBase = RESOURCES_DIR; |
| var id = self.token(); |
| var testUrl = testBase + "content-type.py?cmd=put&id=" + id; |
| |
| promise_test(function(test) { |
| assert_true(navigator.sendBeacon(testUrl, what), "SendBeacon Succeeded"); |
| return pollResult(test, id) .then(result => { |
| if (contentType == "multipart/form-data") { |
| assert_true(result.startsWith(contentType), "Correct Content-Type header result"); |
| } else { |
| assert_equals(result, contentType, "Correct Content-Type header result"); |
| } |
| }); |
| }, "Test content-type header for a body " + title); |
| } |
| |
| function stringToArrayBufferView(input) { |
| var buffer = new ArrayBuffer(input.length * 2); |
| var view = new Uint16Array(buffer); |
| |
| // dumbly copy over the bytes |
| for (var i = 0, len = input.length; i < len; i++) { |
| view[i] = input.charCodeAt(i); |
| } |
| return view; |
| } |
| |
| function stringToArrayBuffer(input) { |
| var buffer = new ArrayBuffer(input.length * 2); |
| var view = new Uint16Array(buffer); |
| |
| // dumbly copy over the bytes |
| for (var i = 0, len = input.length; i < len; i++) { |
| view[i] = input.charCodeAt(i); |
| } |
| return buffer; |
| } |
| |
| function stringToBlob(input) { |
| return new Blob([input], {type: "text/plain"}); |
| } |
| |
| function stringToFormData(input) { |
| var formdata = new FormData(); |
| formdata.append(input, new Blob(['hi'])); |
| return formdata; |
| } |
| |
| function stringToURLSearchParams(input) |
| { |
| return new URLSearchParams(input); |
| } |
| |
| testContentTypeHeader("hi!", "text/plain;charset=UTF-8", "string"); |
| testContentTypeHeader(stringToArrayBufferView("123"), "", "ArrayBufferView"); |
| testContentTypeHeader(stringToArrayBuffer("123"), "", "ArrayBuffer"); |
| testContentTypeHeader(stringToBlob("123"), "text/plain", "Blob"); |
| testContentTypeHeader(stringToFormData("qwerty"), "multipart/form-data", "FormData"); |
| testContentTypeHeader(stringToURLSearchParams("key1=value1&key2=value2"), "application/x-www-form-urlencoded;charset=UTF-8", "URLSearchParams"); |
| </script> |
| </body> |
| </html> |