1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

refactor: remove loops in cli/tests/unit/http_test.ts (#14075)

This commit is contained in:
Bartek Iwańczuk 2022-03-22 23:08:29 +01:00 committed by Kitson Kelly
parent acec460ab5
commit 3f805e61e2

View file

@ -47,15 +47,18 @@ async function writeRequestAndReadResponse(conn: Deno.Conn): Promise<string> {
Deno.test({ permissions: { net: true } }, async function httpServerBasic() {
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(), "");
respondWith(new Response("Hello World", { headers: { "foo": "bar" } }));
}
break;
}
const conn = await listener.accept();
listener.close();
const httpConn = Deno.serveHttp(conn);
const reqEvent = await httpConn.nextRequest();
assert(reqEvent);
const { request, respondWith } = reqEvent;
assertEquals(new URL(request.url).href, "http://127.0.0.1:4501/");
assertEquals(await request.text(), "");
await respondWith(
new Response("Hello World", { headers: { "foo": "bar" } }),
);
httpConn.close();
})();
const resp = await fetch("http://127.0.0.1:4501/", {
@ -595,17 +598,17 @@ Deno.test(
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;
}
const conn = await listener.accept();
listener.close();
const httpConn = Deno.serveHttp(conn);
const reqEvent = await httpConn.nextRequest();
assert(reqEvent);
const { request, respondWith } = reqEvent;
assertEquals(request.headers.get("X-Header-Test"), "á");
await respondWith(
new Response("", { headers: { "X-Header-Test": "Æ" } }),
);
httpConn.close();
})();
const clientConn = await Deno.connect({ port: 4501 });
@ -641,15 +644,16 @@ Deno.test(
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) {
assertEquals(new URL(request.url).href, "http://127.0.0.1:4501/");
assertEquals(await request.text(), "");
respondWith(new Response());
}
break;
}
const conn = await listener.accept();
listener.close();
const httpConn = Deno.serveHttp(conn);
const reqEvent = await httpConn.nextRequest();
assert(reqEvent);
const { request, respondWith } = reqEvent;
assertEquals(new URL(request.url).href, "http://127.0.0.1:4501/");
assertEquals(await request.text(), "");
await respondWith(new Response());
httpConn.close();
})();
const clientConn = await Deno.connect({ port: 4501 });
@ -684,21 +688,22 @@ Deno.test(
Deno.test({ permissions: { net: true } }, async function httpServerWebSocket() {
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,
socket,
} = Deno.upgradeWebSocket(request);
socket.onerror = () => fail();
socket.onmessage = (m) => {
socket.send(m.data);
socket.close(1001);
};
await respondWith(response);
break;
}
const conn = await listener.accept();
listener.close();
const httpConn = Deno.serveHttp(conn);
const reqEvent = await httpConn.nextRequest();
assert(reqEvent);
const { request, respondWith } = reqEvent;
const {
response,
socket,
} = Deno.upgradeWebSocket(request);
socket.onerror = () => fail();
socket.onmessage = (m) => {
socket.send(m.data);
socket.close(1001);
};
await respondWith(response);
})();
const def = deferred();
@ -803,16 +808,17 @@ Deno.test(
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;
}
const conn = await listener.accept();
listener.close();
const httpConn = Deno.serveHttp(conn);
const reqEvent = await httpConn.nextRequest();
assert(reqEvent);
const { request, respondWith } = reqEvent;
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");
await respondWith(new Response("ok"));
httpConn.close();
})();
const resp = await fetch("http://127.0.0.1:4501/", {
@ -1142,18 +1148,18 @@ Deno.test(
const promise = (async () => {
const listener = Deno.listen({ path: filePath, transport: "unix" });
for await (const conn of listener) {
const httpConn = Deno.serveHttp(conn);
for await (const { request, respondWith } of httpConn) {
const url = new URL(request.url);
assertEquals(url.protocol, "http+unix:");
assertEquals(decodeURIComponent(url.host), filePath);
assertEquals(url.pathname, "/path/name");
await respondWith(new Response("", { headers: {} }));
httpConn.close();
}
break;
}
const conn = await listener.accept();
listener.close();
const httpConn = Deno.serveHttp(conn);
const reqEvent = await httpConn.nextRequest();
assert(reqEvent);
const { request, respondWith } = reqEvent;
const url = new URL(request.url);
assertEquals(url.protocol, "http+unix:");
assertEquals(decodeURIComponent(url.host), filePath);
assertEquals(url.pathname, "/path/name");
await respondWith(new Response("", { headers: {} }));
httpConn.close();
})();
// fetch() does not supports unix domain sockets yet https://github.com/denoland/deno/issues/8821