summaryrefslogtreecommitdiff
path: root/js/net_test.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-10-03 23:58:29 -0400
committerGitHub <noreply@github.com>2018-10-03 23:58:29 -0400
commit0422b224e8a55bf259cdbe955d825229496a0687 (patch)
tree1bf47de8ca1a5405a40d2303cff81f72190095bb /js/net_test.ts
parente5e7f0f038494bfe8aa14e31c8d13d6cf481186a (diff)
First pass at support for TCP servers and clients. (#884)
Adds deno.listen(), deno.dial(), deno.Listener and deno.Conn.
Diffstat (limited to 'js/net_test.ts')
-rw-r--r--js/net_test.ts37
1 files changed, 37 insertions, 0 deletions
diff --git a/js/net_test.ts b/js/net_test.ts
new file mode 100644
index 000000000..0b6db7afa
--- /dev/null
+++ b/js/net_test.ts
@@ -0,0 +1,37 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+
+import * as deno from "deno";
+import { testPerm, assert, assertEqual } from "./test_util.ts";
+
+testPerm({ net: true }, function netListenClose() {
+ const listener = deno.listen("tcp", "127.0.0.1:4500");
+ listener.close();
+});
+
+testPerm({ net: true }, async function netDialListen() {
+ let addr = "127.0.0.1:4500";
+ const listener = deno.listen("tcp", addr);
+ listener.accept().then(async conn => {
+ await conn.write(new Uint8Array([1, 2, 3]));
+ conn.close();
+ });
+ const conn = await deno.dial("tcp", addr);
+ const buf = new Uint8Array(1024);
+ const readResult = await conn.read(buf);
+ assertEqual(3, readResult.nread);
+ assertEqual(1, buf[0]);
+ assertEqual(2, buf[1]);
+ assertEqual(3, buf[2]);
+
+ // TODO Currently ReadResult does not properly transmit EOF in the same call.
+ // it requires a second call to get the EOF. Either ReadResult to be an
+ // integer in which 0 signifies EOF or the handler should be modified so that
+ // EOF is properly transmitted.
+ assertEqual(false, readResult.eof);
+
+ const readResult2 = await conn.read(buf);
+ assertEqual(true, readResult2.eof);
+
+ listener.close();
+ conn.close();
+});