-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.ts
179 lines (154 loc) · 4.16 KB
/
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 * as assert from "node:assert/strict";
import { describe, it } from "node:test";
import { join, pathcat } from "./src/index.ts";
describe("join()", () => {
it("Should correctly join two paths", () => {
assert.equal(join("first", "second"), "first/second");
assert.equal(join("first/", "second"), "first/second");
assert.equal(join("first", "/second"), "first/second");
assert.equal(join("first/", "/second"), "first/second");
assert.equal(join("first////", "////second"), "first///////second");
assert.equal(join("", "second"), "/second");
assert.equal(join("first", ""), "first/");
assert.equal(join("/", "second"), "/second");
assert.equal(join("first", "/"), "first/");
assert.equal(join("", ""), "/");
assert.equal(join("/", "/"), "/");
});
it("Should work with many paths", () => {
assert.equal(join("first", "second", "third", "fourth"), "first/second/third/fourth");
assert.equal(
join("first////", "////second", "///third///", "////fourth"),
"first///////second///third//////fourth"
);
});
});
describe("pathcat()", () => {
it("Basic usage", () => {
assert.equal(pathcat("first", "second"), "first/second");
assert.equal(pathcat("https://example.com", ":id", { id: "123" }), "https://example.com/123");
assert.equal(
pathcat("/users/:user/posts/:post", {
user: "1",
post: "2",
}),
"/users/1/posts/2"
);
assert.equal(
pathcat("/users/:user/posts/:post", {
user: "1",
post: "2",
param: true,
}),
"/users/1/posts/2?param=true"
);
assert.equal(
pathcat("/users/:user/posts/:post", {
user: "1",
post: "2",
field: ["name", "age"],
param: true,
}),
"/users/1/posts/2?field=name&field=age¶m=true"
);
assert.equal(
pathcat("https://example.com", "/users/:user_id/posts", {
user_id: "1234",
limit: 20,
skip: 20,
}),
"https://example.com/users/1234/posts?limit=20&skip=20"
);
});
it("Should correct handle double slashes", () => {
assert.equal(
pathcat("/first", "//second/:user_id", {
user_id: "1234",
}),
"/first//second/1234"
);
assert.equal(
pathcat("/first/", "/second/:user_id", {
user_id: "1234",
}),
"/first/second/1234"
);
assert.equal(
pathcat("/first/", "//second/:user_id", {
user_id: "1234",
}),
"/first//second/1234"
);
});
it("Should handle trailing slashes by including them in the path", () => {
assert.equal(
pathcat("/users/:user/posts/:post/", {
user: "1",
post: "2",
}),
"/users/1/posts/2/"
);
assert.equal(
pathcat("/users/:user/posts/:post/", {
user: "1",
post: "2",
limit: 10,
}),
"/users/1/posts/2/?limit=10"
);
});
it("Should handle missing params by avoiding them", () => {
assert.equal(
// @ts-expect-error
pathcat("/users/:user/posts/:post", { user: "1" }),
"/users/1/posts/:post"
);
assert.equal(
// @ts-expect-error
pathcat("https://example.com", "/users/:user/posts/:post", { user: "1" }),
"https://example.com/users/1/posts/:post"
);
});
it("Should work with just a base path and query object", () => {
assert.equal(
pathcat("https://example.com", { user_id: "1234" }),
"https://example.com?user_id=1234"
);
});
it("Should handle empty params as if they existed anyway", () => {
assert.equal(
pathcat("/users/:user/posts/:post/test", { user: "1", post: "" }),
"/users/1/posts//test"
);
assert.equal(
pathcat("https://example.com", "/users/:user/posts/:post/test", {
user: "1",
post: "",
}),
"https://example.com/users/1/posts//test"
);
});
it("Should work with untyped base paths", () => {
let base: string = "test";
assert.equal(
pathcat(base, {
user_id: "1234",
}),
"test?user_id=1234"
);
assert.equal(pathcat(base, "/path/to/resource"), "test/path/to/resource");
});
it("Passing undefined should be treated as if the param was missing", () => {
assert.equal(
pathcat("/users/:user/posts/:post", { user: "1", post: undefined }),
"/users/1/posts/:post"
);
assert.equal(
pathcat("https://example.com", "/users/:user/posts/:post", {
user: "1",
post: undefined,
}),
"https://example.com/users/1/posts/:post"
);
});
});