1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 10:09:20 -05:00
denoland-deno/js/net_test.ts
Ryan Dahl 0422b224e8
First pass at support for TCP servers and clients. (#884)
Adds deno.listen(), deno.dial(), deno.Listener and deno.Conn.
2018-10-03 23:58:29 -04:00

37 lines
1.2 KiB
TypeScript

// 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();
});