-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathhttp_server_native.test.ts
88 lines (74 loc) · 2.48 KB
/
http_server_native.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
// Copyright 2018-2025 the oak authors. All rights reserved. MIT license.
import { assertEquals, assertStrictEquals, unreachable } from "./deps_test.ts";
import { Server } from "./http_server_native.ts";
import { NativeRequest } from "./http_server_native_request.ts";
import { Application } from "./application.ts";
import { isNode } from "./utils/type_guards.ts";
function createMockNetAddr(): Deno.NetAddr {
return { transport: "tcp", hostname: "remote", port: 4567 };
}
Deno.test({
name: "NativeRequest",
ignore: isNode(),
async fn() {
const respondWithStack: Array<Response | Promise<Response>> = [];
const request = new Request("http://localhost:8000/", {
method: "POST",
body: `{"a":"b"}`,
});
const remoteAddr = createMockNetAddr();
const nativeRequest = new NativeRequest(request, { remoteAddr });
assertEquals(nativeRequest.url, `/`);
const response = new Response("hello deno");
nativeRequest.respond(response);
respondWithStack.push(await nativeRequest.response);
assertStrictEquals(await respondWithStack[0], response);
},
});
Deno.test({
name: "HttpServer closes gracefully after serving requests",
ignore: isNode(),
async fn() {
const abortController = new AbortController();
const app = new Application();
const listenOptions = { port: 4505, signal: abortController.signal };
const server = new Server(app, listenOptions);
server.listen();
const expectedBody = "test-body";
(async () => {
for await (const nativeRequest of server) {
nativeRequest.respond(new Response(expectedBody));
}
})();
try {
const response = await fetch(`http://localhost:${listenOptions.port}`);
assertEquals(await response.text(), expectedBody);
} catch (e) {
console.error(e);
unreachable();
} finally {
abortController.abort();
}
},
});
Deno.test({
name:
"HttpServer manages errors from mis-use in the application handler gracefully",
ignore: isNode(),
async fn() {
const app = new Application();
const listenOptions = { port: 4506 };
const server = new Server(app, listenOptions);
server.listen();
(async () => {
for await (const nativeRequest of server) {
// deno-lint-ignore no-explicit-any
nativeRequest.respond(null as any);
}
})();
const res = await fetch(`http://localhost:${listenOptions.port}`);
assertEquals(res.status, 500);
await res.text();
return server.close();
},
});