2019-04-13 15:23:56 -04:00
|
|
|
import { assert, assertEquals } from "../testing/asserts.ts";
|
2020-02-24 22:49:39 -05:00
|
|
|
import { BufReader, BufWriter } from "../io/bufio.ts";
|
2019-04-13 15:23:56 -04:00
|
|
|
import { TextProtoReader } from "../textproto/mod.ts";
|
2020-02-26 10:48:35 -05:00
|
|
|
const { connect, run, test } = Deno;
|
2019-04-13 15:23:56 -04:00
|
|
|
|
2020-06-09 07:18:18 -04:00
|
|
|
let server: Deno.Process<Deno.RunOptions & { stdout: "piped" }>;
|
2019-04-13 15:23:56 -04:00
|
|
|
async function startServer(): Promise<void> {
|
|
|
|
server = run({
|
2020-04-30 11:23:40 -04:00
|
|
|
// TODO(lucacasonato): remove unstable when stabilized
|
|
|
|
cmd: [Deno.execPath(), "run", "--unstable", "-A", "http/racing_server.ts"],
|
2020-03-28 13:03:49 -04:00
|
|
|
stdout: "piped",
|
2019-04-13 15:23:56 -04:00
|
|
|
});
|
2019-07-28 07:35:47 -04:00
|
|
|
// Once racing server is ready it will write to its stdout.
|
2020-02-07 02:23:38 -05:00
|
|
|
assert(server.stdout != null);
|
|
|
|
const r = new TextProtoReader(new BufReader(server.stdout));
|
2019-05-23 22:04:06 -04:00
|
|
|
const s = await r.readLine();
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(s !== null && s.includes("Racing server listening..."));
|
2019-04-13 15:23:56 -04:00
|
|
|
}
|
|
|
|
function killServer(): void {
|
|
|
|
server.close();
|
2020-06-09 07:18:18 -04:00
|
|
|
server.stdout.close();
|
2019-04-13 15:23:56 -04:00
|
|
|
}
|
|
|
|
|
2020-02-24 22:49:39 -05:00
|
|
|
const input = [
|
|
|
|
"GET / HTTP/1.1\r\n\r\n",
|
|
|
|
"GET / HTTP/1.1\r\n\r\n",
|
|
|
|
"GET / HTTP/1.1\r\n\r\n",
|
|
|
|
"POST / HTTP/1.1\r\ncontent-length: 4\r\n\r\ndeno",
|
|
|
|
"POST / HTTP/1.1\r\ntransfer-encoding: chunked\r\n\r\n4\r\ndeno\r\n0\r\n\r\n",
|
|
|
|
"POST / HTTP/1.1\r\ntransfer-encoding: chunked\r\ntrailer: deno\r\n\r\n4\r\ndeno\r\n0\r\n\r\ndeno: land\r\n\r\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
"GET / HTTP/1.1\r\n\r\n",
|
2020-02-24 22:49:39 -05:00
|
|
|
].join("");
|
2019-04-13 15:23:56 -04:00
|
|
|
const HUGE_BODY_SIZE = 1024 * 1024;
|
2019-10-05 12:02:34 -04:00
|
|
|
const output = `HTTP/1.1 200 OK
|
2020-02-24 22:49:39 -05:00
|
|
|
content-length: 6
|
2019-04-13 15:23:56 -04:00
|
|
|
|
2020-02-24 22:49:39 -05:00
|
|
|
Step1
|
2019-04-13 15:23:56 -04:00
|
|
|
HTTP/1.1 200 OK
|
|
|
|
content-length: ${HUGE_BODY_SIZE}
|
|
|
|
|
|
|
|
${"a".repeat(HUGE_BODY_SIZE)}HTTP/1.1 200 OK
|
|
|
|
content-length: ${HUGE_BODY_SIZE}
|
|
|
|
|
|
|
|
${"b".repeat(HUGE_BODY_SIZE)}HTTP/1.1 200 OK
|
2020-02-24 22:49:39 -05:00
|
|
|
content-length: 6
|
|
|
|
|
|
|
|
Step4
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
content-length: 6
|
|
|
|
|
|
|
|
Step5
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
content-length: 6
|
|
|
|
|
|
|
|
Step6
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
content-length: 6
|
2019-04-13 15:23:56 -04:00
|
|
|
|
2020-02-24 22:49:39 -05:00
|
|
|
Step7
|
2019-04-13 15:23:56 -04:00
|
|
|
`;
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("serverPipelineRace", async function (): Promise<void> {
|
2019-04-13 15:23:56 -04:00
|
|
|
await startServer();
|
|
|
|
|
2020-03-24 12:24:58 -04:00
|
|
|
const conn = await connect({ port: 4501 });
|
2019-04-13 15:23:56 -04:00
|
|
|
const r = new TextProtoReader(new BufReader(conn));
|
2020-02-24 22:49:39 -05:00
|
|
|
const w = new BufWriter(conn);
|
|
|
|
await w.write(new TextEncoder().encode(input));
|
|
|
|
await w.flush();
|
2019-04-13 15:23:56 -04:00
|
|
|
const outLines = output.split("\n");
|
|
|
|
// length - 1 to disregard last empty line
|
|
|
|
for (let i = 0; i < outLines.length - 1; i++) {
|
2019-05-23 22:04:06 -04:00
|
|
|
const s = await r.readLine();
|
2019-04-13 15:23:56 -04:00
|
|
|
assertEquals(s, outLines[i]);
|
|
|
|
}
|
|
|
|
killServer();
|
2020-03-18 19:25:55 -04:00
|
|
|
conn.close();
|
2019-04-13 15:23:56 -04:00
|
|
|
});
|