summaryrefslogtreecommitdiffstats
path: root/dom/websocket/tests/websocket_hybi/test_receive-blob.html
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
commit0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch)
treea31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /dom/websocket/tests/websocket_hybi/test_receive-blob.html
parentInitial commit. (diff)
downloadfirefox-esr-upstream/115.8.0esr.tar.xz
firefox-esr-upstream/115.8.0esr.zip
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/websocket/tests/websocket_hybi/test_receive-blob.html')
-rw-r--r--dom/websocket/tests/websocket_hybi/test_receive-blob.html110
1 files changed, 110 insertions, 0 deletions
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>