• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1test(function() {
2    var params = new URLSearchParams();
3    params.append('a', 'b c');
4    assert_equals(params + '', 'a=b+c');
5    params.delete('a');
6    params.append('a b', 'c');
7    assert_equals(params + '', 'a+b=c');
8}, 'Serialize space');
9
10test(function() {
11    var params = new URLSearchParams();
12    params.append('a', '');
13    assert_equals(params + '', 'a=');
14    params.append('a', '');
15    assert_equals(params + '', 'a=&a=');
16    params.append('', 'b');
17    assert_equals(params + '', 'a=&a=&=b');
18    params.append('', '');
19    assert_equals(params + '', 'a=&a=&=b&=');
20    params.append('', '');
21    assert_equals(params + '', 'a=&a=&=b&=&=');
22}, 'Serialize empty value');
23
24test(function() {
25    var params = new URLSearchParams();
26    params.append('', 'b');
27    assert_equals(params + '', '=b');
28    params.append('', 'b');
29    assert_equals(params + '', '=b&=b');
30}, 'Serialize empty name');
31
32test(function() {
33    var params = new URLSearchParams();
34    params.append('', '');
35    assert_equals(params + '', '=');
36    params.append('', '');
37    assert_equals(params + '', '=&=');
38}, 'Serialize empty name and value');
39
40test(function() {
41    var params = new URLSearchParams();
42    params.append('a', 'b+c');
43    assert_equals(params + '', 'a=b%2Bc');
44    params.delete('a');
45    params.append('a+b', 'c');
46    assert_equals(params + '', 'a%2Bb=c');
47}, 'Serialize +');
48
49test(function() {
50    var params = new URLSearchParams();
51    params.append('=', 'a');
52    assert_equals(params + '', '%3D=a');
53    params.append('b', '=');
54    assert_equals(params + '', '%3D=a&b=%3D');
55}, 'Serialize =');
56
57test(function() {
58    var params = new URLSearchParams();
59    params.append('&', 'a');
60    assert_equals(params + '', '%26=a');
61    params.append('b', '&');
62    assert_equals(params + '', '%26=a&b=%26');
63}, 'Serialize &');
64
65test(function() {
66    var params = new URLSearchParams();
67    params.append('a', '*-._');
68    assert_equals(params + '', 'a=*-._');
69    params.delete('a');
70    params.append('*-._', 'c');
71    assert_equals(params + '', '*-._=c');
72}, 'Serialize *-._');
73
74test(function() {
75    var params = new URLSearchParams();
76    params.append('a', 'b%c');
77    assert_equals(params + '', 'a=b%25c');
78    params.delete('a');
79    params.append('a%b', 'c');
80    assert_equals(params + '', 'a%25b=c');
81}, 'Serialize %');
82
83test(function() {
84    var params = new URLSearchParams();
85    params.append('a', 'b\0c');
86    assert_equals(params + '', 'a=b%00c');
87    params.delete('a');
88    params.append('a\0b', 'c');
89    assert_equals(params + '', 'a%00b=c');
90}, 'Serialize \\0');
91
92test(function() {
93    var params = new URLSearchParams();
94    params.append('a', 'b\uD83D\uDCA9c');
95    assert_equals(params + '', 'a=b%F0%9F%92%A9c');
96    params.delete('a');
97    params.append('a\uD83D\uDCA9b', 'c');
98    assert_equals(params + '', 'a%F0%9F%92%A9b=c');
99}, 'Serialize \uD83D\uDCA9');  // Unicode Character 'PILE OF POO' (U+1F4A9)
100
101test(function() {
102    var params;
103    params = new URLSearchParams('a=b&c=d&&e&&');
104    assert_equals(params.toString(), 'a=b&c=d&e=');
105    params = new URLSearchParams('a = b &a=b&c=d%20');
106    assert_equals(params.toString(), 'a+=+b+&a=b&c=d+');
107    // The lone '=' _does_ survive the roundtrip.
108    params = new URLSearchParams('a=&a=b');
109    assert_equals(params.toString(), 'a=&a=b');
110}, 'URLSearchParams.toString');
111
112test(() => {
113    const url = new URL('http://www.example.com/?a=b,c');
114    const params = url.searchParams;
115
116    assert_equals(url.toString(), 'http://www.example.com/?a=b,c');
117    assert_equals(params.toString(), 'a=b%2Cc');
118
119    params.append('x', 'y');
120
121    assert_equals(url.toString(), 'http://www.example.com/?a=b%2Cc&x=y');
122    assert_equals(params.toString(), 'a=b%2Cc&x=y');
123}, 'URLSearchParams connected to URL');
124