From 3f805e61e256a73a1375f7c0579147113f6b876e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 22 Mar 2022 23:08:29 +0100 Subject: [PATCH] refactor: remove loops in cli/tests/unit/http_test.ts (#14075) --- cli/tests/unit/http_test.ts | 138 +++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 66 deletions(-) diff --git a/cli/tests/unit/http_test.ts b/cli/tests/unit/http_test.ts index 2eb8b4553c..eac9f46b1b 100644 --- a/cli/tests/unit/http_test.ts +++ b/cli/tests/unit/http_test.ts @@ -47,15 +47,18 @@ async function writeRequestAndReadResponse(conn: Deno.Conn): Promise { 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