diff options
| author | Yusuke Sakurai <kerokerokerop@gmail.com> | 2019-05-15 04:19:12 +0900 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2019-05-14 15:19:11 -0400 |
| commit | e3e9269c76299df99975e17a04b4d1b1ca39dfcb (patch) | |
| tree | c0bab4773a25589edaea12287c6a8422bd5208f1 /ws/test.ts | |
| parent | a3de8c3d8a376049a37f8193c8538acc0d7a88f3 (diff) | |
feat: ws client (denoland/deno_std#394)
Original: https://github.com/denoland/deno_std/commit/782e3f690ffb9ee0dd89a5a64a3f2b753899719b
Diffstat (limited to 'ws/test.ts')
| -rw-r--r-- | ws/test.ts | 52 |
1 files changed, 39 insertions, 13 deletions
diff --git a/ws/test.ts b/ws/test.ts index 5d0cc9093..7b0bd6949 100644 --- a/ws/test.ts +++ b/ws/test.ts @@ -1,19 +1,21 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import "./sha1_test.ts"; - -const { Buffer } = Deno; import { BufReader } from "../io/bufio.ts"; import { assert, assertEquals } from "../testing/asserts.ts"; -import { test } from "../testing/mod.ts"; +import { runIfMain, test } from "../testing/mod.ts"; import { acceptable, createSecAccept, OpCode, readFrame, - unmask + unmask, + writeFrame } from "./mod.ts"; +import { encode } from "../strings/strings.ts"; + +const { Buffer } = Deno; -test(async function testReadUnmaskedTextFrame(): Promise<void> { +test(async function wsReadUnmaskedTextFrame(): Promise<void> { // unmasked single text frame with payload "Hello" const buf = new BufReader( new Buffer(new Uint8Array([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f])) @@ -25,7 +27,7 @@ test(async function testReadUnmaskedTextFrame(): Promise<void> { assertEquals(frame.isLastFrame, true); }); -test(async function testReadMakedTextFrame(): Promise<void> { +test(async function wsReadMaskedTextFrame(): Promise<void> { //a masked single text frame with payload "Hello" const buf = new BufReader( new Buffer( @@ -52,7 +54,7 @@ test(async function testReadMakedTextFrame(): Promise<void> { assertEquals(frame.isLastFrame, true); }); -test(async function testReadUnmaskedSplittedTextFrames(): Promise<void> { +test(async function wsReadUnmaskedSplitTextFrames(): Promise<void> { const buf1 = new BufReader( new Buffer(new Uint8Array([0x01, 0x03, 0x48, 0x65, 0x6c])) ); @@ -71,7 +73,7 @@ test(async function testReadUnmaskedSplittedTextFrames(): Promise<void> { assertEquals(new Buffer(f2.payload).toString(), "lo"); }); -test(async function testReadUnmaksedPingPongFrame(): Promise<void> { +test(async function wsReadUnmaskedPingPongFrame(): Promise<void> { // unmasked ping with payload "Hello" const buf = new BufReader( new Buffer(new Uint8Array([0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f])) @@ -104,7 +106,7 @@ test(async function testReadUnmaksedPingPongFrame(): Promise<void> { assertEquals(new Buffer(pong.payload).toString(), "Hello"); }); -test(async function testReadUnmaksedBigBinaryFrame(): Promise<void> { +test(async function wsReadUnmaskedBigBinaryFrame(): Promise<void> { const a = [0x82, 0x7e, 0x01, 0x00]; for (let i = 0; i < 256; i++) { a.push(i); @@ -117,7 +119,7 @@ test(async function testReadUnmaksedBigBinaryFrame(): Promise<void> { assertEquals(bin.payload.length, 256); }); -test(async function testReadUnmaskedBigBigBinaryFrame(): Promise<void> { +test(async function wsReadUnmaskedBigBigBinaryFrame(): Promise<void> { const a = [0x82, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00]; for (let i = 0; i < 0xffff; i++) { a.push(i); @@ -130,13 +132,13 @@ test(async function testReadUnmaskedBigBigBinaryFrame(): Promise<void> { assertEquals(bin.payload.length, 0xffff + 1); }); -test(async function testCreateSecAccept(): Promise<void> { +test(async function wsCreateSecAccept(): Promise<void> { const nonce = "dGhlIHNhbXBsZSBub25jZQ=="; const d = createSecAccept(nonce); assertEquals(d, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="); }); -test(function testAcceptable(): void { +test(function wsAcceptable(): void { const ret = acceptable({ headers: new Headers({ upgrade: "websocket", @@ -153,7 +155,7 @@ const invalidHeaders = [ { upgrade: "websocket", "sec-websocket-ky": "" } ]; -test(function testAcceptableInvalid(): void { +test(function wsAcceptableInvalid(): void { for (const pat of invalidHeaders) { const ret = acceptable({ headers: new Headers(pat) @@ -161,3 +163,27 @@ test(function testAcceptableInvalid(): void { assertEquals(ret, false); } }); + +test(async function wsWriteReadMaskedFrame(): Promise<void> { + const mask = new Uint8Array([0, 1, 2, 3]); + const msg = "hello"; + const buf = new Buffer(); + const r = new BufReader(buf); + await writeFrame( + { + isLastFrame: true, + mask, + opcode: OpCode.TextFrame, + payload: encode(msg) + }, + buf + ); + const frame = await readFrame(r); + assertEquals(frame.opcode, OpCode.TextFrame); + assertEquals(frame.isLastFrame, true); + assertEquals(frame.mask, mask); + unmask(frame.payload, frame.mask); + assertEquals(frame.payload, encode(msg)); +}); + +runIfMain(import.meta); |
