-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
server_test.ts
165 lines (160 loc) · 3.76 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
156
157
158
159
160
161
162
163
164
165
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
import { runIfMain, test } from "./vendor/https/deno.land/std/testing/mod.ts";
import { listenAndServe } from "./server.ts";
import { StringReader } from "./vendor/https/deno.land/std/io/readers.ts";
import { StringWriter } from "./vendor/https/deno.land/std/io/writers.ts";
import {
assertEquals,
assertThrowsAsync
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import { encode } from "./vendor/https/deno.land/std/strings/encode.ts";
import { createAgent } from "./agent.ts";
import copy = Deno.copy;
let port = 8880;
test(async function server() {
const listener = listenAndServe(
{
hostname: "0.0.0.0",
port
},
async req => {
await req.respond({
status: 200,
headers: new Headers({
"content-type": "text/plain",
"content-length": "5"
}),
body: new StringReader("hello")
});
}
);
const agent = createAgent("http://127.0.0.1:" + port);
try {
const { headers, status, body } = await agent.send({
path: "/",
method: "GET"
});
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 {
agent.conn.close();
listener.close();
}
});
test(async function serverKeepAliveTimeout() {
port++;
const listener = listenAndServe(
{
hostname: "0.0.0.0",
port
},
async req => {
await req.respond({
status: 200,
body: "ok"
});
},
{
keepAliveTimeout: 0
}
);
const agent = createAgent(`http://127.0.0.1:${port}`);
try {
const req = {
path: "/",
method: "POST",
headers: new Headers({
host: "deno.land"
}),
body: "hello"
};
const { status, finalize } = await agent.send(req);
await finalize();
assertEquals(200, status);
await assertThrowsAsync(async () => {
await agent.send(req);
});
} finally {
agent.conn.close();
listener.close();
}
});
test(async function serverKeepAliveTimeoutMax() {
port++;
const listener = listenAndServe(
{
hostname: "0.0.0.0",
port
},
async req => {
await req.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();
listener.close();
}
});
test(async function serverConnectionClose() {
port++;
const listener = listenAndServe(
{
hostname: "0.0.0.0",
port
},
async req => {
await req.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();
listener.close();
}
});
runIfMain(import.meta);