summaryrefslogtreecommitdiff
path: root/std/http/racing_server_test.ts
blob: 0f228419d4ebdb1bf6a7f196a8b1b5d3d5993c6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { assert, assertEquals } from "../testing/asserts.ts";
import { BufReader, BufWriter } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
const { connect, run, test } = Deno;

let server: Deno.Process;
async function startServer(): Promise<void> {
  server = run({
    args: [Deno.execPath(), "run", "-A", "http/racing_server.ts"],
    stdout: "piped"
  });
  // Once racing server is ready it will write to its stdout.
  assert(server.stdout != null);
  const r = new TextProtoReader(new BufReader(server.stdout));
  const s = await r.readLine();
  assert(s !== Deno.EOF && s.includes("Racing server listening..."));
}
function killServer(): void {
  server.close();
  server.stdout?.close();
}

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",
  "GET / HTTP/1.1\r\n\r\n"
].join("");
const HUGE_BODY_SIZE = 1024 * 1024;
const output = `HTTP/1.1 200 OK
content-length: 6

Step1
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: 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

Step7
`;

test(async function serverPipelineRace(): Promise<void> {
  await startServer();

  const conn = await connect({ port: 4501 });
  const r = new TextProtoReader(new BufReader(conn));
  const w = new BufWriter(conn);
  await w.write(new TextEncoder().encode(input));
  await w.flush();
  const outLines = output.split("\n");
  // length - 1 to disregard last empty line
  for (let i = 0; i < outLines.length - 1; i++) {
    const s = await r.readLine();
    assertEquals(s, outLines[i]);
  }
  killServer();
});