-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.ts
162 lines (133 loc) · 3.77 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
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { convertValue, trimSlashes, uri } from "./uri.ts";
// import example for typechecking
import "./examples/example.ts";
Deno.test({
name: "uri",
fn: () => {
const API_URL = "https://api.example.com/";
// UrlSearchParams does not support non-strings
const id = BigInt("112233445566778899").toString(10);
const limit = 5;
const offset = 5;
assertEquals(
uri`${API_URL}/${"path"}`,
"https://api.example.com/path",
);
assertEquals(
uri`${API_URL}/${"/path"}`,
"https://api.example.com/%2Fpath",
);
assertEquals(
uri`${API_URL}/${trimSlashes("/path")}`,
"https://api.example.com/path",
);
assertEquals(
uri`${API_URL}/${["/path/"]}${["/test/"]}`,
"https://api.example.com//path//test/",
);
assertEquals(
uri`${API_URL}/${trimSlashes("/path")}/${1}`,
"https://api.example.com/path/1",
);
assertEquals(
uri`${API_URL}/${trimSlashes("/path")}/${1}?`,
"https://api.example.com/path/1",
);
assertEquals(
uri`${API_URL}/${trimSlashes("/path")}/left${1}right`,
"https://api.example.com/path/left1right",
);
assertEquals(
uri`${API_URL}/${trimSlashes("/path")}/${{ "a b": "b a" }}`,
"https://api.example.com/path/a%20b=b%20a",
);
assertEquals(
uri`${API_URL}/${"path"}/${{ "a%b": "b%a" }}`,
"https://api.example.com/path/a%25b=b%25a",
);
assertEquals(
uri`${API_URL}/${"query"}?${{ "a%b": "b%a" }}`,
"https://api.example.com/query?a%25b=b%25a",
);
// featured example
assertEquals(
uri`${API_URL}/users/${id}/posts/${{ limit: limit, offset: offset }}`,
"https://api.example.com/users/112233445566778899/posts/limit=5&offset=5",
);
},
});
Deno.test({
name: "convertValue",
fn: () => {
assertEquals(
convertValue("abc"),
"abc",
);
// url encoding
assertEquals(
convertValue("abc✨"),
"abc%E2%9C%A8",
);
assertEquals(
convertValue({ bool: "true", other: "✨" }),
"bool=true&other=%E2%9C%A8",
);
},
});
Deno.test("example", () => {
const API_URL = "https://api.example.com/";
const userPostsUrl = (id: string, limit: number, offset: number) => {
return uri`${API_URL}/users/${id}/posts?${{
limit: limit.toString(),
offset: offset.toString(),
}}`;
};
assertEquals(
userPostsUrl("112233445566778899", 10, 5),
"https://api.example.com/users/112233445566778899/posts?limit=10&offset=5",
);
});
// TODO: when `deno test --doc` supports running the tests, remove this.
Deno.test("documentation", () => {
const HOST = `https://example.com/`;
// removed when nothing after
assertEquals(
uri`https://example.com/example?${null}`,
"https://example.com/example",
);
// kept when something after
assertEquals(
uri`https://example.com/example?${{}}`,
"https://example.com/example?",
);
// avoiding `?` cleanup behavior always
assertEquals(
uri`https://example.com/example${["?"]}${null}`,
"https://example.com/example?",
);
assertEquals(
uri`${HOST}/example`,
"https://example.com/example",
);
assertEquals(
uri`https://example.com/${"hello world?"}`,
"https://example.com/hello%20world%3F",
);
assertEquals(
uri`https://example.com/${{ foo: "bar", key: (10).toString() }}`,
"https://example.com/foo=bar&key=10",
);
assertEquals(
uri`https://example.com/${["get", "user", 120]}`,
"https://example.com/get/user/120",
);
assertEquals(
uri`https://example.com/${">///<"}`,
"https://example.com/%3E%2F%2F%2F%3C",
);
assertEquals(
uri`https://example.com/${[">///<"]}`,
"https://example.com/>///<",
);
});