deno.land / x / servest@v1.3.4 / server_test.ts

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
166
167
168
169
170
171
172
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.import { handleKeepAliveConn, listenAndServe, ServerRequest,} from "./server.ts";import { assertEquals, assertThrowsAsync,} from "./vendor/https/deno.land/std/testing/asserts.ts";import { encode } from "./_util.ts";import { createAgent } from "./agent.ts";import { readResponse, writeRequest } from "./serveio.ts";import { BufReader } from "./vendor/https/deno.land/std/io/bufio.ts";import { deferred, delay } from "./vendor/https/deno.land/std/async/mod.ts";import { Buffer } from "./vendor/https/deno.land/std/io/buffer.ts";import { group } from "./_test_util.ts";
let port = 8880;const handler = (req: ServerRequest) => req.respond({ status: 200, body: "ok" });group("server", (t) => { t.test("basic", async function server() { port++; const listener = listenAndServe({ port }, handler); const agent = createAgent("http://127.0.0.1:" + port); try { const { headers, status, text } = await agent.send({ path: "/", method: "GET", }); assertEquals(headers.get("content-length"), "2"); assertEquals(status, 200); assertEquals(headers.get("content-type"), "text/plain; charset=UTF-8"); assertEquals(await text(), "ok"); } finally { agent.conn.close(); listener.close(); } }); t.test("keepAliveTimeout", async () => { port++; const l = listenAndServe( { port }, handler, { keepAliveTimeout: 10 }, ); 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, body } = await agent.send(req); await body.close(); assertEquals(200, status); await delay(100); await assertThrowsAsync(async () => { await agent.send(req); }); } finally { agent.conn.close(); l.close(); } }); t.test( "serverKeepAliveTimeoutMax", async function serverKeepAliveTimeoutMax() { port++; const listener = listenAndServe({ port }, handler); 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, body } = await agent.send(req); await body.close(); assertEquals(200, status); await assertThrowsAsync(async () => { await agent.send(req); }); } finally { agent.conn.close(); listener.close(); } }, ); t.test("serverConnectionClose", async function serverConnectionClose() { port++; const listener = listenAndServe({ port }, handler); const agent = createAgent(`http://127.0.0.1:${port}`); try { const req = { path: "/", method: "POST", headers: new Headers({ host: "deno.land", connection: "close", }), body: "hello", }; const { status, body } = await agent.send(req); await body.close(); assertEquals(200, status); await assertThrowsAsync(async () => { await agent.send(req); }); } finally { agent.conn.close(); listener.close(); } }); t.test("handleKeepAliveConn should respond exclusively", async () => { const r = new Buffer(); const w = new Buffer(); await writeRequest(r, { method: "GET", url: "http://localhost/?q=1", }); await writeRequest(r, { method: "GET", url: "http://localhost/?q=2", }); await writeRequest(r, { method: "GET", url: "http://localhost/?q=3", }); const d = deferred<void>(); let latch = 3; handleKeepAliveConn(dummyConn(r, w), async (req) => { const url = new URL(req.url, "http://dummy"); const i = url.searchParams.get("q")!; req.respond({ status: 200, body: "resp:" + i }).then(() => { if (--latch === 0) { d.resolve(); } }); }); await d; const responseReader = new BufReader(w); const resp1 = await readResponse(responseReader); assertEquals(await resp1.text(), "resp:1"); const resp2 = await readResponse(responseReader); assertEquals(await resp2.text(), "resp:2"); const resp3 = await readResponse(responseReader); assertEquals(await resp3.text(), "resp:3"); });});
function dummyConn(r: Deno.Reader, w: Deno.Writer): Deno.Conn { const addr: Deno.Addr = { hostname: "0.0.0.0", port: 1, transport: "tcp" }; return { rid: -1, close(): void {}, async closeWrite(): Promise<void> {}, localAddr: addr, remoteAddr: addr, read: (p) => r.read(p), write: (p) => w.write(p), };}
servest

Version Info

Tagged at
2 years ago