-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
server_test.ts
155 lines (150 loc) · 3.92 KB
/
server_test.ts
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
import { runIfMain, test } from "https://deno.land/std@v0.7.0/testing/mod.ts";
import { defer } from "./promises.ts";
import { serve } from "./server.ts";
import { StringReader } from "https://deno.land/std@v0.7.0/io/readers.ts";
import { StringWriter } from "https://deno.land/std@v0.7.0/io/writers.ts";
import {
assertEquals,
assertThrowsAsync
} from "https://deno.land/std@v0.7.0/testing/asserts.ts";
import { createResponder } from "./responder.ts";
import { encode } from "https://deno.land/std@v0.7.0/strings/encode.ts";
import { createAgent } from "./agent.ts";
import copy = Deno.copy;
let port = 8880;
test(async function server() {
const d = defer();
(async function() {
for await (const req of serve(`0.0.0.0:${port}`, { cancel: d.promise })) {
await req.respond({
status: 200,
headers: new Headers({
"Content-Type": "text/plain",
"Content-Length": "5"
}),
body: new StringReader("hello")
});
}
})();
try {
const { status, headers, body } = await fetch(`http://127.0.0.1:${port}`);
assertEquals(headers.get("content-length"), "5");
assertEquals(status, 200);
assertEquals(headers.get("content-type"), "text/plain");
const dest = new StringWriter();
await copy(dest, body);
assertEquals(dest.toString(), "hello");
} finally {
d.resolve();
}
});
test(async function serverKeepAliveTimeout() {
port++;
const d = defer();
(async () => {
for await (const req of serve(`0.0.0.0:${port}`, {
cancel: d.promise,
keepAliveTimeout: 0
})) {
await createResponder(req.bufWriter).respond({
status: 200,
headers: new Headers(),
body: encode("ok")
});
}
})();
const agent = createAgent(`http://127.0.0.1:${port}`);
try {
const req = {
path: "/",
method: "POST",
headers: new Headers({
host: "deno.land"
}),
body: encode("hello")
};
const { status, finalize } = await agent.send(req);
await finalize();
assertEquals(200, status);
await assertThrowsAsync(async () => {
await agent.send(req);
});
} finally {
agent.conn.close();
d.resolve();
}
});
test(async function serverKeepAliveTimeoutMax() {
const d = defer();
port++;
(async () => {
for await (const req of serve(`0.0.0.0:${port}`, {
cancel: d.promise
})) {
await createResponder(req.bufWriter).respond({
status: 200,
headers: new Headers(),
body: encode("ok")
});
}
})();
const agent = createAgent(`http://127.0.0.1:${port}`);
try {
const req = {
path: "/",
method: "POST",
headers: new Headers({
host: "deno.land",
"keep-alive": "max=0, timeout=1000"
}),
body: encode("hello")
};
const { status, finalize } = await agent.send(req);
await finalize();
assertEquals(200, status);
await assertThrowsAsync(async () => {
await agent.send(req);
});
} finally {
agent.conn.close();
d.resolve();
}
});
test(async function serverConnectionClose() {
const d = defer();
port++;
(async () => {
for await (const req of serve(`0.0.0.0:${port}`, {
cancel: d.promise
})) {
await createResponder(req.bufWriter).respond({
status: 200,
headers: new Headers(),
body: encode("ok")
});
}
})();
const agent = createAgent(`http://127.0.0.1:${port}`);
try {
const req = {
path: "/",
method: "POST",
headers: new Headers({
host: "deno.land",
connection: "close"
}),
body: encode("hello")
};
const { status, finalize } = await agent.send(req);
await finalize();
assertEquals(200, status);
await assertThrowsAsync(async () => {
await agent.send(req);
});
} finally {
agent.conn.close();
d.resolve();
}
});
runIfMain(import.meta);