diff options
author | Bert Belder <bertbelder@gmail.com> | 2021-10-04 18:50:40 -0700 |
---|---|---|
committer | Bert Belder <bertbelder@gmail.com> | 2021-11-10 14:51:43 -0800 |
commit | 72a6231a614e71a57c4f8ce5f9de68ab97171dd1 (patch) | |
tree | b19bbd749ad67f606ef331fee00bfe2b34477633 /cli/tests/unit | |
parent | 0cc8a9741a16efe3e37167731238b33d26887fd0 (diff) |
refactor(ext/http): rewrite hyper integration and fix bug (#12732)
Fixes: #12193
Fixes: #12251
Closes: #12714
Diffstat (limited to 'cli/tests/unit')
-rw-r--r-- | cli/tests/unit/http_test.ts | 78 |
1 files changed, 77 insertions, 1 deletions
diff --git a/cli/tests/unit/http_test.ts b/cli/tests/unit/http_test.ts index a6f80eb2c..c57b85441 100644 --- a/cli/tests/unit/http_test.ts +++ b/cli/tests/unit/http_test.ts @@ -7,6 +7,7 @@ import { assert, assertEquals, assertRejects, + assertStrictEquals, assertThrows, deferred, delay, @@ -386,7 +387,7 @@ unitTest( Deno.errors.Http, "connection closed", ); - // The error from `op_http_request_next` reroutes to `respondWith()`. + // The error from `op_http_accept` reroutes to `respondWith()`. assertEquals(await nextRequestPromise, null); listener.close(); })(); @@ -865,6 +866,7 @@ unitTest( const writer = writable.getWriter(); async function writeResponse() { + await delay(50); await writer.write( new TextEncoder().encode( "written to the writable side of a TransformStream", @@ -1000,6 +1002,80 @@ unitTest( }, ); +// https://github.com/denoland/deno/issues/12193 +unitTest( + { permissions: { net: true } }, + async function httpConnConcurrentNextRequestCalls() { + const hostname = "localhost"; + const port = 4501; + + async function server() { + const listener = Deno.listen({ hostname, port }); + const tcpConn = await listener.accept(); + const httpConn = Deno.serveHttp(tcpConn); + const promises = new Array(10).fill(null).map(async (_, i) => { + const event = await httpConn.nextRequest(); + assert(event); + const { pathname } = new URL(event.request.url); + assertStrictEquals(pathname, `/${i}`); + const response = new Response(`Response #${i}`); + await event.respondWith(response); + }); + await Promise.all(promises); + httpConn.close(); + listener.close(); + } + + async function client() { + for (let i = 0; i < 10; i++) { + const response = await fetch(`http://${hostname}:${port}/${i}`); + const body = await response.text(); + assertStrictEquals(body, `Response #${i}`); + } + } + + await Promise.all([server(), delay(100).then(client)]); + }, +); + +// https://github.com/denoland/deno/pull/12704 +// https://github.com/denoland/deno/pull/12732 +unitTest( + { permissions: { net: true } }, + async function httpConnAutoCloseDelayedOnUpgrade() { + const hostname = "localhost"; + const port = 4501; + + async function server() { + const listener = Deno.listen({ hostname, port }); + const tcpConn = await listener.accept(); + const httpConn = Deno.serveHttp(tcpConn); + + const event1 = await httpConn.nextRequest() as Deno.RequestEvent; + const event2Promise = httpConn.nextRequest(); + + const { socket, response } = Deno.upgradeWebSocket(event1.request); + socket.onmessage = (event) => socket.send(event.data); + event1.respondWith(response); + + const event2 = await event2Promise; + assertStrictEquals(event2, null); + + listener.close(); + } + + async function client() { + const socket = new WebSocket(`ws://${hostname}:${port}/`); + socket.onopen = () => socket.send("bla bla"); + const { data } = await new Promise((res) => socket.onmessage = res); + assertStrictEquals(data, "bla bla"); + socket.close(); + } + + await Promise.all([server(), client()]); + }, +); + unitTest( { permissions: { net: true } }, async function httpServerRespondNonAsciiUint8Array() { |