Repository navigation
Expand file tree
/
Copy pathquerystring.test.js
More file actions
78 lines (66 loc) · 2.39 KB
/
Copy pathquerystring.test.js
File metadata and controls
78 lines (66 loc) · 2.39 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 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",
});
});
// Stretch exercise: Handling query strings that contain identical keys
// Delete this test if you are not working on this optional case
test("should store values of a key in an array when the key has 2 or more values", () => {
expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({
key: ["value1", "value2", "value3"],
foo: "bar",
});
});
// Multiple normal key-value pairs
test("should parse multiple key-value pairs", () => {
expect(parseQueryString("name=Maryam&age=25")).toEqual({
name: "Maryam",
age: "25",
});
});
test("should return empty object for empty query string", () => {
expect(parseQueryString("")).toEqual({});
});
test("should replace multiple '+' characters with spaces", () => {
expect(parseQueryString("name=John+Michael+Doe")).toEqual({
name: "John Michael Doe",
});
});
test("should decode encoded keys and values", () => {
expect(parseQueryString("hello%20world=good%20morning")).toEqual({
"hello world": "good morning",
});
});
test("should ignore multiple empty key-value pairs", () => {
expect(parseQueryString("name=Maryam&&&age=25&&&")).toEqual({
name: "Maryam",
age: "25",
});
});