test_websocket_mixed_content.html (3453B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></meta> 5 <title>WebSocket mixed content tests - load secure and insecure websockets</title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 8 <script type="text/javascript" src="websocket_helpers.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 10 </head> 11 <body> 12 <div id="container"></div> 13 <iframe id="frame" sandbox="allow-scripts"></iframe> 14 <script class="testbody" type="text/javascript"> 15 16 let iFrameTests = [testWebSocketSecure, testWebSocketInsecure, testWebSocketInsecureDataURI, testSameOriginSandboxInsecure, testSameOriginSandboxSecure, testCrossOriginSandboxInsecure, testCrossOriginSandboxSecure]; 17 18 function nextIFrameTest() { 19 if(!iFrameTests.length) { 20 document.getElementById("frame").removeAttribute("src"); 21 document.getElementById("frame").remove(); 22 SimpleTest.finish(); 23 } 24 else { 25 let test = iFrameTests.shift(); 26 test(); 27 } 28 } 29 30 function testWebSockets () { 31 nextIFrameTest(); 32 } 33 34 function testWebSocketSecure () { 35 let ws = CreateTestWS("wss://example.com/tests/dom/websocket/tests/file_websocket_hello"); 36 ws.onopen = function() { 37 ws.send("data"); 38 } 39 ws.onmessage = function(e) { 40 is(e.data, "Hello world!", "Wrong data"); 41 ws.close(); 42 nextIFrameTest(); 43 } 44 } 45 46 // Negative test: this should fail as the page was loaded over https 47 function testWebSocketInsecure () { 48 try { 49 new WebSocket("ws://mochi.test:8888/tests/dom/websocket/tests/file_websocket_hello"); 50 ok(false, "Should throw DOMException"); 51 } catch (e) { 52 ok(e instanceof DOMException, "DOMException thrown "); 53 nextIFrameTest(); 54 } 55 } 56 57 // Negative test: this should fail as the page was loaded over https 58 function testWebSocketInsecureDataURI() { 59 document.getElementById("frame").src = "https://example.com/tests/dom/websocket/tests/iframe_webworker_wss.html"; 60 onmessage = function(e) { 61 is(e.data, "SecurityError: The operation is insecure.", "SecurityError received"); 62 nextIFrameTest(); 63 } 64 } 65 66 // Negative test: this should fail as the page was loaded over https 67 function testSameOriginSandboxInsecure() { 68 document.getElementById("frame").src = "https://example.com/tests/dom/websocket/tests/iframe_websocket_wss.html?insecure"; 69 onmessage = function(e) { 70 is(e.data, "SecurityError", "ws://URI cannot be used when loaded over https"); 71 nextIFrameTest(); 72 } 73 } 74 75 function testSameOriginSandboxSecure() { 76 document.getElementById("frame").src = "https://example.com/tests/dom/websocket/tests/iframe_websocket_wss.html" 77 onmessage = function(e) { 78 is(e.data, "WS onopen", "wss://URI opened"); 79 nextIFrameTest(); 80 } 81 } 82 83 // Negative test: this should fail as the page was loaded over https 84 function testCrossOriginSandboxInsecure() { 85 document.getElementById("frame").src = "https://example.org/tests/dom/websocket/tests/iframe_websocket_wss.html?insecure"; 86 onmessage = function(e) { 87 is(e.data, "SecurityError", "ws://URI cannot be used when loaded over https"); 88 nextIFrameTest(); 89 } 90 } 91 92 function testCrossOriginSandboxSecure() { 93 document.getElementById("frame").src = "https://example.org/tests/dom/websocket/tests/iframe_websocket_wss.html" 94 95 onmessage = function(e) { 96 is(e.data, "WS onopen", "wss://URI opened"); 97 nextIFrameTest(); 98 } 99 } 100 101 SimpleTest.waitForExplicitFinish(); 102 testWebSockets(); 103 </script> 104 </body> 105 </html>