1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-26 16:09:27 -05:00
denoland-deno/js/url_search_params_test.ts

179 lines
5.1 KiB
TypeScript
Raw Normal View History

2019-01-21 14:03:30 -05:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assert, assertEquals } from "./test_util.ts";
2018-10-21 11:07:29 -04:00
test(function urlSearchParamsInitString() {
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"
);
});
test(function urlSearchParamsInitIterable() {
const init = [["a", "54"], ["b", "true"]];
const searchParams = new URLSearchParams(init);
assertEquals(searchParams.toString(), "a=54&b=true");
2018-10-21 11:07:29 -04:00
});
test(function urlSearchParamsInitRecord() {
const init = { a: "54", b: "true" };
const searchParams = new URLSearchParams(init);
assertEquals(searchParams.toString(), "a=54&b=true");
2018-10-21 11:07:29 -04:00
});
test(function urlSearchParamsAppendSuccess() {
const searchParams = new URLSearchParams();
searchParams.append("a", "true");
assertEquals(searchParams.toString(), "a=true");
2018-10-21 11:07:29 -04:00
});
test(function urlSearchParamsDeleteSuccess() {
const init = "a=54&b=true";
const searchParams = new URLSearchParams(init);
searchParams.delete("b");
assertEquals(searchParams.toString(), "a=54");
2018-10-21 11:07:29 -04:00
});
test(function urlSearchParamsGetAllSuccess() {
const init = "a=54&b=true&a=true";
const searchParams = new URLSearchParams(init);
assertEquals(searchParams.getAll("a"), ["54", "true"]);
assertEquals(searchParams.getAll("b"), ["true"]);
assertEquals(searchParams.getAll("c"), []);
2018-10-21 11:07:29 -04:00
});
test(function urlSearchParamsGetSuccess() {
const init = "a=54&b=true&a=true";
const searchParams = new URLSearchParams(init);
assertEquals(searchParams.get("a"), "54");
assertEquals(searchParams.get("b"), "true");
assertEquals(searchParams.get("c"), null);
2018-10-21 11:07:29 -04:00
});
test(function urlSearchParamsHasSuccess() {
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"));
});
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");
assertEquals(searchParams.toString(), "a=false&b=true");
});
test(function urlSearchParamsSetAppendNew() {
const init = "a=54&b=true&a=true";
const searchParams = new URLSearchParams(init);
searchParams.set("c", "foo");
assertEquals(searchParams.toString(), "a=54&b=true&a=true&c=foo");
2018-10-21 11:07:29 -04:00
});
test(function urlSearchParamsSortSuccess() {
const init = "c=4&a=2&b=3&a=1";
const searchParams = new URLSearchParams(init);
searchParams.sort();
assertEquals(searchParams.toString(), "a=2&a=1&b=3&c=4");
2018-10-21 11:07:29 -04:00
});
test(function urlSearchParamsForEachSuccess() {
const init = [["a", "54"], ["b", "true"]];
const searchParams = new URLSearchParams(init);
let callNum = 0;
searchParams.forEach((value, key, parent) => {
assertEquals(searchParams, parent);
assertEquals(value, init[callNum][1]);
assertEquals(key, init[callNum][0]);
2018-10-21 11:07:29 -04:00
callNum++;
});
assertEquals(callNum, init.length);
2018-10-21 11:07:29 -04:00
});
test(function urlSearchParamsMissingName() {
const init = "=4";
const searchParams = new URLSearchParams(init);
assertEquals(searchParams.get(""), "4");
assertEquals(searchParams.toString(), "=4");
2018-10-21 11:07:29 -04:00
});
test(function urlSearchParamsMissingValue() {
const init = "4=";
const searchParams = new URLSearchParams(init);
assertEquals(searchParams.get("4"), "");
assertEquals(searchParams.toString(), "4=");
2018-10-21 11:07:29 -04:00
});
test(function urlSearchParamsMissingEqualSign() {
const init = "4";
const searchParams = new URLSearchParams(init);
assertEquals(searchParams.get("4"), "");
assertEquals(searchParams.toString(), "4=");
2018-10-21 11:07:29 -04:00
});
test(function urlSearchParamsMissingPair() {
const init = "c=4&&a=54&";
const searchParams = new URLSearchParams(init);
assertEquals(searchParams.toString(), "c=4&a=54");
2018-10-21 11:07:29 -04:00
});
// If pair does not contain exactly two items, then throw a TypeError.
// ref https://url.spec.whatwg.org/#interface-urlsearchparams
test(function urlSearchParamsShouldThrowTypeError() {
let hasThrown = 0;
try {
new URLSearchParams([["1"]]);
hasThrown = 1;
} catch (err) {
if (err instanceof TypeError) {
hasThrown = 2;
} else {
hasThrown = 3;
}
}
assertEquals(hasThrown, 2);
});
test(function urlSearchParamsAppendArgumentsCheck() {
const methodRequireOneParam = ["delete", "getAll", "get", "has", "forEach"];
const methodRequireTwoParams = ["append", "set"];
methodRequireOneParam.concat(methodRequireTwoParams).forEach(method => {
const searchParams = new URLSearchParams();
let hasThrown = 0;
try {
searchParams[method]();
hasThrown = 1;
} catch (err) {
if (err instanceof TypeError) {
hasThrown = 2;
} else {
hasThrown = 3;
}
}
assertEquals(hasThrown, 2);
});
methodRequireTwoParams.forEach(method => {
const searchParams = new URLSearchParams();
let hasThrown = 0;
try {
searchParams[method]("foo");
hasThrown = 1;
} catch (err) {
if (err instanceof TypeError) {
hasThrown = 2;
} else {
hasThrown = 3;
}
}
assertEquals(hasThrown, 2);
});
});