Repository navigation
Expand file tree
/
Copy pathquerystring.test.js
More file actions
59 lines (48 loc) · 1.67 KB
/
Copy pathquerystring.test.js
File metadata and controls
59 lines (48 loc) · 1.67 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
// In the prep, we implemented a function to parse query strings.
// Unfortunately, it contains several bugs!
// Below are some test cases the implementation doesn't handle well.
// Fix the implementation for these tests, and try to think of as many other edge cases as possible - write tests and fix those too.
const parseQueryString = require("./querystring.js")
test("should parse values containing '='", () => {
expect(parseQueryString("equation=a=b-2")).toEqual({
equation: "a=b-2",
});
});
test("should ignore empty key-value pairs", () => {
expect(parseQueryString("key1=value1&&key2=value2&")).toEqual({
key1: "value1",
key2: "value2",
});
});
test("should accept empty string as key or as value", () => {
expect(parseQueryString("=value")).toEqual({ "": "value" });
expect(parseQueryString("key")).toEqual({ key: "" });
expect(parseQueryString("key=")).toEqual({ key: "" });
expect(parseQueryString("=")).toEqual({ "": "" });
});
test("should decode percent-encoded characters", () => {
expect(parseQueryString("%24half=1%2F2")).toEqual({
$half: "1/2",
});
});
test("should replace '+' by ' '", () => {
expect(parseQueryString("full+name=John+Doe")).toEqual({
"full name": "John Doe",
});
});
test("returns empty object for empty query", () => {
expect(parseQueryString("")).toEqual({});
});
test("ignores multiple empty pairs", () => {
expect(parseQueryString("&&&&")).toEqual({});
});
test("decodes encoded spaces", () => {
expect(parseQueryString("name=John%20Doe")).toEqual({
name: "John Doe",
});
});
test("keeps all '=' in values", () => {
expect(parseQueryString("expr=a=b=c=d")).toEqual({
expr: "a=b=c=d",
});
});