diff options
Diffstat (limited to 'dom/websocket/tests/websocket_hybi')
7 files changed, 424 insertions, 0 deletions
diff --git a/dom/websocket/tests/websocket_hybi/file_binary-frames_wsh.py b/dom/websocket/tests/websocket_hybi/file_binary-frames_wsh.py new file mode 100644 index 0000000000..16ace4d3a6 --- /dev/null +++ b/dom/websocket/tests/websocket_hybi/file_binary-frames_wsh.py @@ -0,0 +1,19 @@ +import six +from mod_pywebsocket import common, stream + + +def web_socket_do_extra_handshake(request): + pass + + +def web_socket_transfer_data(request): + messages_to_send = ["Hello, world!", "", all_distinct_bytes()] + for message in messages_to_send: + message = six.b(message) + # FIXME: Should use better API to send binary messages when pywebsocket supports it. + header = stream.create_header(common.OPCODE_BINARY, len(message), 1, 0, 0, 0, 0) + request.connection.write(header + message) + + +def all_distinct_bytes(): + return "".join([chr(i) for i in range(256)]) diff --git a/dom/websocket/tests/websocket_hybi/file_check-binary-messages_wsh.py b/dom/websocket/tests/websocket_hybi/file_check-binary-messages_wsh.py new file mode 100644 index 0000000000..d72ec1f462 --- /dev/null +++ b/dom/websocket/tests/websocket_hybi/file_check-binary-messages_wsh.py @@ -0,0 +1,27 @@ +import six +from mod_pywebsocket import msgutil + + +def web_socket_do_extra_handshake(request): + pass # Always accept. + + +def web_socket_transfer_data(request): + expected_messages = ["Hello, world!", "", all_distinct_bytes()] + + for test_number, expected_message in enumerate(expected_messages): + expected_message = six.b(expected_message) + message = msgutil.receive_message(request) + if message == expected_message: + msgutil.send_message(request, "PASS: Message #{:d}.".format(test_number)) + else: + msgutil.send_message( + request, + "FAIL: Message #{:d}: Received unexpected message: {!r}".format( + test_number, message + ), + ) + + +def all_distinct_bytes(): + return "".join([chr(i) for i in range(256)]) diff --git a/dom/websocket/tests/websocket_hybi/mochitest.toml b/dom/websocket/tests/websocket_hybi/mochitest.toml new file mode 100644 index 0000000000..1b00e0c8b3 --- /dev/null +++ b/dom/websocket/tests/websocket_hybi/mochitest.toml @@ -0,0 +1,17 @@ +[DEFAULT] +skip-if = [ + "http3", + "http2", +] +support-files = [ + "file_binary-frames_wsh.py", + "file_check-binary-messages_wsh.py", +] + +["test_receive-arraybuffer.html"] + +["test_receive-blob.html"] + +["test_send-arraybuffer.html"] + +["test_send-blob.html"] diff --git a/dom/websocket/tests/websocket_hybi/test_receive-arraybuffer.html b/dom/websocket/tests/websocket_hybi/test_receive-arraybuffer.html new file mode 100644 index 0000000000..6d465b7677 --- /dev/null +++ b/dom/websocket/tests/websocket_hybi/test_receive-arraybuffer.html @@ -0,0 +1,97 @@ +<!DOCTYPE html> +<html> +<head> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> + +<p id="display"></p> +<div id="content" style="display: none"> +</div> +<pre id="test"> + +<script class="testbody" type="text/javascript"> + +function debug(msg) { + ok(1, msg); +} + +function createArrayBufferContainingHelloWorld() +{ + var hello = "Hello, world!"; + var array = new Uint8Array(hello.length); + for (var i = 0; i < hello.length; ++i) + array[i] = hello.charCodeAt(i); + return array.buffer; +} + +function createEmptyArrayBuffer() +{ + return new ArrayBuffer(0); +} + +function createArrayBufferContainingAllDistinctBytes() +{ + var array = new Uint8Array(256); + for (var i = 0; i < 256; ++i) + array[i] = i; + return array.buffer; +} + +var ws = new WebSocket("ws://mochi.test:8888/tests/dom/websocket/tests/websocket_hybi/file_binary-frames"); +ws.binaryType = "arraybuffer"; +is(ws.binaryType, "arraybuffer", "should be equal to 'arraybuffer'"); + +var closeEvent; +var receivedMessages = []; +var expectedValues = [createArrayBufferContainingHelloWorld(), createEmptyArrayBuffer(), createArrayBufferContainingAllDistinctBytes()]; + +ws.onmessage = function(event) +{ + receivedMessages.push(event.data); +}; + +ws.onclose = function(event) +{ + closeEvent = event; + + is(receivedMessages.length, expectedValues.length, "lengths not equal"); + for (var i = 0; i < expectedValues.length; ++i) + check(i); + SimpleTest.finish(); +}; + +var responseType; + +function check(index) +{ + debug("Checking message #" + index + "."); + ok(receivedMessages[index] instanceof ArrayBuffer, + "Should receive an arraybuffer!"); + checkArrayBuffer(index, receivedMessages[index], expectedValues[index]); +} + +var actualArray; +var expectedArray; + +function checkArrayBuffer(testIndex, actual, expected) +{ + actualArray = new Uint8Array(actual); + expectedArray = new Uint8Array(expected); + is(actualArray.length, expectedArray.length, "lengths not equal"); + // Print only the first mismatched byte in order not to flood console. + for (var i = 0; i < expectedArray.length; ++i) { + if (actualArray[i] != expectedArray[i]) { + ok(false, "Value mismatch: actualArray[" + i + "] = " + actualArray[i] + ", expectedArray[" + i + "] = " + expectedArray[i]); + return; + } + } + ok(true, "Passed: Message #" + testIndex + "."); +} + +SimpleTest.waitForExplicitFinish(); + +</script> +</body> +</html> diff --git a/dom/websocket/tests/websocket_hybi/test_receive-blob.html b/dom/websocket/tests/websocket_hybi/test_receive-blob.html new file mode 100644 index 0000000000..5589633a7c --- /dev/null +++ b/dom/websocket/tests/websocket_hybi/test_receive-blob.html @@ -0,0 +1,110 @@ +<!DOCTYPE html> +<html> +<head> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> + +<p id="display"></p> +<div id="content" style="display: none"> +</div> +<pre id="test"> + +<script class="testbody" type="text/javascript"> + +function debug(msg) { + ok(true, msg); +} + +function createArrayBufferContainingHelloWorld() +{ + var hello = "Hello, world!"; + var array = new Uint8Array(hello.length); + for (var i = 0; i < hello.length; ++i) + array[i] = hello.charCodeAt(i); + return array.buffer; +} + +function createEmptyArrayBuffer() +{ + return new ArrayBuffer(0); +} + +function createArrayBufferContainingAllDistinctBytes() +{ + var array = new Uint8Array(256); + for (var i = 0; i < 256; ++i) + array[i] = i; + return array.buffer; +} + +var ws = new WebSocket("ws://mochi.test:8888/tests/dom/websocket/tests/websocket_hybi/file_binary-frames"); +is(ws.binaryType, "blob", "should be 'blob'"); + +var closeEvent; +var receivedMessages = []; +var expectedValues = [createArrayBufferContainingHelloWorld(), createEmptyArrayBuffer(), createArrayBufferContainingAllDistinctBytes()]; + +ws.onmessage = function(event) +{ + receivedMessages.push(event.data); +}; + +ws.onclose = function(event) +{ + closeEvent = event; + + is(receivedMessages.length, expectedValues.length, "lengths not same"); + check(0); +}; + +var responseType; + +function check(index) +{ + if (index == expectedValues.length) { + SimpleTest.finish(); + return; + } + + debug("Checking message #" + index + "."); + ok(receivedMessages[index] instanceof Blob, + "We should be receiving a Blob"); + var reader = new FileReader(); + reader.readAsArrayBuffer(receivedMessages[index]); + reader.onload = function(event) + { + checkArrayBuffer(index, reader.result, expectedValues[index]); + check(index + 1); + }; + reader.onerror = function(event) + { + ok(false, "Failed to read blob: error code = " + reader.error.code); + check(index + 1); + }; +} + +var actualArray; +var expectedArray; + +function checkArrayBuffer(testIndex, actual, expected) +{ + actualArray = new Uint8Array(actual); + expectedArray = new Uint8Array(expected); + is(actualArray.length, expectedArray.length, "lengths not same"); + // Print only the first mismatched byte in order not to flood console. + for (var i = 0; i < expectedArray.length; ++i) { + if (actualArray[i] != expectedArray[i]) { + ok(false, "Value mismatch: actualArray[" + i + "] = " + actualArray[i] + ", expectedArray[" + i + "] = " + expectedArray[i]); + return; + } + } + ok(true, "Passed: Message #" + testIndex + "."); +} + +SimpleTest.waitForExplicitFinish(); + +</script> +</body> +</html> diff --git a/dom/websocket/tests/websocket_hybi/test_send-arraybuffer.html b/dom/websocket/tests/websocket_hybi/test_send-arraybuffer.html new file mode 100644 index 0000000000..6c71ca5415 --- /dev/null +++ b/dom/websocket/tests/websocket_hybi/test_send-arraybuffer.html @@ -0,0 +1,82 @@ +<!DOCTYPE html> +<html> +<head> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> + +<p id="display"></p> +<div id="content" style="display: none"> +</div> +<pre id="test"> + +<script class="testbody" type="text/javascript"> + +function debug(msg) { + ok(1, msg); +} + +function startsWith(target, prefix) +{ + return target.indexOf(prefix) === 0; +} + +function createArrayBufferContainingHelloWorld() +{ + var hello = "Hello, world!"; + var array = new Uint8Array(hello.length); + for (var i = 0; i < hello.length; ++i) + array[i] = hello.charCodeAt(i); + return array.buffer; +} + +function createEmptyArrayBuffer() +{ + return new ArrayBuffer(0); +} + +function createArrayBufferContainingAllDistinctBytes() +{ + var array = new Uint8Array(256); + for (var i = 0; i < 256; ++i) + array[i] = i; + return array.buffer; +} + +var ws = new WebSocket("ws://mochi.test:8888/tests/dom/websocket/tests/websocket_hybi/file_check-binary-messages"); +var closeEvent; + +ws.onopen = function() +{ + ok(true, "onopen reached"); + ws.send(createArrayBufferContainingHelloWorld()); + ws.send(createEmptyArrayBuffer()); + ws.send(createArrayBufferContainingAllDistinctBytes()); +}; + +ws.onmessage = function(event) +{ + var message = event.data; + if (startsWith(message, "PASS")) + ok(true, message); + else + ok(false, message); +}; + +ws.onclose = function(event) +{ + ok(event.wasClean, "should have closed cleanly"); + SimpleTest.finish(); +}; + +ws.onerror = function(event) +{ + ok(false, "onerror should not have been called"); +}; + +SimpleTest.waitForExplicitFinish(); + +</script> +</body> +</html> diff --git a/dom/websocket/tests/websocket_hybi/test_send-blob.html b/dom/websocket/tests/websocket_hybi/test_send-blob.html new file mode 100644 index 0000000000..6af1e2df03 --- /dev/null +++ b/dom/websocket/tests/websocket_hybi/test_send-blob.html @@ -0,0 +1,72 @@ +<!DOCTYPE html> +<html> +<head> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> + +<p id="display"> +</p> +<div id="content" style="display: none"> +</div> +<pre id="test"> + +<script class="testbody" type="text/javascript"> + +function startsWith(target, prefix) +{ + return target.indexOf(prefix) === 0; +} + +function distinctBytes() +{ + var array = new Array(); + for (var i = 0; i < 256; ++i) + array[i] = i; + // Concatenates chars into a single binary string + return String.fromCharCode.apply(null, array); +} + +var filesToCreate = [ + {name: "hellofile", data: "Hello, world!"}, + {name: "emptyfile"}, + {name: "allchars", data: distinctBytes()}, +]; + +SpecialPowers.createFiles(filesToCreate, function (files) { + var ws = new WebSocket("ws://mochi.test:8888/tests/dom/websocket/tests/websocket_hybi/file_check-binary-messages"); + var closeEvent; + + ws.onopen = function() + { + ws.send(files[0]); + ws.send(files[1]); + ws.send(files[2]); + }; + + ws.onmessage = function(event) + { + var message = event.data; + if (startsWith(message, "PASS")) + ok(true, message); + else + ok(false, message); + }; + + ws.onclose = function(event) + { + ok(event.wasClean, "should have closed cleanly"); + SimpleTest.finish(); + }; +}, +function (msg) { + ok(false, "Failed to create files: " + msg); + SimpleTest.finish(); +}); + +SimpleTest.waitForExplicitFinish(); + +</script> +</body> +</html> |