2021-04-08 18:34:15 -04:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-06-14 08:52:49 -04:00
|
|
|
import { chunkedBodyReader } from "../../../test_util/std/http/_io.ts";
|
|
|
|
import { BufReader, BufWriter } from "../../../test_util/std/io/bufio.ts";
|
|
|
|
import { Buffer } from "../../../test_util/std/io/buffer.ts";
|
|
|
|
import { TextProtoReader } from "../../../test_util/std/textproto/mod.ts";
|
2021-04-08 18:34:15 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2021-09-05 16:43:46 -04:00
|
|
|
assertRejects,
|
2021-08-24 18:55:32 -04:00
|
|
|
assertThrows,
|
2021-06-14 16:10:55 -04:00
|
|
|
deferred,
|
2021-06-24 21:44:14 -04:00
|
|
|
delay,
|
2021-07-08 07:33:01 -04:00
|
|
|
fail,
|
2021-04-08 18:34:15 -04:00
|
|
|
unitTest,
|
|
|
|
} from "./test_util.ts";
|
|
|
|
|
2021-06-14 08:52:49 -04:00
|
|
|
async function writeRequestAndReadResponse(conn: Deno.Conn): Promise<string> {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
|
|
|
const w = new BufWriter(conn);
|
|
|
|
const r = new BufReader(conn);
|
|
|
|
const body = `GET / HTTP/1.1\r\nHost: 127.0.0.1:4501\r\n\r\n`;
|
|
|
|
const writeResult = await w.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
await w.flush();
|
|
|
|
const tpr = new TextProtoReader(r);
|
|
|
|
const statusLine = await tpr.readLine();
|
|
|
|
assert(statusLine !== null);
|
|
|
|
const headers = await tpr.readMIMEHeader();
|
|
|
|
assert(headers !== null);
|
|
|
|
|
|
|
|
const chunkedReader = chunkedBodyReader(headers, r);
|
|
|
|
const buf = new Uint8Array(5);
|
|
|
|
const dest = new Buffer();
|
|
|
|
let result: number | null;
|
|
|
|
while ((result = await chunkedReader.read(buf)) !== null) {
|
|
|
|
const len = Math.min(buf.byteLength, result);
|
|
|
|
await dest.write(buf.subarray(0, len));
|
|
|
|
}
|
|
|
|
return decoder.decode(dest.bytes());
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
unitTest({ permissions: { net: true } }, async function httpServerBasic() {
|
2021-04-08 18:34:15 -04:00
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
for await (const conn of listener) {
|
2021-04-09 11:54:27 -04:00
|
|
|
const httpConn = Deno.serveHttp(conn);
|
2021-04-08 18:34:15 -04:00
|
|
|
for await (const { request, respondWith } of httpConn) {
|
2021-04-19 11:07:44 -04:00
|
|
|
assertEquals(new URL(request.url).href, "http://127.0.0.1:4501/");
|
2021-04-08 18:34:15 -04:00
|
|
|
assertEquals(await request.text(), "");
|
2021-04-12 10:24:45 -04:00
|
|
|
respondWith(new Response("Hello World", { headers: { "foo": "bar" } }));
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
const resp = await fetch("http://127.0.0.1:4501/", {
|
|
|
|
headers: { "connection": "close" },
|
|
|
|
});
|
2021-06-25 18:01:25 -04:00
|
|
|
const clone = resp.clone();
|
2021-04-08 18:34:15 -04:00
|
|
|
const text = await resp.text();
|
|
|
|
assertEquals(text, "Hello World");
|
2021-04-12 10:24:45 -04:00
|
|
|
assertEquals(resp.headers.get("foo"), "bar");
|
2021-06-25 18:01:25 -04:00
|
|
|
const cloneText = await clone.text();
|
|
|
|
assertEquals(cloneText, "Hello World");
|
2021-04-08 18:34:15 -04:00
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
2021-10-11 12:39:55 -04:00
|
|
|
unitTest(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpServerGetRequestBody() {
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
const conn = await listener.accept();
|
|
|
|
listener.close();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const e = await httpConn.nextRequest();
|
|
|
|
assert(e);
|
|
|
|
const { request, respondWith } = e;
|
|
|
|
assertEquals(request.body, null);
|
|
|
|
await respondWith(new Response("", { headers: {} }));
|
|
|
|
httpConn.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
const conn = await Deno.connect({ port: 4501 });
|
|
|
|
// Send GET request with a body + content-length.
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const body =
|
|
|
|
`GET / HTTP/1.1\r\nHost: 127.0.0.1:4501\r\nContent-Length: 5\r\n\r\n12345`;
|
|
|
|
const writeResult = await conn.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
|
|
|
|
const resp = new Uint8Array(200);
|
|
|
|
const readResult = await conn.read(resp);
|
|
|
|
assertEquals(readResult, 115);
|
|
|
|
|
|
|
|
conn.close();
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-04-08 18:34:15 -04:00
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-04-08 18:34:15 -04:00
|
|
|
async function httpServerStreamResponse() {
|
|
|
|
const stream = new TransformStream();
|
|
|
|
const writer = stream.writable.getWriter();
|
|
|
|
writer.write(new TextEncoder().encode("hello "));
|
|
|
|
writer.write(new TextEncoder().encode("world"));
|
|
|
|
writer.close();
|
|
|
|
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
const conn = await listener.accept();
|
2021-04-09 11:54:27 -04:00
|
|
|
const httpConn = Deno.serveHttp(conn);
|
2021-04-08 18:34:15 -04:00
|
|
|
const evt = await httpConn.nextRequest();
|
|
|
|
assert(evt);
|
|
|
|
const { request, respondWith } = evt;
|
|
|
|
assert(!request.body);
|
|
|
|
await respondWith(new Response(stream.readable));
|
|
|
|
httpConn.close();
|
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
const resp = await fetch("http://127.0.0.1:4501/");
|
|
|
|
const respBody = await resp.text();
|
|
|
|
assertEquals("hello world", respBody);
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-04-08 18:34:15 -04:00
|
|
|
async function httpServerStreamRequest() {
|
|
|
|
const stream = new TransformStream();
|
|
|
|
const writer = stream.writable.getWriter();
|
|
|
|
writer.write(new TextEncoder().encode("hello "));
|
|
|
|
writer.write(new TextEncoder().encode("world"));
|
|
|
|
writer.close();
|
|
|
|
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
const conn = await listener.accept();
|
2021-04-09 11:54:27 -04:00
|
|
|
const httpConn = Deno.serveHttp(conn);
|
2021-04-08 18:34:15 -04:00
|
|
|
const evt = await httpConn.nextRequest();
|
|
|
|
assert(evt);
|
|
|
|
const { request, respondWith } = evt;
|
|
|
|
const reqBody = await request.text();
|
|
|
|
assertEquals("hello world", reqBody);
|
|
|
|
await respondWith(new Response(""));
|
|
|
|
|
|
|
|
// TODO(ry) If we don't call httpConn.nextRequest() here we get "error sending
|
|
|
|
// request for url (https://localhost:4501/): connection closed before
|
|
|
|
// message completed".
|
|
|
|
assertEquals(await httpConn.nextRequest(), null);
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
const resp = await fetch("http://127.0.0.1:4501/", {
|
|
|
|
body: stream.readable,
|
|
|
|
method: "POST",
|
|
|
|
headers: { "connection": "close" },
|
|
|
|
});
|
|
|
|
|
|
|
|
await resp.arrayBuffer();
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
unitTest(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpServerStreamDuplex() {
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const evt = await httpConn.nextRequest();
|
|
|
|
assert(evt);
|
|
|
|
const { request, respondWith } = evt;
|
|
|
|
assert(request.body);
|
|
|
|
await respondWith(new Response(request.body));
|
|
|
|
httpConn.close();
|
|
|
|
listener.close();
|
|
|
|
})();
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
const ts = new TransformStream();
|
|
|
|
const writable = ts.writable.getWriter();
|
|
|
|
const resp = await fetch("http://127.0.0.1:4501/", {
|
|
|
|
method: "POST",
|
|
|
|
body: ts.readable,
|
|
|
|
});
|
|
|
|
assert(resp.body);
|
|
|
|
const reader = resp.body.getReader();
|
|
|
|
await writable.write(new Uint8Array([1]));
|
|
|
|
const chunk1 = await reader.read();
|
|
|
|
assert(!chunk1.done);
|
|
|
|
assertEquals(chunk1.value, new Uint8Array([1]));
|
|
|
|
await writable.write(new Uint8Array([2]));
|
|
|
|
const chunk2 = await reader.read();
|
|
|
|
assert(!chunk2.done);
|
|
|
|
assertEquals(chunk2.value, new Uint8Array([2]));
|
|
|
|
await writable.close();
|
|
|
|
const chunk3 = await reader.read();
|
|
|
|
assert(chunk3.done);
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
unitTest({ permissions: { net: true } }, async function httpServerClose() {
|
2021-04-08 18:34:15 -04:00
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
const client = await Deno.connect({ port: 4501 });
|
2021-04-09 11:54:27 -04:00
|
|
|
const httpConn = Deno.serveHttp(await listener.accept());
|
2021-04-08 18:34:15 -04:00
|
|
|
client.close();
|
|
|
|
const evt = await httpConn.nextRequest();
|
|
|
|
assertEquals(evt, null);
|
|
|
|
// Note httpConn is automatically closed when "done" is reached.
|
|
|
|
listener.close();
|
|
|
|
});
|
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
unitTest(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpServerInvalidMethod() {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
const client = await Deno.connect({ port: 4501 });
|
|
|
|
const httpConn = Deno.serveHttp(await listener.accept());
|
|
|
|
await client.write(new Uint8Array([1, 2, 3]));
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await httpConn.nextRequest();
|
|
|
|
},
|
|
|
|
Deno.errors.Http,
|
|
|
|
"invalid HTTP method parsed",
|
|
|
|
);
|
|
|
|
// Note httpConn is automatically closed when it errors.
|
|
|
|
client.close();
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
2021-04-08 18:34:15 -04:00
|
|
|
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function httpServerWithTls() {
|
2021-04-08 18:34:15 -04:00
|
|
|
const hostname = "localhost";
|
|
|
|
const port = 4501;
|
|
|
|
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listenTls({
|
|
|
|
hostname,
|
|
|
|
port,
|
2021-08-11 10:20:47 -04:00
|
|
|
certFile: "cli/tests/testdata/tls/localhost.crt",
|
|
|
|
keyFile: "cli/tests/testdata/tls/localhost.key",
|
2021-04-08 18:34:15 -04:00
|
|
|
});
|
|
|
|
const conn = await listener.accept();
|
2021-04-09 11:54:27 -04:00
|
|
|
const httpConn = Deno.serveHttp(conn);
|
2021-04-08 18:34:15 -04:00
|
|
|
const evt = await httpConn.nextRequest();
|
|
|
|
assert(evt);
|
2021-04-09 17:35:29 -04:00
|
|
|
const { respondWith } = evt;
|
2021-04-08 18:34:15 -04:00
|
|
|
await respondWith(new Response("Hello World"));
|
|
|
|
|
|
|
|
// TODO(ry) If we don't call httpConn.nextRequest() here we get "error sending
|
|
|
|
// request for url (https://localhost:4501/): connection closed before
|
|
|
|
// message completed".
|
|
|
|
assertEquals(await httpConn.nextRequest(), null);
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
2021-09-30 03:26:15 -04:00
|
|
|
const caCert = Deno.readTextFileSync("cli/tests/testdata/tls/RootCA.pem");
|
|
|
|
const client = Deno.createHttpClient({ caCerts: [caCert] });
|
2021-04-08 18:34:15 -04:00
|
|
|
const resp = await fetch(`https://${hostname}:${port}/`, {
|
|
|
|
client,
|
|
|
|
headers: { "connection": "close" },
|
|
|
|
});
|
|
|
|
const respBody = await resp.text();
|
|
|
|
assertEquals("Hello World", respBody);
|
|
|
|
await promise;
|
|
|
|
client.close();
|
|
|
|
},
|
|
|
|
);
|
2021-04-15 18:48:56 -04:00
|
|
|
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-04-15 18:48:56 -04:00
|
|
|
async function httpServerRegressionHang() {
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const event = await httpConn.nextRequest();
|
|
|
|
assert(event);
|
|
|
|
const { request, respondWith } = event;
|
|
|
|
const reqBody = await request.text();
|
|
|
|
assertEquals("request", reqBody);
|
|
|
|
await respondWith(new Response("response"));
|
|
|
|
httpConn.close();
|
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
const resp = await fetch("http://127.0.0.1:4501/", {
|
|
|
|
method: "POST",
|
|
|
|
body: "request",
|
|
|
|
});
|
|
|
|
const respBody = await resp.text();
|
|
|
|
assertEquals("response", respBody);
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-04-23 06:34:04 -04:00
|
|
|
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-04-23 06:34:04 -04:00
|
|
|
async function httpServerCancelBodyOnResponseFailure() {
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const event = await httpConn.nextRequest();
|
|
|
|
assert(event);
|
|
|
|
const { respondWith } = event;
|
2021-09-05 16:43:46 -04:00
|
|
|
let cancelReason: string;
|
|
|
|
await assertRejects(
|
2021-04-23 06:34:04 -04:00
|
|
|
async () => {
|
|
|
|
let interval = 0;
|
|
|
|
await respondWith(
|
|
|
|
new Response(
|
|
|
|
new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
interval = setInterval(() => {
|
|
|
|
const message = `data: ${Date.now()}\n\n`;
|
|
|
|
controller.enqueue(new TextEncoder().encode(message));
|
|
|
|
}, 200);
|
|
|
|
},
|
|
|
|
cancel(reason) {
|
|
|
|
cancelReason = reason;
|
|
|
|
clearInterval(interval);
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Deno.errors.Http,
|
2021-09-05 16:43:46 -04:00
|
|
|
cancelReason!,
|
2021-04-23 06:34:04 -04:00
|
|
|
);
|
2021-09-05 16:43:46 -04:00
|
|
|
assert(cancelReason!);
|
2021-04-23 06:34:04 -04:00
|
|
|
httpConn.close();
|
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
const resp = await fetch("http://127.0.0.1:4501/");
|
|
|
|
await resp.body!.cancel();
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-05-19 08:39:52 -04:00
|
|
|
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-05-19 08:39:52 -04:00
|
|
|
async function httpServerNextRequestErrorExposedInResponse() {
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const event = await httpConn.nextRequest();
|
|
|
|
assert(event);
|
|
|
|
// Start polling for the next request before awaiting response.
|
|
|
|
const nextRequestPromise = httpConn.nextRequest();
|
|
|
|
const { respondWith } = event;
|
2021-09-05 16:43:46 -04:00
|
|
|
await assertRejects(
|
2021-05-19 08:39:52 -04:00
|
|
|
async () => {
|
|
|
|
let interval = 0;
|
|
|
|
await respondWith(
|
|
|
|
new Response(
|
|
|
|
new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
interval = setInterval(() => {
|
|
|
|
const message = `data: ${Date.now()}\n\n`;
|
|
|
|
controller.enqueue(new TextEncoder().encode(message));
|
|
|
|
}, 200);
|
|
|
|
},
|
|
|
|
cancel() {
|
|
|
|
clearInterval(interval);
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Deno.errors.Http,
|
|
|
|
"connection closed",
|
|
|
|
);
|
|
|
|
// The error from `op_http_request_next` reroutes to `respondWith()`.
|
|
|
|
assertEquals(await nextRequestPromise, null);
|
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
const resp = await fetch("http://127.0.0.1:4501/");
|
|
|
|
await resp.body!.cancel();
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-05-20 21:11:53 -04:00
|
|
|
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-05-20 21:11:53 -04:00
|
|
|
async function httpServerEmptyBlobResponse() {
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const event = await httpConn.nextRequest();
|
|
|
|
assert(event);
|
|
|
|
const { respondWith } = event;
|
|
|
|
await respondWith(new Response(new Blob([])));
|
|
|
|
httpConn.close();
|
|
|
|
listener.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
const resp = await fetch("http://127.0.0.1:4501/");
|
|
|
|
const respBody = await resp.text();
|
|
|
|
assertEquals("", respBody);
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-06-03 19:32:36 -04:00
|
|
|
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-06-03 19:32:36 -04:00
|
|
|
async function httpServerNextRequestResolvesOnClose() {
|
|
|
|
const httpConnList: Deno.HttpConn[] = [];
|
|
|
|
|
|
|
|
async function serve(l: Deno.Listener) {
|
|
|
|
for await (const conn of l) {
|
|
|
|
(async () => {
|
|
|
|
const c = Deno.serveHttp(conn);
|
|
|
|
httpConnList.push(c);
|
|
|
|
for await (const { respondWith } of c) {
|
|
|
|
respondWith(new Response("hello"));
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const l = Deno.listen({ port: 4500 });
|
|
|
|
serve(l);
|
|
|
|
|
|
|
|
await delay(300);
|
|
|
|
const res = await fetch("http://localhost:4500/");
|
|
|
|
const _text = await res.text();
|
|
|
|
|
|
|
|
// Close connection and listener.
|
|
|
|
httpConnList.forEach((conn) => conn.close());
|
|
|
|
l.close();
|
|
|
|
|
|
|
|
await delay(300);
|
|
|
|
},
|
|
|
|
);
|
2021-06-14 08:52:49 -04:00
|
|
|
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-06-14 08:52:49 -04:00
|
|
|
// Issue: https://github.com/denoland/deno/issues/10870
|
|
|
|
async function httpServerHang() {
|
|
|
|
// Quick and dirty way to make a readable stream from a string. Alternatively,
|
|
|
|
// `readableStreamFromReader(file)` could be used.
|
|
|
|
function stream(s: string): ReadableStream<Uint8Array> {
|
|
|
|
return new Response(s).body!;
|
|
|
|
}
|
|
|
|
|
|
|
|
const httpConns: Deno.HttpConn[] = [];
|
|
|
|
const promise = (async () => {
|
|
|
|
let count = 0;
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
for await (const conn of listener) {
|
|
|
|
(async () => {
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
httpConns.push(httpConn);
|
|
|
|
for await (const { respondWith } of httpConn) {
|
|
|
|
respondWith(new Response(stream("hello")));
|
|
|
|
|
|
|
|
count++;
|
|
|
|
if (count >= 2) {
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
const clientConn = await Deno.connect({ port: 4501 });
|
|
|
|
|
|
|
|
const r1 = await writeRequestAndReadResponse(clientConn);
|
|
|
|
assertEquals(r1, "hello");
|
|
|
|
|
|
|
|
const r2 = await writeRequestAndReadResponse(clientConn);
|
|
|
|
assertEquals(r2, "hello");
|
|
|
|
|
|
|
|
clientConn.close();
|
|
|
|
await promise;
|
|
|
|
for (const conn of httpConns) {
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2021-06-14 16:10:55 -04:00
|
|
|
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-06-14 16:10:55 -04:00
|
|
|
// Issue: https://github.com/denoland/deno/issues/10930
|
|
|
|
async function httpServerStreamingResponse() {
|
|
|
|
// This test enqueues a single chunk for readable
|
|
|
|
// stream and waits for client to read that chunk and signal
|
|
|
|
// it before enqueueing subsequent chunk. Issue linked above
|
|
|
|
// presented a situation where enqueued chunks were not
|
|
|
|
// written to the HTTP connection until the next chunk was enqueued.
|
|
|
|
|
|
|
|
let counter = 0;
|
|
|
|
|
|
|
|
const deferreds = [
|
|
|
|
deferred(),
|
|
|
|
deferred(),
|
|
|
|
deferred(),
|
|
|
|
];
|
|
|
|
|
|
|
|
async function writeRequest(conn: Deno.Conn) {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
|
|
|
const w = new BufWriter(conn);
|
|
|
|
const r = new BufReader(conn);
|
|
|
|
const body = `GET / HTTP/1.1\r\nHost: 127.0.0.1:4501\r\n\r\n`;
|
|
|
|
const writeResult = await w.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
await w.flush();
|
|
|
|
const tpr = new TextProtoReader(r);
|
|
|
|
const statusLine = await tpr.readLine();
|
|
|
|
assert(statusLine !== null);
|
|
|
|
const headers = await tpr.readMIMEHeader();
|
|
|
|
assert(headers !== null);
|
|
|
|
|
|
|
|
const chunkedReader = chunkedBodyReader(headers, r);
|
|
|
|
const buf = new Uint8Array(5);
|
|
|
|
const dest = new Buffer();
|
|
|
|
let result: number | null;
|
|
|
|
while ((result = await chunkedReader.read(buf)) !== null) {
|
|
|
|
const len = Math.min(buf.byteLength, result);
|
|
|
|
await dest.write(buf.subarray(0, len));
|
|
|
|
// Resolve a deferred - this will make response stream to
|
|
|
|
// enqueue next chunk.
|
|
|
|
deferreds[counter - 1].resolve();
|
|
|
|
}
|
|
|
|
return decoder.decode(dest.bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
function periodicStream() {
|
|
|
|
return new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
controller.enqueue(`${counter}\n`);
|
|
|
|
counter++;
|
|
|
|
},
|
|
|
|
|
|
|
|
async pull(controller) {
|
|
|
|
if (counter >= 3) {
|
|
|
|
return controller.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
await deferreds[counter - 1];
|
|
|
|
|
|
|
|
controller.enqueue(`${counter}\n`);
|
|
|
|
counter++;
|
|
|
|
},
|
|
|
|
}).pipeThrough(new TextEncoderStream());
|
|
|
|
}
|
|
|
|
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
const finished = (async () => {
|
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const requestEvent = await httpConn.nextRequest();
|
|
|
|
const { respondWith } = requestEvent!;
|
|
|
|
await respondWith(new Response(periodicStream()));
|
|
|
|
httpConn.close();
|
|
|
|
})();
|
|
|
|
|
|
|
|
// start a client
|
|
|
|
const clientConn = await Deno.connect({ port: 4501 });
|
|
|
|
|
|
|
|
const r1 = await writeRequest(clientConn);
|
|
|
|
assertEquals(r1, "0\n1\n2\n");
|
|
|
|
|
|
|
|
await finished;
|
|
|
|
clientConn.close();
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|
2021-06-26 20:29:01 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
unitTest(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpRequestLatin1Headers() {
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
for await (const conn of listener) {
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
for await (const { request, respondWith } of httpConn) {
|
|
|
|
assertEquals(request.headers.get("X-Header-Test"), "á");
|
|
|
|
await respondWith(
|
|
|
|
new Response("", { headers: { "X-Header-Test": "Æ" } }),
|
|
|
|
);
|
|
|
|
httpConn.close();
|
|
|
|
}
|
|
|
|
break;
|
2021-06-26 20:29:01 -04:00
|
|
|
}
|
2021-09-22 19:50:50 -04:00
|
|
|
})();
|
2021-06-26 20:29:01 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
const clientConn = await Deno.connect({ port: 4501 });
|
|
|
|
const requestText =
|
|
|
|
"GET / HTTP/1.1\r\nHost: 127.0.0.1:4501\r\nX-Header-Test: á\r\n\r\n";
|
|
|
|
const requestBytes = new Uint8Array(requestText.length);
|
|
|
|
for (let i = 0; i < requestText.length; i++) {
|
|
|
|
requestBytes[i] = requestText.charCodeAt(i);
|
|
|
|
}
|
|
|
|
let written = 0;
|
|
|
|
while (written < requestBytes.byteLength) {
|
|
|
|
written += await clientConn.write(requestBytes.slice(written));
|
|
|
|
}
|
2021-06-26 20:29:01 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
let responseText = "";
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
let read;
|
|
|
|
while ((read = await clientConn.read(buf)) !== null) {
|
|
|
|
for (let i = 0; i < read; i++) {
|
|
|
|
responseText += String.fromCharCode(buf[i]);
|
|
|
|
}
|
2021-06-26 20:29:01 -04:00
|
|
|
}
|
2021-09-22 19:50:50 -04:00
|
|
|
clientConn.close();
|
2021-06-26 20:29:01 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
assert(/\r\n[Xx]-[Hh]eader-[Tt]est: Æ\r\n/.test(responseText));
|
2021-06-26 20:29:01 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-06-27 18:19:40 -04:00
|
|
|
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-06-27 18:19:40 -04:00
|
|
|
async function httpServerRequestWithoutPath() {
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
for await (const conn of listener) {
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
for await (const { request, respondWith } of httpConn) {
|
2021-09-26 14:26:16 -04:00
|
|
|
assertEquals(new URL(request.url).href, "http://127.0.0.1:4501/");
|
2021-06-27 18:19:40 -04:00
|
|
|
assertEquals(await request.text(), "");
|
|
|
|
respondWith(new Response());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
const clientConn = await Deno.connect({ port: 4501 });
|
|
|
|
|
|
|
|
async function writeRequest(conn: Deno.Conn) {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
|
|
|
const w = new BufWriter(conn);
|
|
|
|
const r = new BufReader(conn);
|
|
|
|
const body =
|
|
|
|
`CONNECT 127.0.0.1:4501 HTTP/1.1\r\nHost: 127.0.0.1:4501\r\n\r\n`;
|
|
|
|
const writeResult = await w.write(encoder.encode(body));
|
|
|
|
assertEquals(body.length, writeResult);
|
|
|
|
await w.flush();
|
|
|
|
const tpr = new TextProtoReader(r);
|
|
|
|
const statusLine = await tpr.readLine();
|
|
|
|
assert(statusLine !== null);
|
|
|
|
const m = statusLine.match(/^(.+?) (.+?) (.+?)$/);
|
|
|
|
assert(m !== null, "must be matched");
|
|
|
|
const [_, _proto, status, _ok] = m;
|
|
|
|
assertEquals(status, "200");
|
|
|
|
const headers = await tpr.readMIMEHeader();
|
|
|
|
assert(headers !== null);
|
|
|
|
}
|
|
|
|
|
|
|
|
await writeRequest(clientConn);
|
|
|
|
clientConn.close();
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-07-08 07:33:01 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
unitTest({ permissions: { net: true } }, async function httpServerWebSocket() {
|
2021-07-08 07:33:01 -04:00
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
for await (const conn of listener) {
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const { request, respondWith } = (await httpConn.nextRequest())!;
|
|
|
|
const {
|
|
|
|
response,
|
2021-07-28 07:13:14 -04:00
|
|
|
socket,
|
2021-07-22 08:28:55 -04:00
|
|
|
} = Deno.upgradeWebSocket(request);
|
2021-07-28 07:13:14 -04:00
|
|
|
socket.onerror = () => fail();
|
|
|
|
socket.onmessage = (m) => {
|
|
|
|
socket.send(m.data);
|
2021-08-09 04:45:59 -04:00
|
|
|
socket.close(1001);
|
2021-07-08 07:33:01 -04:00
|
|
|
};
|
|
|
|
await respondWith(response);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
const def = deferred();
|
|
|
|
const ws = new WebSocket("ws://localhost:4501");
|
|
|
|
ws.onmessage = (m) => assertEquals(m.data, "foo");
|
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onclose = () => def.resolve();
|
|
|
|
ws.onopen = () => ws.send("foo");
|
|
|
|
await def;
|
|
|
|
await promise;
|
|
|
|
});
|
2021-07-16 10:31:16 -04:00
|
|
|
|
2021-07-22 08:28:55 -04:00
|
|
|
unitTest(function httpUpgradeWebSocket() {
|
|
|
|
const request = new Request("https://deno.land/", {
|
|
|
|
headers: {
|
|
|
|
connection: "Upgrade",
|
|
|
|
upgrade: "websocket",
|
|
|
|
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { response } = Deno.upgradeWebSocket(request);
|
|
|
|
assertEquals(response.status, 101);
|
|
|
|
assertEquals(response.headers.get("connection"), "Upgrade");
|
|
|
|
assertEquals(response.headers.get("upgrade"), "websocket");
|
|
|
|
assertEquals(
|
|
|
|
response.headers.get("sec-websocket-accept"),
|
|
|
|
"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(function httpUpgradeWebSocketLowercaseUpgradeHeader() {
|
|
|
|
const request = new Request("https://deno.land/", {
|
|
|
|
headers: {
|
|
|
|
connection: "upgrade",
|
|
|
|
upgrade: "websocket",
|
2021-07-23 22:14:03 -04:00
|
|
|
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { response } = Deno.upgradeWebSocket(request);
|
|
|
|
assertEquals(response.status, 101);
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(function httpUpgradeWebSocketMultipleConnectionOptions() {
|
|
|
|
const request = new Request("https://deno.land/", {
|
|
|
|
headers: {
|
|
|
|
connection: "keep-alive, upgrade",
|
|
|
|
upgrade: "websocket",
|
2021-07-22 08:28:55 -04:00
|
|
|
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { response } = Deno.upgradeWebSocket(request);
|
|
|
|
assertEquals(response.status, 101);
|
|
|
|
});
|
|
|
|
|
2021-08-24 18:55:32 -04:00
|
|
|
unitTest(function httpUpgradeWebSocketCaseInsensitiveUpgradeHeader() {
|
|
|
|
const request = new Request("https://deno.land/", {
|
|
|
|
headers: {
|
|
|
|
connection: "upgrade",
|
2021-09-05 16:43:46 -04:00
|
|
|
upgrade: "websocket",
|
2021-08-24 18:55:32 -04:00
|
|
|
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { response } = Deno.upgradeWebSocket(request);
|
|
|
|
assertEquals(response.status, 101);
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(function httpUpgradeWebSocketInvalidUpgradeHeader() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
const request = new Request("https://deno.land/", {
|
|
|
|
headers: {
|
|
|
|
connection: "upgrade",
|
|
|
|
upgrade: "invalid",
|
|
|
|
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
Deno.upgradeWebSocket(request);
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Invalid Header: 'upgrade' header must be 'websocket'",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(function httpUpgradeWebSocketWithoutUpgradeHeader() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
const request = new Request("https://deno.land/", {
|
|
|
|
headers: {
|
|
|
|
connection: "upgrade",
|
|
|
|
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
Deno.upgradeWebSocket(request);
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Invalid Header: 'upgrade' header must be 'websocket'",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
unitTest(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function httpCookieConcatenation() {
|
|
|
|
const promise = (async () => {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
for await (const conn of listener) {
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
for await (const { request, respondWith } of httpConn) {
|
|
|
|
assertEquals(new URL(request.url).href, "http://127.0.0.1:4501/");
|
|
|
|
assertEquals(await request.text(), "");
|
|
|
|
assertEquals(request.headers.get("cookie"), "foo=bar; bar=foo");
|
|
|
|
respondWith(new Response("ok"));
|
|
|
|
}
|
|
|
|
break;
|
2021-07-16 10:31:16 -04:00
|
|
|
}
|
2021-09-22 19:50:50 -04:00
|
|
|
})();
|
2021-07-16 10:31:16 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
const resp = await fetch("http://127.0.0.1:4501/", {
|
|
|
|
headers: [
|
|
|
|
["connection", "close"],
|
|
|
|
["cookie", "foo=bar"],
|
|
|
|
["cookie", "bar=foo"],
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const text = await resp.text();
|
|
|
|
assertEquals(text, "ok");
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-08-13 06:07:05 -04:00
|
|
|
|
|
|
|
// https://github.com/denoland/deno/issues/11651
|
2021-09-22 19:50:50 -04:00
|
|
|
unitTest({ permissions: { net: true } }, async function httpServerPanic() {
|
2021-08-13 06:07:05 -04:00
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
const client = await Deno.connect({ port: 4501 });
|
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
|
|
|
|
// This message is incomplete on purpose, we'll forcefully close client connection
|
|
|
|
// after it's flushed to cause connection to error out on the server side.
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
await client.write(encoder.encode("GET / HTTP/1.1"));
|
|
|
|
|
|
|
|
httpConn.nextRequest();
|
|
|
|
await client.write(encoder.encode("\r\n\r\n"));
|
|
|
|
httpConn.close();
|
|
|
|
|
|
|
|
client.close();
|
|
|
|
listener.close();
|
|
|
|
});
|
2021-08-16 07:43:43 -04:00
|
|
|
|
|
|
|
// https://github.com/denoland/deno/issues/11595
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-16 07:43:43 -04:00
|
|
|
async function httpServerIncompleteMessage() {
|
|
|
|
const listener = Deno.listen({ port: 4501 });
|
|
|
|
|
|
|
|
const client = await Deno.connect({ port: 4501 });
|
|
|
|
await client.write(new TextEncoder().encode(
|
|
|
|
`GET / HTTP/1.0\r\n\r\n`,
|
|
|
|
));
|
|
|
|
|
|
|
|
const conn = await listener.accept();
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
const ev = await httpConn.nextRequest();
|
|
|
|
const { respondWith } = ev!;
|
|
|
|
|
|
|
|
const { readable, writable } = new TransformStream<Uint8Array>();
|
|
|
|
const writer = writable.getWriter();
|
|
|
|
|
|
|
|
async function writeResponse() {
|
|
|
|
await writer.write(
|
|
|
|
new TextEncoder().encode(
|
|
|
|
"written to the writable side of a TransformStream",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
await writer.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
const errors: Error[] = [];
|
|
|
|
|
2021-08-25 16:04:14 -04:00
|
|
|
const writePromise = writeResponse()
|
2021-08-16 07:43:43 -04:00
|
|
|
.catch((error: Error) => {
|
|
|
|
errors.push(error);
|
2021-08-25 16:04:14 -04:00
|
|
|
});
|
2021-08-16 07:43:43 -04:00
|
|
|
|
|
|
|
const res = new Response(readable);
|
|
|
|
|
2021-08-25 16:04:14 -04:00
|
|
|
const respondPromise = respondWith(res)
|
|
|
|
.catch((error: Error) => errors.push(error));
|
2021-08-16 07:43:43 -04:00
|
|
|
|
|
|
|
client.close();
|
|
|
|
|
|
|
|
await Promise.all([
|
2021-08-25 16:04:14 -04:00
|
|
|
writePromise,
|
|
|
|
respondPromise,
|
2021-08-16 07:43:43 -04:00
|
|
|
]);
|
|
|
|
|
2021-10-09 16:37:19 -04:00
|
|
|
httpConn.close();
|
2021-08-16 07:43:43 -04:00
|
|
|
listener.close();
|
|
|
|
|
2021-09-13 11:05:00 -04:00
|
|
|
assert(errors.length >= 1);
|
2021-08-16 07:43:43 -04:00
|
|
|
for (const error of errors) {
|
|
|
|
assertEquals(error.name, "Http");
|
2021-10-09 16:37:19 -04:00
|
|
|
assert(error.message.includes("connection"));
|
2021-08-16 07:43:43 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2021-08-23 10:15:59 -04:00
|
|
|
|
|
|
|
// https://github.com/denoland/deno/issues/11743
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-23 10:15:59 -04:00
|
|
|
async function httpServerDoesntLeakResources() {
|
|
|
|
const listener = Deno.listen({ port: 4505 });
|
|
|
|
const [conn, clientConn] = await Promise.all([
|
|
|
|
listener.accept(),
|
|
|
|
Deno.connect({ port: 4505 }),
|
|
|
|
]);
|
|
|
|
const httpConn = Deno.serveHttp(conn);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
httpConn.nextRequest(),
|
|
|
|
clientConn.write(new TextEncoder().encode(
|
|
|
|
`GET / HTTP/1.1\r\nHost: 127.0.0.1:4505\r\n\r\n`,
|
|
|
|
)),
|
|
|
|
]);
|
|
|
|
|
|
|
|
httpConn.close();
|
|
|
|
listener.close();
|
|
|
|
clientConn.close();
|
|
|
|
},
|
|
|
|
);
|
2021-09-08 04:12:23 -04:00
|
|
|
|
|
|
|
// https://github.com/denoland/deno/issues/11926
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-09-08 04:12:23 -04:00
|
|
|
async function httpServerDoesntLeakResources2() {
|
|
|
|
let listener: Deno.Listener;
|
|
|
|
let httpConn: Deno.HttpConn;
|
|
|
|
|
|
|
|
const promise = (async () => {
|
|
|
|
listener = Deno.listen({ port: 4502 });
|
|
|
|
for await (const conn of listener) {
|
|
|
|
httpConn = Deno.serveHttp(conn);
|
|
|
|
for await (const { request, respondWith } of httpConn) {
|
|
|
|
assertEquals(new URL(request.url).href, "http://127.0.0.1:4502/");
|
|
|
|
// not reading request body on purpose
|
|
|
|
respondWith(new Response("ok"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
const resourcesBefore = Deno.resources();
|
|
|
|
const response = await fetch("http://127.0.0.1:4502", {
|
|
|
|
method: "POST",
|
|
|
|
body: "hello world",
|
|
|
|
});
|
|
|
|
await response.text();
|
|
|
|
const resourcesAfter = Deno.resources();
|
|
|
|
// verify that the only new resource is "httpConnection", to make
|
|
|
|
// sure "request" resource is closed even if its body was not read
|
|
|
|
// by server handler
|
|
|
|
|
|
|
|
for (const rid of Object.keys(resourcesBefore)) {
|
|
|
|
delete resourcesAfter[Number(rid)];
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(Object.keys(resourcesAfter).length, 1);
|
|
|
|
|
|
|
|
listener!.close();
|
|
|
|
httpConn!.close();
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
);
|
2021-09-25 07:22:19 -04:00
|
|
|
|
|
|
|
// https://github.com/denoland/deno/pull/12216
|
|
|
|
unitTest(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function droppedConnSenderNoPanic() {
|
2021-10-09 16:37:19 -04:00
|
|
|
async function server() {
|
|
|
|
const listener = Deno.listen({ port: 8000 });
|
2021-09-25 07:22:19 -04:00
|
|
|
const conn = await listener.accept();
|
|
|
|
const http = Deno.serveHttp(conn);
|
2021-10-09 16:37:19 -04:00
|
|
|
const evt = await http.nextRequest();
|
|
|
|
http.close();
|
2021-09-25 07:22:19 -04:00
|
|
|
try {
|
2021-10-09 16:37:19 -04:00
|
|
|
await evt!.respondWith(new Response("boom"));
|
2021-09-25 07:22:19 -04:00
|
|
|
} catch {
|
2021-10-09 16:37:19 -04:00
|
|
|
// Ignore error.
|
2021-09-25 07:22:19 -04:00
|
|
|
}
|
|
|
|
listener.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function client() {
|
2021-10-09 16:37:19 -04:00
|
|
|
try {
|
|
|
|
const resp = await fetch("http://127.0.0.1:8000/");
|
|
|
|
await resp.body?.cancel();
|
|
|
|
} catch {
|
|
|
|
// Ignore error
|
|
|
|
}
|
2021-09-25 07:22:19 -04:00
|
|
|
}
|
|
|
|
|
2021-10-09 16:37:19 -04:00
|
|
|
await Promise.all([server(), client()]);
|
2021-09-25 07:22:19 -04:00
|
|
|
},
|
|
|
|
);
|