diff options
Diffstat (limited to 'cli/tests/unit')
-rw-r--r-- | cli/tests/unit/tls_test.ts | 62 |
1 files changed, 57 insertions, 5 deletions
diff --git a/cli/tests/unit/tls_test.ts b/cli/tests/unit/tls_test.ts index 4b3dba082..26a166801 100644 --- a/cli/tests/unit/tls_test.ts +++ b/cli/tests/unit/tls_test.ts @@ -15,6 +15,9 @@ import { TextProtoReader } from "../../../test_util/std/textproto/mod.ts"; const encoder = new TextEncoder(); const decoder = new TextDecoder(); +const cert = await Deno.readTextFile("cli/tests/testdata/tls/localhost.crt"); +const key = await Deno.readTextFile("cli/tests/testdata/tls/localhost.key"); +const caCerts = [await Deno.readTextFile("cli/tests/testdata/tls/RootCA.pem")]; async function sleep(msec: number) { await new Promise((res, _rej) => setTimeout(res, msec)); @@ -184,11 +187,60 @@ Deno.test( }, ); - const conn = await Deno.connectTls({ - hostname, - port, - caCerts: [Deno.readTextFileSync("cli/tests/testdata/tls/RootCA.pem")], - }); + const conn = await Deno.connectTls({ hostname, port, caCerts }); + assert(conn.rid > 0); + const w = new BufWriter(conn); + const r = new BufReader(conn); + const body = `GET / HTTP/1.1\r\nHost: ${hostname}:${port}\r\n\r\n`; + const writeResult = await w.write(encoder.encode(body)); + assertEquals(body.length, writeResult); + await w.flush(); + const tpr = new TextProtoReader(r); + const statusLine = await tpr.readLine(); + assert(statusLine !== null, `line must be read: ${String(statusLine)}`); + const m = statusLine.match(/^(.+?) (.+?) (.+?)$/); + assert(m !== null, "must be matched"); + const [_, proto, status, ok] = m; + assertEquals(proto, "HTTP/1.1"); + assertEquals(status, "200"); + assertEquals(ok, "OK"); + const headers = await tpr.readMIMEHeader(); + assert(headers !== null); + const contentLength = parseInt(headers.get("content-length")!); + const bodyBuf = new Uint8Array(contentLength); + await r.readFull(bodyBuf); + assertEquals(decoder.decode(bodyBuf), "Hello World\n"); + conn.close(); + listener.close(); + await resolvable; + }, +); +Deno.test( + { permissions: { read: false, net: true } }, + async function listenTlsWithCertAndKey() { + const resolvable = deferred(); + const hostname = "localhost"; + const port = 3500; + + const listener = Deno.listenTls({ hostname, port, cert, key }); + + const response = encoder.encode( + "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n", + ); + + listener.accept().then( + async (conn) => { + assert(conn.remoteAddr != null); + assert(conn.localAddr != null); + await conn.write(response); + setTimeout(() => { + conn.close(); + resolvable.resolve(); + }, 0); + }, + ); + + const conn = await Deno.connectTls({ hostname, port, caCerts }); assert(conn.rid > 0); const w = new BufWriter(conn); const r = new BufReader(conn); |