2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
import { test, assert, assertEquals } from "./test_util.ts";
|
2018-10-21 11:07:29 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsInitString(): void {
|
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(),
|
|
|
|
"The init query string does not match"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsInitIterable(): void {
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsInitRecord(): void {
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsAppendSuccess(): void {
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsDeleteSuccess(): void {
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsGetAllSuccess(): void {
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsGetSuccess(): void {
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsHasSuccess(): void {
|
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"));
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsSetReplaceFirstAndRemoveOthers(): void {
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsSetAppendNew(): void {
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsSortSuccess(): void {
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsForEachSuccess(): void {
|
2018-10-21 11:07:29 -04:00
|
|
|
const init = [["a", "54"], ["b", "true"]];
|
|
|
|
const searchParams = new URLSearchParams(init);
|
|
|
|
let callNum = 0;
|
2019-04-21 16:40:10 -04:00
|
|
|
searchParams.forEach(
|
|
|
|
(value, key, parent): void => {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsMissingName(): void {
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsMissingValue(): void {
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsMissingEqualSign(): void {
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsMissingPair(): void {
|
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
|
|
|
|
|
|
|
// If pair does not contain exactly two items, then throw a TypeError.
|
|
|
|
// ref https://url.spec.whatwg.org/#interface-urlsearchparams
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsShouldThrowTypeError(): void {
|
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);
|
2018-12-18 20:14:59 -05:00
|
|
|
});
|
2018-12-23 23:07:58 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsAppendArgumentsCheck(): void {
|
2018-12-23 23:07:58 -05:00
|
|
|
const methodRequireOneParam = ["delete", "getAll", "get", "has", "forEach"];
|
|
|
|
|
|
|
|
const methodRequireTwoParams = ["append", "set"];
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
methodRequireOneParam.concat(methodRequireTwoParams).forEach(
|
|
|
|
(method: string): void => {
|
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
|
|
|
searchParams[method]();
|
|
|
|
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);
|
2018-12-23 23:07:58 -05:00
|
|
|
}
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
methodRequireTwoParams.forEach(
|
|
|
|
(method: string): void => {
|
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
|
|
|
searchParams[method]("foo");
|
|
|
|
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);
|
2018-12-23 23:07:58 -05:00
|
|
|
}
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
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
|
|
|
|
test(function urlSearchParamsDeletingAppendedMultiple(): void {
|
|
|
|
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);
|
|
|
|
});
|