test_websocket_permessage_deflate.html (2692B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Basic test of permessage compression websocket extension</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 7 </head> 8 <body onload="testDeflate()"> 9 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=792831">Mozilla Bug </a> 10 <p id="display"></p> 11 <div id="content" style="display: none"> 12 </div> 13 <pre id="test"> 14 <script class="testbody" type="text/javascript"> 15 16 var ws; 17 var textMessage = "This is a text message"; 18 var binaryMessage = "This is a binary message"; 19 var testIdx = 0; 20 var sendText = true; 21 22 let tests = [ 23 // enable PMCE 24 [ true, "ws://mochi.test:8888/tests/dom/websocket/tests/file_websocket_permessage_deflate" ], 25 // server rejects offered PMCE 26 [ false, "ws://mochi.test:8888/tests/dom/websocket/tests/file_websocket_permessage_deflate_rejected" ], 27 // server returns parameters in the handshake 28 [ true, "ws://mochi.test:8888/tests/dom/websocket/tests/file_websocket_permessage_deflate_params" ] 29 ] 30 31 function ab2str(buf) { 32 return String.fromCharCode.apply(null, new Uint16Array(buf)); 33 } 34 35 function str2ab(str) { 36 var buf = new ArrayBuffer(str.length*2); 37 var bufView = new Uint16Array(buf); 38 for (var i=0, strLen=str.length; i<strLen; i++) { 39 bufView[i] = str.charCodeAt(i); 40 } 41 return buf; 42 } 43 44 function sendMessage() { 45 if (sendText) { 46 ws.send(textMessage); 47 } else { 48 ws.binaryType = "arraybuffer"; 49 ws.send(str2ab(binaryMessage)); 50 } 51 } 52 53 function testDeflate() { 54 ws = new WebSocket(tests[testIdx][1]); 55 56 ws.onopen = function() { 57 if (tests[testIdx][0]) { 58 is(ws.extensions, "permessage-deflate", "permessage-deflate not negotiated!"); 59 } else { 60 is(ws.extensions, "", "permessage-deflate should not be negotiated!"); 61 } 62 63 sendMessage(); 64 } 65 66 ws.onclose = function(e) { 67 if (!e.wasClean) { 68 ok(false, "Connection should be closed cleanly!"); 69 SimpleTest.finish(); 70 } 71 } 72 73 ws.onerror = function() { 74 ok(false, "onerror called!"); 75 SimpleTest.finish(); 76 } 77 78 ws.onmessage = function(e) { 79 if (sendText) { 80 is(e.data, textMessage, "Text message not received successfully!"); 81 sendText = false; 82 sendMessage(); 83 } else { 84 ok(e.data instanceof ArrayBuffer, "Should receive an arraybuffer!"); 85 is(ab2str(e.data), binaryMessage, "Binary message not received successfully!"); 86 ws.close(); 87 88 sendText = true; 89 testIdx++; 90 if (testIdx < tests.length) { 91 testDeflate(); 92 } else { 93 SimpleTest.finish(); 94 } 95 } 96 } 97 } 98 99 SimpleTest.waitForExplicitFinish(); 100 101 </script> 102 </pre> 103 </body> 104 </html>