-
Notifications
You must be signed in to change notification settings - Fork 230
/
http_server_native_test.ts
44 lines (39 loc) · 1.24 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
// Copyright 2018-2021 the oak authors. All rights reserved. MIT license.
import { assertEquals, assertStrictEquals, test } from "./test_deps.ts";
import { hasNativeHttp, NativeRequest } from "./http_server_native.ts";
function createMockConn() {
return {
localAddr: { transport: "tcp", hostname: "localhost", port: 8000 },
remoteAddr: { transport: "tcp", hostname: "remote", port: 4567 },
rid: 1,
} as Deno.Conn<Deno.NetAddr>;
}
test({
name: "hasNativeHttp()",
fn() {
assertEquals(hasNativeHttp(), "serveHttp" in Deno);
},
});
test({
name: "NativeRequest",
async fn() {
const respondWithStack: Array<Response | Promise<Response>> = [];
const request = new Request("http://localhost:8000/", {
method: "GET",
body: `{"a":"b"}`,
});
const conn = createMockConn();
const nativeRequest = new NativeRequest({
request,
respondWith(v) {
respondWithStack.push(v);
return Promise.resolve();
},
}, conn);
assertEquals(nativeRequest.url, `http://localhost:8000/`);
assertEquals(respondWithStack.length, 1);
const response = new Response("hello deno");
nativeRequest.respond(response);
assertStrictEquals(await respondWithStack[0], response);
},
});