-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathparse.test.ts
62 lines (50 loc) · 1.95 KB
/
parse.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
import querystring from "querystring";
import { assert, test } from "vitest";
import qs from "../lib";
import { qsNoMungeTestCases, qsTestCases, qsWeirdObjects } from "./node";
test("should succeed on node.js tests", () => {
qsWeirdObjects.forEach((t) =>
assert.deepEqual(qs.parse(t[1] as string), t[2] as Record<string, unknown>),
);
qsNoMungeTestCases.forEach((t) => assert.deepEqual(qs.parse(t[0]), t[1]));
qsTestCases.forEach((t) => assert.deepEqual(qs.parse(t[0]), t[2]));
});
test("native querystring module should match the test suite result", () => {
qsTestCases.forEach((t) => assert.deepEqual(querystring.parse(t[0]), t[2]));
qsNoMungeTestCases.forEach((t) =>
assert.deepEqual(querystring.parse(t[0]), t[1]),
);
});
test("handles & on first/last character", () => {
assert.deepEqual(qs.parse("&hello=world"), { hello: "world" });
assert.deepEqual(qs.parse("hello=world&"), { hello: "world" });
});
test("handles ? on first character", () => {
// This aligns with `node:querystring` functionality
assert.deepEqual(qs.parse("?hello=world"), { "?hello": "world" });
});
test("handles + character", () => {
assert.deepEqual(qs.parse("author=Yagiz+Nizipli"), {
author: "Yagiz Nizipli",
});
});
test("should accept pairs with missing values", () => {
assert.deepEqual(qs.parse("foo=bar&hey"), { foo: "bar", hey: "" });
assert.deepEqual(qs.parse("hey"), { hey: "" });
});
test("should decode key", () => {
assert.deepEqual(qs.parse("invalid%key=hello"), { "invalid%key": "hello" });
assert.deepEqual(qs.parse("full%20name=Yagiz"), { "full name": "Yagiz" });
});
test("should handle really large object", () => {
const query = {};
for (let i = 0; i < 2000; i++) query[i] = i;
const url = qs.stringify(query);
assert.strictEqual(Object.keys(qs.parse(url)).length, 2000);
});
test("should parse large numbers", () => {
assert.strictEqual(
qs.parse("id=918854443121279438895193").id,
"918854443121279438895193",
);
});