2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-04 11:31:14 -05:00
|
|
|
import { unitTest, assert, assertEquals } from "./test_util.ts";
|
2018-11-04 13:05:02 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function formDataHasCorrectNameProp(): void {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(FormData.name, "FormData");
|
2018-12-24 11:18:01 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function formDataParamsAppendSuccess(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("a", "true");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(formData.get("a"), "true");
|
2018-11-04 13:05:02 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function formDataParamsDeleteSuccess(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("a", "true");
|
|
|
|
formData.append("b", "false");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(formData.get("b"), "false");
|
2018-11-04 13:05:02 -05:00
|
|
|
formData.delete("b");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(formData.get("a"), "true");
|
|
|
|
assertEquals(formData.get("b"), null);
|
2018-11-04 13:05:02 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function formDataParamsGetAllSuccess(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("a", "true");
|
|
|
|
formData.append("b", "false");
|
|
|
|
formData.append("a", "null");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(formData.getAll("a"), ["true", "null"]);
|
|
|
|
assertEquals(formData.getAll("b"), ["false"]);
|
|
|
|
assertEquals(formData.getAll("c"), []);
|
2018-11-04 13:05:02 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function formDataParamsGetSuccess(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("a", "true");
|
|
|
|
formData.append("b", "false");
|
|
|
|
formData.append("a", "null");
|
2020-02-19 15:36:18 -05:00
|
|
|
// @ts-ignore
|
2018-11-04 13:05:02 -05:00
|
|
|
formData.append("d", undefined);
|
2020-02-19 15:36:18 -05:00
|
|
|
// @ts-ignore
|
2018-11-04 13:05:02 -05:00
|
|
|
formData.append("e", null);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(formData.get("a"), "true");
|
|
|
|
assertEquals(formData.get("b"), "false");
|
|
|
|
assertEquals(formData.get("c"), null);
|
|
|
|
assertEquals(formData.get("d"), "undefined");
|
|
|
|
assertEquals(formData.get("e"), "null");
|
2018-11-04 13:05:02 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function formDataParamsHasSuccess(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("a", "true");
|
|
|
|
formData.append("b", "false");
|
|
|
|
assert(formData.has("a"));
|
|
|
|
assert(formData.has("b"));
|
|
|
|
assert(!formData.has("c"));
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function formDataParamsSetSuccess(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("a", "true");
|
|
|
|
formData.append("b", "false");
|
|
|
|
formData.append("a", "null");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(formData.getAll("a"), ["true", "null"]);
|
|
|
|
assertEquals(formData.getAll("b"), ["false"]);
|
2018-11-04 13:05:02 -05:00
|
|
|
formData.set("a", "false");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(formData.getAll("a"), ["false"]);
|
2020-02-19 15:36:18 -05:00
|
|
|
// @ts-ignore
|
2018-11-04 13:05:02 -05:00
|
|
|
formData.set("d", undefined);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(formData.get("d"), "undefined");
|
2020-02-19 15:36:18 -05:00
|
|
|
// @ts-ignore
|
2018-11-04 13:05:02 -05:00
|
|
|
formData.set("e", null);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(formData.get("e"), "null");
|
2018-11-04 13:05:02 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function formDataSetEmptyBlobSuccess(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.set("a", new Blob([]), "blank.txt");
|
2019-03-09 12:30:38 -05:00
|
|
|
formData.get("a");
|
2018-12-23 11:44:08 -05:00
|
|
|
/* TODO Fix this test.
|
2018-11-04 13:05:02 -05:00
|
|
|
assert(file instanceof File);
|
|
|
|
if (typeof file !== "string") {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(file.name, "blank.txt");
|
2018-11-04 13:05:02 -05:00
|
|
|
}
|
2018-12-23 11:44:08 -05:00
|
|
|
*/
|
2018-11-04 13:05:02 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function formDataParamsForEachSuccess(): void {
|
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-11-04 13:05:02 -05:00
|
|
|
const formData = new FormData();
|
|
|
|
for (const [name, value] of init) {
|
|
|
|
formData.append(name, value);
|
|
|
|
}
|
|
|
|
let callNum = 0;
|
2019-11-13 13:42:34 -05:00
|
|
|
formData.forEach((value, key, parent): void => {
|
|
|
|
assertEquals(formData, 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-11-04 13:05:02 -05:00
|
|
|
});
|
2018-12-26 21:12:55 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function formDataParamsArgumentsCheck(): void {
|
2020-02-19 15:36:18 -05:00
|
|
|
const methodRequireOneParam = [
|
|
|
|
"delete",
|
|
|
|
"getAll",
|
|
|
|
"get",
|
|
|
|
"has",
|
2020-03-28 13:03:49 -04:00
|
|
|
"forEach",
|
2020-02-19 15:36:18 -05:00
|
|
|
] as const;
|
2018-12-26 21:12:55 -05:00
|
|
|
|
2020-02-19 15:36:18 -05:00
|
|
|
const methodRequireTwoParams = ["append", "set"] as const;
|
2018-12-26 21:12:55 -05:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
methodRequireOneParam.forEach((method): void => {
|
|
|
|
const formData = new FormData();
|
|
|
|
let hasThrown = 0;
|
|
|
|
let errMsg = "";
|
|
|
|
try {
|
2020-02-19 15:36:18 -05:00
|
|
|
// @ts-ignore
|
2019-11-13 13:42:34 -05:00
|
|
|
formData[method]();
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
errMsg = err.message;
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
2018-12-26 21:12:55 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-13 13:42:34 -05:00
|
|
|
assertEquals(hasThrown, 2);
|
|
|
|
assertEquals(
|
|
|
|
errMsg,
|
|
|
|
`FormData.${method} requires at least 1 argument, but only 0 present`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
methodRequireTwoParams.forEach((method: string): void => {
|
|
|
|
const formData = new FormData();
|
|
|
|
let hasThrown = 0;
|
|
|
|
let errMsg = "";
|
|
|
|
|
|
|
|
try {
|
2020-02-19 15:36:18 -05:00
|
|
|
// @ts-ignore
|
2019-11-13 13:42:34 -05:00
|
|
|
formData[method]();
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
errMsg = err.message;
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
2018-12-26 21:12:55 -05:00
|
|
|
}
|
2019-11-13 13:42:34 -05:00
|
|
|
}
|
|
|
|
assertEquals(hasThrown, 2);
|
|
|
|
assertEquals(
|
|
|
|
errMsg,
|
|
|
|
`FormData.${method} requires at least 2 arguments, but only 0 present`
|
|
|
|
);
|
|
|
|
|
|
|
|
hasThrown = 0;
|
|
|
|
errMsg = "";
|
|
|
|
try {
|
2020-02-19 15:36:18 -05:00
|
|
|
// @ts-ignore
|
2019-11-13 13:42:34 -05:00
|
|
|
formData[method]("foo");
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
errMsg = err.message;
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
2018-12-26 21:12:55 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-13 13:42:34 -05:00
|
|
|
assertEquals(hasThrown, 2);
|
|
|
|
assertEquals(
|
|
|
|
errMsg,
|
|
|
|
`FormData.${method} requires at least 2 arguments, but only 1 present`
|
|
|
|
);
|
|
|
|
});
|
2018-12-26 21:12:55 -05:00
|
|
|
});
|
2019-04-03 08:41:05 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function toStringShouldBeWebCompatibility(): void {
|
2019-04-03 08:41:05 -04:00
|
|
|
const formData = new FormData();
|
|
|
|
assertEquals(formData.toString(), "[object FormData]");
|
|
|
|
});
|