2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-09-07 12:20:30 -04:00
|
|
|
import {
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest,
|
2019-09-07 12:20:30 -04:00
|
|
|
assert,
|
|
|
|
assertEquals,
|
2019-09-11 17:34:22 -04:00
|
|
|
assertStrContains,
|
2020-03-04 11:31:14 -05:00
|
|
|
assertThrows,
|
2020-03-28 13:03:49 -04:00
|
|
|
fail,
|
2019-09-07 12:20:30 -04:00
|
|
|
} from "./test_util.ts";
|
2018-08-30 13:49:24 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchProtocolError(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2020-02-23 09:45:02 -05:00
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await fetch("file:///");
|
|
|
|
} catch (err_) {
|
|
|
|
err = err_;
|
|
|
|
}
|
|
|
|
assert(err instanceof TypeError);
|
|
|
|
assertStrContains(err.message, "not supported");
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchConnectionError(): Promise<void> {
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await fetch("http://localhost:4000");
|
|
|
|
} catch (err_) {
|
|
|
|
err = err_;
|
|
|
|
}
|
|
|
|
assert(err instanceof Deno.errors.Http);
|
|
|
|
assertStrContains(err.message, "error trying to connect");
|
2019-09-11 17:34:22 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-09-11 17:34:22 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchJsonSuccess(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2018-08-30 13:49:24 -04:00
|
|
|
const json = await response.json();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(json.name, "deno");
|
2018-08-30 13:49:24 -04:00
|
|
|
});
|
2018-09-05 02:29:02 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function fetchPerm(): Promise<void> {
|
2018-09-05 02:29:02 -04:00
|
|
|
let err;
|
|
|
|
try {
|
2019-10-31 22:33:27 -04:00
|
|
|
await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2018-09-05 02:29:02 -04:00
|
|
|
} catch (err_) {
|
|
|
|
err = err_;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-09-05 02:29:02 -04:00
|
|
|
});
|
2018-09-12 15:16:42 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchUrl(): Promise<void> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
|
|
|
assertEquals(response.url, "http://localhost:4545/cli/tests/fixture.json");
|
2020-04-10 09:51:17 -04:00
|
|
|
const _json = await response.json();
|
2019-12-14 07:49:30 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchURL(): Promise<void> {
|
2019-12-14 07:49:30 -05:00
|
|
|
const response = await fetch(
|
|
|
|
new URL("http://localhost:4545/cli/tests/fixture.json")
|
|
|
|
);
|
|
|
|
assertEquals(response.url, "http://localhost:4545/cli/tests/fixture.json");
|
2020-04-10 09:51:17 -04:00
|
|
|
const _json = await response.json();
|
2019-08-16 18:20:04 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchHeaders(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2018-09-12 15:16:42 -04:00
|
|
|
const headers = response.headers;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(headers.get("Content-Type"), "application/json");
|
2020-02-19 15:36:18 -05:00
|
|
|
assert(headers.get("Server")!.startsWith("SimpleHTTP"));
|
2020-04-10 09:51:17 -04:00
|
|
|
const _json = await response.json();
|
2018-09-12 15:16:42 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchBlob(): Promise<void> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2018-09-14 13:56:37 -04:00
|
|
|
const headers = response.headers;
|
|
|
|
const blob = await response.blob();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(blob.type, headers.get("Content-Type"));
|
|
|
|
assertEquals(blob.size, Number(headers.get("Content-Length")));
|
2018-09-14 13:56:37 -04:00
|
|
|
});
|
2018-09-30 10:31:50 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchBodyUsed(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2019-09-07 12:20:30 -04:00
|
|
|
assertEquals(response.bodyUsed, false);
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
// Assigning to read-only property throws in the strict mode.
|
2020-05-26 10:02:16 -04:00
|
|
|
// @ts-expect-error
|
2019-11-13 13:42:34 -05:00
|
|
|
response.bodyUsed = true;
|
|
|
|
});
|
2019-09-07 12:20:30 -04:00
|
|
|
await response.blob();
|
|
|
|
assertEquals(response.bodyUsed, true);
|
|
|
|
});
|
|
|
|
|
2020-04-10 09:51:17 -04:00
|
|
|
// TODO(ry) response.body shouldn't be iterable. Instead we should use
|
|
|
|
// response.body.getReader().
|
|
|
|
/*
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchAsyncIterator(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2019-06-22 10:22:27 -04:00
|
|
|
const headers = response.headers;
|
|
|
|
let total = 0;
|
|
|
|
for await (const chunk of response.body) {
|
|
|
|
total += chunk.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(total, Number(headers.get("Content-Length")));
|
2020-04-10 09:51:17 -04:00
|
|
|
const _json = await response.json();
|
2019-06-22 10:22:27 -04:00
|
|
|
});
|
2020-04-10 09:51:17 -04:00
|
|
|
*/
|
2019-06-22 10:22:27 -04:00
|
|
|
|
2020-05-25 12:55:16 -04:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchBodyReader(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
|
|
|
const headers = response.headers;
|
|
|
|
assert(response.body !== null);
|
|
|
|
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, Number(headers.get("Content-Length")));
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchBodyReaderBigBody(): Promise<void> {
|
|
|
|
const data = "a".repeat(10 << 10); // 10mb
|
|
|
|
const response = await fetch(
|
|
|
|
"http://localhost:4545/cli/tests/echo_server",
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
body: data,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
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-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function responseClone(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
2018-10-21 17:42:18 -04:00
|
|
|
const response1 = response.clone();
|
|
|
|
assert(response !== response1);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(response.status, response1.status);
|
|
|
|
assertEquals(response.statusText, response1.statusText);
|
2020-02-19 15:36:18 -05:00
|
|
|
const u8a = new Uint8Array(await response.arrayBuffer());
|
|
|
|
const u8a1 = new Uint8Array(await response1.arrayBuffer());
|
|
|
|
for (let i = 0; i < u8a.byteLength; i++) {
|
|
|
|
assertEquals(u8a[i], u8a1[i]);
|
2018-10-21 17:42:18 -04:00
|
|
|
}
|
|
|
|
});
|
2018-11-14 20:36:34 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchEmptyInvalid(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2018-12-21 04:47:09 -05:00
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await fetch("");
|
|
|
|
} catch (err_) {
|
|
|
|
err = err_;
|
|
|
|
}
|
2020-02-21 10:36:13 -05:00
|
|
|
assert(err instanceof URIError);
|
2018-12-21 04:47:09 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchMultipartFormDataSuccess(): Promise<void> {
|
|
|
|
const response = await fetch(
|
|
|
|
"http://localhost:4545/cli/tests/subdir/multipart_form_data.txt"
|
|
|
|
);
|
|
|
|
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"));
|
|
|
|
/* TODO(ry) Re-enable this test once we bring back the global File type.
|
2018-12-21 17:09:53 -05:00
|
|
|
const file = formData.get("field_2") as File;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(file.name, "file.js");
|
2018-12-23 11:44:08 -05:00
|
|
|
*/
|
2020-03-04 11:31:14 -05:00
|
|
|
// Currently we cannot read from file...
|
|
|
|
}
|
|
|
|
);
|
2018-12-21 17:09:53 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
2019-04-21 16:40:10 -04:00
|
|
|
async function fetchURLEncodedFormDataSuccess(): Promise<void> {
|
|
|
|
const response = await fetch(
|
2020-02-02 16:55:22 -05:00
|
|
|
"http://localhost:4545/cli/tests/subdir/form_urlencoded.txt"
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
|
|
|
const formData = await response.formData();
|
|
|
|
assert(formData.has("field_1"));
|
2020-02-19 15:36:18 -05:00
|
|
|
assertEquals(formData.get("field_1")!.toString(), "Hi");
|
2019-04-21 16:40:10 -04:00
|
|
|
assert(formData.has("field_2"));
|
2020-02-19 15:36:18 -05:00
|
|
|
assertEquals(formData.get("field_2")!.toString(), "<Deno>");
|
2019-04-21 16:40:10 -04:00
|
|
|
}
|
|
|
|
);
|
2018-12-21 17:09:53 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
|
|
|
async function fetchWithRedirection(): Promise<void> {
|
|
|
|
const response = await fetch("http://localhost:4546/"); // will redirect to http://localhost:4545/
|
|
|
|
assertEquals(response.status, 200);
|
|
|
|
assertEquals(response.statusText, "OK");
|
|
|
|
assertEquals(response.url, "http://localhost:4545/");
|
|
|
|
const body = await response.text();
|
|
|
|
assert(body.includes("<title>Directory listing for /</title>"));
|
|
|
|
}
|
|
|
|
);
|
2019-06-24 09:34:09 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
|
|
|
async function fetchWithRelativeRedirection(): Promise<void> {
|
|
|
|
const response = await fetch("http://localhost:4545/cli/tests"); // will redirect to /cli/tests/
|
|
|
|
assertEquals(response.status, 200);
|
|
|
|
assertEquals(response.statusText, "OK");
|
|
|
|
const body = await response.text();
|
|
|
|
assert(body.includes("<title>Directory listing for /cli/tests/</title>"));
|
|
|
|
}
|
|
|
|
);
|
2019-06-24 09:34:09 -04:00
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
// FIXME(bartlomieju):
|
|
|
|
// The feature below is not implemented, but the test should work after implementation
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
|
|
|
async function fetchWithInfRedirection(): Promise<void> {
|
|
|
|
const response = await fetch("http://localhost:4549/cli/tests"); // will redirect to the same place
|
|
|
|
assertEquals(response.status, 0); // network error
|
|
|
|
}
|
|
|
|
);
|
2019-06-24 09:34:09 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitStringBody(): Promise<void> {
|
|
|
|
const data = "Hello World";
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: data,
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
|
|
|
const text = await response.text();
|
|
|
|
assertEquals(text, data);
|
|
|
|
assert(response.headers.get("content-type")!.startsWith("text/plain"));
|
|
|
|
}
|
|
|
|
);
|
2019-01-03 06:41:20 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchRequestInitStringBody(): Promise<void> {
|
|
|
|
const data = "Hello World";
|
|
|
|
const req = new Request("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: data,
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
|
|
|
const response = await fetch(req);
|
|
|
|
const text = await response.text();
|
|
|
|
assertEquals(text, data);
|
|
|
|
}
|
|
|
|
);
|
2019-05-01 23:56:42 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitTypedArrayBody(): Promise<void> {
|
|
|
|
const data = "Hello World";
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: new TextEncoder().encode(data),
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
|
|
|
const text = await response.text();
|
|
|
|
assertEquals(text, data);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-05-25 09:26:36 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitArrayBufferBody(): Promise<void> {
|
|
|
|
const data = "Hello World";
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
|
|
|
body: new TextEncoder().encode(data).buffer,
|
|
|
|
});
|
|
|
|
const text = await response.text();
|
|
|
|
assertEquals(text, data);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitURLSearchParamsBody(): Promise<void> {
|
|
|
|
const data = "param1=value1¶m2=value2";
|
|
|
|
const params = new URLSearchParams(data);
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: params,
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
|
|
|
const text = await response.text();
|
|
|
|
assertEquals(text, data);
|
|
|
|
assert(
|
|
|
|
response.headers
|
|
|
|
.get("content-type")!
|
|
|
|
.startsWith("application/x-www-form-urlencoded")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2019-01-03 06:41:20 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchInitBlobBody(): Promise<
|
2019-04-21 16:40:10 -04:00
|
|
|
void
|
|
|
|
> {
|
2019-01-03 06:41:20 -05:00
|
|
|
const data = "const a = 1";
|
|
|
|
const blob = new Blob([data], {
|
2020-03-28 13:03:49 -04:00
|
|
|
type: "text/javascript",
|
2019-01-03 06:41:20 -05:00
|
|
|
});
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: blob,
|
2019-01-03 06:41:20 -05:00
|
|
|
});
|
|
|
|
const text = await response.text();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(text, data);
|
2020-02-19 15:36:18 -05:00
|
|
|
assert(response.headers.get("content-type")!.startsWith("text/javascript"));
|
2019-01-03 06:41:20 -05:00
|
|
|
});
|
|
|
|
|
2020-03-17 02:32:43 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchInitFormDataBody(): Promise<void> {
|
|
|
|
const form = new FormData();
|
|
|
|
form.append("field", "value");
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: form,
|
2020-03-17 02:32:43 -04:00
|
|
|
});
|
|
|
|
const resultForm = await response.formData();
|
|
|
|
assertEquals(form.get("field"), resultForm.get("field"));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function fetchUserAgent(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-09-11 07:31:00 -04:00
|
|
|
const data = "Hello World";
|
|
|
|
const response = await fetch("http://localhost:4545/echo_server", {
|
|
|
|
method: "POST",
|
2020-03-28 13:03:49 -04:00
|
|
|
body: new TextEncoder().encode(data),
|
2019-09-11 07:31:00 -04:00
|
|
|
});
|
|
|
|
assertEquals(response.headers.get("user-agent"), `Deno/${Deno.version.deno}`);
|
|
|
|
await response.text();
|
|
|
|
});
|
|
|
|
|
2019-01-17 16:58:06 -05:00
|
|
|
// TODO(ry) The following tests work but are flaky. There's a race condition
|
|
|
|
// somewhere. Here is what one of these flaky failures looks like:
|
|
|
|
//
|
2020-03-04 11:31:14 -05:00
|
|
|
// unitTest fetchPostBodyString_permW0N1E0R0
|
2019-03-06 20:48:46 -05:00
|
|
|
// assertEquals failed. actual = expected = POST /blah HTTP/1.1
|
2019-01-17 16:58:06 -05:00
|
|
|
// hello: World
|
|
|
|
// foo: Bar
|
|
|
|
// host: 127.0.0.1:4502
|
|
|
|
// content-length: 11
|
|
|
|
// hello world
|
|
|
|
// Error: actual: expected: POST /blah HTTP/1.1
|
|
|
|
// hello: World
|
|
|
|
// foo: Bar
|
|
|
|
// host: 127.0.0.1:4502
|
|
|
|
// content-length: 11
|
|
|
|
// hello world
|
2019-03-06 20:48:46 -05:00
|
|
|
// at Object.assertEquals (file:///C:/deno/js/testing/util.ts:29:11)
|
2019-01-17 16:58:06 -05:00
|
|
|
// at fetchPostBodyString (file
|
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
function bufferServer(addr: string): Deno.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;
|
2019-02-12 10:08:56 -05:00
|
|
|
const buf = new Deno.Buffer();
|
2020-03-10 11:38:02 -04:00
|
|
|
listener.accept().then(async (conn: Deno.Conn) => {
|
2018-11-14 21:19:38 -05:00
|
|
|
const p1 = buf.readFrom(conn);
|
|
|
|
const p2 = conn.write(
|
2018-11-14 20:36:34 -05:00
|
|
|
new TextEncoder().encode(
|
|
|
|
"HTTP/1.0 404 Not Found\r\nContent-Length: 2\r\n\r\nNF"
|
|
|
|
)
|
|
|
|
);
|
2018-11-14 21:19:38 -05:00
|
|
|
// Wait for both an EOF on the read side of the socket and for the write to
|
|
|
|
// complete before closing it. Due to keep-alive, the EOF won't be sent
|
|
|
|
// until the Connection close (HTTP/1.0) response, so readFrom() can't
|
|
|
|
// proceed write. Conversely, if readFrom() is async, waiting for the
|
|
|
|
// write() to complete is not a guarantee that we've read the incoming
|
|
|
|
// request.
|
|
|
|
await Promise.all([p1, p2]);
|
2018-11-14 20:36:34 -05:00
|
|
|
conn.close();
|
2018-11-14 21:19:38 -05:00
|
|
|
listener.close();
|
2018-11-14 20:36:34 -05:00
|
|
|
});
|
2018-11-14 21:19:38 -05:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
|
|
|
async function fetchRequest(): Promise<void> {
|
|
|
|
const addr = "127.0.0.1:4501";
|
|
|
|
const buf = bufferServer(addr);
|
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Hello", "World"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["Foo", "Bar"],
|
|
|
|
],
|
2020-03-10 11:38:02 -04:00
|
|
|
});
|
2020-05-24 12:04:57 -04:00
|
|
|
await response.arrayBuffer();
|
2020-03-10 11:38:02 -04:00
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
|
|
|
|
|
|
|
const actual = new TextDecoder().decode(buf.bytes());
|
|
|
|
const expected = [
|
|
|
|
"POST /blah HTTP/1.1\r\n",
|
|
|
|
"hello: World\r\n",
|
|
|
|
"foo: Bar\r\n",
|
2020-05-24 12:04:57 -04:00
|
|
|
"accept: */*\r\n",
|
|
|
|
`user-agent: Deno/${Deno.version.deno}\r\n`,
|
|
|
|
"accept-encoding: gzip, br\r\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
`host: ${addr}\r\n\r\n`,
|
2020-03-10 11:38:02 -04:00
|
|
|
].join("");
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
}
|
|
|
|
);
|
2018-11-14 21:19:38 -05:00
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
|
|
|
async function fetchPostBodyString(): Promise<void> {
|
|
|
|
const addr = "127.0.0.1:4502";
|
|
|
|
const buf = bufferServer(addr);
|
|
|
|
const body = "hello world";
|
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Hello", "World"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["Foo", "Bar"],
|
2020-03-10 11:38:02 -04:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
body,
|
2020-03-10 11:38:02 -04:00
|
|
|
});
|
2020-05-24 12:04:57 -04:00
|
|
|
await response.arrayBuffer();
|
2020-03-10 11:38:02 -04:00
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
|
|
|
|
|
|
|
const actual = new TextDecoder().decode(buf.bytes());
|
|
|
|
const expected = [
|
|
|
|
"POST /blah HTTP/1.1\r\n",
|
|
|
|
"hello: World\r\n",
|
|
|
|
"foo: Bar\r\n",
|
2020-05-24 12:04:57 -04:00
|
|
|
"content-type: text/plain;charset=UTF-8\r\n",
|
|
|
|
"accept: */*\r\n",
|
|
|
|
`user-agent: Deno/${Deno.version.deno}\r\n`,
|
|
|
|
"accept-encoding: gzip, br\r\n",
|
2020-03-10 11:38:02 -04:00
|
|
|
`host: ${addr}\r\n`,
|
|
|
|
`content-length: ${body.length}\r\n\r\n`,
|
2020-03-28 13:03:49 -04:00
|
|
|
body,
|
2020-03-10 11:38:02 -04:00
|
|
|
].join("");
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
}
|
|
|
|
);
|
2018-11-14 21:19:38 -05:00
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
|
|
|
async function fetchPostBodyTypedArray(): Promise<void> {
|
|
|
|
const addr = "127.0.0.1:4503";
|
|
|
|
const buf = bufferServer(addr);
|
|
|
|
const bodyStr = "hello world";
|
|
|
|
const body = new TextEncoder().encode(bodyStr);
|
|
|
|
const response = await fetch(`http://${addr}/blah`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: [
|
|
|
|
["Hello", "World"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["Foo", "Bar"],
|
2020-03-10 11:38:02 -04:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
body,
|
2020-03-10 11:38:02 -04:00
|
|
|
});
|
2020-05-24 12:04:57 -04:00
|
|
|
await response.arrayBuffer();
|
2020-03-10 11:38:02 -04:00
|
|
|
assertEquals(response.status, 404);
|
|
|
|
assertEquals(response.headers.get("Content-Length"), "2");
|
|
|
|
|
|
|
|
const actual = new TextDecoder().decode(buf.bytes());
|
|
|
|
const expected = [
|
|
|
|
"POST /blah HTTP/1.1\r\n",
|
|
|
|
"hello: World\r\n",
|
|
|
|
"foo: Bar\r\n",
|
2020-05-24 12:04:57 -04:00
|
|
|
"accept: */*\r\n",
|
|
|
|
`user-agent: Deno/${Deno.version.deno}\r\n`,
|
|
|
|
"accept-encoding: gzip, br\r\n",
|
2020-03-10 11:38:02 -04:00
|
|
|
`host: ${addr}\r\n`,
|
|
|
|
`content-length: ${body.byteLength}\r\n\r\n`,
|
2020-03-28 13:03:49 -04:00
|
|
|
bodyStr,
|
2020-03-10 11:38:02 -04:00
|
|
|
].join("");
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
}
|
|
|
|
);
|
2020-02-03 09:54:47 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
|
|
|
async function fetchWithManualRedirection(): Promise<void> {
|
|
|
|
const response = await fetch("http://localhost:4546/", {
|
2020-03-28 13:03:49 -04:00
|
|
|
redirect: "manual",
|
2020-03-04 11:31:14 -05:00
|
|
|
}); // will redirect to http://localhost:4545/
|
|
|
|
assertEquals(response.status, 0);
|
|
|
|
assertEquals(response.statusText, "");
|
|
|
|
assertEquals(response.url, "");
|
|
|
|
assertEquals(response.type, "opaqueredirect");
|
|
|
|
try {
|
|
|
|
await response.text();
|
|
|
|
fail(
|
|
|
|
"Reponse.text() didn't throw on a filtered response without a body (type opaqueredirect)"
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-03 09:54:47 -05:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-02-03 09:54:47 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
|
|
|
async function fetchWithErrorRedirection(): Promise<void> {
|
|
|
|
const response = await fetch("http://localhost:4546/", {
|
2020-03-28 13:03:49 -04:00
|
|
|
redirect: "error",
|
2020-03-04 11:31:14 -05:00
|
|
|
}); // will redirect to http://localhost:4545/
|
|
|
|
assertEquals(response.status, 0);
|
|
|
|
assertEquals(response.statusText, "");
|
|
|
|
assertEquals(response.url, "");
|
|
|
|
assertEquals(response.type, "error");
|
|
|
|
try {
|
|
|
|
await response.text();
|
|
|
|
fail(
|
|
|
|
"Reponse.text() didn't throw on a filtered response without a body (type error)"
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-03 09:54:47 -05:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-02-03 09:54:47 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function responseRedirect(): void {
|
2020-04-10 09:51:17 -04:00
|
|
|
const redir = Response.redirect("example.com/newLocation", 301);
|
2020-02-03 09:54:47 -05:00
|
|
|
assertEquals(redir.status, 301);
|
|
|
|
assertEquals(redir.statusText, "");
|
|
|
|
assertEquals(redir.url, "");
|
|
|
|
assertEquals(redir.headers.get("Location"), "example.com/newLocation");
|
|
|
|
assertEquals(redir.type, "default");
|
|
|
|
});
|
2020-05-25 16:20:09 -04:00
|
|
|
|
|
|
|
unitTest({ perms: { net: true } }, async function fetchBodyReadTwice(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const response = await fetch("http://localhost:4545/cli/tests/fixture.json");
|
|
|
|
|
|
|
|
// Read body
|
|
|
|
const _json = await response.json();
|
|
|
|
assert(_json);
|
|
|
|
|
|
|
|
// All calls after the body was consumed, should fail
|
|
|
|
const methods = ["json", "text", "formData", "arrayBuffer"];
|
|
|
|
for (const method of methods) {
|
|
|
|
try {
|
2020-05-26 10:02:16 -04:00
|
|
|
// @ts-expect-error
|
2020-05-25 16:20:09 -04:00
|
|
|
await response[method]();
|
|
|
|
fail(
|
|
|
|
"Reading body multiple times should failed, the stream should've been locked."
|
|
|
|
);
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchBodyReaderAfterRead(): Promise<void> {
|
|
|
|
const response = await fetch(
|
|
|
|
"http://localhost:4545/cli/tests/fixture.json"
|
|
|
|
);
|
|
|
|
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.");
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchBodyReaderWithCancelAndNewReader(): Promise<void> {
|
|
|
|
const data = "a".repeat(1 << 10);
|
|
|
|
const response = await fetch(
|
|
|
|
"http://localhost:4545/cli/tests/echo_server",
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
body: data,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function fetchBodyReaderWithReadCancelAndNewReader(): Promise<void> {
|
|
|
|
const data = "a".repeat(1 << 10);
|
|
|
|
|
|
|
|
const response = await fetch(
|
|
|
|
"http://localhost:4545/cli/tests/echo_server",
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
body: data,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|