2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2019-09-07 12:20:30 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrowsAsync,
|
2020-03-28 13:03:49 -04:00
|
|
|
fail,
|
2021-02-22 07:26:17 -05:00
|
|
|
unimplemented,
|
2020-09-27 06:22:32 -04:00
|
|
|
unitTest,
|
2019-09-07 12:20:30 -04:00
|
|
|
} from "./test_util.ts";
|
2021-04-05 18:05:36 -04:00
|
|
|
import { Buffer } from "../../../test_util/std/io/buffer.ts";
|
2018-08-30 13:49:24 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchProtocolError(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await fetch("file:///");
|
|
|
|
},
|
|
|
|
TypeError,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not supported",
|
2020-06-24 18:57:08 -04:00
|
|
|
);
|
2020-02-23 09:45:02 -05:00
|
|
|
});
|
|
|
|
|
2021-02-22 07:26:17 -05:00
|
|
|
function findClosedPortInRange(
|
|
|
|
minPort: number,
|
|
|
|
maxPort: number,
|
|
|
|
): number | never {
|
|
|
|
let port = minPort;
|
|
|
|
|
|
|
|
// If we hit the return statement of this loop
|
|
|
|
// that means that we did not throw an
|
|
|
|
// AddrInUse error when we executed Deno.listen.
|
|
|
|
while (port < maxPort) {
|
|
|
|
try {
|
|
|
|
const listener = Deno.listen({ port });
|
|
|
|
listener.close();
|
|
|
|
return port;
|
2021-04-09 17:35:29 -04:00
|
|
|
} catch (_e) {
|
2021-02-22 07:26:17 -05:00
|
|
|
port++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unimplemented(
|
|
|
|
`No available ports between ${minPort} and ${maxPort} to test fetch`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchConnectionError(): Promise<void> {
|
2021-02-22 07:26:17 -05:00
|
|
|
const port = findClosedPortInRange(4000, 9999);
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
2021-02-22 07:26:17 -05:00
|
|
|
await fetch(`http://localhost:${port}`);
|
2020-06-24 18:57:08 -04:00
|
|
|
},
|
2020-12-26 08:06:00 -05:00
|
|
|
TypeError,
|
2020-07-14 15:24:17 -04:00
|
|
|
"error trying to connect",
|
2020-06-24 18:57:08 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-09-11 17:34:22 -04:00
|
|
|
|
2020-12-26 08:06:00 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchDnsError(): Promise<void> {
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await fetch("http://nil/");
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"error trying to connect",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInvalidUriError(): Promise<void> {
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await fetch("http://<invalid>/");
|
|
|
|
},
|
2021-04-20 08:47:22 -04:00
|
|
|
TypeError,
|
2020-12-26 08:06:00 -05:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchJsonSuccess(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2018-08-30 13:49:24 -04:00
|
|
|
const json = await response.json();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(json.name, "deno");
|
2018-08-30 13:49:24 -04:00
|
|
|
});
|
2018-09-05 02:29:02 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function fetchPerm(): Promise<void> {
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2019-10-31 22:33:27 -04:00
|
|
|
await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2018-09-05 02:29:02 -04:00
|
|
|
});
|
2018-09-12 15:16:42 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchUrl(): Promise<void> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
|
|
|
assertEquals(response.url, "http://localhost:4545/cli/tests/fixture.json");
|
2020-04-10 09:51:17 -04:00
|
|
|
const _json = await response.json();
|
2019-12-14 07:49:30 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchURL(): Promise<void> {
|
2019-12-14 07:49:30 -05:00
|
|
|
const response = await fetch(
|
2020-07-14 15:24:17 -04:00
|
|
|
new URL("http://localhost:4545/cli/tests/fixture.json"),
|
2019-12-14 07:49:30 -05:00
|
|
|
);
|
|
|
|
assertEquals(response.url, "http://localhost:4545/cli/tests/fixture.json");
|
2020-04-10 09:51:17 -04:00
|
|
|
const _json = await response.json();
|
2019-08-16 18:20:04 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchHeaders(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2018-09-12 15:16:42 -04:00
|
|
|
const headers = response.headers;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(headers.get("Content-Type"), "application/json");
|
2020-04-10 09:51:17 -04:00
|
|
|
const _json = await response.json();
|
2018-09-12 15:16:42 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchBlob(): Promise<void> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2018-09-14 13:56:37 -04:00
|
|
|
const headers = response.headers;
|
|
|
|
const blob = await response.blob();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(blob.type, headers.get("Content-Type"));
|
|
|
|
assertEquals(blob.size, Number(headers.get("Content-Length")));
|
2018-09-14 13:56:37 -04:00
|
|
|
});
|
2018-09-30 10:31:50 -04:00
|
|
|
|
2020-06-01 08:37:46 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchBodyUsedReader(): Promise<void> {
|
|
|
|
const response = await fetch(
|
2020-07-14 15:24:17 -04:00
|
|
|
"http://localhost:4545/cli/tests/fixture.json",
|
2020-06-01 08:37:46 -04:00
|
|
|
);
|
|
|
|
assert(response.body !== null);
|
|
|
|
|
|
|
|
const reader = response.body.getReader();
|
|
|
|
// Getting a reader should lock the stream but does not consume the body
|
|
|
|
// so bodyUsed should not be true
|
|
|
|
assertEquals(response.bodyUsed, false);
|
|
|
|
reader.releaseLock();
|
|
|
|
await response.json();
|
|
|
|
assertEquals(response.bodyUsed, true);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-01 08:37:46 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchBodyUsedCancelStream(): Promise<void> {
|
|
|
|
const response = await fetch(
|
2020-07-14 15:24:17 -04:00
|
|
|
"http://localhost:4545/cli/tests/fixture.json",
|
2020-06-01 08:37:46 -04:00
|
|
|
);
|
|
|
|
assert(response.body !== null);
|
|
|
|
|
|
|
|
assertEquals(response.bodyUsed, false);
|
|
|
|
const promise = response.body.cancel();
|
|
|
|
assertEquals(response.bodyUsed, true);
|
|
|
|
await promise;
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-01 08:37:46 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchAsyncIterator(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2019-06-22 10:22:27 -04:00
|
|
|
const headers = response.headers;
|
2020-05-27 16:37:19 -04:00
|
|
|
|
|
|
|
assert(response.body !== null);
|
2019-06-22 10:22:27 -04:00
|
|
|
let total = 0;
|
|
|
|
for await (const chunk of response.body) {
|
2020-10-19 11:01:36 -04:00
|
|
|
assert(chunk instanceof Uint8Array);
|
2019-06-22 10:22:27 -04:00
|
|
|
total += chunk.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(total, Number(headers.get("Content-Length")));
|
|
|
|
});
|
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchBodyReader(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
|
|
|
const headers = response.headers;
|
|
|
|
assert(response.body !== null);
|
2020-10-19 11:01:36 -04:00
|
|
|
const reader = response.body.getReader();
|
2020-05-25 12:55:16 -04:00
|
|
|
let total = 0;
|
|
|
|
while (true) {
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
if (done) break;
|
|
|
|
assert(value);
|
2020-10-19 11:01:36 -04:00
|
|
|
assert(value instanceof Uint8Array);
|
2020-05-25 12:55:16 -04:00
|
|
|
total += value.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(total, Number(headers.get("Content-Length")));
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchBodyReaderBigBody(): Promise<void> {
|
|
|
|
const data = "a".repeat(10 << 10); // 10mb
|
2020-07-04 13:05:01 -04:00
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: data,
|
|
|
|
});
|
2020-05-25 12:55:16 -04:00
|
|
|
assert(response.body !== null);
|
|
|
|
const reader = await response.body.getReader();
|
|
|
|
let total = 0;
|
|
|
|
while (true) {
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
if (done) break;
|
|
|
|
assert(value);
|
|
|
|
total += value.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(total, data.length);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-05-25 12:55:16 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function responseClone(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2018-10-21 17:42:18 -04:00
|
|
|
const response1 = response.clone();
|
|
|
|
assert(response !== response1);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(response.status, response1.status);
|
|
|
|
assertEquals(response.statusText, response1.statusText);
|
2020-02-19 15:36:18 -05:00
|
|
|
const u8a = new Uint8Array(await response.arrayBuffer());
|
|
|
|
const u8a1 = new Uint8Array(await response1.arrayBuffer());
|
|
|
|
for (let i = 0; i < u8a.byteLength; i++) {
|
|
|
|
assertEquals(u8a[i], u8a1[i]);
|
2018-10-21 17:42:18 -04:00
|
|
|
}
|
|
|
|
});
|
2018-11-14 20:36:34 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchMultipartFormDataSuccess(): Promise<void> {
|
|
|
|
const response = await fetch(
|
2020-07-14 15:24:17 -04:00
|
|
|
"http://localhost:4545/multipart_form_data.txt",
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
const formData = await response.formData();
|
|
|
|
assert(formData.has("field_1"));
|
|
|
|
assertEquals(formData.get("field_1")!.toString(), "value_1 \r\n");
|
|
|
|
assert(formData.has("field_2"));
|
2020-05-27 16:37:19 -04:00
|
|
|
const file = formData.get("field_2") as File;
|
|
|
|
assertEquals(file.name, "file.js");
|
|
|
|
|
|
|
|
assertEquals(await file.text(), `console.log("Hi")`);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-12-21 17:09:53 -05:00
|
|
|
|
2020-12-30 17:46:08 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchMultipartFormBadContentType(): Promise<void> {
|
|
|
|
const response = await fetch(
|
|
|
|
"http://localhost:4545/multipart_form_bad_content_type",
|
|
|
|
);
|
|
|
|
assert(response.body !== null);
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await response.formData();
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Invalid form data",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
2019-04-21 16:40:10 -04:00
|
|
|
async function fetchURLEncodedFormDataSuccess(): Promise<void> {
|
|
|
|
const response = await fetch(
|
2020-07-14 15:24:17 -04:00
|
|
|
"http://localhost:4545/cli/tests/subdir/form_urlencoded.txt",
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
|
|
|
const formData = await response.formData();
|
|
|
|
assert(formData.has("field_1"));
|
2020-02-19 15:36:18 -05:00
|
|
|
assertEquals(formData.get("field_1")!.toString(), "Hi");
|
2019-04-21 16:40:10 -04:00
|
|
|
assert(formData.has("field_2"));
|
2020-02-19 15:36:18 -05:00
|
|
|
assertEquals(formData.get("field_2")!.toString(), "<Deno>");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2018-12-21 17:09:53 -05:00
|
|
|
|
2020-06-01 08:32:08 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitFormDataBinaryFileBody(): Promise<void> {
|
|
|
|
// Some random bytes
|
2020-07-14 15:24:17 -04:00
|
|
|
// deno-fmt-ignore
|
2020-06-01 08:32:08 -04:00
|
|
|
const binaryFile = new Uint8Array([108,2,0,0,145,22,162,61,157,227,166,77,138,75,180,56,119,188,177,183]);
|
|
|
|
const response = await fetch("http://localhost:4545/echo_multipart_file", {
|
|
|
|
method: "POST",
|
|
|
|
body: binaryFile,
|
|
|
|
});
|
|
|
|
const resultForm = await response.formData();
|
|
|
|
const resultFile = resultForm.get("file") as File;
|
|
|
|
|
|
|
|
assertEquals(resultFile.type, "application/octet-stream");
|
|
|
|
assertEquals(resultFile.name, "file.bin");
|
|
|
|
assertEquals(new Uint8Array(await resultFile.arrayBuffer()), binaryFile);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-01 08:32:08 -04:00
|
|
|
);
|
|
|
|
|
2020-06-08 12:08:26 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitFormDataMultipleFilesBody(): Promise<void> {
|
|
|
|
const files = [
|
|
|
|
{
|
2020-07-14 15:24:17 -04:00
|
|
|
// deno-fmt-ignore
|
2020-06-08 12:08:26 -04:00
|
|
|
content: new Uint8Array([137,80,78,71,13,10,26,10, 137, 1, 25]),
|
|
|
|
type: "image/png",
|
|
|
|
name: "image",
|
|
|
|
fileName: "some-image.png",
|
|
|
|
},
|
|
|
|
{
|
2020-07-14 15:24:17 -04:00
|
|
|
// deno-fmt-ignore
|
2020-06-08 12:08:26 -04:00
|
|
|
content: new Uint8Array([108,2,0,0,145,22,162,61,157,227,166,77,138,75,180,56,119,188,177,183]),
|
|
|
|
name: "file",
|
|
|
|
fileName: "file.bin",
|
|
|
|
expectedType: "application/octet-stream",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
content: new TextEncoder().encode("deno land"),
|
|
|
|
type: "text/plain",
|
|
|
|
name: "text",
|
|
|
|
fileName: "deno.txt",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const form = new FormData();
|
|
|
|
form.append("field", "value");
|
|
|
|
for (const file of files) {
|
|
|
|
form.append(
|
|
|
|
file.name,
|
|
|
|
new Blob([file.content], { type: file.type }),
|
2020-07-14 15:24:17 -04:00
|
|
|
file.fileName,
|
2020-06-08 12:08:26 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: form,
|
|
|
|
});
|
|
|
|
const resultForm = await response.formData();
|
|
|
|
assertEquals(form.get("field"), resultForm.get("field"));
|
|
|
|
for (const file of files) {
|
|
|
|
const inputFile = form.get(file.name) as File;
|
|
|
|
const resultFile = resultForm.get(file.name) as File;
|
|
|
|
assertEquals(inputFile.size, resultFile.size);
|
|
|
|
assertEquals(inputFile.name, resultFile.name);
|
|
|
|
assertEquals(file.expectedType || file.type, resultFile.type);
|
|
|
|
assertEquals(
|
|
|
|
new Uint8Array(await resultFile.arrayBuffer()),
|
2020-07-14 15:24:17 -04:00
|
|
|
file.content,
|
2020-06-08 12:08:26 -04:00
|
|
|
);
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-08 12:08:26 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
|
|
|
async function fetchWithRedirection(): Promise<void> {
|
2020-07-04 13:05:01 -04:00
|
|
|
const response = await fetch("http://localhost:4546/README.md");
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(response.status, 200);
|
|
|
|
assertEquals(response.statusText, "OK");
|
2020-07-04 13:05:01 -04:00
|
|
|
assertEquals(response.url, "http://localhost:4545/README.md");
|
2020-03-04 11:31:14 -05:00
|
|
|
const body = await response.text();
|
2020-07-04 13:05:01 -04:00
|
|
|
assert(body.includes("Deno"));
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-06-24 09:34:09 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
|
|
|
async function fetchWithRelativeRedirection(): Promise<void> {
|
2020-07-04 13:05:01 -04:00
|
|
|
const response = await fetch(
|
2020-07-14 15:24:17 -04:00
|
|
|
"http://localhost:4545/cli/tests/001_hello.js",
|
2020-07-04 13:05:01 -04:00
|
|
|
);
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(response.status, 200);
|
|
|
|
assertEquals(response.statusText, "OK");
|
|
|
|
const body = await response.text();
|
2020-07-04 13:05:01 -04:00
|
|
|
assert(body.includes("Hello"));
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-06-24 09:34:09 -04:00
|
|
|
|
2020-07-13 00:53:36 -04:00
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
perms: { net: true },
|
|
|
|
},
|
|
|
|
async function fetchWithRelativeRedirectionUrl(): Promise<void> {
|
|
|
|
const cases = [
|
|
|
|
["end", "http://localhost:4550/a/b/end"],
|
|
|
|
["/end", "http://localhost:4550/end"],
|
|
|
|
];
|
|
|
|
for (const [loc, redUrl] of cases) {
|
|
|
|
const response = await fetch("http://localhost:4550/a/b/c", {
|
|
|
|
headers: new Headers([["x-location", loc]]),
|
|
|
|
});
|
|
|
|
assertEquals(response.url, redUrl);
|
|
|
|
assertEquals(response.redirected, true);
|
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(await response.text(), "");
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-13 00:53:36 -04:00
|
|
|
);
|
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
|
|
|
async function fetchWithInfRedirection(): Promise<void> {
|
2021-04-20 08:47:22 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
() => fetch("http://localhost:4549/cli/tests"),
|
|
|
|
TypeError,
|
|
|
|
"redirect",
|
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-10 11:38:02 -04:00
|
|
|
);
|
2019-06-24 09:34:09 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitStringBody(): Promise<void> {
|
|
|
|
const data = "Hello World";
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: data,
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
|
|
|
const text = await response.text();
|
|
|
|
assertEquals(text, data);
|
|
|
|
assert(response.headers.get("content-type")!.startsWith("text/plain"));
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-01-03 06:41:20 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchRequestInitStringBody(): Promise<void> {
|
|
|
|
const data = "Hello World";
|
|
|
|
const req = new Request("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: data,
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
|
|
|
const response = await fetch(req);
|
|
|
|
const text = await response.text();
|
|
|
|
assertEquals(text, data);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-05-01 23:56:42 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitTypedArrayBody(): Promise<void> {
|
|
|
|
const data = "Hello World";
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: new TextEncoder().encode(data),
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
|
|
|
const text = await response.text();
|
|
|
|
assertEquals(text, data);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
2020-05-25 09:26:36 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitArrayBufferBody(): Promise<void> {
|
|
|
|
const data = "Hello World";
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: new TextEncoder().encode(data).buffer,
|
|
|
|
});
|
|
|
|
const text = await response.text();
|
|
|
|
assertEquals(text, data);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-05-25 09:26:36 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitURLSearchParamsBody(): Promise<void> {
|
|
|
|
const data = "param1=value1¶m2=value2";
|
|
|
|
const params = new URLSearchParams(data);
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: params,
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
|
|
|
const text = await response.text();
|
|
|
|
assertEquals(text, data);
|
|
|
|
assert(
|
|
|
|
response.headers
|
|
|
|
.get("content-type")!
|
2020-07-14 15:24:17 -04:00
|
|
|
.startsWith("application/x-www-form-urlencoded"),
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-01-03 06:41:20 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchInitBlobBody(): Promise<
|
2019-04-21 16:40:10 -04:00
|
|
|
void
|
|
|
|
> {
|
2019-01-03 06:41:20 -05:00
|
|
|
const data = "const a = 1";
|
|
|
|
const blob = new Blob([data], {
|
2020-03-28 13:03:49 -04:00
|
|
|
type: "text/javascript",
|
2019-01-03 06:41:20 -05:00
|
|
|
});
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: blob,
|
2019-01-03 06:41:20 -05:00
|
|
|
});
|
|
|
|
const text = await response.text();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(text, data);
|
2020-02-19 15:36:18 -05:00
|
|
|
assert(response.headers.get("content-type")!.startsWith("text/javascript"));
|
2019-01-03 06:41:20 -05:00
|
|
|
});
|
|
|
|
|
2020-03-17 02:32:43 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitFormDataBody(): Promise<void> {
|
|
|
|
const form = new FormData();
|
|
|
|
form.append("field", "value");
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: form,
|
2020-03-17 02:32:43 -04:00
|
|
|
});
|
|
|
|
const resultForm = await response.formData();
|
|
|
|
assertEquals(form.get("field"), resultForm.get("field"));
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-17 02:32:43 -04:00
|
|
|
);
|
|
|
|
|
2020-05-28 09:02:00 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitFormDataBlobFilenameBody(): Promise<void> {
|
|
|
|
const form = new FormData();
|
|
|
|
form.append("field", "value");
|
|
|
|
form.append("file", new Blob([new TextEncoder().encode("deno")]));
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: form,
|
|
|
|
});
|
|
|
|
const resultForm = await response.formData();
|
|
|
|
assertEquals(form.get("field"), resultForm.get("field"));
|
|
|
|
const file = resultForm.get("file");
|
|
|
|
assert(file instanceof File);
|
|
|
|
assertEquals(file.name, "blob");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-05-28 09:02:00 -04:00
|
|
|
);
|
|
|
|
|
2020-06-08 12:08:26 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitFormDataTextFileBody(): Promise<void> {
|
|
|
|
const fileContent = "deno land";
|
|
|
|
const form = new FormData();
|
|
|
|
form.append("field", "value");
|
|
|
|
form.append(
|
|
|
|
"file",
|
|
|
|
new Blob([new TextEncoder().encode(fileContent)], {
|
|
|
|
type: "text/plain",
|
|
|
|
}),
|
2020-07-14 15:24:17 -04:00
|
|
|
"deno.txt",
|
2020-06-08 12:08:26 -04:00
|
|
|
);
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: form,
|
|
|
|
});
|
|
|
|
const resultForm = await response.formData();
|
|
|
|
assertEquals(form.get("field"), resultForm.get("field"));
|
|
|
|
|
|
|
|
const file = form.get("file") as File;
|
|
|
|
const resultFile = resultForm.get("file") as File;
|
|
|
|
|
|
|
|
assertEquals(file.size, resultFile.size);
|
|
|
|
assertEquals(file.name, resultFile.name);
|
|
|
|
assertEquals(file.type, resultFile.type);
|
|
|
|
assertEquals(await file.text(), await resultFile.text());
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-08 12:08:26 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchUserAgent(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-09-11 07:31:00 -04:00
|
|
|
const data = "Hello World";
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: new TextEncoder().encode(data),
|
2019-09-11 07:31:00 -04:00
|
|
|
});
|
|
|
|
assertEquals(response.headers.get("user-agent"), `Deno/${Deno.version.deno}`);
|
|
|
|
await response.text();
|
|
|
|
});
|
|
|
|
|
2019-01-17 16:58:06 -05:00
|
|
|
// 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:
|
|
|
|
//
|
2020-03-04 11:31:14 -05:00
|
|
|
// unitTest fetchPostBodyString_permW0N1E0R0
|
2019-03-06 20:48:46 -05:00
|
|
|
// assertEquals failed. actual = expected = POST /blah HTTP/1.1
|
2019-01-17 16:58:06 -05:00
|
|
|
// hello: World
|
|
|
|
// foo: Bar
|
|
|
|
// host: 127.0.0.1:4502
|
|
|
|
// content-length: 11
|
|
|
|
// hello world
|
|
|
|
// Error: actual: expected: POST /blah HTTP/1.1
|
|
|
|
// hello: World
|
|
|
|
// foo: Bar
|
|
|
|
// host: 127.0.0.1:4502
|
|
|
|
// content-length: 11
|
|
|
|
// hello world
|
2019-03-06 20:48:46 -05:00
|
|
|
// at Object.assertEquals (file:///C:/deno/js/testing/util.ts:29:11)
|
2019-01-17 16:58:06 -05:00
|
|
|
// at fetchPostBodyString (file
|
|
|
|
|
2021-04-05 18:05:36 -04:00
|
|
|
function bufferServer(addr: string): Buffer {
|
2020-03-10 11:38:02 -04:00
|
|
|
const [hostname, port] = addr.split(":");
|
|
|
|
const listener = Deno.listen({
|
|
|
|
hostname,
|
2020-03-28 13:03:49 -04:00
|
|
|
port: Number(port),
|
2020-03-10 11:38:02 -04:00
|
|
|
}) as Deno.Listener;
|
2021-04-05 18:05:36 -04:00
|
|
|
const buf = new Buffer();
|
2020-03-10 11:38:02 -04:00
|
|
|
listener.accept().then(async (conn: Deno.Conn) => {
|
2018-11-14 21:19:38 -05:00
|
|
|
const p1 = buf.readFrom(conn);
|
|
|
|
const p2 = conn.write(
|
2018-11-14 20:36:34 -05:00
|
|
|
new TextEncoder().encode(
|
2020-07-14 15:24:17 -04:00
|
|
|
"HTTP/1.0 404 Not Found\r\nContent-Length: 2\r\n\r\nNF",
|
|
|
|
),
|
2018-11-14 20:36:34 -05:00
|
|
|
);
|
2018-11-14 21:19:38 -05:00
|
|
|
// Wait for both an EOF on the read side of the socket and for the write to
|
|
|
|
// complete before closing it. Due to keep-alive, the EOF won't be sent
|
|
|
|
// until the Connection close (HTTP/1.0) response, so readFrom() can't
|
|
|
|
// proceed write. Conversely, if readFrom() is async, waiting for the
|
|
|
|
// write() to complete is not a guarantee that we've read the incoming
|
|
|
|
// request.
|
|
|
|
await Promise.all([p1, p2]);
|
2018-11-14 20:36:34 -05:00
|
|
|
conn.close();
|
2018-11-14 21:19:38 -05:00
|
|
|
listener.close();
|
2018-11-14 20:36:34 -05:00
|
|
|
});
|
2018-11-14 21:19:38 -05:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
|
|
|
async function fetchRequest(): Promise<void> {
|
|
|
|
const addr = "127.0.0.1:4501";
|
|
|
|
const buf = bufferServer(addr);
|
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Hello", "World"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["Foo", "Bar"],
|
|
|
|
],
|
2020-03-10 11:38:02 -04:00
|
|
|
});
|
2020-05-24 12:04:57 -04:00
|
|
|
await response.arrayBuffer();
|
2020-03-10 11:38:02 -04:00
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
|
|
|
|
|
|
|
const actual = new TextDecoder().decode(buf.bytes());
|
|
|
|
const expected = [
|
|
|
|
"POST /blah HTTP/1.1\r\n",
|
2021-04-18 19:00:13 -04:00
|
|
|
"hello: World\r\n",
|
2021-04-20 08:47:22 -04:00
|
|
|
"foo: Bar\r\n",
|
2020-05-24 12:04:57 -04:00
|
|
|
"accept: */*\r\n",
|
|
|
|
`user-agent: Deno/${Deno.version.deno}\r\n`,
|
|
|
|
"accept-encoding: gzip, br\r\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
`host: ${addr}\r\n\r\n`,
|
2020-03-10 11:38:02 -04:00
|
|
|
].join("");
|
|
|
|
assertEquals(actual, expected);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-10 11:38:02 -04:00
|
|
|
);
|
2018-11-14 21:19:38 -05:00
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
|
|
|
async function fetchPostBodyString(): Promise<void> {
|
|
|
|
const addr = "127.0.0.1:4502";
|
|
|
|
const buf = bufferServer(addr);
|
|
|
|
const body = "hello world";
|
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Hello", "World"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["Foo", "Bar"],
|
2020-03-10 11:38:02 -04:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
body,
|
2020-03-10 11:38:02 -04:00
|
|
|
});
|
2020-05-24 12:04:57 -04:00
|
|
|
await response.arrayBuffer();
|
2020-03-10 11:38:02 -04:00
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
|
|
|
|
|
|
|
const actual = new TextDecoder().decode(buf.bytes());
|
|
|
|
const expected = [
|
|
|
|
"POST /blah HTTP/1.1\r\n",
|
2021-04-18 19:00:13 -04:00
|
|
|
"hello: World\r\n",
|
2021-04-20 08:47:22 -04:00
|
|
|
"foo: Bar\r\n",
|
|
|
|
"content-type: text/plain;charset=UTF-8\r\n",
|
2020-05-24 12:04:57 -04:00
|
|
|
"accept: */*\r\n",
|
|
|
|
`user-agent: Deno/${Deno.version.deno}\r\n`,
|
|
|
|
"accept-encoding: gzip, br\r\n",
|
2020-03-10 11:38:02 -04:00
|
|
|
`host: ${addr}\r\n`,
|
|
|
|
`content-length: ${body.length}\r\n\r\n`,
|
2020-03-28 13:03:49 -04:00
|
|
|
body,
|
2020-03-10 11:38:02 -04:00
|
|
|
].join("");
|
|
|
|
assertEquals(actual, expected);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-10 11:38:02 -04:00
|
|
|
);
|
2018-11-14 21:19:38 -05:00
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
|
|
|
async function fetchPostBodyTypedArray(): Promise<void> {
|
|
|
|
const addr = "127.0.0.1:4503";
|
|
|
|
const buf = bufferServer(addr);
|
|
|
|
const bodyStr = "hello world";
|
|
|
|
const body = new TextEncoder().encode(bodyStr);
|
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Hello", "World"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["Foo", "Bar"],
|
2020-03-10 11:38:02 -04:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
body,
|
2020-03-10 11:38:02 -04:00
|
|
|
});
|
2020-05-24 12:04:57 -04:00
|
|
|
await response.arrayBuffer();
|
2020-03-10 11:38:02 -04:00
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
|
|
|
|
|
|
|
const actual = new TextDecoder().decode(buf.bytes());
|
|
|
|
const expected = [
|
|
|
|
"POST /blah HTTP/1.1\r\n",
|
2021-04-18 19:00:13 -04:00
|
|
|
"hello: World\r\n",
|
2021-04-20 08:47:22 -04:00
|
|
|
"foo: Bar\r\n",
|
2020-05-24 12:04:57 -04:00
|
|
|
"accept: */*\r\n",
|
|
|
|
`user-agent: Deno/${Deno.version.deno}\r\n`,
|
|
|
|
"accept-encoding: gzip, br\r\n",
|
2020-03-10 11:38:02 -04:00
|
|
|
`host: ${addr}\r\n`,
|
|
|
|
`content-length: ${body.byteLength}\r\n\r\n`,
|
2020-03-28 13:03:49 -04:00
|
|
|
bodyStr,
|
2020-03-10 11:38:02 -04:00
|
|
|
].join("");
|
|
|
|
assertEquals(actual, expected);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-10 11:38:02 -04:00
|
|
|
);
|
2020-02-03 09:54:47 -05:00
|
|
|
|
2020-12-09 10:48:06 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
perms: { net: true },
|
|
|
|
},
|
|
|
|
async function fetchWithNonAsciiRedirection(): Promise<void> {
|
|
|
|
const response = await fetch("http://localhost:4545/non_ascii_redirect", {
|
|
|
|
redirect: "manual",
|
|
|
|
});
|
|
|
|
assertEquals(response.status, 301);
|
|
|
|
assertEquals(response.headers.get("location"), "/redirect®");
|
|
|
|
await response.text();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
|
|
|
async function fetchWithManualRedirection(): Promise<void> {
|
|
|
|
const response = await fetch("http://localhost:4546/", {
|
2020-03-28 13:03:49 -04:00
|
|
|
redirect: "manual",
|
2020-03-04 11:31:14 -05:00
|
|
|
}); // will redirect to http://localhost:4545/
|
2020-11-24 15:00:35 -05:00
|
|
|
assertEquals(response.status, 301);
|
|
|
|
assertEquals(response.url, "http://localhost:4546/");
|
2021-04-20 08:47:22 -04:00
|
|
|
assertEquals(response.type, "basic");
|
2020-11-24 15:00:35 -05:00
|
|
|
assertEquals(response.headers.get("Location"), "http://localhost:4545/");
|
2021-04-20 08:47:22 -04:00
|
|
|
await response.body!.cancel();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-02-03 09:54:47 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
|
|
|
async function fetchWithErrorRedirection(): Promise<void> {
|
2021-04-20 08:47:22 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
() =>
|
|
|
|
fetch("http://localhost:4546/", {
|
|
|
|
redirect: "error",
|
|
|
|
}),
|
|
|
|
TypeError,
|
|
|
|
"redirect",
|
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-02-03 09:54:47 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function responseRedirect(): void {
|
2020-04-10 09:51:17 -04:00
|
|
|
const redir = Response.redirect("example.com/newLocation", 301);
|
2020-02-03 09:54:47 -05:00
|
|
|
assertEquals(redir.status, 301);
|
|
|
|
assertEquals(redir.statusText, "");
|
|
|
|
assertEquals(redir.url, "");
|
2021-04-20 08:47:22 -04:00
|
|
|
assertEquals(
|
|
|
|
redir.headers.get("Location"),
|
|
|
|
"http://js-unit-tests/foo/example.com/newLocation",
|
|
|
|
);
|
2020-02-03 09:54:47 -05:00
|
|
|
assertEquals(redir.type, "default");
|
|
|
|
});
|
2020-05-25 16:20:09 -04:00
|
|
|
|
2020-09-08 05:46:15 -04:00
|
|
|
unitTest(async function responseWithoutBody(): Promise<void> {
|
|
|
|
const response = new Response();
|
|
|
|
assertEquals(await response.arrayBuffer(), new ArrayBuffer(0));
|
|
|
|
assertEquals(await response.blob(), new Blob([]));
|
|
|
|
assertEquals(await response.text(), "");
|
|
|
|
await assertThrowsAsync(async () => {
|
|
|
|
await response.json();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-25 16:20:09 -04:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchBodyReadTwice(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
|
|
|
|
|
|
|
// Read body
|
|
|
|
const _json = await response.json();
|
|
|
|
assert(_json);
|
|
|
|
|
|
|
|
// All calls after the body was consumed, should fail
|
2020-06-02 00:24:44 -04:00
|
|
|
const methods = ["json", "text", "formData", "arrayBuffer"] as const;
|
2020-05-25 16:20:09 -04:00
|
|
|
for (const method of methods) {
|
|
|
|
try {
|
|
|
|
await response[method]();
|
|
|
|
fail(
|
2020-07-14 15:24:17 -04:00
|
|
|
"Reading body multiple times should failed, the stream should've been locked.",
|
2020-05-25 16:20:09 -04:00
|
|
|
);
|
2020-06-19 05:05:37 -04:00
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
2020-05-25 16:20:09 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchBodyReaderAfterRead(): Promise<void> {
|
|
|
|
const response = await fetch(
|
2020-07-14 15:24:17 -04:00
|
|
|
"http://localhost:4545/cli/tests/fixture.json",
|
2020-05-25 16:20:09 -04:00
|
|
|
);
|
|
|
|
assert(response.body !== null);
|
|
|
|
const reader = await response.body.getReader();
|
|
|
|
while (true) {
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
if (done) break;
|
|
|
|
assert(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
response.body.getReader();
|
|
|
|
fail("The stream should've been locked.");
|
2020-06-19 05:05:37 -04:00
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-05-25 16:20:09 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchBodyReaderWithCancelAndNewReader(): Promise<void> {
|
|
|
|
const data = "a".repeat(1 << 10);
|
2020-07-04 13:05:01 -04:00
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: data,
|
|
|
|
});
|
2020-05-25 16:20:09 -04:00
|
|
|
assert(response.body !== null);
|
|
|
|
const firstReader = await response.body.getReader();
|
|
|
|
|
|
|
|
// Acquire reader without reading & release
|
|
|
|
await firstReader.releaseLock();
|
|
|
|
|
|
|
|
const reader = await response.body.getReader();
|
|
|
|
|
|
|
|
let total = 0;
|
|
|
|
while (true) {
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
if (done) break;
|
|
|
|
assert(value);
|
|
|
|
total += value.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(total, data.length);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-05-25 16:20:09 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchBodyReaderWithReadCancelAndNewReader(): Promise<void> {
|
|
|
|
const data = "a".repeat(1 << 10);
|
|
|
|
|
2020-07-04 13:05:01 -04:00
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: data,
|
|
|
|
});
|
2020-05-25 16:20:09 -04:00
|
|
|
assert(response.body !== null);
|
|
|
|
const firstReader = await response.body.getReader();
|
|
|
|
|
|
|
|
// Do one single read with first reader
|
|
|
|
const { value: firstValue } = await firstReader.read();
|
|
|
|
assert(firstValue);
|
|
|
|
await firstReader.releaseLock();
|
|
|
|
|
|
|
|
// Continue read with second reader
|
|
|
|
const reader = await response.body.getReader();
|
|
|
|
let total = firstValue.length || 0;
|
|
|
|
while (true) {
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
if (done) break;
|
|
|
|
assert(value);
|
|
|
|
total += value.length;
|
|
|
|
}
|
|
|
|
assertEquals(total, data.length);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-05-25 16:20:09 -04:00
|
|
|
);
|
2020-05-30 01:02:41 -04:00
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchResourceCloseAfterStreamCancel(): Promise<void> {
|
|
|
|
const res = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
|
|
|
assert(res.body !== null);
|
|
|
|
|
|
|
|
// After ReadableStream.cancel is called, resource handle must be closed
|
|
|
|
// The test should not fail with: Test case is leaking resources
|
|
|
|
await res.body.cancel();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-05-30 01:02:41 -04:00
|
|
|
);
|
2020-05-31 19:21:14 -04:00
|
|
|
|
2021-01-04 08:27:29 -05:00
|
|
|
// FIXME(bartlomieju): for reasons unknown after working for
|
|
|
|
// a few months without a problem; this test started failing
|
|
|
|
// consistently on Windows CI with following error:
|
|
|
|
// TypeError: error sending request for url (http://localhost:4545/echo_server):
|
|
|
|
// connection error: An established connection was aborted by
|
|
|
|
// the software in your host machine. (os error 10053)
|
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true }, ignore: Deno.build.os == "windows" },
|
2020-05-31 19:21:14 -04:00
|
|
|
async function fetchNullBodyStatus(): Promise<void> {
|
2020-06-03 09:43:11 -04:00
|
|
|
const nullBodyStatus = [101, 204, 205, 304];
|
2020-05-31 19:21:14 -04:00
|
|
|
|
|
|
|
for (const status of nullBodyStatus) {
|
|
|
|
const headers = new Headers([["x-status", String(status)]]);
|
2020-07-04 13:05:01 -04:00
|
|
|
const res = await fetch("http://localhost:4545/echo_server", {
|
2020-05-31 19:21:14 -04:00
|
|
|
body: "deno",
|
|
|
|
method: "POST",
|
|
|
|
headers,
|
|
|
|
});
|
|
|
|
assertEquals(res.body, null);
|
|
|
|
assertEquals(res.status, status);
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-05-31 19:21:14 -04:00
|
|
|
);
|
|
|
|
|
2020-07-07 22:25:34 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchResponseContentLength(): Promise<void> {
|
|
|
|
const body = new Uint8Array(2 ** 16);
|
|
|
|
const headers = new Headers([["content-type", "application/octet-stream"]]);
|
|
|
|
const res = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
body: body,
|
|
|
|
method: "POST",
|
|
|
|
headers,
|
|
|
|
});
|
|
|
|
assertEquals(Number(res.headers.get("content-length")), body.byteLength);
|
|
|
|
|
|
|
|
const blob = await res.blob();
|
|
|
|
// Make sure Body content-type is correctly set
|
|
|
|
assertEquals(blob.type, "application/octet-stream");
|
|
|
|
assertEquals(blob.size, body.byteLength);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-07 22:25:34 -04:00
|
|
|
);
|
|
|
|
|
2020-07-05 21:37:18 -04:00
|
|
|
unitTest(function fetchResponseConstructorNullBody(): void {
|
|
|
|
const nullBodyStatus = [204, 205, 304];
|
2020-05-31 19:21:14 -04:00
|
|
|
|
2020-07-05 21:37:18 -04:00
|
|
|
for (const status of nullBodyStatus) {
|
|
|
|
try {
|
|
|
|
new Response("deno", { status });
|
|
|
|
fail("Response with null body status cannot have body");
|
|
|
|
} catch (e) {
|
|
|
|
assert(e instanceof TypeError);
|
|
|
|
assertEquals(
|
|
|
|
e.message,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Response with null body status cannot have body",
|
2020-07-05 21:37:18 -04:00
|
|
|
);
|
2020-05-31 19:21:14 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-05 21:37:18 -04:00
|
|
|
});
|
2020-06-03 09:43:11 -04:00
|
|
|
|
2020-07-05 21:37:18 -04:00
|
|
|
unitTest(function fetchResponseConstructorInvalidStatus(): void {
|
|
|
|
const invalidStatus = [101, 600, 199, null, "", NaN];
|
|
|
|
|
|
|
|
for (const status of invalidStatus) {
|
|
|
|
try {
|
|
|
|
// deno-lint-ignore ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
new Response("deno", { status });
|
|
|
|
fail(`Invalid status: ${status}`);
|
|
|
|
} catch (e) {
|
|
|
|
assert(e instanceof RangeError);
|
2021-04-20 08:47:22 -04:00
|
|
|
assert(e.message.endsWith("is outside the range [200, 599]."));
|
2020-06-03 09:43:11 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-05 21:37:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(function fetchResponseEmptyConstructor(): void {
|
|
|
|
const response = new Response();
|
|
|
|
assertEquals(response.status, 200);
|
|
|
|
assertEquals(response.body, null);
|
|
|
|
assertEquals(response.type, "default");
|
|
|
|
assertEquals(response.url, "");
|
|
|
|
assertEquals(response.redirected, false);
|
|
|
|
assertEquals(response.ok, true);
|
|
|
|
assertEquals(response.bodyUsed, false);
|
|
|
|
assertEquals([...response.headers], []);
|
|
|
|
});
|
2020-08-05 14:44:03 -04:00
|
|
|
|
2021-04-20 08:47:22 -04:00
|
|
|
// TODO(lucacasonato): reenable this test
|
2020-12-19 17:13:48 -05:00
|
|
|
unitTest(
|
2021-04-20 08:47:22 -04:00
|
|
|
{ perms: { net: true }, ignore: true },
|
2020-12-19 17:13:48 -05:00
|
|
|
async function fetchCustomHttpClientParamCertificateSuccess(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const client = Deno.createHttpClient(
|
|
|
|
{
|
|
|
|
caData: `-----BEGIN CERTIFICATE-----
|
|
|
|
MIIDIzCCAgugAwIBAgIJAMKPPW4tsOymMA0GCSqGSIb3DQEBCwUAMCcxCzAJBgNV
|
|
|
|
BAYTAlVTMRgwFgYDVQQDDA9FeGFtcGxlLVJvb3QtQ0EwIBcNMTkxMDIxMTYyODIy
|
|
|
|
WhgPMjExODA5MjcxNjI4MjJaMCcxCzAJBgNVBAYTAlVTMRgwFgYDVQQDDA9FeGFt
|
|
|
|
cGxlLVJvb3QtQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDMH/IO
|
|
|
|
2qtHfyBKwANNPB4K0q5JVSg8XxZdRpTTlz0CwU0oRO3uHrI52raCCfVeiQutyZop
|
|
|
|
eFZTDWeXGudGAFA2B5m3orWt0s+touPi8MzjsG2TQ+WSI66QgbXTNDitDDBtTVcV
|
|
|
|
5G3Ic+3SppQAYiHSekLISnYWgXLl+k5CnEfTowg6cjqjVr0KjL03cTN3H7b+6+0S
|
|
|
|
ws4rYbW1j4ExR7K6BFNH6572yq5qR20E6GqlY+EcOZpw4CbCk9lS8/CWuXze/vMs
|
|
|
|
OfDcc6K+B625d27wyEGZHedBomT2vAD7sBjvO8hn/DP1Qb46a8uCHR6NSfnJ7bXO
|
|
|
|
G1igaIbgY1zXirNdAgMBAAGjUDBOMB0GA1UdDgQWBBTzut+pwwDfqmMYcI9KNWRD
|
|
|
|
hxcIpTAfBgNVHSMEGDAWgBTzut+pwwDfqmMYcI9KNWRDhxcIpTAMBgNVHRMEBTAD
|
|
|
|
AQH/MA0GCSqGSIb3DQEBCwUAA4IBAQB9AqSbZ+hEglAgSHxAMCqRFdhVu7MvaQM0
|
|
|
|
P090mhGlOCt3yB7kdGfsIrUW6nQcTz7PPQFRaJMrFHPvFvPootkBUpTYR4hTkdce
|
|
|
|
H6RCRu2Jxl4Y9bY/uezd9YhGCYfUtfjA6/TH9FcuZfttmOOlxOt01XfNvVMIR6RM
|
|
|
|
z/AYhd+DeOXjr35F/VHeVpnk+55L0PYJsm1CdEbOs5Hy1ecR7ACuDkXnbM4fpz9I
|
|
|
|
kyIWJwk2zJReKcJMgi1aIinDM9ao/dca1G99PHOw8dnr4oyoTiv8ao6PWiSRHHMi
|
|
|
|
MNf4EgWfK+tZMnuqfpfO9740KzfcVoMNo4QJD4yn5YxroUOO/Azi
|
|
|
|
-----END CERTIFICATE-----
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const response = await fetch(
|
|
|
|
"https://localhost:5545/cli/tests/fixture.json",
|
|
|
|
{ client },
|
|
|
|
);
|
|
|
|
const json = await response.json();
|
|
|
|
assertEquals(json.name, "deno");
|
|
|
|
client.close();
|
|
|
|
},
|
|
|
|
);
|
2020-12-22 08:14:23 -05:00
|
|
|
|
2021-03-18 18:54:26 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchCustomClientUserAgent(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const data = "Hello World";
|
|
|
|
const client = Deno.createHttpClient({});
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
client,
|
|
|
|
method: "POST",
|
|
|
|
body: new TextEncoder().encode(data),
|
|
|
|
});
|
|
|
|
assertEquals(
|
|
|
|
response.headers.get("user-agent"),
|
|
|
|
`Deno/${Deno.version.deno}`,
|
|
|
|
);
|
|
|
|
await response.text();
|
|
|
|
client.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-12-22 08:14:23 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
perms: { net: true },
|
|
|
|
},
|
|
|
|
async function fetchPostBodyReadableStream(): Promise<void> {
|
|
|
|
const addr = "127.0.0.1:4502";
|
|
|
|
const buf = bufferServer(addr);
|
|
|
|
const stream = new TransformStream();
|
|
|
|
const writer = stream.writable.getWriter();
|
2021-01-14 16:57:19 -05:00
|
|
|
// transformer writes don't resolve until they are read, so awaiting these
|
|
|
|
// will cause the transformer to hang, as the suspend the transformer, it
|
|
|
|
// is also illogical to await for the reads, as that is the whole point of
|
|
|
|
// streams is to have a "queue" which gets drained...
|
|
|
|
writer.write(new TextEncoder().encode("hello "));
|
|
|
|
writer.write(new TextEncoder().encode("world"));
|
|
|
|
writer.close();
|
2020-12-22 08:14:23 -05:00
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Hello", "World"],
|
|
|
|
["Foo", "Bar"],
|
|
|
|
],
|
|
|
|
body: stream.readable,
|
|
|
|
});
|
|
|
|
await response.arrayBuffer();
|
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
|
|
|
|
|
|
|
const actual = new TextDecoder().decode(buf.bytes());
|
|
|
|
const expected = [
|
|
|
|
"POST /blah HTTP/1.1\r\n",
|
2021-04-18 19:00:13 -04:00
|
|
|
"hello: World\r\n",
|
2021-04-20 08:47:22 -04:00
|
|
|
"foo: Bar\r\n",
|
2020-12-22 08:14:23 -05:00
|
|
|
"accept: */*\r\n",
|
|
|
|
`user-agent: Deno/${Deno.version.deno}\r\n`,
|
|
|
|
"accept-encoding: gzip, br\r\n",
|
|
|
|
`host: ${addr}\r\n`,
|
2021-01-10 14:54:29 -05:00
|
|
|
`transfer-encoding: chunked\r\n\r\n`,
|
|
|
|
"6\r\n",
|
|
|
|
"hello \r\n",
|
|
|
|
"5\r\n",
|
|
|
|
"world\r\n",
|
|
|
|
"0\r\n\r\n",
|
2020-12-22 08:14:23 -05:00
|
|
|
].join("");
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
},
|
|
|
|
);
|