summaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
Diffstat (limited to 'std')
-rw-r--r--std/ws/mod.ts2
-rw-r--r--std/ws/test.ts8
2 files changed, 9 insertions, 1 deletions
diff --git a/std/ws/mod.ts b/std/ws/mod.ts
index 5a8f0bd2e..3128a88b5 100644
--- a/std/ws/mod.ts
+++ b/std/ws/mod.ts
@@ -457,7 +457,7 @@ const kSecChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.~_";
export function createSecKey(): string {
let key = "";
for (let i = 0; i < 16; i++) {
- const j = Math.round(Math.random() * kSecChars.length);
+ const j = Math.floor(Math.random() * kSecChars.length);
key += kSecChars[j];
}
return btoa(key);
diff --git a/std/ws/test.ts b/std/ws/test.ts
index 5a0ff9929..3f5475c80 100644
--- a/std/ws/test.ts
+++ b/std/ws/test.ts
@@ -8,6 +8,7 @@ import {
acceptable,
connectWebSocket,
createSecAccept,
+ createSecKey,
handshake,
OpCode,
readFrame,
@@ -328,6 +329,13 @@ test("WebSocket.send(), WebSocket.ping() should be exclusive", async (): Promise
assertEquals(bytes.equal(third.payload, new Uint8Array([3])), true);
});
+test(function createSecKeyHasCorrectLength(): void {
+ // Note: relies on --seed=86 being passed to deno to reproduce failure in
+ // #4063.
+ const secKey = createSecKey();
+ assertEquals(atob(secKey).length, 16);
+});
+
test("WebSocket should throw SocketClosedError when peer closed connection without close frame", async () => {
const buf = new Buffer();
const eofReader: Deno.Reader = {