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