Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { decode, encode } from '../src/qss'
3β
4describe('encode function', () => {
5 it('should encode an object into a query string without a prefix', () => {
6 const obj = { token: 'foo', key: 'value' }
7 const queryString = encode(obj)
8 expect(queryString).toEqual('token=foo&key=value')
9 })
10β
11 it('should handle encoding an object with empty values and trailing equal signs', () => {
12 const obj = { token: '', key: 'value=' }
13 const queryString = encode(obj)
14 expect(queryString).toEqual('token=&key=value%3D') // token=&key=value=
15 })
16β
17 it('should handle encoding an object with array values', () => {
18 const obj = { token: ['foo', 'bar'], key: 'value' }
19 const queryString = encode(obj)
20 expect(queryString).toEqual('token=foo%2Cbar&key=value')
21 })
22β
23 it('should handle encoding an object with special characters', () => {
24 const obj = { token: 'foo?', key: 'value=' }
25 const queryString = encode(obj)
26 expect(queryString).toEqual('token=foo%3F&key=value%3D')
27 })
28β
29 it('should handle encoding a top-level key with a special character', () => {
30 const obj = { 'foo=bar': 1 }
31 const queryString = encode(obj)
32 expect(queryString).toEqual('foo%3Dbar=1')
33 })
34})
35β
36describe('decode function', () => {
37 it('should decode a query string without a prefix', () => {
38 const queryString = 'token=foo&key=value'
39 const decodedObj = decode(queryString)
40 expect(decodedObj).toEqual({ token: 'foo', key: 'value' })
41 })
42β
43 it('should handle missing values and trailing equal signs', () => {
44 const queryString = 'token=&key=value='
45 const decodedObj = decode(queryString)
46 expect(decodedObj).toEqual({ token: '', key: 'value=' })
47 })
48β
49 it('should handle decoding a query string with array values', () => {
50 const queryString = 'token=foo&token=bar&key=value'
51 const decodedObj = decode(queryString)
52 expect(decodedObj).toEqual({ token: ['foo', 'bar'], key: 'value' })
53 })
54β
55 it('should handle decoding a query string with special characters', () => {
56 const queryString = 'token=foo%3F&key=value%3D'
57 const decodedObj = decode(queryString)
58 expect(decodedObj).toEqual({ token: 'foo?', key: 'value=' })
59 })
60β
61 it('should handle decoding a top-level key with a special character', () => {
62 const queryString = 'foo%3Dbar=1'
63 const decodedObj = decode(queryString)
64 expect(decodedObj).toEqual({ 'foo=bar': 1 })
65 })
66β
67 it('should handle decoding a top-level key with a special character and without a value', () => {
68 const queryString = 'foo%3Dbar='
69 const decodedObj = decode(queryString)
70 expect(decodedObj).toEqual({ 'foo=bar': '' })
71 })
72β
73 it('should handle decoding a value-less top-level key with a special character', () => {
74 const queryString = 'foo%3Dbar'
75 const decodedObj = decode(queryString)
76 expect(decodedObj).toEqual({ 'foo=bar': '' })
77 })
78β
79 it('should handle decoding a query with "100%" as a value', () => {
80 const queryString = 'percentage=100%&name=Sean&foo%3Dbar&key=value%3D'
81 const decodedObj = decode(queryString)
82 expect(decodedObj).toEqual({
83 percentage: '100%',
84 name: 'Sean',
85 'foo=bar': '',
86 key: 'value=',
87 })
88 })
89β
90 it('should handle decoding a query with plus', () => {
91 const queryString = 'q=red%2Byellow+orange'
92 const decodedObj = decode(queryString)
93 expect(decodedObj).toEqual({ q: 'red+yellow orange' })
94 })
95β
96 it('should decode once percent characters (%) encoded twice', () => {
97 const queryString = 'q=%2540'
98 const decodedObj = decode(queryString)
99 expect(decodedObj).toEqual({ q: '%40' })
100 })
101})