| 1 |
"use strict"; |
| 2 |
|
| 3 |
const assert = require("../../assert.js"); |
| 4 |
|
| 5 |
let params; |
| 6 |
|
| 7 |
function testCtor(value, expected) { |
| 8 |
assert.sameValue(new URLSearchParams(value).toString(), expected); |
| 9 |
} |
| 10 |
|
| 11 |
testCtor("user=abc&query=xyz", "user=abc&query=xyz"); |
| 12 |
testCtor("?user=abc&query=xyz", "user=abc&query=xyz"); |
| 13 |
|
| 14 |
testCtor( |
| 15 |
{ |
| 16 |
num: 1, |
| 17 |
user: "abc", |
| 18 |
query: ["first", "second"], |
| 19 |
obj: { prop: "value" }, |
| 20 |
b: true, |
| 21 |
}, |
| 22 |
"num=1&user=abc&query=first%2Csecond&obj=%5Bobject+Object%5D&b=true" |
| 23 |
); |
| 24 |
|
| 25 |
const map = new Map(); |
| 26 |
map.set("user", "abc"); |
| 27 |
map.set("query", "xyz"); |
| 28 |
testCtor(map, "user=abc&query=xyz"); |
| 29 |
|
| 30 |
testCtor( |
| 31 |
[ |
| 32 |
["user", "abc"], |
| 33 |
["query", "first"], |
| 34 |
["query", "second"], |
| 35 |
], |
| 36 |
"user=abc&query=first&query=second" |
| 37 |
); |
| 38 |
|
| 39 |
// Each key-value pair must have exactly two elements |
| 40 |
assert.throwsNodeError(() => new URLSearchParams([["single_value"]]), TypeError, "ERR_INVALID_TUPLE"); |
| 41 |
assert.throwsNodeError(() => new URLSearchParams([["too", "many", "values"]]), TypeError, "ERR_INVALID_TUPLE"); |
| 42 |
|
| 43 |
params = new URLSearchParams("a=b&cc=d"); |
| 44 |
params.forEach((value, name, searchParams) => { |
| 45 |
if (name === "a") { |
| 46 |
assert.sameValue(value, "b"); |
| 47 |
} |
| 48 |
if (name === "cc") { |
| 49 |
assert.sameValue(value, "d"); |
| 50 |
} |
| 51 |
assert.sameValue(searchParams, params); |
| 52 |
}); |
| 53 |
|
| 54 |
params.forEach((value, name, searchParams) => { |
| 55 |
if (name === "a") { |
| 56 |
assert.sameValue(value, "b"); |
| 57 |
searchParams.set("cc", "d1"); |
| 58 |
} |
| 59 |
if (name === "cc") { |
| 60 |
assert.sameValue(value, "d1"); |
| 61 |
} |
| 62 |
assert.sameValue(searchParams, params); |
| 63 |
}); |
| 64 |
|
| 65 |
assert.throwsNodeError(() => params.forEach(123), TypeError, "ERR_INVALID_ARG_TYPE"); |
| 66 |
|
| 67 |
assert.throwsNodeError(() => params.forEach.call(1, 2), TypeError, "ERR_INVALID_THIS"); |
| 68 |
|
| 69 |
params = new URLSearchParams("a=1=2&b=3"); |
| 70 |
assert.sameValue(params.size, 2); |
| 71 |
assert.sameValue(params.get("a"), "1=2"); |
| 72 |
assert.sameValue(params.get("b"), "3"); |
| 73 |
|
| 74 |
params = new URLSearchParams("&"); |
| 75 |
assert.sameValue(params.size, 0); |
| 76 |
|
| 77 |
params = new URLSearchParams("& "); |
| 78 |
assert.sameValue(params.size, 1); |
| 79 |
assert.sameValue(params.get(" "), ""); |
| 80 |
|
| 81 |
params = new URLSearchParams(" &"); |
| 82 |
assert.sameValue(params.size, 1); |
| 83 |
assert.sameValue(params.get(" "), ""); |
| 84 |
|
| 85 |
params = new URLSearchParams("="); |
| 86 |
assert.sameValue(params.size, 1); |
| 87 |
assert.sameValue(params.get(""), ""); |
| 88 |
|
| 89 |
params = new URLSearchParams("&=2"); |
| 90 |
assert.sameValue(params.size, 1); |
| 91 |
assert.sameValue(params.get(""), "2"); |
| 92 |
|
| 93 |
params = new URLSearchParams("?user=abc"); |
| 94 |
assert.throwsNodeError(() => params.append(), TypeError, "ERR_MISSING_ARGS"); |
| 95 |
params.append("query", "first"); |
| 96 |
assert.sameValue(params.toString(), "user=abc&query=first"); |
| 97 |
|
| 98 |
params = new URLSearchParams("first=one&second=two&third=three"); |
| 99 |
assert.throwsNodeError(() => params.delete(), TypeError, "ERR_MISSING_ARGS"); |
| 100 |
params.delete("second", "fake-value"); |
| 101 |
assert.sameValue(params.toString(), "first=one&second=two&third=three"); |
| 102 |
params.delete("third", "three"); |
| 103 |
assert.sameValue(params.toString(), "first=one&second=two"); |
| 104 |
params.delete("second"); |
| 105 |
assert.sameValue(params.toString(), "first=one"); |
| 106 |
|
| 107 |
params = new URLSearchParams("user=abc&query=xyz"); |
| 108 |
assert.throwsNodeError(() => params.get(), TypeError, "ERR_MISSING_ARGS"); |
| 109 |
assert.sameValue(params.get("user"), "abc"); |
| 110 |
assert.sameValue(params.get("non-existant"), null); |
| 111 |
|
| 112 |
params = new URLSearchParams("query=first&query=second"); |
| 113 |
assert.throwsNodeError(() => params.getAll(), TypeError, "ERR_MISSING_ARGS"); |
| 114 |
const all = params.getAll("query"); |
| 115 |
assert.sameValue(all.includes("first"), true); |
| 116 |
assert.sameValue(all.includes("second"), true); |
| 117 |
assert.sameValue(all.length, 2); |
| 118 |
const getAllUndefined = params.getAll(undefined); |
| 119 |
assert.sameValue(getAllUndefined.length, 0); |
| 120 |
const getAllNonExistant = params.getAll("does_not_exists"); |
| 121 |
assert.sameValue(getAllNonExistant.length, 0); |
| 122 |
|
| 123 |
params = new URLSearchParams("user=abc&query=xyz"); |
| 124 |
assert.throwsNodeError(() => params.has(), TypeError, "ERR_MISSING_ARGS"); |
| 125 |
assert.sameValue(params.has(undefined), false); |
| 126 |
assert.sameValue(params.has("user"), true); |
| 127 |
assert.sameValue(params.has("user", "abc"), true); |
| 128 |
assert.sameValue(params.has("user", "abc", "extra-param"), true); |
| 129 |
assert.sameValue(params.has("user", "efg"), false); |
| 130 |
assert.sameValue(params.has("user", undefined), true); |
| 131 |
|
| 132 |
params = new URLSearchParams(); |
| 133 |
params.append("foo", "bar"); |
| 134 |
params.append("foo", "baz"); |
| 135 |
params.append("abc", "def"); |
| 136 |
assert.sameValue(params.toString(), "foo=bar&foo=baz&abc=def"); |
| 137 |
params.set("foo", "def"); |
| 138 |
params.set("xyz", "opq"); |
| 139 |
assert.sameValue(params.toString(), "foo=def&abc=def&xyz=opq"); |
| 140 |
|
| 141 |
params = new URLSearchParams("query=first&query=second&user=abc&double=first,second"); |
| 142 |
const URLSearchIteratorPrototype = params.entries().__proto__; |
| 143 |
assert.sameValue(typeof URLSearchIteratorPrototype, "object"); |
| 144 |
assert.sameValue(URLSearchIteratorPrototype.__proto__, [][Symbol.iterator]().__proto__.__proto__); |
| 145 |
|
| 146 |
assert.sameValue(params[Symbol.iterator], params.entries); |
| 147 |
|
| 148 |
{ |
| 149 |
const entries = params.entries(); |
| 150 |
assert.sameValue(entries.toString(), "[object URLSearchParams Iterator]"); |
| 151 |
assert.sameValue(entries.__proto__, URLSearchIteratorPrototype); |
| 152 |
|
| 153 |
let item = entries.next(); |
| 154 |
assert.sameValue(item.value.toString(), ["query", "first"].toString()); |
| 155 |
assert.sameValue(item.done, false); |
| 156 |
|
| 157 |
item = entries.next(); |
| 158 |
assert.sameValue(item.value.toString(), ["query", "second"].toString()); |
| 159 |
assert.sameValue(item.done, false); |
| 160 |
|
| 161 |
item = entries.next(); |
| 162 |
assert.sameValue(item.value.toString(), ["user", "abc"].toString()); |
| 163 |
assert.sameValue(item.done, false); |
| 164 |
|
| 165 |
item = entries.next(); |
| 166 |
assert.sameValue(item.value.toString(), ["double", "first,second"].toString()); |
| 167 |
assert.sameValue(item.done, false); |
| 168 |
|
| 169 |
item = entries.next(); |
| 170 |
assert.sameValue(item.value, undefined); |
| 171 |
assert.sameValue(item.done, true); |
| 172 |
} |
| 173 |
|
| 174 |
params = new URLSearchParams("query=first&query=second&user=abc"); |
| 175 |
{ |
| 176 |
const keys = params.keys(); |
| 177 |
assert.sameValue(keys.__proto__, URLSearchIteratorPrototype); |
| 178 |
|
| 179 |
let item = keys.next(); |
| 180 |
assert.sameValue(item.value, "query"); |
| 181 |
assert.sameValue(item.done, false); |
| 182 |
|
| 183 |
item = keys.next(); |
| 184 |
assert.sameValue(item.value, "query"); |
| 185 |
assert.sameValue(item.done, false); |
| 186 |
|
| 187 |
item = keys.next(); |
| 188 |
assert.sameValue(item.value, "user"); |
| 189 |
assert.sameValue(item.done, false); |
| 190 |
|
| 191 |
item = keys.next(); |
| 192 |
assert.sameValue(item.value, undefined); |
| 193 |
assert.sameValue(item.done, true); |
| 194 |
|
| 195 |
assert.sameValue(Array.from(params.keys()).length, 3); |
| 196 |
} |
| 197 |
|
| 198 |
params = new URLSearchParams("query=first&query=second&user=abc"); |
| 199 |
{ |
| 200 |
const values = params.values(); |
| 201 |
assert.sameValue(values.__proto__, URLSearchIteratorPrototype); |
| 202 |
|
| 203 |
let item = values.next(); |
| 204 |
assert.sameValue(item.value, "first"); |
| 205 |
assert.sameValue(item.done, false); |
| 206 |
|
| 207 |
item = values.next(); |
| 208 |
assert.sameValue(item.value, "second"); |
| 209 |
assert.sameValue(item.done, false); |
| 210 |
|
| 211 |
item = values.next(); |
| 212 |
assert.sameValue(item.value, "abc"); |
| 213 |
assert.sameValue(item.done, false); |
| 214 |
|
| 215 |
item = values.next(); |
| 216 |
assert.sameValue(item.value, undefined); |
| 217 |
assert.sameValue(item.done, true); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
params = new URLSearchParams("query[]=abc&type=search&query[]=123"); |
| 222 |
params.sort(); |
| 223 |
assert.sameValue(params.toString(), "query%5B%5D=abc&query%5B%5D=123&type=search"); |
| 224 |
|
| 225 |
params = new URLSearchParams("query=first&query=second&user=abc"); |
| 226 |
assert.sameValue(params.size, 3); |
| 227 |
|
| 228 |
params = new URLSearchParams("%"); |
| 229 |
assert.sameValue(params.has("%"), true); |
| 230 |
assert.sameValue(params.toString(), "%25="); |
| 231 |
|
| 232 |
{ |
| 233 |
const params = new URLSearchParams(""); |
| 234 |
assert.sameValue(params.size, 0); |
| 235 |
assert.sameValue(params.toString(), ""); |
| 236 |
assert.sameValue(params.get(undefined), null); |
| 237 |
params.set(undefined, true); |
| 238 |
assert.sameValue(params.has(undefined), true); |
| 239 |
assert.sameValue(params.has("undefined"), true); |
| 240 |
assert.sameValue(params.get("undefined"), "true"); |
| 241 |
assert.sameValue(params.get(undefined), "true"); |
| 242 |
assert.sameValue(params.getAll(undefined).toString(), ["true"].toString()); |
| 243 |
params.delete(undefined); |
| 244 |
assert.sameValue(params.has(undefined), false); |
| 245 |
assert.sameValue(params.has("undefined"), false); |
| 246 |
|
| 247 |
assert.sameValue(params.has(null), false); |
| 248 |
params.set(null, "nullval"); |
| 249 |
assert.sameValue(params.has(null), true); |
| 250 |
assert.sameValue(params.has("null"), true); |
| 251 |
assert.sameValue(params.get(null), "nullval"); |
| 252 |
assert.sameValue(params.get("null"), "nullval"); |
| 253 |
params.delete(null); |
| 254 |
assert.sameValue(params.has(null), false); |
| 255 |
assert.sameValue(params.has("null"), false); |
| 256 |
} |
| 257 |
|
| 258 |
function* functionGeneratorExample() { |
| 259 |
yield ["user", "abc"]; |
| 260 |
yield ["query", "first"]; |
| 261 |
yield ["query", "second"]; |
| 262 |
} |
| 263 |
|
| 264 |
params = new URLSearchParams(functionGeneratorExample()); |
| 265 |
assert.sameValue(params.toString(), "user=abc&query=first&query=second"); |
| 266 |
|
| 267 |
assert.sameValue(params.__proto__.constructor, URLSearchParams); |
| 268 |
assert.sameValue(params instanceof URLSearchParams, true); |
| 269 |
|
| 270 |
{ |
| 271 |
const params = new URLSearchParams("1=2&1=3"); |
| 272 |
assert.sameValue(params.get(1), "2"); |
| 273 |
assert.sameValue(params.getAll(1).toString(), ["2", "3"].toString()); |
| 274 |
assert.sameValue(params.getAll("x").toString(), [].toString()); |
| 275 |
} |
| 276 |
|
| 277 |
// Sync |
| 278 |
{ |
| 279 |
const url = new URL("https://test.com/"); |
| 280 |
const params = url.searchParams; |
| 281 |
assert.sameValue(params.size, 0); |
| 282 |
url.search = "a=1"; |
| 283 |
assert.sameValue(params.size, 1); |
| 284 |
assert.sameValue(params.get("a"), "1"); |
| 285 |
} |
| 286 |
|
| 287 |
{ |
| 288 |
const url = new URL("https://test.com/?a=1"); |
| 289 |
const params = url.searchParams; |
| 290 |
assert.sameValue(params.size, 1); |
| 291 |
url.search = ""; |
| 292 |
assert.sameValue(params.size, 0); |
| 293 |
url.search = "b=2"; |
| 294 |
assert.sameValue(params.size, 1); |
| 295 |
} |
| 296 |
|
| 297 |
{ |
| 298 |
const url = new URL("https://test.com/"); |
| 299 |
const params = url.searchParams; |
| 300 |
params.append("a", "1"); |
| 301 |
assert.sameValue(url.toString(), "https://test.com/?a=1"); |
| 302 |
} |
| 303 |
|
| 304 |
{ |
| 305 |
const url = new URL("https://test.com/"); |
| 306 |
url.searchParams.append("a", "1"); |
| 307 |
url.searchParams.append("b", "1"); |
| 308 |
assert.sameValue(url.toString(), "https://test.com/?a=1&b=1"); |
| 309 |
} |
| 310 |
|
| 311 |
{ |
| 312 |
const url = new URL("https://test.com/"); |
| 313 |
url.searchParams.append("a", "1"); |
| 314 |
assert.sameValue(url.search, "?a=1"); |
| 315 |
} |
| 316 |
|
| 317 |
{ |
| 318 |
const url = new URL("https://test.com/?a=1"); |
| 319 |
const params = url.searchParams; |
| 320 |
params.append("a", "2"); |
| 321 |
assert.sameValue(url.search, "?a=1&a=2"); |
| 322 |
} |
| 323 |
|
| 324 |
{ |
| 325 |
const url = new URL("https://test.com/"); |
| 326 |
const params = url.searchParams; |
| 327 |
params.set("a", "1"); |
| 328 |
assert.sameValue(url.search, "?a=1"); |
| 329 |
} |
| 330 |
|
| 331 |
{ |
| 332 |
const url = new URL("https://test.com/"); |
| 333 |
url.searchParams.set("a", "1"); |
| 334 |
url.searchParams.set("b", "1"); |
| 335 |
assert.sameValue(url.toString(), "https://test.com/?a=1&b=1"); |
| 336 |
} |
| 337 |
|
| 338 |
{ |
| 339 |
const url = new URL("https://test.com/?a=1&b=2"); |
| 340 |
const params = url.searchParams; |
| 341 |
params.delete("a"); |
| 342 |
assert.sameValue(url.search, "?b=2"); |
| 343 |
} |
| 344 |
|
| 345 |
{ |
| 346 |
const url = new URL("https://test.com/?b=2&a=1"); |
| 347 |
const params = url.searchParams; |
| 348 |
params.sort(); |
| 349 |
assert.sameValue(url.search, "?a=1&b=2"); |
| 350 |
} |
| 351 |
|
| 352 |
{ |
| 353 |
const url = new URL("https://test.com/?a=1"); |
| 354 |
const params = url.searchParams; |
| 355 |
params.delete("a"); |
| 356 |
assert.sameValue(url.search, ""); |
| 357 |
|
| 358 |
params.set("a", 2); |
| 359 |
assert.sameValue(url.search, "?a=2"); |
| 360 |
} |
| 361 |
|
| 362 |
// FAILING: no custom properties on wrapped Go structs |
| 363 |
/* |
| 364 |
{ |
| 365 |
const params = new URLSearchParams(""); |
| 366 |
assert.sameValue(Object.isExtensible(params), true); |
| 367 |
assert.sameValue(Reflect.defineProperty(params, "customField", {value: 42, configurable: true}), true); |
| 368 |
assert.sameValue(params.customField, 42); |
| 369 |
const desc = Reflect.getOwnPropertyDescriptor(params, "customField"); |
| 370 |
assert.sameValue(desc.value, 42); |
| 371 |
assert.sameValue(desc.writable, false); |
| 372 |
assert.sameValue(desc.enumerable, false); |
| 373 |
assert.sameValue(desc.configurable, true); |
| 374 |
} |
| 375 |
*/ |
| 376 |
|
| 377 |
// Escape |
| 378 |
{ |
| 379 |
const myURL = new URL('https://example.org/abc?fo~o=~ba r%z'); |
| 380 |
|
| 381 |
assert.sameValue(myURL.search, "?fo~o=~ba%20r%z"); |
| 382 |
|
| 383 |
// Modify the URL via searchParams... |
| 384 |
myURL.searchParams.sort(); |
| 385 |
|
| 386 |
assert.sameValue(myURL.search, "?fo%7Eo=%7Eba+r%25z"); |
| 387 |
} |