2022-01-20 02:10:16 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2019-09-07 12:20:30 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects,
|
2021-06-30 12:05:58 -04:00
|
|
|
deferred,
|
2020-03-28 13:03:49 -04:00
|
|
|
fail,
|
2021-02-22 07:26:17 -05:00
|
|
|
unimplemented,
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchRequiresOneArgument() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-05-03 03:05:42 -04:00
|
|
|
fetch as unknown as () => Promise<void>,
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function fetchProtocolError() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-08-05 07:08:58 -04:00
|
|
|
async () => {
|
2021-11-01 00:29:46 -04:00
|
|
|
await fetch("ftp://localhost:21/a/file");
|
2020-06-24 18:57:08 -04:00
|
|
|
},
|
|
|
|
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`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchConnectionError() {
|
2021-02-22 07:26:17 -05:00
|
|
|
const port = findClosedPortInRange(4000, 9999);
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-08-05 07:08:58 -04:00
|
|
|
async () => {
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchDnsError() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-08-05 07:08:58 -04:00
|
|
|
async () => {
|
2020-12-26 08:06:00 -05:00
|
|
|
await fetch("http://nil/");
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"error trying to connect",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchInvalidUriError() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-08-05 07:08:58 -04:00
|
|
|
async () => {
|
2020-12-26 08:06:00 -05:00
|
|
|
await fetch("http://<invalid>/");
|
|
|
|
},
|
2021-04-20 08:47:22 -04:00
|
|
|
TypeError,
|
2020-12-26 08:06:00 -05:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function fetchJsonSuccess() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const response = await fetch("http://localhost:4545/assets/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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: false } }, async function fetchPerm() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2022-09-19 10:32:21 -04:00
|
|
|
await fetch("http://localhost:4545/assets/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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function fetchUrl() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const response = await fetch("http://localhost:4545/assets/fixture.json");
|
|
|
|
assertEquals(response.url, "http://localhost:4545/assets/fixture.json");
|
2020-04-10 09:51:17 -04:00
|
|
|
const _json = await response.json();
|
2019-12-14 07:49:30 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function fetchURL() {
|
2019-12-14 07:49:30 -05:00
|
|
|
const response = await fetch(
|
2022-09-19 10:32:21 -04:00
|
|
|
new URL("http://localhost:4545/assets/fixture.json"),
|
2019-12-14 07:49:30 -05:00
|
|
|
);
|
2022-09-19 10:32:21 -04:00
|
|
|
assertEquals(response.url, "http://localhost:4545/assets/fixture.json");
|
2020-04-10 09:51:17 -04:00
|
|
|
const _json = await response.json();
|
2019-08-16 18:20:04 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function fetchHeaders() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const response = await fetch("http://localhost:4545/assets/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
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function fetchBlob() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const response = await fetch("http://localhost:4545/assets/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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchBodyUsedReader() {
|
2020-06-01 08:37:46 -04:00
|
|
|
const response = await fetch(
|
2022-09-19 10:32:21 -04:00
|
|
|
"http://localhost:4545/assets/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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchBodyUsedCancelStream() {
|
2020-06-01 08:37:46 -04:00
|
|
|
const response = await fetch(
|
2022-09-19 10:32:21 -04:00
|
|
|
"http://localhost:4545/assets/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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function fetchAsyncIterator() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const response = await fetch("http://localhost:4545/assets/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")));
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function fetchBodyReader() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const response = await fetch("http://localhost:4545/assets/fixture.json");
|
2020-05-25 12:55:16 -04:00
|
|
|
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")));
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchBodyReaderBigBody() {
|
2020-05-25 12:55:16 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function responseClone() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const response = await fetch("http://localhost:4545/assets/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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchMultipartFormDataSuccess() {
|
2020-03-04 11:31:14 -05:00
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchMultipartFormBadContentType() {
|
2020-12-30 17:46:08 -05:00
|
|
|
const response = await fetch(
|
|
|
|
"http://localhost:4545/multipart_form_bad_content_type",
|
|
|
|
);
|
|
|
|
assert(response.body !== null);
|
|
|
|
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-08-05 07:08:58 -04:00
|
|
|
async () => {
|
2020-12-30 17:46:08 -05:00
|
|
|
await response.formData();
|
|
|
|
},
|
|
|
|
TypeError,
|
2021-07-19 18:11:50 -04:00
|
|
|
"Body can not be decoded as form data",
|
2020-12-30 17:46:08 -05:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchURLEncodedFormDataSuccess() {
|
2019-04-21 16:40:10 -04:00
|
|
|
const response = await fetch(
|
2021-08-11 10:20:47 -04:00
|
|
|
"http://localhost:4545/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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchInitFormDataBinaryFileBody() {
|
2020-06-01 08:32:08 -04:00
|
|
|
// 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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchInitFormDataMultipleFilesBody() {
|
2020-06-08 12:08:26 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-04 11:31:14 -05:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchWithRedirection() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const response = await fetch("http://localhost:4546/assets/hello.txt");
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(response.status, 200);
|
|
|
|
assertEquals(response.statusText, "OK");
|
2022-09-19 10:32:21 -04:00
|
|
|
assertEquals(response.url, "http://localhost:4545/assets/hello.txt");
|
2020-03-04 11:31:14 -05:00
|
|
|
const body = await response.text();
|
2021-08-11 10:20:47 -04:00
|
|
|
assert(body.includes("Hello world!"));
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-06-24 09:34:09 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-04 11:31:14 -05:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchWithRelativeRedirection() {
|
2020-07-04 13:05:01 -04:00
|
|
|
const response = await fetch(
|
2022-09-19 10:32:21 -04:00
|
|
|
"http://localhost:4545/run/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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-07-13 00:53:36 -04:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-07-13 00:53:36 -04:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchWithRelativeRedirectionUrl() {
|
2020-07-13 00:53:36 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-10 11:38:02 -04:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchWithInfRedirection() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-08-11 10:20:47 -04:00
|
|
|
() => fetch("http://localhost:4549"),
|
2021-04-20 08:47:22 -04:00
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchInitStringBody() {
|
2020-03-04 11:31:14 -05: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: 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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchRequestInitStringBody() {
|
2020-03-04 11:31:14 -05:00
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchSeparateInit() {
|
2021-04-29 13:56:59 -04:00
|
|
|
// related to: https://github.com/denoland/deno/issues/10396
|
2022-09-19 10:32:21 -04:00
|
|
|
const req = new Request("http://localhost:4545/run/001_hello.js");
|
2021-04-29 13:56:59 -04:00
|
|
|
const init = {
|
|
|
|
method: "GET",
|
|
|
|
};
|
|
|
|
req.headers.set("foo", "bar");
|
|
|
|
const res = await fetch(req, init);
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
await res.text();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchInitTypedArrayBody() {
|
2020-03-04 11:31:14 -05: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),
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchInitArrayBufferBody() {
|
2020-05-25 09:26:36 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchInitURLSearchParamsBody() {
|
2020-03-04 11:31:14 -05:00
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function fetchInitBlobBody() {
|
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
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchInitFormDataBody() {
|
2020-03-17 02:32:43 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchInitFormDataBlobFilenameBody() {
|
2020-05-28 09:02:00 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchInitFormDataTextFileBody() {
|
2020-06-08 12:08:26 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function fetchUserAgent() {
|
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();
|
|
|
|
});
|
|
|
|
|
2021-09-17 09:02:44 -04:00
|
|
|
function bufferServer(addr: string): Promise<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-09-17 09:02:44 -04:00
|
|
|
return listener.accept().then(async (conn: Deno.Conn) => {
|
|
|
|
const buf = new Buffer();
|
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();
|
2021-09-17 09:02:44 -04:00
|
|
|
return buf;
|
2018-11-14 20:36:34 -05:00
|
|
|
});
|
2018-11-14 21:19:38 -05:00
|
|
|
}
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-10 11:38:02 -04:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchRequest() {
|
2020-03-10 11:38:02 -04:00
|
|
|
const addr = "127.0.0.1:4501";
|
2021-09-17 09:02:44 -04:00
|
|
|
const bufPromise = bufferServer(addr);
|
2020-03-10 11:38:02 -04:00
|
|
|
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");
|
|
|
|
|
2021-09-17 09:02:44 -04:00
|
|
|
const actual = new TextDecoder().decode((await bufPromise).bytes());
|
2020-03-10 11:38:02 -04:00
|
|
|
const expected = [
|
|
|
|
"POST /blah HTTP/1.1\r\n",
|
2021-11-09 06:10:40 -05:00
|
|
|
"content-length: 0\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",
|
2022-06-19 17:33:24 -04:00
|
|
|
"accept-language: *\r\n",
|
|
|
|
`user-agent: Deno/${Deno.version.deno}\r\n`,
|
|
|
|
"accept-encoding: gzip, br\r\n",
|
|
|
|
`host: ${addr}\r\n\r\n`,
|
|
|
|
].join("");
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
permissions: { net: true },
|
|
|
|
},
|
|
|
|
async function fetchRequestAcceptHeaders() {
|
|
|
|
const addr = "127.0.0.1:4501";
|
|
|
|
const bufPromise = bufferServer(addr);
|
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Accept", "text/html"],
|
|
|
|
["Accept-Language", "en-US"],
|
|
|
|
],
|
|
|
|
});
|
|
|
|
await response.arrayBuffer();
|
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
|
|
|
|
|
|
|
const actual = new TextDecoder().decode((await bufPromise).bytes());
|
|
|
|
const expected = [
|
|
|
|
"POST /blah HTTP/1.1\r\n",
|
|
|
|
"content-length: 0\r\n",
|
|
|
|
"accept: text/html\r\n",
|
|
|
|
"accept-language: en-US\r\n",
|
2020-05-24 12:04:57 -04:00
|
|
|
`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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-10 11:38:02 -04:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchPostBodyString() {
|
2022-02-16 10:51:32 -05:00
|
|
|
const addr = "127.0.0.1:4511";
|
2021-09-17 09:02:44 -04:00
|
|
|
const bufPromise = bufferServer(addr);
|
2020-03-10 11:38:02 -04:00
|
|
|
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");
|
|
|
|
|
2021-09-17 09:02:44 -04:00
|
|
|
const actual = new TextDecoder().decode((await bufPromise).bytes());
|
2020-03-10 11:38:02 -04:00
|
|
|
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",
|
2022-06-19 17:33:24 -04:00
|
|
|
"accept-language: *\r\n",
|
2020-05-24 12:04:57 -04:00
|
|
|
`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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-10 11:38:02 -04:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchPostBodyTypedArray() {
|
2020-03-10 11:38:02 -04:00
|
|
|
const addr = "127.0.0.1:4503";
|
2021-09-17 09:02:44 -04:00
|
|
|
const bufPromise = bufferServer(addr);
|
2020-03-10 11:38:02 -04:00
|
|
|
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");
|
|
|
|
|
2021-09-17 09:02:44 -04:00
|
|
|
const actual = new TextDecoder().decode((await bufPromise).bytes());
|
2020-03-10 11:38:02 -04:00
|
|
|
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",
|
2022-06-19 17:33:24 -04:00
|
|
|
"accept-language: *\r\n",
|
2020-05-24 12:04:57 -04:00
|
|
|
`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
|
|
|
|
2022-08-23 06:43:04 -04:00
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
permissions: { net: true },
|
|
|
|
},
|
|
|
|
async function fetchUserSetContentLength() {
|
|
|
|
const addr = "127.0.0.1:4501";
|
|
|
|
const bufPromise = bufferServer(addr);
|
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Content-Length", "10"],
|
|
|
|
],
|
|
|
|
});
|
|
|
|
await response.arrayBuffer();
|
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
|
|
|
|
|
|
|
const actual = new TextDecoder().decode((await bufPromise).bytes());
|
|
|
|
const expected = [
|
|
|
|
"POST /blah HTTP/1.1\r\n",
|
|
|
|
"content-length: 0\r\n",
|
|
|
|
"accept: */*\r\n",
|
|
|
|
"accept-language: *\r\n",
|
|
|
|
`user-agent: Deno/${Deno.version.deno}\r\n`,
|
|
|
|
"accept-encoding: gzip, br\r\n",
|
|
|
|
`host: ${addr}\r\n\r\n`,
|
|
|
|
].join("");
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
permissions: { net: true },
|
|
|
|
},
|
|
|
|
async function fetchUserSetTransferEncoding() {
|
|
|
|
const addr = "127.0.0.1:4501";
|
|
|
|
const bufPromise = bufferServer(addr);
|
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Transfer-Encoding", "chunked"],
|
|
|
|
],
|
|
|
|
});
|
|
|
|
await response.arrayBuffer();
|
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
|
|
|
|
|
|
|
const actual = new TextDecoder().decode((await bufPromise).bytes());
|
|
|
|
const expected = [
|
|
|
|
"POST /blah HTTP/1.1\r\n",
|
|
|
|
"content-length: 0\r\n",
|
|
|
|
`host: ${addr}\r\n`,
|
|
|
|
"accept: */*\r\n",
|
|
|
|
"accept-language: *\r\n",
|
|
|
|
`user-agent: Deno/${Deno.version.deno}\r\n`,
|
|
|
|
"accept-encoding: gzip, br\r\n\r\n",
|
|
|
|
].join("");
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-12-09 10:48:06 -05:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-12-09 10:48:06 -05:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchWithNonAsciiRedirection() {
|
2020-12-09 10:48:06 -05:00
|
|
|
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();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-04 11:31:14 -05:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchWithManualRedirection() {
|
2020-03-04 11:31:14 -05:00
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-04 11:31:14 -05:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchWithErrorRedirection() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(
|
2021-04-20 08:47:22 -04:00
|
|
|
() =>
|
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function responseRedirect() {
|
2022-10-09 10:49:25 -04:00
|
|
|
const redir = Response.redirect("http://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"),
|
2022-10-09 10:49:25 -04:00
|
|
|
"http://example.com/newLocation",
|
2021-04-20 08:47:22 -04:00
|
|
|
);
|
2020-02-03 09:54:47 -05:00
|
|
|
assertEquals(redir.type, "default");
|
|
|
|
});
|
2020-05-25 16:20:09 -04:00
|
|
|
|
2022-07-20 06:30:41 -04:00
|
|
|
Deno.test(function responseRedirectTakeURLObjectAsParameter() {
|
|
|
|
const redir = Response.redirect(new URL("https://example.com/"));
|
|
|
|
assertEquals(
|
|
|
|
redir.headers.get("Location"),
|
|
|
|
"https://example.com/",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function responseWithoutBody() {
|
2020-09-08 05:46:15 -04:00
|
|
|
const response = new Response();
|
|
|
|
assertEquals(await response.arrayBuffer(), new ArrayBuffer(0));
|
2021-07-05 09:34:37 -04:00
|
|
|
const blob = await response.blob();
|
|
|
|
assertEquals(blob.size, 0);
|
|
|
|
assertEquals(await blob.arrayBuffer(), new ArrayBuffer(0));
|
2020-09-08 05:46:15 -04:00
|
|
|
assertEquals(await response.text(), "");
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-09-08 05:46:15 -04:00
|
|
|
await response.json();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { net: true } }, async function fetchBodyReadTwice() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const response = await fetch("http://localhost:4545/assets/fixture.json");
|
2020-05-25 16:20:09 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchBodyReaderAfterRead() {
|
2020-05-25 16:20:09 -04:00
|
|
|
const response = await fetch(
|
2022-09-19 10:32:21 -04:00
|
|
|
"http://localhost:4545/assets/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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchBodyReaderWithCancelAndNewReader() {
|
2020-05-25 16:20:09 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchBodyReaderWithReadCancelAndNewReader() {
|
2020-05-25 16:20:09 -04:00
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchResourceCloseAfterStreamCancel() {
|
2022-09-19 10:32:21 -04:00
|
|
|
const res = await fetch("http://localhost:4545/assets/fixture.json");
|
2020-05-30 01:02:41 -04:00
|
|
|
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-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2022-09-23 08:34:04 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchNullBodyStatus() {
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchResponseContentLength() {
|
2020-07-07 22:25:34 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function fetchResponseConstructorNullBody() {
|
2020-07-05 21:37:18 -04:00
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function fetchResponseConstructorInvalidStatus() {
|
2022-03-16 09:30:43 -04:00
|
|
|
const invalidStatus = [100, 600, 199, null, "", NaN];
|
2020-07-05 21:37:18 -04:00
|
|
|
|
|
|
|
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);
|
2022-03-16 09:30:43 -04:00
|
|
|
assert(
|
|
|
|
e.message.endsWith(
|
|
|
|
"is not equal to 101 and outside the range [200, 599].",
|
|
|
|
),
|
|
|
|
);
|
2020-06-03 09:43:11 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-05 21:37:18 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function fetchResponseEmptyConstructor() {
|
2020-07-05 21:37:18 -04:00
|
|
|
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-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-30 03:26:15 -04:00
|
|
|
{ permissions: { net: true, read: true } },
|
2020-12-19 17:13:48 -05:00
|
|
|
async function fetchCustomHttpClientParamCertificateSuccess(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2021-09-30 03:26:15 -04:00
|
|
|
const caCert = Deno.readTextFileSync("cli/tests/testdata/tls/RootCA.pem");
|
|
|
|
const client = Deno.createHttpClient({ caCerts: [caCert] });
|
2022-09-19 10:32:21 -04:00
|
|
|
const response = await fetch("https://localhost:5545/assets/fixture.json", {
|
2021-09-30 03:26:15 -04:00
|
|
|
client,
|
|
|
|
});
|
2020-12-19 17:13:48 -05:00
|
|
|
const json = await response.json();
|
|
|
|
assertEquals(json.name, "deno");
|
|
|
|
client.close();
|
|
|
|
},
|
|
|
|
);
|
2020-12-22 08:14:23 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-03-18 18:54:26 -04:00
|
|
|
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();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-12-22 08:14:23 -05:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { net: true },
|
2020-12-22 08:14:23 -05:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fetchPostBodyReadableStream() {
|
2022-02-16 10:51:32 -05:00
|
|
|
const addr = "127.0.0.1:4511";
|
2021-09-17 09:02:44 -04:00
|
|
|
const bufPromise = bufferServer(addr);
|
2020-12-22 08:14:23 -05:00
|
|
|
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");
|
|
|
|
|
2021-09-17 09:02:44 -04:00
|
|
|
const actual = new TextDecoder().decode((await bufPromise).bytes());
|
2020-12-22 08:14:23 -05:00
|
|
|
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",
|
2022-06-19 17:33:24 -04:00
|
|
|
"accept-language: *\r\n",
|
2020-12-22 08:14:23 -05:00
|
|
|
`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);
|
|
|
|
},
|
|
|
|
);
|
2021-05-26 17:44:42 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({}, function fetchWritableRespProps() {
|
2021-05-26 17:44:42 -04:00
|
|
|
const original = new Response("https://deno.land", {
|
|
|
|
status: 404,
|
|
|
|
headers: { "x-deno": "foo" },
|
|
|
|
});
|
|
|
|
const new_ = new Response("https://deno.land", original);
|
|
|
|
assertEquals(original.status, new_.status);
|
|
|
|
assertEquals(new_.headers.get("x-deno"), "foo");
|
|
|
|
});
|
2021-06-21 23:42:04 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-06-21 23:42:04 -04:00
|
|
|
async function fetchFilterOutCustomHostHeader(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2022-02-16 10:51:32 -05:00
|
|
|
const addr = "127.0.0.1:4511";
|
2022-09-06 13:38:37 -04:00
|
|
|
const [hostname, port] = addr.split(":");
|
|
|
|
const listener = Deno.listen({
|
|
|
|
hostname,
|
|
|
|
port: Number(port),
|
|
|
|
}) as Deno.Listener;
|
|
|
|
|
|
|
|
let httpConn: Deno.HttpConn;
|
|
|
|
listener.accept().then(async (conn: Deno.Conn) => {
|
|
|
|
httpConn = Deno.serveHttp(conn);
|
|
|
|
|
|
|
|
await httpConn.nextRequest()
|
|
|
|
.then(async (requestEvent: Deno.RequestEvent | null) => {
|
|
|
|
const hostHeader = requestEvent?.request.headers.get("Host");
|
|
|
|
const headersToReturn = hostHeader
|
|
|
|
? { "Host": hostHeader }
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
await requestEvent?.respondWith(
|
|
|
|
new Response("", {
|
|
|
|
status: 200,
|
|
|
|
headers: headersToReturn,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-21 23:42:04 -04:00
|
|
|
const response = await fetch(`http://${addr}/`, {
|
|
|
|
headers: { "Host": "example.com" },
|
|
|
|
});
|
|
|
|
await response.text();
|
|
|
|
listener.close();
|
2022-09-06 13:38:37 -04:00
|
|
|
httpConn!.close();
|
2021-06-21 23:42:04 -04:00
|
|
|
|
|
|
|
assertEquals(response.headers.get("Host"), addr);
|
|
|
|
},
|
|
|
|
);
|
2021-06-30 12:05:58 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-06-30 12:05:58 -04:00
|
|
|
async function fetchNoServerReadableStreamBody() {
|
|
|
|
const done = deferred();
|
|
|
|
const body = new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
controller.enqueue(new Uint8Array([1]));
|
|
|
|
setTimeout(() => {
|
|
|
|
controller.enqueue(new Uint8Array([2]));
|
|
|
|
done.resolve();
|
|
|
|
}, 1000);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const nonExistantHostname = "http://localhost:47582";
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2021-06-30 12:05:58 -04:00
|
|
|
await fetch(nonExistantHostname, { body, method: "POST" });
|
|
|
|
}, TypeError);
|
|
|
|
await done;
|
|
|
|
},
|
|
|
|
);
|
2021-07-05 06:38:12 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-07-05 06:38:12 -04:00
|
|
|
async function fetchHeadRespBody() {
|
|
|
|
const res = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "HEAD",
|
|
|
|
});
|
|
|
|
assertEquals(res.body, null);
|
|
|
|
},
|
|
|
|
);
|
2021-08-25 08:25:12 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-25 08:25:12 -04:00
|
|
|
async function fetchClientCertWrongPrivateKey(): Promise<void> {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2021-08-25 08:25:12 -04:00
|
|
|
const client = Deno.createHttpClient({
|
|
|
|
certChain: "bad data",
|
|
|
|
privateKey: await Deno.readTextFile(
|
|
|
|
"cli/tests/testdata/tls/localhost.key",
|
|
|
|
),
|
|
|
|
});
|
2022-09-19 10:32:21 -04:00
|
|
|
await fetch("https://localhost:5552/assets/fixture.json", {
|
2021-08-25 08:25:12 -04:00
|
|
|
client,
|
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-25 08:25:12 -04:00
|
|
|
async function fetchClientCertBadPrivateKey(): Promise<void> {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2021-08-25 08:25:12 -04:00
|
|
|
const client = Deno.createHttpClient({
|
|
|
|
certChain: await Deno.readTextFile(
|
|
|
|
"cli/tests/testdata/tls/localhost.crt",
|
|
|
|
),
|
|
|
|
privateKey: "bad data",
|
|
|
|
});
|
2022-09-19 10:32:21 -04:00
|
|
|
await fetch("https://localhost:5552/assets/fixture.json", {
|
2021-08-25 08:25:12 -04:00
|
|
|
client,
|
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-25 08:25:12 -04:00
|
|
|
async function fetchClientCertNotPrivateKey(): Promise<void> {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2021-08-25 08:25:12 -04:00
|
|
|
const client = Deno.createHttpClient({
|
|
|
|
certChain: await Deno.readTextFile(
|
|
|
|
"cli/tests/testdata/tls/localhost.crt",
|
|
|
|
),
|
|
|
|
privateKey: "",
|
|
|
|
});
|
2022-09-19 10:32:21 -04:00
|
|
|
await fetch("https://localhost:5552/assets/fixture.json", {
|
2021-08-25 08:25:12 -04:00
|
|
|
client,
|
|
|
|
});
|
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-25 08:25:12 -04:00
|
|
|
async function fetchCustomClientPrivateKey(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const data = "Hello World";
|
2021-09-30 03:26:15 -04:00
|
|
|
const caCert = await Deno.readTextFile("cli/tests/testdata/tls/RootCA.crt");
|
2021-08-25 08:25:12 -04:00
|
|
|
const client = Deno.createHttpClient({
|
|
|
|
certChain: await Deno.readTextFile(
|
|
|
|
"cli/tests/testdata/tls/localhost.crt",
|
|
|
|
),
|
|
|
|
privateKey: await Deno.readTextFile(
|
|
|
|
"cli/tests/testdata/tls/localhost.key",
|
|
|
|
),
|
2021-09-30 03:26:15 -04:00
|
|
|
caCerts: [caCert],
|
2021-08-25 08:25:12 -04:00
|
|
|
});
|
|
|
|
const response = await fetch("https://localhost:5552/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();
|
|
|
|
},
|
|
|
|
);
|
2021-09-12 19:19:38 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-09-12 19:19:38 -04:00
|
|
|
async function fetchAbortWhileUploadStreaming(): Promise<void> {
|
|
|
|
const abortController = new AbortController();
|
|
|
|
try {
|
|
|
|
await fetch(
|
|
|
|
"http://localhost:5552/echo_server",
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
body: new ReadableStream({
|
|
|
|
pull(controller) {
|
|
|
|
abortController.abort();
|
|
|
|
controller.enqueue(new Uint8Array([1, 2, 3, 4]));
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
signal: abortController.signal,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
fail("Fetch didn't reject.");
|
|
|
|
} catch (error) {
|
|
|
|
assert(error instanceof DOMException);
|
|
|
|
assertEquals(error.name, "AbortError");
|
2021-12-16 06:58:24 -05:00
|
|
|
assertEquals(error.message, "The signal has been aborted");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchAbortWhileUploadStreamingWithReason(): Promise<void> {
|
|
|
|
const abortController = new AbortController();
|
|
|
|
const abortReason = new Error();
|
|
|
|
try {
|
|
|
|
await fetch(
|
|
|
|
"http://localhost:5552/echo_server",
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
body: new ReadableStream({
|
|
|
|
pull(controller) {
|
|
|
|
abortController.abort(abortReason);
|
|
|
|
controller.enqueue(new Uint8Array([1, 2, 3, 4]));
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
signal: abortController.signal,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
fail("Fetch didn't reject.");
|
|
|
|
} catch (error) {
|
|
|
|
assertEquals(error, abortReason);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchAbortWhileUploadStreamingWithPrimitiveReason(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const abortController = new AbortController();
|
|
|
|
try {
|
|
|
|
await fetch(
|
|
|
|
"http://localhost:5552/echo_server",
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
body: new ReadableStream({
|
|
|
|
pull(controller) {
|
|
|
|
abortController.abort("Abort reason");
|
|
|
|
controller.enqueue(new Uint8Array([1, 2, 3, 4]));
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
signal: abortController.signal,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
fail("Fetch didn't reject.");
|
|
|
|
} catch (error) {
|
|
|
|
assertEquals(error, "Abort reason");
|
2021-09-12 19:19:38 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2021-09-29 12:42:06 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-29 12:42:06 -04:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchHeaderValueShouldNotPanic() {
|
|
|
|
for (let i = 0; i < 0x21; i++) {
|
|
|
|
if (i === 0x09 || i === 0x0A || i === 0x0D || i === 0x20) {
|
|
|
|
continue; // these header value will be normalized, will not cause an error.
|
|
|
|
}
|
|
|
|
// ensure there will be an error instead of panic.
|
|
|
|
await assertRejects(() =>
|
|
|
|
fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "HEAD",
|
|
|
|
headers: { "val": String.fromCharCode(i) },
|
|
|
|
}), TypeError);
|
|
|
|
}
|
|
|
|
await assertRejects(() =>
|
|
|
|
fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "HEAD",
|
|
|
|
headers: { "val": String.fromCharCode(127) },
|
|
|
|
}), TypeError);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-29 12:42:06 -04:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchHeaderNameShouldNotPanic() {
|
|
|
|
const validTokens =
|
|
|
|
"!#$%&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUWVXYZ^_`abcdefghijklmnopqrstuvwxyz|~"
|
|
|
|
.split("");
|
|
|
|
for (let i = 0; i <= 255; i++) {
|
|
|
|
const token = String.fromCharCode(i);
|
|
|
|
if (validTokens.includes(token)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// ensure there will be an error instead of panic.
|
|
|
|
await assertRejects(() =>
|
|
|
|
fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "HEAD",
|
|
|
|
headers: { [token]: "value" },
|
|
|
|
}), TypeError);
|
|
|
|
}
|
|
|
|
await assertRejects(() =>
|
|
|
|
fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "HEAD",
|
|
|
|
headers: { "": "value" },
|
|
|
|
}), TypeError);
|
|
|
|
},
|
|
|
|
);
|
2021-10-25 12:41:06 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-25 12:41:06 -04:00
|
|
|
{ permissions: { net: true, read: true } },
|
|
|
|
async function fetchSupportsHttp1Only() {
|
|
|
|
const caCert = await Deno.readTextFile("cli/tests/testdata/tls/RootCA.pem");
|
|
|
|
const client = Deno.createHttpClient({ caCerts: [caCert] });
|
|
|
|
const res = await fetch("https://localhost:5546/http_version", { client });
|
|
|
|
assert(res.ok);
|
|
|
|
assertEquals(await res.text(), "HTTP/1.1");
|
|
|
|
client.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-25 12:41:06 -04:00
|
|
|
{ permissions: { net: true, read: true } },
|
|
|
|
async function fetchSupportsHttp2() {
|
|
|
|
const caCert = await Deno.readTextFile("cli/tests/testdata/tls/RootCA.pem");
|
|
|
|
const client = Deno.createHttpClient({ caCerts: [caCert] });
|
|
|
|
const res = await fetch("https://localhost:5547/http_version", { client });
|
|
|
|
assert(res.ok);
|
|
|
|
assertEquals(await res.text(), "HTTP/2.0");
|
|
|
|
client.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-25 12:41:06 -04:00
|
|
|
{ permissions: { net: true, read: true } },
|
|
|
|
async function fetchPrefersHttp2() {
|
|
|
|
const caCert = await Deno.readTextFile("cli/tests/testdata/tls/RootCA.pem");
|
|
|
|
const client = Deno.createHttpClient({ caCerts: [caCert] });
|
|
|
|
const res = await fetch("https://localhost:5545/http_version", { client });
|
|
|
|
assert(res.ok);
|
|
|
|
assertEquals(await res.text(), "HTTP/2.0");
|
|
|
|
client.close();
|
|
|
|
},
|
|
|
|
);
|
2021-11-01 00:29:46 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: false } }, async function fetchFilePerm() {
|
2021-11-01 00:29:46 -04:00
|
|
|
await assertRejects(async () => {
|
2022-07-20 10:56:53 -04:00
|
|
|
await fetch(import.meta.resolve("../testdata/subdir/json_1.json"));
|
2021-11-01 00:29:46 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: false } },
|
|
|
|
async function fetchFilePermDoesNotExist() {
|
|
|
|
await assertRejects(async () => {
|
2022-07-20 10:56:53 -04:00
|
|
|
await fetch(import.meta.resolve("./bad.json"));
|
2021-11-23 11:45:18 -05:00
|
|
|
}, Deno.errors.PermissionDenied);
|
|
|
|
},
|
|
|
|
);
|
2021-11-01 00:29:46 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-11-01 00:29:46 -04:00
|
|
|
{ permissions: { read: true } },
|
|
|
|
async function fetchFileBadMethod() {
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await fetch(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("../testdata/subdir/json_1.json"),
|
2021-11-01 00:29:46 -04:00
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Fetching files only supports the GET method. Received POST.",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-11-01 00:29:46 -04:00
|
|
|
{ permissions: { read: true } },
|
|
|
|
async function fetchFileDoesNotExist() {
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
2022-07-20 10:56:53 -04:00
|
|
|
await fetch(import.meta.resolve("./bad.json"));
|
2021-11-01 00:29:46 -04:00
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-11-01 00:29:46 -04:00
|
|
|
{ permissions: { read: true } },
|
|
|
|
async function fetchFile() {
|
|
|
|
const res = await fetch(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("../testdata/subdir/json_1.json"),
|
2021-11-01 00:29:46 -04:00
|
|
|
);
|
|
|
|
assert(res.ok);
|
|
|
|
const fixture = await Deno.readTextFile(
|
|
|
|
"cli/tests/testdata/subdir/json_1.json",
|
|
|
|
);
|
|
|
|
assertEquals(await res.text(), fixture);
|
|
|
|
},
|
|
|
|
);
|
2021-11-09 06:10:40 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-11-09 06:10:40 -05:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchContentLengthPost() {
|
|
|
|
const response = await fetch("http://localhost:4545/content_length", {
|
|
|
|
method: "POST",
|
|
|
|
});
|
|
|
|
const length = await response.text();
|
|
|
|
assertEquals(length, 'Some("0")');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-11-09 06:10:40 -05:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchContentLengthPut() {
|
|
|
|
const response = await fetch("http://localhost:4545/content_length", {
|
|
|
|
method: "PUT",
|
|
|
|
});
|
|
|
|
const length = await response.text();
|
|
|
|
assertEquals(length, 'Some("0")');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-11-09 06:10:40 -05:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchContentLengthPatch() {
|
|
|
|
const response = await fetch("http://localhost:4545/content_length", {
|
|
|
|
method: "PATCH",
|
|
|
|
});
|
|
|
|
const length = await response.text();
|
|
|
|
assertEquals(length, "None");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-11-09 06:10:40 -05:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchContentLengthPostWithStringBody() {
|
|
|
|
const response = await fetch("http://localhost:4545/content_length", {
|
|
|
|
method: "POST",
|
|
|
|
body: "Hey!",
|
|
|
|
});
|
|
|
|
const length = await response.text();
|
|
|
|
assertEquals(length, 'Some("4")');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-11-09 06:10:40 -05:00
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchContentLengthPostWithBufferBody() {
|
|
|
|
const response = await fetch("http://localhost:4545/content_length", {
|
|
|
|
method: "POST",
|
|
|
|
body: new TextEncoder().encode("Hey!"),
|
|
|
|
});
|
|
|
|
const length = await response.text();
|
|
|
|
assertEquals(length, 'Some("4")');
|
|
|
|
},
|
|
|
|
);
|
2022-05-18 10:16:11 -04:00
|
|
|
|
|
|
|
Deno.test(async function staticResponseJson() {
|
|
|
|
const data = { hello: "world" };
|
|
|
|
const resp = Response.json(data);
|
|
|
|
assertEquals(resp.status, 200);
|
|
|
|
assertEquals(resp.headers.get("content-type"), "application/json");
|
|
|
|
const res = await resp.json();
|
|
|
|
assertEquals(res, data);
|
|
|
|
});
|
2022-09-26 14:27:50 -04:00
|
|
|
|
|
|
|
function invalidServer(addr: string, body: Uint8Array): Deno.Listener {
|
|
|
|
const [hostname, port] = addr.split(":");
|
|
|
|
const listener = Deno.listen({
|
|
|
|
hostname,
|
|
|
|
port: Number(port),
|
|
|
|
}) as Deno.Listener;
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
for await (const conn of listener) {
|
|
|
|
const p1 = conn.read(new Uint8Array(2 ** 14));
|
|
|
|
const p2 = conn.write(body);
|
|
|
|
|
|
|
|
await Promise.all([p1, p2]);
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchWithInvalidContentLengthAndTransferEncoding(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const addr = "127.0.0.1:4516";
|
|
|
|
const data = "a".repeat(10 << 10);
|
|
|
|
|
|
|
|
const body = new TextEncoder().encode(
|
|
|
|
`HTTP/1.1 200 OK\r\nContent-Length: ${
|
|
|
|
Math.round(data.length * 2)
|
|
|
|
}\r\nTransfer-Encoding: chunked\r\n\r\n${
|
|
|
|
data.length.toString(16)
|
|
|
|
}\r\n${data}\r\n0\r\n\r\n`,
|
|
|
|
);
|
|
|
|
|
|
|
|
// if transfer-encoding is sent, content-length is ignored
|
|
|
|
// even if it has an invalid value (content-length > totalLength)
|
|
|
|
const listener = invalidServer(addr, body);
|
|
|
|
const response = await fetch(`http://${addr}/`);
|
|
|
|
|
|
|
|
const res = await response.arrayBuffer();
|
|
|
|
const buf = new TextEncoder().encode(data);
|
|
|
|
assertEquals(res.byteLength, buf.byteLength);
|
|
|
|
assertEquals(new Uint8Array(res), buf);
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchWithInvalidContentLength(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const addr = "127.0.0.1:4517";
|
|
|
|
const data = "a".repeat(10 << 10);
|
|
|
|
|
|
|
|
const body = new TextEncoder().encode(
|
|
|
|
`HTTP/1.1 200 OK\r\nContent-Length: ${
|
|
|
|
Math.round(data.length / 2)
|
|
|
|
}\r\nContent-Length: ${data.length}\r\n\r\n${data}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
// It should fail if multiple content-length headers with different values are sent
|
|
|
|
const listener = invalidServer(addr, body);
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await fetch(`http://${addr}/`);
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"invalid content-length parsed",
|
|
|
|
);
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchWithInvalidContentLength(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const addr = "127.0.0.1:4518";
|
|
|
|
const data = "a".repeat(10 << 10);
|
|
|
|
|
|
|
|
const contentLength = data.length / 2;
|
|
|
|
const body = new TextEncoder().encode(
|
|
|
|
`HTTP/1.1 200 OK\r\nContent-Length: ${contentLength}\r\n\r\n${data}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
const listener = invalidServer(addr, body);
|
|
|
|
const response = await fetch(`http://${addr}/`);
|
|
|
|
|
|
|
|
// If content-length < totalLength, a maximum of content-length bytes
|
|
|
|
// should be returned.
|
|
|
|
const res = await response.arrayBuffer();
|
|
|
|
const buf = new TextEncoder().encode(data);
|
|
|
|
assertEquals(res.byteLength, contentLength);
|
|
|
|
assertEquals(new Uint8Array(res), buf.subarray(contentLength));
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchWithInvalidContentLength(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const addr = "127.0.0.1:4519";
|
|
|
|
const data = "a".repeat(10 << 10);
|
|
|
|
|
|
|
|
const contentLength = data.length * 2;
|
|
|
|
const body = new TextEncoder().encode(
|
|
|
|
`HTTP/1.1 200 OK\r\nContent-Length: ${contentLength}\r\n\r\n${data}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
const listener = invalidServer(addr, body);
|
|
|
|
const response = await fetch(`http://${addr}/`);
|
|
|
|
// If content-length > totalLength, a maximum of content-length bytes
|
|
|
|
// should be returned.
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await response.arrayBuffer();
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"end of file before message length reached",
|
|
|
|
);
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
2022-09-27 12:37:46 -04:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchBlobUrl(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const blob = new Blob(["ok"], { type: "text/plain" });
|
|
|
|
const url = URL.createObjectURL(blob);
|
|
|
|
const res = await fetch(url);
|
|
|
|
assert(res.url.startsWith("blob:http://js-unit-tests/"));
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertEquals(res.headers.get("content-length"), "2");
|
|
|
|
assertEquals(res.headers.get("content-type"), "text/plain");
|
|
|
|
assertEquals(await res.text(), "ok");
|
|
|
|
},
|
|
|
|
);
|
2022-10-04 09:48:50 -04:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchResponseStreamIsLockedWhileReading() {
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
body: new Uint8Array(5000),
|
|
|
|
method: "POST",
|
|
|
|
});
|
|
|
|
|
|
|
|
assertEquals(response.body!.locked, false);
|
|
|
|
const promise = response.arrayBuffer();
|
|
|
|
assertEquals(response.body!.locked, true);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|