2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-08-20 15:14:37 -04:00
|
|
|
|
|
|
|
// deno-lint-ignore-file no-console
|
|
|
|
|
2019-09-07 12:20:30 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects,
|
2024-08-08 11:47:15 -04:00
|
|
|
assertStringIncludes,
|
2023-12-01 10:56:10 -05:00
|
|
|
assertThrows,
|
2022-12-19 06:49:00 -05:00
|
|
|
delay,
|
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";
|
2024-07-25 01:30:28 -04:00
|
|
|
import { Buffer } from "@std/io/buffer";
|
2018-08-30 13:49:24 -04:00
|
|
|
|
2023-07-31 09:29:17 -04:00
|
|
|
const listenPort = 4506;
|
2023-05-22 15:35:59 -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(
|
2023-03-22 10:19:11 -04:00
|
|
|
// TODO(bartlomieju): reenable this test
|
|
|
|
// https://github.com/denoland/deno/issues/18350
|
|
|
|
{ ignore: Deno.build.os === "windows", permissions: { net: true } },
|
|
|
|
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,
|
2024-07-17 19:37:31 -04:00
|
|
|
"client error (Connect)",
|
2020-06-24 18:57:08 -04:00
|
|
|
);
|
2023-03-22 10:19:11 -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,
|
2024-07-17 19:37:31 -04:00
|
|
|
"client error (Connect)",
|
2020-12-26 08:06:00 -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 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
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-12-23 11:39:14 -05:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchMalformedUriError() {
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
const url = new URL("http://{{google/");
|
|
|
|
await fetch(url);
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
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");
|
2024-09-10 14:12:24 -04:00
|
|
|
}, Deno.errors.NotCapable);
|
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);
|
2024-05-22 20:27:58 -04:00
|
|
|
const u8a = await response.bytes();
|
|
|
|
const u8a1 = await response1.bytes();
|
2020-02-19 15:36:18 -05:00
|
|
|
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() {
|
2023-12-01 10:56:10 -05:00
|
|
|
const data = "const a = 1 🦕";
|
2019-01-03 06:41:20 -05:00
|
|
|
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");
|
2023-12-01 10:56:10 -05:00
|
|
|
form.append(
|
|
|
|
"file",
|
|
|
|
new Blob([new TextEncoder().encode("deno")]),
|
|
|
|
"file name",
|
|
|
|
);
|
|
|
|
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, "file name");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchInitFormDataFileFilenameBody() {
|
|
|
|
const form = new FormData();
|
|
|
|
form.append("field", "value");
|
|
|
|
form.append(
|
|
|
|
"file",
|
|
|
|
new File([new Blob([new TextEncoder().encode("deno")])], "file name"),
|
|
|
|
);
|
2020-05-28 09:02:00 -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 = resultForm.get("file");
|
|
|
|
assert(file instanceof File);
|
2023-12-01 10:56:10 -05:00
|
|
|
assertEquals(file.name, "file name");
|
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() {
|
2023-05-22 15:35:59 -04:00
|
|
|
const addr = `127.0.0.1:${listenPort}`;
|
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
|
|
|
});
|
2024-05-22 20:27:58 -04:00
|
|
|
await response.body?.cancel();
|
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`,
|
2024-07-17 19:37:31 -04:00
|
|
|
"accept-encoding: gzip,br\r\n",
|
2022-06-19 17:33:24 -04:00
|
|
|
`host: ${addr}\r\n\r\n`,
|
|
|
|
].join("");
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
permissions: { net: true },
|
|
|
|
},
|
|
|
|
async function fetchRequestAcceptHeaders() {
|
2023-05-22 15:35:59 -04:00
|
|
|
const addr = `127.0.0.1:${listenPort}`;
|
2022-06-19 17:33:24 -04:00
|
|
|
const bufPromise = bufferServer(addr);
|
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Accept", "text/html"],
|
|
|
|
["Accept-Language", "en-US"],
|
|
|
|
],
|
|
|
|
});
|
2024-05-22 20:27:58 -04:00
|
|
|
await response.body?.cancel();
|
2022-06-19 17:33:24 -04:00
|
|
|
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`,
|
2024-07-17 19:37:31 -04:00
|
|
|
"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() {
|
2023-05-22 15:35:59 -04:00
|
|
|
const addr = `127.0.0.1:${listenPort}`;
|
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
|
|
|
});
|
2024-05-22 20:27:58 -04:00
|
|
|
await response.body?.cancel();
|
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",
|
2024-07-17 19:37:31 -04:00
|
|
|
`content-length: ${body.length}\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`,
|
2024-07-17 19:37:31 -04:00
|
|
|
"accept-encoding: gzip,br\r\n",
|
2020-03-10 11:38:02 -04:00
|
|
|
`host: ${addr}\r\n`,
|
2024-07-17 19:37:31 -04:00
|
|
|
`\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() {
|
2023-05-22 15:35:59 -04:00
|
|
|
const addr = `127.0.0.1:${listenPort}`;
|
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
|
|
|
});
|
2024-05-22 20:27:58 -04:00
|
|
|
await response.body?.cancel();
|
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",
|
2024-07-17 19:37:31 -04:00
|
|
|
`content-length: ${body.byteLength}\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`,
|
2024-07-17 19:37:31 -04:00
|
|
|
"accept-encoding: gzip,br\r\n",
|
2020-03-10 11:38:02 -04:00
|
|
|
`host: ${addr}\r\n`,
|
2024-07-17 19:37:31 -04:00
|
|
|
`\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() {
|
2023-05-22 15:35:59 -04:00
|
|
|
const addr = `127.0.0.1:${listenPort}`;
|
2022-08-23 06:43:04 -04:00
|
|
|
const bufPromise = bufferServer(addr);
|
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Content-Length", "10"],
|
|
|
|
],
|
|
|
|
});
|
2024-05-22 20:27:58 -04:00
|
|
|
await response.body?.cancel();
|
2022-08-23 06:43:04 -04:00
|
|
|
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`,
|
2024-07-17 19:37:31 -04:00
|
|
|
"accept-encoding: gzip,br\r\n",
|
2022-08-23 06:43:04 -04:00
|
|
|
`host: ${addr}\r\n\r\n`,
|
|
|
|
].join("");
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
permissions: { net: true },
|
|
|
|
},
|
|
|
|
async function fetchUserSetTransferEncoding() {
|
2023-05-22 15:35:59 -04:00
|
|
|
const addr = `127.0.0.1:${listenPort}`;
|
2022-08-23 06:43:04 -04:00
|
|
|
const bufPromise = bufferServer(addr);
|
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Transfer-Encoding", "chunked"],
|
|
|
|
],
|
|
|
|
});
|
2024-05-22 20:27:58 -04:00
|
|
|
await response.body?.cancel();
|
2022-08-23 06:43:04 -04:00
|
|
|
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`,
|
2024-07-17 19:37:31 -04:00
|
|
|
"accept-encoding: gzip,br\r\n\r\n",
|
2022-08-23 06:43:04 -04:00
|
|
|
].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();
|
2024-05-22 20:27:58 -04:00
|
|
|
assertEquals(await response.bytes(), new Uint8Array(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(
|
2024-09-04 03:05:29 -04:00
|
|
|
"is not equal to 101 and outside the range [200, 599]",
|
2022-03-16 09:30:43 -04:00
|
|
|
),
|
|
|
|
);
|
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
|
|
|
|
> {
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
const caCert = Deno.readTextFileSync("tests/testdata/tls/RootCA.pem");
|
2021-09-30 03:26:15 -04:00
|
|
|
const client = Deno.createHttpClient({ caCerts: [caCert] });
|
2024-09-24 10:04:52 -04:00
|
|
|
assert(client instanceof Deno.HttpClient);
|
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
|
|
|
|
2023-12-31 07:45:12 -05:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true, read: true } },
|
|
|
|
function createHttpClientAcceptPoolIdleTimeout() {
|
|
|
|
const client = Deno.createHttpClient({
|
|
|
|
poolIdleTimeout: 1000,
|
|
|
|
});
|
|
|
|
client.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
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() {
|
2023-05-22 15:35:59 -04:00
|
|
|
const addr = `127.0.0.1:${listenPort}`;
|
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,
|
|
|
|
});
|
2024-05-22 20:27:58 -04:00
|
|
|
await response.body?.cancel();
|
2020-12-22 08:14:23 -05: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-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`,
|
2024-07-17 19:37:31 -04:00
|
|
|
"accept-encoding: gzip,br\r\n",
|
2020-12-22 08:14:23 -05:00
|
|
|
`host: ${addr}\r\n`,
|
2021-01-10 14:54:29 -05:00
|
|
|
`transfer-encoding: chunked\r\n\r\n`,
|
2023-12-01 10:56:10 -05:00
|
|
|
"B\r\n",
|
|
|
|
"hello world\r\n",
|
2021-01-10 14:54:29 -05:00
|
|
|
"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 } },
|
2024-04-18 13:21:25 -04:00
|
|
|
async function fetchFilterOutCustomHostHeader() {
|
2023-05-22 15:35:59 -04:00
|
|
|
const addr = `127.0.0.1:${listenPort}`;
|
2024-04-18 13:21:25 -04:00
|
|
|
const server = Deno.serve({ port: listenPort }, (req) => {
|
|
|
|
return new Response(`Host header was ${req.headers.get("Host")}`);
|
2022-09-06 13:38:37 -04:00
|
|
|
});
|
2021-06-21 23:42:04 -04:00
|
|
|
const response = await fetch(`http://${addr}/`, {
|
|
|
|
headers: { "Host": "example.com" },
|
|
|
|
});
|
2024-04-18 13:21:25 -04:00
|
|
|
assertEquals(await response.text(), `Host header was ${addr}`);
|
|
|
|
await server.shutdown();
|
2021-06-21 23:42:04 -04:00
|
|
|
},
|
|
|
|
);
|
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() {
|
2023-12-01 10:56:10 -05:00
|
|
|
const completed = Promise.withResolvers<void>();
|
|
|
|
const failed = Promise.withResolvers<void>();
|
2021-06-30 12:05:58 -04:00
|
|
|
const body = new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
controller.enqueue(new Uint8Array([1]));
|
2023-12-01 10:56:10 -05:00
|
|
|
setTimeout(async () => {
|
|
|
|
// This is technically a race. If the fetch has failed by this point, the enqueue will
|
|
|
|
// throw. If not, it will succeed. Windows appears to take a while to time out the fetch,
|
|
|
|
// so we will just wait for that here before we attempt to enqueue so it's consistent
|
|
|
|
// across platforms.
|
|
|
|
await failed.promise;
|
|
|
|
assertThrows(() => controller.enqueue(new Uint8Array([2])));
|
|
|
|
completed.resolve();
|
2021-06-30 12:05:58 -04:00
|
|
|
}, 1000);
|
|
|
|
},
|
|
|
|
});
|
2023-06-26 09:10:27 -04:00
|
|
|
const nonExistentHostname = "http://localhost:47582";
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2023-06-26 09:10:27 -04:00
|
|
|
await fetch(nonExistentHostname, { body, method: "POST" });
|
2021-06-30 12:05:58 -04:00
|
|
|
}, TypeError);
|
2023-12-01 10:56:10 -05:00
|
|
|
failed.resolve();
|
|
|
|
await completed.promise;
|
2021-06-30 12:05:58 -04:00
|
|
|
},
|
|
|
|
);
|
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({
|
2024-02-18 09:26:16 -05:00
|
|
|
cert: "bad data",
|
|
|
|
key: await Deno.readTextFile(
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
"tests/testdata/tls/localhost.key",
|
2021-08-25 08:25:12 -04:00
|
|
|
),
|
|
|
|
});
|
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({
|
2024-02-18 09:26:16 -05:00
|
|
|
cert: await Deno.readTextFile(
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
"tests/testdata/tls/localhost.crt",
|
2021-08-25 08:25:12 -04:00
|
|
|
),
|
2024-02-18 09:26:16 -05:00
|
|
|
key: "bad data",
|
2021-08-25 08:25:12 -04:00
|
|
|
});
|
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({
|
2024-02-18 09:26:16 -05:00
|
|
|
cert: await Deno.readTextFile(
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
"tests/testdata/tls/localhost.crt",
|
2021-08-25 08:25:12 -04:00
|
|
|
),
|
2024-02-18 09:26:16 -05:00
|
|
|
key: "",
|
2021-08-25 08:25:12 -04:00
|
|
|
});
|
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(
|
2024-09-11 21:30:12 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-25 08:25:12 -04:00
|
|
|
async function fetchCustomClientPrivateKey(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const data = "Hello World";
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
const caCert = await Deno.readTextFile("tests/testdata/tls/RootCA.crt");
|
2021-08-25 08:25:12 -04:00
|
|
|
const client = Deno.createHttpClient({
|
2024-02-18 09:26:16 -05:00
|
|
|
cert: await Deno.readTextFile(
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
"tests/testdata/tls/localhost.crt",
|
2021-08-25 08:25:12 -04:00
|
|
|
),
|
2024-02-18 09:26:16 -05:00
|
|
|
key: await Deno.readTextFile(
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
"tests/testdata/tls/localhost.key",
|
2021-08-25 08:25:12 -04:00
|
|
|
),
|
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
|
|
|
|
2023-05-16 20:19:23 -04:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true, read: true } },
|
|
|
|
async function fetchSupportsHttpsOverIpAddress() {
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
const caCert = await Deno.readTextFile("tests/testdata/tls/RootCA.pem");
|
2023-05-16 20:19:23 -04:00
|
|
|
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 fetchSupportsHttp1Only() {
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
const caCert = await Deno.readTextFile("tests/testdata/tls/RootCA.pem");
|
2021-10-25 12:41:06 -04:00
|
|
|
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() {
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
const caCert = await Deno.readTextFile("tests/testdata/tls/RootCA.pem");
|
2021-10-25 12:41:06 -04:00
|
|
|
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();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-05-20 21:43:54 -04:00
|
|
|
Deno.test(
|
2023-05-21 15:52:45 -04:00
|
|
|
{ permissions: { net: true, read: true } },
|
2023-05-20 21:43:54 -04:00
|
|
|
async function fetchForceHttp1OnHttp2Server() {
|
|
|
|
const client = Deno.createHttpClient({ http2: false, http1: true });
|
|
|
|
await assertRejects(
|
|
|
|
() => fetch("http://localhost:5549/http_version", { client }),
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
client.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
2023-05-21 15:52:45 -04:00
|
|
|
{ permissions: { net: true, read: true } },
|
2023-05-20 21:43:54 -04:00
|
|
|
async function fetchForceHttp2OnHttp1Server() {
|
|
|
|
const client = Deno.createHttpClient({ http2: true, http1: false });
|
|
|
|
await assertRejects(
|
|
|
|
() => fetch("http://localhost:5548/http_version", { client }),
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
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() {
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
const caCert = await Deno.readTextFile("tests/testdata/tls/RootCA.pem");
|
2021-10-25 12:41:06 -04:00
|
|
|
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
|
|
|
|
2023-07-28 03:01:06 -04:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true, read: true } },
|
|
|
|
async function createHttpClientAllowHost() {
|
|
|
|
const client = Deno.createHttpClient({
|
|
|
|
allowHost: true,
|
|
|
|
});
|
|
|
|
const res = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
headers: {
|
|
|
|
"host": "example.com",
|
|
|
|
},
|
|
|
|
client,
|
|
|
|
});
|
|
|
|
assert(res.ok);
|
|
|
|
assertEquals(res.headers.get("host"), "example.com");
|
|
|
|
await res.body?.cancel();
|
|
|
|
client.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-12-06 02:52:59 -05:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function createHttpClientExplicitResourceManagement() {
|
|
|
|
using client = Deno.createHttpClient({});
|
|
|
|
const response = await fetch("http://localhost:4545/assets/fixture.json", {
|
|
|
|
client,
|
|
|
|
});
|
|
|
|
const json = await response.json();
|
|
|
|
assertEquals(json.name, "deno");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function createHttpClientExplicitResourceManagementDoubleClose() {
|
|
|
|
using client = Deno.createHttpClient({});
|
|
|
|
const response = await fetch("http://localhost:4545/assets/fixture.json", {
|
|
|
|
client,
|
|
|
|
});
|
|
|
|
const json = await response.json();
|
|
|
|
assertEquals(json.name, "deno");
|
|
|
|
// Close the client even though we declared it with `using` to confirm that
|
|
|
|
// the cleanup done as per `Symbol.dispose` will not throw any errors.
|
|
|
|
client.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
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"));
|
2024-09-10 14:12:24 -04:00
|
|
|
}, Deno.errors.NotCapable);
|
2021-11-01 00:29:46 -04:00
|
|
|
});
|
|
|
|
|
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"));
|
2024-09-10 14:12:24 -04:00
|
|
|
}, Deno.errors.NotCapable);
|
2021-11-23 11:45:18 -05:00
|
|
|
},
|
|
|
|
);
|
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,
|
2024-09-04 03:05:29 -04:00
|
|
|
"Fetching files only supports the GET method: received POST",
|
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 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(
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
"tests/testdata/subdir/json_1.json",
|
2021-11-01 00:29:46 -04:00
|
|
|
);
|
|
|
|
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 } },
|
2023-03-22 10:19:11 -04:00
|
|
|
async function fetchWithInvalidContentLengthAndTransferEncoding(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2023-05-22 15:35:59 -04:00
|
|
|
const addr = `127.0.0.1:${listenPort}`;
|
2023-03-22 10:19:11 -04:00
|
|
|
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`,
|
|
|
|
);
|
2022-09-26 14:27:50 -04:00
|
|
|
|
2023-03-22 10:19:11 -04:00
|
|
|
// if transfer-encoding is sent, content-length is ignored
|
|
|
|
// even if it has an invalid value (content-length > totalLength)
|
|
|
|
const listener = invalidServer(addr, body);
|
2023-09-26 06:46:06 -04:00
|
|
|
const response = await fetch(`http://${addr}/`);
|
2022-09-26 14:27:50 -04:00
|
|
|
|
2024-05-22 20:27:58 -04:00
|
|
|
const res = await response.bytes();
|
2023-03-22 10:19:11 -04:00
|
|
|
const buf = new TextEncoder().encode(data);
|
2024-05-22 20:27:58 -04:00
|
|
|
assertEquals(res, buf);
|
2022-09-26 14:27:50 -04:00
|
|
|
|
2023-03-22 10:19:11 -04:00
|
|
|
listener.close();
|
|
|
|
},
|
2022-09-26 14:27:50 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
2023-03-22 10:19:11 -04:00
|
|
|
// TODO(bartlomieju): reenable this test
|
|
|
|
// https://github.com/denoland/deno/issues/18350
|
|
|
|
{ ignore: Deno.build.os === "windows", permissions: { net: true } },
|
2022-09-26 14:27:50 -04:00
|
|
|
async function fetchWithInvalidContentLength(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2023-05-22 15:35:59 -04:00
|
|
|
const addr = `127.0.0.1:${listenPort}`;
|
2022-09-26 14:27:50 -04:00
|
|
|
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 () => {
|
2023-09-26 06:46:06 -04:00
|
|
|
await fetch(`http://${addr}/`);
|
2022-09-26 14:27:50 -04:00
|
|
|
},
|
|
|
|
TypeError,
|
2024-07-17 19:37:31 -04:00
|
|
|
"client error",
|
2022-09-26 14:27:50 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
2023-09-16 01:48:31 -04:00
|
|
|
async function fetchWithInvalidContentLength2(): Promise<
|
2022-09-26 14:27:50 -04:00
|
|
|
void
|
|
|
|
> {
|
2023-05-22 15:35:59 -04:00
|
|
|
const addr = `127.0.0.1:${listenPort}`;
|
2022-09-26 14:27:50 -04:00
|
|
|
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);
|
2023-09-26 06:46:06 -04:00
|
|
|
const response = await fetch(`http://${addr}/`);
|
2022-09-26 14:27:50 -04:00
|
|
|
|
|
|
|
// If content-length < totalLength, a maximum of content-length bytes
|
|
|
|
// should be returned.
|
2024-05-22 20:27:58 -04:00
|
|
|
const res = await response.bytes();
|
2022-09-26 14:27:50 -04:00
|
|
|
const buf = new TextEncoder().encode(data);
|
|
|
|
assertEquals(res.byteLength, contentLength);
|
2024-05-22 20:27:58 -04:00
|
|
|
assertEquals(res, buf.subarray(contentLength));
|
2022-09-26 14:27:50 -04:00
|
|
|
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
2023-09-16 01:48:31 -04:00
|
|
|
async function fetchWithInvalidContentLength3(): Promise<
|
2022-09-26 14:27:50 -04:00
|
|
|
void
|
|
|
|
> {
|
2023-05-22 15:35:59 -04:00
|
|
|
const addr = `127.0.0.1:${listenPort}`;
|
2022-09-26 14:27:50 -04:00
|
|
|
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);
|
2023-09-26 06:46:06 -04:00
|
|
|
const response = await fetch(`http://${addr}/`);
|
2022-09-26 14:27:50 -04:00
|
|
|
// If content-length > totalLength, a maximum of content-length bytes
|
|
|
|
// should be returned.
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await response.arrayBuffer();
|
|
|
|
},
|
|
|
|
Error,
|
2024-07-17 19:37:31 -04:00
|
|
|
"body",
|
2022-09-26 14:27:50 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
2022-09-27 12:37:46 -04:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
2022-12-06 03:39:04 -05:00
|
|
|
async function fetchBlobUrl(): Promise<void> {
|
2022-09-27 12:37:46 -04:00
|
|
|
const blob = new Blob(["ok"], { type: "text/plain" });
|
|
|
|
const url = URL.createObjectURL(blob);
|
2023-12-01 10:56:10 -05:00
|
|
|
assert(url.startsWith("blob:"), `URL was ${url}`);
|
2022-09-27 12:37:46 -04:00
|
|
|
const res = await fetch(url);
|
2023-12-01 10:56:10 -05:00
|
|
|
assertEquals(res.url, url);
|
2022-09-27 12:37:46 -04:00
|
|
|
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;
|
|
|
|
},
|
|
|
|
);
|
2022-12-06 03:39:04 -05:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchConstructorClones() {
|
|
|
|
const req = new Request("https://example.com", {
|
|
|
|
method: "POST",
|
|
|
|
body: "foo",
|
|
|
|
});
|
|
|
|
assertEquals(await req.text(), "foo");
|
|
|
|
await assertRejects(() => req.text());
|
|
|
|
|
|
|
|
const req2 = new Request(req, { method: "PUT", body: "bar" }); // should not have any impact on req
|
|
|
|
await assertRejects(() => req.text());
|
|
|
|
assertEquals(await req2.text(), "bar");
|
|
|
|
|
|
|
|
assertEquals(req.method, "POST");
|
|
|
|
assertEquals(req2.method, "PUT");
|
|
|
|
|
|
|
|
assertEquals(req.headers.get("x-foo"), null);
|
|
|
|
assertEquals(req2.headers.get("x-foo"), null);
|
|
|
|
req2.headers.set("x-foo", "bar"); // should not have any impact on req
|
|
|
|
assertEquals(req.headers.get("x-foo"), null);
|
|
|
|
assertEquals(req2.headers.get("x-foo"), "bar");
|
|
|
|
},
|
|
|
|
);
|
2022-12-19 06:49:00 -05:00
|
|
|
|
|
|
|
Deno.test(
|
2023-03-22 10:19:11 -04:00
|
|
|
// TODO(bartlomieju): reenable this test
|
|
|
|
// https://github.com/denoland/deno/issues/18350
|
|
|
|
{ ignore: Deno.build.os === "windows", permissions: { net: true } },
|
2022-12-19 06:49:00 -05:00
|
|
|
async function fetchRequestBodyErrorCatchable() {
|
2023-07-31 09:29:17 -04:00
|
|
|
const listener = Deno.listen({ hostname: "127.0.0.1", port: listenPort });
|
2022-12-19 06:49:00 -05:00
|
|
|
const server = (async () => {
|
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
2022-12-19 09:18:14 -05:00
|
|
|
const buf = new Uint8Array(256);
|
2022-12-19 06:49:00 -05:00
|
|
|
const n = await conn.read(buf);
|
2022-12-19 09:18:14 -05:00
|
|
|
const data = new TextDecoder().decode(buf.subarray(0, n!)); // this is the request headers + first body chunk
|
|
|
|
assert(data.startsWith("POST / HTTP/1.1\r\n"));
|
|
|
|
assert(data.endsWith("1\r\na\r\n"));
|
2022-12-19 06:49:00 -05:00
|
|
|
const n2 = await conn.read(buf);
|
|
|
|
assertEquals(n2, 6); // this is the second body chunk
|
|
|
|
const n3 = await conn.read(buf);
|
|
|
|
assertEquals(n3, null); // the connection now abruptly closes because the client has errored
|
|
|
|
conn.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
const stream = new ReadableStream({
|
|
|
|
async start(controller) {
|
|
|
|
controller.enqueue(new TextEncoder().encode("a"));
|
|
|
|
await delay(1000);
|
|
|
|
controller.enqueue(new TextEncoder().encode("b"));
|
|
|
|
await delay(1000);
|
|
|
|
controller.error(new Error("foo"));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-08-08 02:18:33 -04:00
|
|
|
const url = `http://localhost:${listenPort}/`;
|
2024-08-08 11:47:15 -04:00
|
|
|
const err = await assertRejects(() =>
|
|
|
|
fetch(url, {
|
|
|
|
body: stream,
|
|
|
|
method: "POST",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(err instanceof TypeError, `err was ${err}`);
|
|
|
|
|
|
|
|
assertStringIncludes(
|
|
|
|
err.message,
|
|
|
|
"error sending request from 127.0.0.1:",
|
|
|
|
`err.message was ${err.message}`,
|
|
|
|
);
|
|
|
|
assertStringIncludes(
|
|
|
|
err.message,
|
|
|
|
` for http://localhost:${listenPort}/ (127.0.0.1:${listenPort}): client error (SendRequest): error from user's Body stream`,
|
|
|
|
`err.message was ${err.message}`,
|
2022-12-19 06:49:00 -05:00
|
|
|
);
|
|
|
|
|
2023-12-01 10:56:10 -05:00
|
|
|
assert(err.cause, `err.cause was null ${err}`);
|
|
|
|
assert(
|
|
|
|
err.cause instanceof Error,
|
|
|
|
`err.cause was not an Error ${err.cause}`,
|
|
|
|
);
|
2022-12-19 06:49:00 -05:00
|
|
|
assertEquals(err.cause.message, "foo");
|
|
|
|
|
|
|
|
await server;
|
|
|
|
},
|
|
|
|
);
|
2023-04-28 08:26:21 -04:00
|
|
|
|
2023-08-16 08:02:15 -04:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function fetchRequestBodyEmptyStream() {
|
|
|
|
const body = new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
controller.enqueue(new Uint8Array([]));
|
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
const controller = new AbortController();
|
|
|
|
const promise = fetch("http://localhost:4545/echo_server", {
|
|
|
|
body,
|
|
|
|
method: "POST",
|
|
|
|
signal: controller.signal,
|
|
|
|
});
|
2023-12-01 10:56:10 -05:00
|
|
|
try {
|
|
|
|
controller.abort();
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
fail("abort should not throw");
|
|
|
|
}
|
2023-08-16 08:02:15 -04:00
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
DOMException,
|
|
|
|
"The signal has been aborted",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-04-28 08:26:21 -04:00
|
|
|
Deno.test("Request with subarray TypedArray body", async () => {
|
|
|
|
const body = new Uint8Array([1, 2, 3, 4, 5]).subarray(1);
|
|
|
|
const req = new Request("https://example.com", { method: "POST", body });
|
2024-05-22 20:27:58 -04:00
|
|
|
const actual = await req.bytes();
|
2023-04-28 08:26:21 -04:00
|
|
|
const expected = new Uint8Array([2, 3, 4, 5]);
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Response with subarray TypedArray body", async () => {
|
|
|
|
const body = new Uint8Array([1, 2, 3, 4, 5]).subarray(1);
|
|
|
|
const req = new Response(body);
|
2024-05-22 20:27:58 -04:00
|
|
|
const actual = await req.bytes();
|
2023-04-28 08:26:21 -04:00
|
|
|
const expected = new Uint8Array([2, 3, 4, 5]);
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
});
|
2024-07-24 17:22:43 -04:00
|
|
|
|
|
|
|
// Regression test for https://github.com/denoland/deno/issues/24697
|
|
|
|
Deno.test("URL authority is used as 'Authorization' header", async () => {
|
|
|
|
const deferred = Promise.withResolvers<string | null | undefined>();
|
|
|
|
const ac = new AbortController();
|
|
|
|
|
|
|
|
const server = Deno.serve({ port: 4502, signal: ac.signal }, (req) => {
|
|
|
|
deferred.resolve(req.headers.get("authorization"));
|
|
|
|
return new Response("Hello world");
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = await fetch("http://deno:land@localhost:4502");
|
|
|
|
await res.text();
|
|
|
|
const authHeader = await deferred.promise;
|
|
|
|
ac.abort();
|
|
|
|
await server.finished;
|
|
|
|
assertEquals(authHeader, "Basic ZGVubzpsYW5k");
|
|
|
|
});
|
2024-08-08 02:18:33 -04:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
2024-08-08 11:47:15 -04:00
|
|
|
async function errorMessageIncludesUrlAndDetailsWithNoTcpInfo() {
|
2024-08-08 02:18:33 -04:00
|
|
|
await assertRejects(
|
|
|
|
() => fetch("http://example.invalid"),
|
|
|
|
TypeError,
|
|
|
|
"error sending request for url (http://example.invalid/): client error (Connect): dns error: ",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2024-08-08 11:47:15 -04:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function errorMessageIncludesUrlAndDetailsWithTcpInfo() {
|
|
|
|
const listener = Deno.listen({ port: listenPort });
|
|
|
|
const server = (async () => {
|
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
|
|
|
// Immediately close the connection to simulate a connection error
|
|
|
|
conn.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
const url = `http://localhost:${listenPort}`;
|
|
|
|
const err = await assertRejects(() => fetch(url));
|
|
|
|
|
|
|
|
assert(err instanceof TypeError, `${err}`);
|
|
|
|
assertStringIncludes(
|
|
|
|
err.message,
|
|
|
|
"error sending request from 127.0.0.1:",
|
|
|
|
`${err.message}`,
|
|
|
|
);
|
|
|
|
assertStringIncludes(
|
|
|
|
err.message,
|
|
|
|
` for http://localhost:${listenPort}/ (127.0.0.1:${listenPort}): client error (SendRequest): `,
|
|
|
|
`${err.message}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
await server;
|
|
|
|
},
|
|
|
|
);
|