-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseQuery.test.ts
More file actions
63 lines (51 loc) · 2.06 KB
/
Copy pathparseQuery.test.ts
File metadata and controls
63 lines (51 loc) · 2.06 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
import { describe, it, expect } from "vitest";
import { parseQuery } from "./parseQuery";
describe("parseQuery", () => {
it("empty inputs -> {}", () => {
expect(parseQuery("")).toEqual({});
expect(parseQuery("?")).toEqual({});
});
it("strips a leading ? and parses simple pairs", () => {
expect(parseQuery("?a=1&b=2")).toEqual({ a: "1", b: "2" });
expect(parseQuery("a=1&b=2")).toEqual({ a: "1", b: "2" });
});
it("skips empty segments (&&, leading &, trailing &)", () => {
expect(parseQuery("a=1&&b=2")).toEqual({ a: "1", b: "2" });
expect(parseQuery("&a=1&")).toEqual({ a: "1" });
});
it("bare key and key= both yield empty-string values", () => {
expect(parseQuery("flag")).toEqual({ flag: "" });
expect(parseQuery("flag=")).toEqual({ flag: "" });
});
it("splits only on the first = so values may contain =", () => {
expect(parseQuery("a=b=c")).toEqual({ a: "b=c" });
});
it("percent-decodes keys and values", () => {
expect(parseQuery("a%20b=c%2Bd")).toEqual({ "a b": "c+d" });
});
it("treats + as space in keys and values", () => {
expect(parseQuery("x+y=1+2")).toEqual({ "x y": "1 2" });
});
it("collapses duplicate keys into an array in order", () => {
expect(parseQuery("t=a&t=b&t=c")).toEqual({ t: ["a", "b", "c"] });
});
it("a single occurrence stays a string, not a 1-element array", () => {
const r = parseQuery("t=a");
expect(r.t).toBe("a");
expect(Array.isArray(r.t)).toBe(false);
});
it("does not throw on malformed percent escapes; falls back to raw token", () => {
expect(() => parseQuery("a=%zz")).not.toThrow();
expect(parseQuery("a=%zz")).toEqual({ a: "%zz" });
expect(parseQuery("bad%=x")).toEqual({ "bad%": "x" });
});
it("ignores segments whose key is empty", () => {
expect(parseQuery("=v&a=1")).toEqual({ a: "1" });
});
it("returns {} for non-string input", () => {
// @ts-expect-error deliberate misuse
expect(parseQuery(undefined)).toEqual({});
// @ts-expect-error deliberate misuse
expect(parseQuery(null)).toEqual({});
});
});