2022-01-20 02:10:16 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-11-23 11:45:18 -05:00
|
|
|
import { assert, assertEquals } from "./test_util.ts";
|
2018-10-21 11:07:29 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsWithMultipleSpaces() {
|
2020-08-17 14:52:45 -04:00
|
|
|
const init = { str: "this string has spaces in it" };
|
2020-08-12 06:08:24 -04:00
|
|
|
const searchParams = new URLSearchParams(init).toString();
|
2020-08-17 14:52:45 -04:00
|
|
|
assertEquals(searchParams, "str=this+string+has+spaces+in+it");
|
2020-08-12 06:08:24 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsWithExclamation() {
|
2020-08-12 06:08:24 -04:00
|
|
|
const init = [
|
|
|
|
["str", "hello, world!"],
|
|
|
|
];
|
|
|
|
const searchParams = new URLSearchParams(init).toString();
|
|
|
|
assertEquals(searchParams, "str=hello%2C+world%21");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsWithQuotes() {
|
2020-08-12 06:08:24 -04:00
|
|
|
const init = [
|
|
|
|
["str", "'hello world'"],
|
|
|
|
];
|
|
|
|
const searchParams = new URLSearchParams(init).toString();
|
|
|
|
assertEquals(searchParams, "str=%27hello+world%27");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsWithBraket() {
|
2020-08-12 06:08:24 -04:00
|
|
|
const init = [
|
|
|
|
["str", "(hello world)"],
|
|
|
|
];
|
|
|
|
const searchParams = new URLSearchParams(init).toString();
|
|
|
|
assertEquals(searchParams, "str=%28hello+world%29");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsWithTilde() {
|
2020-08-12 06:08:24 -04:00
|
|
|
const init = [
|
|
|
|
["str", "hello~world"],
|
|
|
|
];
|
|
|
|
const searchParams = new URLSearchParams(init).toString();
|
|
|
|
assertEquals(searchParams, "str=hello%7Eworld");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsInitString() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = "c=4&a=2&b=3&%C3%A1=1";
|
|
|
|
const searchParams = new URLSearchParams(init);
|
|
|
|
assert(
|
|
|
|
init === searchParams.toString(),
|
2020-07-14 15:24:17 -04:00
|
|
|
"The init query string does not match",
|
2018-10-21 11:07:29 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsInitStringWithPlusCharacter() {
|
2020-11-02 12:58:29 -05:00
|
|
|
let params = new URLSearchParams("q=a+b");
|
|
|
|
assertEquals(params.toString(), "q=a+b");
|
|
|
|
assertEquals(params.get("q"), "a b");
|
|
|
|
|
|
|
|
params = new URLSearchParams("q=a+b+c");
|
|
|
|
assertEquals(params.toString(), "q=a+b+c");
|
|
|
|
assertEquals(params.get("q"), "a b c");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsInitStringWithMalformedParams() {
|
2020-11-02 12:58:29 -05:00
|
|
|
// These test cases are copied from Web Platform Tests
|
|
|
|
// https://github.com/web-platform-tests/wpt/blob/54c6d64/url/urlsearchparams-constructor.any.js#L60-L80
|
|
|
|
let params = new URLSearchParams("id=0&value=%");
|
|
|
|
assert(params != null, "constructor returned non-null value.");
|
|
|
|
assert(params.has("id"), 'Search params object has name "id"');
|
|
|
|
assert(params.has("value"), 'Search params object has name "value"');
|
|
|
|
assertEquals(params.get("id"), "0");
|
|
|
|
assertEquals(params.get("value"), "%");
|
|
|
|
|
|
|
|
params = new URLSearchParams("b=%2sf%2a");
|
|
|
|
assert(params != null, "constructor returned non-null value.");
|
|
|
|
assert(params.has("b"), 'Search params object has name "b"');
|
|
|
|
assertEquals(params.get("b"), "%2sf*");
|
|
|
|
|
|
|
|
params = new URLSearchParams("b=%2%2af%2a");
|
|
|
|
assert(params != null, "constructor returned non-null value.");
|
|
|
|
assert(params.has("b"), 'Search params object has name "b"');
|
|
|
|
assertEquals(params.get("b"), "%2*f*");
|
|
|
|
|
|
|
|
params = new URLSearchParams("b=%%2a");
|
|
|
|
assert(params != null, "constructor returned non-null value.");
|
|
|
|
assert(params.has("b"), 'Search params object has name "b"');
|
|
|
|
assertEquals(params.get("b"), "%*");
|
2020-09-01 14:34:41 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsInitIterable() {
|
2019-11-13 13:42:34 -05:00
|
|
|
const init = [
|
|
|
|
["a", "54"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["b", "true"],
|
2019-11-13 13:42:34 -05:00
|
|
|
];
|
2018-10-21 11:07:29 -04:00
|
|
|
const searchParams = new URLSearchParams(init);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.toString(), "a=54&b=true");
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsInitRecord() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = { a: "54", b: "true" };
|
|
|
|
const searchParams = new URLSearchParams(init);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.toString(), "a=54&b=true");
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsInit() {
|
2019-04-27 13:16:29 -04:00
|
|
|
const params1 = new URLSearchParams("a=b");
|
|
|
|
assertEquals(params1.toString(), "a=b");
|
2021-08-21 02:55:28 -04:00
|
|
|
const params2 = new URLSearchParams(params1);
|
2019-04-27 13:16:29 -04:00
|
|
|
assertEquals(params2.toString(), "a=b");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsAppendSuccess() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
searchParams.append("a", "true");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.toString(), "a=true");
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsDeleteSuccess() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = "a=54&b=true";
|
|
|
|
const searchParams = new URLSearchParams(init);
|
|
|
|
searchParams.delete("b");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.toString(), "a=54");
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsGetAllSuccess() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = "a=54&b=true&a=true";
|
|
|
|
const searchParams = new URLSearchParams(init);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.getAll("a"), ["54", "true"]);
|
|
|
|
assertEquals(searchParams.getAll("b"), ["true"]);
|
|
|
|
assertEquals(searchParams.getAll("c"), []);
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsGetSuccess() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = "a=54&b=true&a=true";
|
|
|
|
const searchParams = new URLSearchParams(init);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.get("a"), "54");
|
|
|
|
assertEquals(searchParams.get("b"), "true");
|
|
|
|
assertEquals(searchParams.get("c"), null);
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsHasSuccess() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = "a=54&b=true&a=true";
|
|
|
|
const searchParams = new URLSearchParams(init);
|
|
|
|
assert(searchParams.has("a"));
|
|
|
|
assert(searchParams.has("b"));
|
|
|
|
assert(!searchParams.has("c"));
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsSetReplaceFirstAndRemoveOthers() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = "a=54&b=true&a=true";
|
|
|
|
const searchParams = new URLSearchParams(init);
|
|
|
|
searchParams.set("a", "false");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.toString(), "a=false&b=true");
|
2018-12-18 20:14:59 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsSetAppendNew() {
|
2018-12-18 20:14:59 -05:00
|
|
|
const init = "a=54&b=true&a=true";
|
|
|
|
const searchParams = new URLSearchParams(init);
|
|
|
|
searchParams.set("c", "foo");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.toString(), "a=54&b=true&a=true&c=foo");
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsSortSuccess() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = "c=4&a=2&b=3&a=1";
|
|
|
|
const searchParams = new URLSearchParams(init);
|
|
|
|
searchParams.sort();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.toString(), "a=2&a=1&b=3&c=4");
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsForEachSuccess() {
|
2019-11-13 13:42:34 -05:00
|
|
|
const init = [
|
|
|
|
["a", "54"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["b", "true"],
|
2019-11-13 13:42:34 -05:00
|
|
|
];
|
2018-10-21 11:07:29 -04:00
|
|
|
const searchParams = new URLSearchParams(init);
|
|
|
|
let callNum = 0;
|
2021-08-05 07:08:58 -04:00
|
|
|
searchParams.forEach((value, key, parent) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
assertEquals(searchParams, parent);
|
|
|
|
assertEquals(value, init[callNum][1]);
|
|
|
|
assertEquals(key, init[callNum][0]);
|
|
|
|
callNum++;
|
|
|
|
});
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(callNum, init.length);
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsMissingName() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = "=4";
|
|
|
|
const searchParams = new URLSearchParams(init);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.get(""), "4");
|
|
|
|
assertEquals(searchParams.toString(), "=4");
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsMissingValue() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = "4=";
|
|
|
|
const searchParams = new URLSearchParams(init);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.get("4"), "");
|
|
|
|
assertEquals(searchParams.toString(), "4=");
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsMissingEqualSign() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = "4";
|
|
|
|
const searchParams = new URLSearchParams(init);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.get("4"), "");
|
|
|
|
assertEquals(searchParams.toString(), "4=");
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsMissingPair() {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = "c=4&&a=54&";
|
|
|
|
const searchParams = new URLSearchParams(init);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(searchParams.toString(), "c=4&a=54");
|
2018-10-21 11:07:29 -04:00
|
|
|
});
|
2018-12-18 20:14:59 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsForShortEncodedChar() {
|
2020-10-10 01:20:24 -04:00
|
|
|
const init = { linefeed: "\n", tab: "\t" };
|
|
|
|
const searchParams = new URLSearchParams(init);
|
|
|
|
assertEquals(searchParams.toString(), "linefeed=%0A&tab=%09");
|
|
|
|
});
|
|
|
|
|
2018-12-18 20:14:59 -05:00
|
|
|
// If pair does not contain exactly two items, then throw a TypeError.
|
|
|
|
// ref https://url.spec.whatwg.org/#interface-urlsearchparams
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsShouldThrowTypeError() {
|
2018-12-18 20:14:59 -05:00
|
|
|
let hasThrown = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
new URLSearchParams([["1"]]);
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(hasThrown, 2);
|
2019-06-10 07:20:59 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
new URLSearchParams([["1", "2", "3"]]);
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(hasThrown, 2);
|
2018-12-18 20:14:59 -05:00
|
|
|
});
|
2018-12-23 23:07:58 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsAppendArgumentsCheck() {
|
2018-12-23 23:07:58 -05:00
|
|
|
const methodRequireOneParam = ["delete", "getAll", "get", "has", "forEach"];
|
|
|
|
|
|
|
|
const methodRequireTwoParams = ["append", "set"];
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
methodRequireOneParam
|
|
|
|
.concat(methodRequireTwoParams)
|
2021-08-05 07:08:58 -04:00
|
|
|
.forEach((method: string) => {
|
2019-04-21 16:40:10 -04:00
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-06-02 00:24:44 -04:00
|
|
|
(searchParams as any)[method]();
|
2019-04-21 16:40:10 -04:00
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
|
|
|
}
|
2018-12-23 23:07:58 -05:00
|
|
|
}
|
2019-04-21 16:40:10 -04:00
|
|
|
assertEquals(hasThrown, 2);
|
2019-11-13 13:42:34 -05:00
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
methodRequireTwoParams.forEach((method: string) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-06-02 00:24:44 -04:00
|
|
|
(searchParams as any)[method]("foo");
|
2019-11-13 13:42:34 -05:00
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
2018-12-23 23:07:58 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-13 13:42:34 -05:00
|
|
|
assertEquals(hasThrown, 2);
|
|
|
|
});
|
2018-12-23 23:07:58 -05:00
|
|
|
});
|
2019-04-25 09:29:50 -04:00
|
|
|
|
|
|
|
// ref: https://github.com/web-platform-tests/wpt/blob/master/url/urlsearchparams-delete.any.js
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsDeletingAppendedMultiple() {
|
2019-04-25 09:29:50 -04:00
|
|
|
const params = new URLSearchParams();
|
|
|
|
params.append("first", (1 as unknown) as string);
|
|
|
|
assert(params.has("first"));
|
|
|
|
assertEquals(params.get("first"), "1");
|
|
|
|
params.delete("first");
|
|
|
|
assertEquals(params.has("first"), false);
|
|
|
|
params.append("first", (1 as unknown) as string);
|
|
|
|
params.append("first", (10 as unknown) as string);
|
|
|
|
params.delete("first");
|
|
|
|
assertEquals(params.has("first"), false);
|
|
|
|
});
|
2019-06-14 11:46:07 -04:00
|
|
|
|
|
|
|
// ref: https://github.com/web-platform-tests/wpt/blob/master/url/urlsearchparams-constructor.any.js#L176-L182
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsCustomSymbolIterator() {
|
2019-06-14 11:46:07 -04:00
|
|
|
const params = new URLSearchParams();
|
2020-03-28 13:03:49 -04:00
|
|
|
params[Symbol.iterator] = function* (): IterableIterator<[string, string]> {
|
2019-06-14 11:46:07 -04:00
|
|
|
yield ["a", "b"];
|
|
|
|
};
|
|
|
|
const params1 = new URLSearchParams((params as unknown) as string[][]);
|
|
|
|
assertEquals(params1.get("a"), "b");
|
|
|
|
});
|
2019-06-17 11:39:23 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-08-05 07:08:58 -04:00
|
|
|
function urlSearchParamsCustomSymbolIteratorWithNonStringParams() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const params = {};
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-06-02 00:24:44 -04:00
|
|
|
(params as any)[Symbol.iterator] = function* (): IterableIterator<
|
|
|
|
[number, number]
|
|
|
|
> {
|
2020-03-04 11:31:14 -05:00
|
|
|
yield [1, 2];
|
|
|
|
};
|
|
|
|
const params1 = new URLSearchParams((params as unknown) as string[][]);
|
|
|
|
assertEquals(params1.get("1"), "2");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-04-23 10:30:32 -04:00
|
|
|
|
|
|
|
// If a class extends URLSearchParams, override one method should not change another's behavior.
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-08-05 07:08:58 -04:00
|
|
|
function urlSearchParamsOverridingAppendNotChangeConstructorAndSet() {
|
2020-04-23 10:30:32 -04:00
|
|
|
let overridedAppendCalled = 0;
|
|
|
|
class CustomSearchParams extends URLSearchParams {
|
2021-08-05 07:08:58 -04:00
|
|
|
append(name: string, value: string) {
|
2020-04-23 10:30:32 -04:00
|
|
|
++overridedAppendCalled;
|
|
|
|
super.append(name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new CustomSearchParams("foo=bar");
|
|
|
|
new CustomSearchParams([["foo", "bar"]]);
|
|
|
|
new CustomSearchParams(new CustomSearchParams({ foo: "bar" }));
|
|
|
|
new CustomSearchParams().set("foo", "bar");
|
|
|
|
assertEquals(overridedAppendCalled, 0);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-23 10:30:32 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlSearchParamsOverridingEntriesNotChangeForEach() {
|
2020-04-23 10:30:32 -04:00
|
|
|
class CustomSearchParams extends URLSearchParams {
|
|
|
|
*entries(): IterableIterator<[string, string]> {
|
|
|
|
yield* [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let loopCount = 0;
|
|
|
|
const params = new CustomSearchParams({ foo: "bar" });
|
|
|
|
params.forEach(() => void ++loopCount);
|
|
|
|
assertEquals(loopCount, 1);
|
|
|
|
});
|