2019-04-13 15:23:56 -04:00
|
|
|
const { dial, run } = Deno;
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
import { test, runIfMain } from "../testing/mod.ts";
|
2019-04-13 15:23:56 -04:00
|
|
|
import { assert, assertEquals } from "../testing/asserts.ts";
|
2019-07-07 15:20:41 -04:00
|
|
|
import { BufReader } from "../io/bufio.ts";
|
2019-04-13 15:23:56 -04:00
|
|
|
import { TextProtoReader } from "../textproto/mod.ts";
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
let server: Deno.Process;
|
2019-04-13 15:23:56 -04:00
|
|
|
async function startServer(): Promise<void> {
|
|
|
|
server = run({
|
2019-07-29 04:46:21 -04:00
|
|
|
args: [Deno.execPath, "run", "-A", "http/racing_server.ts"],
|
2019-04-13 15:23:56 -04:00
|
|
|
stdout: "piped"
|
|
|
|
});
|
2019-07-28 07:35:47 -04:00
|
|
|
// Once racing server is ready it will write to its stdout.
|
2019-05-30 08:59:30 -04:00
|
|
|
const r = new TextProtoReader(new BufReader(server.stdout!));
|
2019-05-23 22:04:06 -04:00
|
|
|
const s = await r.readLine();
|
2019-07-07 15:20:41 -04:00
|
|
|
assert(s !== Deno.EOF && s.includes("Racing server listening..."));
|
2019-04-13 15:23:56 -04:00
|
|
|
}
|
|
|
|
function killServer(): void {
|
|
|
|
server.close();
|
2019-05-30 08:59:30 -04:00
|
|
|
server.stdout!.close();
|
2019-04-13 15:23:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let input = `GET / HTTP/1.1
|
|
|
|
|
|
|
|
GET / HTTP/1.1
|
|
|
|
|
|
|
|
GET / HTTP/1.1
|
|
|
|
|
|
|
|
GET / HTTP/1.1
|
|
|
|
|
|
|
|
`;
|
|
|
|
const HUGE_BODY_SIZE = 1024 * 1024;
|
|
|
|
let output = `HTTP/1.1 200 OK
|
|
|
|
content-length: 8
|
|
|
|
|
|
|
|
Hello 1
|
|
|
|
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
|
|
|
|
content-length: 8
|
|
|
|
|
|
|
|
World 4
|
|
|
|
`;
|
|
|
|
|
|
|
|
test(async function serverPipelineRace(): Promise<void> {
|
|
|
|
await startServer();
|
|
|
|
|
|
|
|
const conn = await dial("tcp", "127.0.0.1:4501");
|
|
|
|
const r = new TextProtoReader(new BufReader(conn));
|
|
|
|
await conn.write(new TextEncoder().encode(input));
|
|
|
|
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();
|
|
|
|
});
|
2019-05-23 22:04:06 -04:00
|
|
|
|
|
|
|
runIfMain(import.meta);
|