-
Notifications
You must be signed in to change notification settings - Fork 231
/
serve.test.ts
146 lines (138 loc) · 5.45 KB
/
serve.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
import { type State } from "../application.ts";
import { Context } from "../context.ts";
import { assert } from "../deps.ts";
import { NativeRequest } from "../http_server_native_request.ts";
import { type Next } from "../middleware.ts";
import { type RouteParams, Router, type RouterContext } from "../router.ts";
import { assertEquals, assertStrictEquals } from "../test_deps.ts";
import { createMockApp, createMockNext } from "../testing.ts";
import { isNode } from "../util.ts";
import { route, serve } from "./serve.ts";
function setup<R extends string = "/", S extends State = State>(
request: Request,
remoteAddr: Deno.NetAddr = {
transport: "tcp",
hostname: "localhost",
port: 8080,
},
): [Context<S>, RouterContext<R, RouteParams<R>, S>, Next] {
const app = createMockApp();
const serverRequest = new NativeRequest(request, { remoteAddr });
const context = new Context<S>(app, serverRequest, app.state as S);
const routerContext = new Context(
app,
serverRequest,
app.state,
) as RouterContext<R, RouteParams<R>, S>;
Object.assign(routerContext, {
captures: [],
params: { "a": "b" },
router: {} as Router,
routeName: "c",
routePath: "d",
});
return [context, routerContext, createMockNext()];
}
Deno.test({
name: "serve - source request and response are strictly equal",
async fn() {
const request = new Request("http://localhost:8888/index.html");
const [context, , next] = setup(request);
let response: Response;
const mw = serve((req) => {
assertStrictEquals(req, request);
return response = new Response();
});
await mw(context, next);
assertStrictEquals(await context.response.toDomResponse(), response!);
},
});
Deno.test({
name: "serve - context is valid",
async fn() {
const request = new Request("http://localhost:8888/index.html");
const [context, , next] = setup(request);
const mw = serve((_req, ctx) => {
assert(ctx.app);
assert(ctx.state);
assertEquals(typeof ctx.assert, "function");
assertEquals(typeof ctx.throw, "function");
return new Response();
});
await mw(context, next);
},
});
Deno.test({
name: "serve - inspection is expected",
async fn() {
const request = new Request("http://localhost:8888/index.html");
const [context, , next] = setup(request);
const mw = serve((_req, ctx) => {
assertEquals(
Deno.inspect(ctx),
isNode()
? `ServeContext { app: MockApplication {}, ip: 'localhost', ips: [], state: {} }`
: `ServeContext { app: MockApplication {}, ip: "localhost", ips: [], state: {} }`,
);
return new Response();
});
await mw(context, next);
},
});
Deno.test({
name: "route - source request and response are strictly equal",
async fn() {
const request = new Request("http://localhost:8888/index.html");
const [, context, next] = setup(request);
let response: Response;
const mw = route<"/", RouteParams<"/">, State>((req) => {
assertStrictEquals(req, request);
return response = new Response();
});
await mw(context, next);
assertStrictEquals(await context.response.toDomResponse(), response!);
},
});
Deno.test({
name: "route - context is valid",
async fn() {
const request = new Request("http://localhost:8888/book/1234");
const [context, , next] = setup(request);
const router = new Router();
router.get(
"/book/:id",
route((_req, ctx) => {
assertEquals(ctx.captures, ["1234"]);
assertEquals(ctx.params, { id: "1234" });
assertEquals(ctx.routeName, undefined);
assertStrictEquals(ctx.router, router);
assertEquals(ctx.routerPath, undefined);
return new Response();
}),
);
const mw = router.routes();
await mw(context, next);
},
});
Deno.test({
name: "route - inspection is expected",
async fn() {
const request = new Request("http://localhost:8888/book/1234");
const [context, , next] = setup(request);
const router = new Router();
router.get(
"/book/:id",
route((_req, ctx) => {
assertEquals(
Deno.inspect(ctx),
isNode()
? `RouteContext {\n app: MockApplication {},\n captures: [ '1234' ],\n matched: [ [Layer] ],\n ip: 'localhost',\n ips: [],\n params: { id: '1234' },\n router: Router { '#params': {}, '#stack': [Array] },\n routeName: undefined,\n routerPath: undefined,\n state: {}\n}`
: `RouteContext {\n app: MockApplication {},\n captures: [ "1234" ],\n matched: [\n Layer {\n methods: [ "HEAD", "GET" ],\n middleware: [ [AsyncFunction (anonymous)] ],\n options: {\n end: undefined,\n sensitive: undefined,\n strict: undefined,\n ignoreCaptures: undefined\n },\n paramNames: [ "id" ],\n path: "/book/:id",\n regexp: /^\\/book(?:\\/([^\\/#\\?]+?))[\\/#\\?]?$/i\n}\n ],\n ip: "localhost",\n ips: [],\n params: { id: "1234" },\n router: Router {\n "#params": {},\n "#stack": [\n Layer {\n methods: [ "HEAD", "GET" ],\n middleware: [ [AsyncFunction (anonymous)] ],\n options: {\n end: undefined,\n sensitive: undefined,\n strict: undefined,\n ignoreCaptures: undefined\n },\n paramNames: [ "id" ],\n path: "/book/:id",\n regexp: /^\\/book(?:\\/([^\\/#\\?]+?))[\\/#\\?]?$/i\n}\n ]\n},\n routeName: undefined,\n routerPath: undefined,\n state: {}\n}`,
);
return new Response();
}),
);
const mw = router.routes();
await mw(context, next);
},
});