-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSearch.spec.ts
More file actions
67 lines (66 loc) · 3.57 KB
/
Search.spec.ts
File metadata and controls
67 lines (66 loc) · 3.57 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
import * as Search from "./Search"
describe("Search", () => {
it("stringify", async () => {
const result = Search.stringify({ value: 1337, nested: { value: 42, string: "The power of attraction." } })
expect(result.toString()).toEqual("value=1337&nested[value]=42&nested[string]=The%20power%20of%20attraction.")
})
it("stringify 2", async () => {
const result = Search.stringify({
threeDSSessionData: "",
cres: "eyJhY3NUcmFuc0lEIjoiZjE5MjEwMWYtYjJlYi00YjBlLThkMzYtY2ZlZjJmOWM1NWY4IiwiY2hhbGxlbmdlQ29tcGxldGlvbkluZCI6IlkiLCJtZXNzYWdlVHlwZSI6IkNSZXMiLCJtZXNzYWdlVmVyc2lvbiI6IjIuMS4wIiwidGhyZWVEU1NlcnZlclRyYW5zSUQiOiI4YmZkZDQ2MC1mZDc3LTRlZWYtOWE4Ny1hOTc4ZTJlMzAzY2YiLCJ0cmFuc1N0YXR1cyI6IlkifQ==",
})
expect(result.toString()).toEqual(
"threeDSSessionData=&cres=eyJhY3NUcmFuc0lEIjoiZjE5MjEwMWYtYjJlYi00YjBlLThkMzYtY2ZlZjJmOWM1NWY4IiwiY2hhbGxlbmdlQ29tcGxldGlvbkluZCI6IlkiLCJtZXNzYWdlVHlwZSI6IkNSZXMiLCJtZXNzYWdlVmVyc2lvbiI6IjIuMS4wIiwidGhyZWVEU1NlcnZlclRyYW5zSUQiOiI4YmZkZDQ2MC1mZDc3LTRlZWYtOWE4Ny1hOTc4ZTJlMzAzY2YiLCJ0cmFuc1N0YXR1cyI6IlkifQ%3D%3D"
)
})
it("parse", async () => {
const result = Search.parse("value=1337&nested[value]=42&nested[string]=The%20power%20of%20attraction.")
expect(result).toEqual({ value: "1337", nested: { value: "42", string: "The power of attraction." } })
})
it("parse nested", async () => {
const result = Search.parse(
"value=1337&nested%5Bvalue%5D=42&nested%5Bstring%5D=The%20power%20of%20attraction.&nested%5Bobject%5D%5Bvalue%5D=Deepest%20level."
)
expect(result).toEqual({
value: "1337",
nested: { value: "42", string: "The power of attraction.", object: { value: "Deepest level." } },
})
})
it("parse space seperatly +", async () => {
const result = Search.parse("value=Some+nice+pants+and+an+Iphone%2B")
expect(result).toEqual({
value: "Some nice pants and an Iphone+",
})
})
it("parse 2", async () => {
const result = Search.parse(
"threeDSSessionData=&cres=eyJhY3NUcmFuc0lEIjoiZjE5MjEwMWYtYjJlYi00YjBlLThkMzYtY2ZlZjJmOWM1NWY4IiwiY2hhbGxlbmdlQ29tcGxldGlvbkluZCI6IlkiLCJtZXNzYWdlVHlwZSI6IkNSZXMiLCJtZXNzYWdlVmVyc2lvbiI6IjIuMS4wIiwidGhyZWVEU1NlcnZlclRyYW5zSUQiOiI4YmZkZDQ2MC1mZDc3LTRlZWYtOWE4Ny1hOTc4ZTJlMzAzY2YiLCJ0cmFuc1N0YXR1cyI6IlkifQ%3D%3D"
)
expect(result).toEqual({
threeDSSessionData: "",
cres: "eyJhY3NUcmFuc0lEIjoiZjE5MjEwMWYtYjJlYi00YjBlLThkMzYtY2ZlZjJmOWM1NWY4IiwiY2hhbGxlbmdlQ29tcGxldGlvbkluZCI6IlkiLCJtZXNzYWdlVHlwZSI6IkNSZXMiLCJtZXNzYWdlVmVyc2lvbiI6IjIuMS4wIiwidGhyZWVEU1NlcnZlclRyYW5zSUQiOiI4YmZkZDQ2MC1mZDc3LTRlZWYtOWE4Ny1hOTc4ZTJlMzAzY2YiLCJ0cmFuc1N0YXR1cyI6IlkifQ==",
})
})
it("parse with nested arrays", async () => {
const result = Search.parse(
"values[0]=first&values[1]=second&values[2]=the+third&" +
"nested[people][0][fname]=John&nested[people][0][lname]=Smith&" +
"nested[people][2][fname]=Nick&nested[people][2][lname]=Cage&" +
"nested[people][2][kittens][0][name]=purry&nested[people][2][kittens][0][age]=3&nested[people][2][kittens][1][name]=furry&" +
"nested[people][1][fname]=foo&nested[people][1][lname]=bar&" +
"nested[people][1][puppies][0]=snowy&nested[people][1][puppies][1]=sunny&nested[people][1][puppies][2]=rainy&nested[people][1][puppies][3]=cloudy&" +
"nested[groupname]=Random+People"
)
expect(result).toEqual({
values: ["first", "second", "the third"],
nested: {
people: [
{ fname: "John", lname: "Smith" },
{ fname: "foo", lname: "bar", puppies: ["snowy", "sunny", "rainy", "cloudy"] },
{ fname: "Nick", lname: "Cage", kittens: [{ name: "purry", age: "3" }, { name: "furry" }] },
],
groupname: "Random People",
},
})
})
})