-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain_test.ts
179 lines (137 loc) · 4.97 KB
/
chain_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
173
174
175
176
177
178
179
import { Chain, chain } from "./chain.ts";
import type { ChainableHandler } from "./types.ts";
import {
assertEquals,
assertNotEquals,
assertSpyCalls,
describe,
it,
spy,
} from "./_dev_deps.ts";
describe("chain", () => {
it("should return cloned response when handler is not exists", async () => {
const response = new Response();
const res = await chain(new Request("http://localhost"), response);
assertNotEquals(response, res);
assertEquals(response.url, res.url);
});
it("should return first handler called response", async () => {
const response = new Response();
const promise = chain(
new Request("http://localhost"),
response,
() => new Response("test"),
);
assertNotEquals(response, promise);
const newResponse = await promise;
assertEquals(await newResponse.text(), "test");
});
it("should call next handler when the previous handler call next", () => {
const nextHandler = spy<unknown, [Request], Response>(() => new Response());
chain(
new Request("http://localhost"),
new Response(),
(_, next) => next(),
nextHandler,
);
assertSpyCalls(nextHandler, 1);
});
it("should not call next handler when the previous handler not call next", () => {
const nextHandler = spy<unknown, [Request], Response>(() => new Response());
chain(
new Request("http://localhost"),
new Response(),
() => new Response(),
nextHandler,
);
assertSpyCalls(nextHandler, 0);
});
it("should pass new cloned request object to next handler", () => {
const nextHandler = spy<unknown, [Request], Response>(() => new Response());
const initRequest = new Request("http://localhost");
const nextRequest = new Request("http://proxy");
chain(initRequest, new Response(), (_, next) => {
return next(nextRequest);
}, (request) => {
// cloned
assertNotEquals(request, nextRequest);
assertEquals(request.url, nextRequest.url);
return nextHandler(request);
});
assertSpyCalls(nextHandler, 1);
});
it("should not mutate request when not pass response object itself to next handler", () => {
const nextHandler = spy<unknown, [Request], Response>(() => new Response());
const initRequest = new Request("http://localhost");
chain(initRequest, new Response(), (request, next) => {
request.headers.append("server", "deno");
return next();
}, (request) => {
assertEquals(request.headers.get("server"), null);
return nextHandler(request);
});
assertSpyCalls(nextHandler, 1);
});
it("should mutate request when pass response object itself to next handler", () => {
const nextHandler = spy<unknown, [Request], Response>(() => new Response());
const initRequest = new Request("http://localhost");
chain(initRequest, new Response(), (request, next) => {
request.headers.append("server", "deno");
return next(request);
}, (request) => {
assertEquals(request.headers.get("server"), "deno");
return nextHandler(request);
});
assertSpyCalls(nextHandler, 1);
});
it("should pass response by next handler but it is cloned", async () => {
const initRequest = new Request("http://localhost");
const initResponse = new Response("test");
const spyInit = spy<unknown, [Request], Response>(() => initResponse);
const response = await chain(
initRequest,
new Response(),
async (_, next) => {
const response = await next();
spyInit(_);
assertNotEquals(response, initResponse);
assertEquals(response.url, initResponse.url);
return response;
},
() => initResponse,
);
assertSpyCalls(spyInit, 1);
assertNotEquals(response, initResponse);
assertEquals(response.url, initResponse.url);
});
});
describe("Chain", () => {
it("should init empty handlers", () => {
assertEquals(new Chain().handlers, []);
});
it("should return registered handlers", () => {
const handlers = [() => new Response()];
assertEquals(new Chain(...handlers).handlers, handlers);
});
it("should return cloned response", async () => {
const initResponse = new Response();
const response = await new Chain(() => initResponse).respond(
new Request("http://localhost"),
);
assertNotEquals(initResponse, response);
assertEquals(initResponse.url, response.url);
});
it("should register via next", () => {
const handler: ChainableHandler = () => new Response();
const chain = new Chain(handler);
chain.next(handler, handler, handler).next(handler);
assertEquals(chain.handlers.length, 5);
});
it("should chain handler", async () => {
const handler: ChainableHandler = (_, next) => next();
const chain = new Chain(handler);
chain.next(handler, handler, handler).next(() => new Response("test"));
const response = await chain.respond(new Request("http://localhost"));
assertEquals(await response.text(), "test");
});
});