2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-02-23 08:51:04 +09:00
|
|
|
import { assert, assertEquals } from "../../testing/asserts.ts";
|
|
|
|
import { TextProtoReader } from "../../textproto/mod.ts";
|
|
|
|
import { BufReader } from "../../io/bufio.ts";
|
2020-05-09 13:34:47 +01:00
|
|
|
import { delay } from "../../async/delay.ts";
|
2020-09-27 06:22:32 -04:00
|
|
|
import { dirname, fromFileUrl, resolve } from "../../path/mod.ts";
|
2020-09-10 02:57:49 +08:00
|
|
|
|
|
|
|
const moduleDir = resolve(dirname(fromFileUrl(import.meta.url)));
|
2020-02-23 08:51:04 +09:00
|
|
|
|
2020-06-09 12:18:18 +01:00
|
|
|
async function startServer(): Promise<
|
|
|
|
Deno.Process<Deno.RunOptions & { stdout: "piped" }>
|
|
|
|
> {
|
2020-03-19 00:25:55 +01:00
|
|
|
const server = Deno.run({
|
2020-04-30 17:23:40 +02:00
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
2020-05-04 13:03:30 +02:00
|
|
|
"run",
|
2020-11-20 18:01:58 +01:00
|
|
|
"--quiet",
|
2020-04-30 17:23:40 +02:00
|
|
|
"--allow-net",
|
|
|
|
"--allow-read",
|
|
|
|
"server.ts",
|
|
|
|
],
|
2020-09-10 02:57:49 +08:00
|
|
|
cwd: moduleDir,
|
2020-03-29 04:03:49 +11:00
|
|
|
stdout: "piped",
|
2020-02-23 08:51:04 +09:00
|
|
|
});
|
|
|
|
try {
|
|
|
|
assert(server.stdout != null);
|
|
|
|
const r = new TextProtoReader(new BufReader(server.stdout));
|
|
|
|
const s = await r.readLine();
|
2020-04-28 17:40:43 +01:00
|
|
|
assert(s !== null && s.includes("chat server starting"));
|
2020-03-20 14:38:34 +01:00
|
|
|
} catch (err) {
|
2020-06-09 12:18:18 +01:00
|
|
|
server.stdout.close();
|
2020-02-23 08:51:04 +09:00
|
|
|
server.close();
|
|
|
|
}
|
|
|
|
|
2020-03-19 00:25:55 +01:00
|
|
|
return server;
|
|
|
|
}
|
2020-02-23 08:51:04 +09:00
|
|
|
|
2020-06-12 20:23:38 +01:00
|
|
|
Deno.test({
|
2020-04-30 15:17:53 +04:30
|
|
|
name: "[examples/chat] GET / should serve html",
|
2020-03-15 12:03:25 +00:00
|
|
|
async fn() {
|
2020-03-19 00:25:55 +01:00
|
|
|
const server = await startServer();
|
|
|
|
try {
|
2020-03-24 17:24:58 +01:00
|
|
|
const resp = await fetch("http://127.0.0.1:8080/");
|
2020-03-19 00:25:55 +01:00
|
|
|
assertEquals(resp.status, 200);
|
|
|
|
assertEquals(resp.headers.get("content-type"), "text/html");
|
2020-04-10 09:51:17 -04:00
|
|
|
const html = await resp.text();
|
2020-03-19 00:25:55 +01:00
|
|
|
assert(html.includes("ws chat example"), "body is ok");
|
|
|
|
} finally {
|
|
|
|
server.close();
|
2020-06-09 12:18:18 +01:00
|
|
|
server.stdout.close();
|
2020-03-19 00:25:55 +01:00
|
|
|
}
|
|
|
|
await delay(10);
|
2020-03-29 04:03:49 +11:00
|
|
|
},
|
2020-03-15 12:03:25 +00:00
|
|
|
});
|
|
|
|
|
2020-06-12 20:23:38 +01:00
|
|
|
Deno.test({
|
2020-04-30 15:17:53 +04:30
|
|
|
name: "[examples/chat] GET /ws should upgrade conn to ws",
|
2020-03-15 12:03:25 +00:00
|
|
|
async fn() {
|
2020-03-19 00:25:55 +01:00
|
|
|
const server = await startServer();
|
2020-09-09 21:33:38 +02:00
|
|
|
let ws: WebSocket;
|
2020-03-19 00:25:55 +01:00
|
|
|
try {
|
2020-09-09 21:33:38 +02:00
|
|
|
ws = new WebSocket("ws://127.0.0.1:8080/ws");
|
2020-11-24 09:31:10 +11:00
|
|
|
await new Promise<void>((resolve) => {
|
2020-09-09 21:33:38 +02:00
|
|
|
ws.onmessage = ((message) => {
|
|
|
|
assertEquals(message.data, "Connected: [1]");
|
|
|
|
ws.onmessage = ((message) => {
|
|
|
|
assertEquals(message.data, "[1]: Hello");
|
|
|
|
ws.close();
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
ws.send("Hello");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
2020-03-19 00:25:55 +01:00
|
|
|
} finally {
|
|
|
|
server.close();
|
2020-06-09 12:18:18 +01:00
|
|
|
server.stdout.close();
|
2020-03-19 00:25:55 +01:00
|
|
|
}
|
2020-03-29 04:03:49 +11:00
|
|
|
},
|
2020-03-15 12:03:25 +00:00
|
|
|
});
|