summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ws/mod.ts18
-rw-r--r--ws/test.ts14
2 files changed, 22 insertions, 10 deletions
diff --git a/ws/mod.ts b/ws/mod.ts
index 316e19abd..2f85e58f8 100644
--- a/ws/mod.ts
+++ b/ws/mod.ts
@@ -480,15 +480,17 @@ export async function connectWebSocket(
headers: Headers = new Headers()
): Promise<WebSocket> {
const url = new URL(endpoint);
- let { hostname, port } = url;
- if (!port) {
- if (url.protocol === "http" || url.protocol === "ws") {
- port = "80";
- } else if (url.protocol === "https" || url.protocol === "wss") {
- throw new Error("currently https/wss is not supported");
- }
+ let { hostname } = url;
+ let conn: Conn;
+ if (url.protocol === "http:" || url.protocol === "ws:") {
+ const port = parseInt(url.port || "80");
+ conn = await Deno.dial({ hostname, port });
+ } else if (url.protocol === "https:" || url.protocol === "wss:") {
+ const port = parseInt(url.port || "443");
+ conn = await Deno.dialTLS({ hostname, port });
+ } else {
+ throw new Error("ws: unsupported protocol: " + url.protocol);
}
- const conn = await Deno.dial({ hostname, port: Number(port) });
const bufWriter = new BufWriter(conn);
const bufReader = new BufReader(conn);
try {
diff --git a/ws/test.ts b/ws/test.ts
index b8eb42803..4351a391b 100644
--- a/ws/test.ts
+++ b/ws/test.ts
@@ -1,9 +1,10 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { BufReader } from "../io/bufio.ts";
-import { assert, assertEquals } from "../testing/asserts.ts";
+import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
import { runIfMain, test } from "../testing/mod.ts";
import {
acceptable,
+ connectWebSocket,
createSecAccept,
OpCode,
readFrame,
@@ -46,7 +47,6 @@ test(async function wsReadMaskedTextFrame(): Promise<void> {
)
);
const frame = await readFrame(buf);
- console.dir(frame);
assertEquals(frame.opcode, OpCode.TextFrame);
unmask(frame.payload, frame.mask);
assertEquals(new Buffer(frame.payload).toString(), "Hello");
@@ -192,6 +192,16 @@ test(function wsAcceptableInvalid(): void {
);
});
+test("connectWebSocket should throw invalid scheme of url", async (): Promise<
+ void
+> => {
+ await assertThrowsAsync(
+ async (): Promise<void> => {
+ await connectWebSocket("file://hoge/hoge");
+ }
+ );
+});
+
test(async function wsWriteReadMaskedFrame(): Promise<void> {
const mask = new Uint8Array([0, 1, 2, 3]);
const msg = "hello";