2020-01-18 12:35:12 -05:00
|
|
|
const { connect, run } = Deno;
|
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-08-13 20:03:29 -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.
|
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();
|
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();
|
2020-02-07 02:23:38 -05:00
|
|
|
server.stdout?.close();
|
2019-04-13 15:23:56 -04:00
|
|
|
}
|
|
|
|
|
2019-10-05 12:02:34 -04:00
|
|
|
const input = `GET / HTTP/1.1
|
2019-04-13 15:23:56 -04:00
|
|
|
|
|
|
|
GET / HTTP/1.1
|
|
|
|
|
|
|
|
GET / HTTP/1.1
|
|
|
|
|
|
|
|
GET / HTTP/1.1
|
|
|
|
|
|
|
|
`;
|
|
|
|
const HUGE_BODY_SIZE = 1024 * 1024;
|
2019-10-05 12:02:34 -04:00
|
|
|
const output = `HTTP/1.1 200 OK
|
2019-04-13 15:23:56 -04:00
|
|
|
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
|
|
|
|
`;
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function serverPipelineRace(): Promise<void> {
|
2019-04-13 15:23:56 -04:00
|
|
|
await startServer();
|
|
|
|
|
2020-01-18 12:35:12 -05:00
|
|
|
const conn = await connect({ port: 4501 });
|
2019-04-13 15:23:56 -04:00
|
|
|
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();
|
|
|
|
});
|