test_beaconOriginHeader.html (2222B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Bug 1280692 - navigator.sendBeacon() should send origin header</title> 5 <!-- Including SimpleTest.js so we can use waitForExplicitFinish !--> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 8 </head> 9 <body> 10 <p id="display"></p> 11 <div id="content" style="visibility: hidden"> 12 <iframe style="width:100%;" id="testframe"></iframe> 13 </div> 14 15 <script class="testbody" type="text/javascript"> 16 17 SimpleTest.waitForExplicitFinish(); 18 19 const BEACON_URL = "http://example.com/tests/dom/tests/mochitest/beacon/beacon-originheader-handler.sjs"; 20 // server returns any origin-header or 'no-header' if there is no header sent. 21 const ORIGIN_HEADER = "http://mochi.test:8888"; 22 23 /* Description of the test: 24 * We call sendBeacon() cross origin and make sure that the 25 * origin header is actually set in the request. 26 * 27 * Since sendBeacon() does not expect any response, we are storing any 28 * header on the server (*.sjs) and use an XMLHttpRequest to actually 29 * retrieve the potentially set header back from the server. We assert 30 * that the header is indeed *not* sent with the request. Since sendBeacon() 31 * and also the XMLHttpRequest() are performed in an asynchronous fashion, 32 * there is no guarantee that the sendBeacon() is actually executed before 33 * the XMLHttpRequest(). 34 * Hence the xhr-response might be processed asynchronously. 35 */ 36 37 SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, runTest); 38 39 function queryHeaderFromServer() { 40 var xhr = new XMLHttpRequest(); 41 xhr.open("GET", "beacon-originheader-handler.sjs?queryheader", true); 42 xhr.onload = function() { 43 is(xhr.responseText, ORIGIN_HEADER, "SendBeacon should send origin header"); 44 SimpleTest.finish(); 45 }; 46 xhr.onerror = function() { 47 ok(false, "xhr request returned error"); 48 SimpleTest.finish(); 49 }; 50 xhr.send(); 51 } 52 53 function runTest() { 54 // generate data and send beacon 55 var formData = new FormData(); 56 formData.append('name', 'value'); 57 navigator.sendBeacon(BEACON_URL, formData); 58 59 // start quering the result from the server 60 queryHeaderFromServer(); 61 } 62 63 </script> 64 </pre> 65 </body> 66 </html>