Support more fetch init body types (#1449)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2019-01-03 06:41:20 -05:00 committed by Ryan Dahl
parent 5b9c488921
commit 8d452d74fa
4 changed files with 91 additions and 3 deletions

View file

@ -84,6 +84,57 @@ testPerm({ net: true }, async function fetchURLEncodedFormDataSuccess() {
assertEqual(formData.get("field_2").toString(), "<Deno>");
});
testPerm({ net: true }, async function fetchInitStringBody() {
const data = "Hello World";
const response = await fetch("http://localhost:4545/echo_server", {
method: "POST",
body: data
});
const text = await response.text();
assertEqual(text, data);
assert(response.headers.get("content-type").startsWith("text/plain"));
});
testPerm({ net: true }, async function fetchInitTypedArrayBody() {
const data = "Hello World";
const response = await fetch("http://localhost:4545/echo_server", {
method: "POST",
body: new TextEncoder().encode(data)
});
const text = await response.text();
assertEqual(text, data);
});
testPerm({ net: true }, async function fetchInitURLSearchParamsBody() {
const data = "param1=value1&param2=value2";
const params = new URLSearchParams(data);
const response = await fetch("http://localhost:4545/echo_server", {
method: "POST",
body: params
});
const text = await response.text();
assertEqual(text, data);
assert(
response.headers
.get("content-type")
.startsWith("application/x-www-form-urlencoded")
);
});
testPerm({ net: true }, async function fetchInitBlobBody() {
const data = "const a = 1";
const blob = new Blob([data], {
type: "text/javascript"
});
const response = await fetch("http://localhost:4545/echo_server", {
method: "POST",
body: blob
});
const text = await response.text();
assertEqual(text, data);
assert(response.headers.get("content-type").startsWith("text/javascript"));
});
// TODO(ry) The following tests work but are flaky. There's a race condition
// somewhere. Here is what one of these flaky failures looks like:
//