forked from denoland/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter_test.ts
More file actions
217 lines (190 loc) · 6.24 KB
/
router_test.ts
File metadata and controls
217 lines (190 loc) · 6.24 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { expect } from "@std/expect";
import { IS_PATTERN, pathToPattern, UrlPatternRouter } from "./router.ts";
Deno.test("IS_PATTERN", () => {
expect(IS_PATTERN.test("/foo")).toEqual(false);
expect(IS_PATTERN.test("/foo/bar/baz.jpg")).toEqual(false);
expect(IS_PATTERN.test("/foo/:path")).toEqual(true);
expect(IS_PATTERN.test("/foo/*")).toEqual(true);
expect(IS_PATTERN.test("/foo{/bar}?")).toEqual(true);
expect(IS_PATTERN.test("/foo/(\\d+)")).toEqual(true);
expect(IS_PATTERN.test("/foo/(a)")).toEqual(true);
});
Deno.test("UrlPatternRouter - GET get first match", () => {
const router = new UrlPatternRouter();
const A = () => {};
const B = () => {};
const C = () => {};
router.add("GET", "/", [A]);
router.add("GET", "/", [B]);
router.add("GET", "/", [C]);
const res = router.match("GET", new URL("/", "http://localhost"));
expect(res).toEqual({
params: {},
handlers: [[A]],
methodMatch: true,
pattern: "/",
patternMatch: true,
});
});
Deno.test("UrlPatternRouter - GET get matches with middlewares", () => {
const router = new UrlPatternRouter();
const A = () => {};
const B = () => {};
const C = () => {};
router.add("ALL", "/*", [A]);
router.add("ALL", "/*", [B]);
router.add("GET", "/", [C]);
const res = router.match("GET", new URL("/", "http://localhost"));
expect(res).toEqual({
params: {},
handlers: [[A], [B], [C]],
methodMatch: true,
pattern: "/",
patternMatch: true,
});
});
Deno.test("UrlPatternRouter - GET get matches with middlewares with Path", () => {
const router = new UrlPatternRouter();
const A = () => {};
const B = () => {};
const C = () => {};
const D = () => {};
router.addMiddleware("/*", D);
router.add("ALL", "/*", [A]);
router.add("ALL", "/*", [B]);
router.add("GET", "/", [C]);
const res = router.match("GET", new URL("/", "http://localhost"));
expect(res).toEqual({
params: {},
handlers: [[D], [A], [B], [C]],
methodMatch: true,
pattern: "/",
patternMatch: true,
});
});
Deno.test("UrlPatternRouter - GET get matches with middlewares with Path(eq endpoint path)", () => {
const router = new UrlPatternRouter();
const A = () => {};
const B = () => {};
const C = () => {};
const D = () => {};
router.addMiddleware("/a/1", D);
router.add("ALL", "/*", [A]);
router.add("ALL", "/*", [B]);
router.add("GET", "/a/1", [C]);
const res = router.match("GET", new URL("/a/1", "http://localhost"));
expect(res).toEqual({
params: {},
handlers: [[D], [A], [B], [C]],
methodMatch: true,
pattern: "/a/1",
patternMatch: true,
});
});
Deno.test("UrlPatternRouter - GET extract params", () => {
const router = new UrlPatternRouter();
const A = () => {};
router.add("GET", new URLPattern({ pathname: "/:foo/:bar/c" }), [A]);
let res = router.match("GET", new URL("/a/b/c", "http://localhost"));
expect(res).toEqual({
params: { foo: "a", bar: "b" },
handlers: [[A]],
methodMatch: true,
pattern: "/:foo/:bar/c",
patternMatch: true,
});
// Decode params
res = router.match("GET", new URL("/a%20a/b/c", "http://localhost"));
expect(res).toEqual({
params: { foo: "a a", bar: "b" },
handlers: [[A]],
methodMatch: true,
pattern: "/:foo/:bar/c",
patternMatch: true,
});
});
Deno.test("UrlPatternRouter - Wrong method match", () => {
const router = new UrlPatternRouter();
const A = () => {};
router.add("GET", "/foo", [A]);
const res = router.match("POST", new URL("/foo", "http://localhost"));
expect(res).toEqual({
params: {},
handlers: [],
methodMatch: false,
pattern: "/foo",
patternMatch: true,
});
});
Deno.test("UrlPatternRouter - wrong + correct method", () => {
const router = new UrlPatternRouter();
const A = () => {};
const B = () => {};
router.add("GET", "/foo", [A]);
router.add("POST", "/foo", [B]);
const res = router.match("POST", new URL("/foo", "http://localhost"));
expect(res).toEqual({
params: {},
handlers: [[B]],
methodMatch: true,
pattern: "/foo",
patternMatch: true,
});
});
Deno.test("UrlPatternRouter - convert patterns automatically", () => {
const router = new UrlPatternRouter();
const A = () => {};
router.add("GET", "/books/:id", [A]);
const res = router.match("GET", new URL("/books/foo", "http://localhost"));
expect(res).toEqual({
params: {
id: "foo",
},
handlers: [[A]],
methodMatch: true,
pattern: "/books/:id",
patternMatch: true,
});
});
Deno.test("pathToPattern", async (t) => {
await t.step("creates pattern", () => {
expect(pathToPattern("foo/bar")).toEqual("/foo/bar");
});
await t.step("parses index routes", () => {
expect(pathToPattern("foo/index")).toEqual("/foo");
});
await t.step("parses parameters", () => {
expect(pathToPattern("foo/[name]")).toEqual("/foo/:name");
expect(pathToPattern("foo/[name]/bar/[bob]")).toEqual(
"/foo/:name/bar/:bob",
);
});
await t.step("parses catchall", () => {
expect(pathToPattern("foo/[...name]")).toEqual("/foo/:name*");
});
await t.step("parses multiple params in same part", () => {
expect(pathToPattern("foo/[mod]@[version]")).toEqual("/foo/:mod@:version");
expect(pathToPattern("foo/[bar].json")).toEqual("/foo/:bar.json");
expect(pathToPattern("foo/foo[bar]")).toEqual("/foo/foo:bar");
});
await t.step("parses optional params", () => {
expect(pathToPattern("foo/[[name]]")).toEqual("/foo{/:name}?");
expect(pathToPattern("foo/[name]/[[bob]]")).toEqual("/foo/:name{/:bob}?");
expect(pathToPattern("foo/[[name]]/bar")).toEqual("/foo{/:name}?/bar");
expect(
pathToPattern("foo/[[name]]/bar/[[bob]]"),
).toEqual(
"/foo{/:name}?/bar{/:bob}?",
);
});
await t.step("throws on invalid patterns", () => {
expect(() => pathToPattern("foo/[foo][bar]")).toThrow();
expect(() => pathToPattern("foo/foo]")).toThrow();
expect(() => pathToPattern("foo/[foo]]")).toThrow();
expect(() => pathToPattern("foo/foo-[[name]]-bar/baz")).toThrow();
expect(() => pathToPattern("foo/[[name]]-bar/baz")).toThrow();
expect(() => pathToPattern("foo/foo-[[name]]/baz")).toThrow();
expect(() => pathToPattern("foo/foo-[[name]]")).toThrow();
expect(() => pathToPattern("foo/[[name]]-bar")).toThrow();
});
});