diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2024-04-08 22:02:49 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-08 15:02:49 -0600 |
commit | d3b63bb315c7573974d8bd79dcfb6849cb29cf4e (patch) | |
tree | 6f216dc558642f06bd9439e1b696118eab4622b3 /tests/unit/http_test.ts | |
parent | cb12a9350332860971387e3a1fb40dc77fa992d3 (diff) |
fix(ext/http): Make `Deno.serveHttp()` work when proxying (#23269)
Closes https://github.com/denoland/deno/issues/21900
Diffstat (limited to 'tests/unit/http_test.ts')
-rw-r--r-- | tests/unit/http_test.ts | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/unit/http_test.ts b/tests/unit/http_test.ts index 108c43e40..607f2fc6e 100644 --- a/tests/unit/http_test.ts +++ b/tests/unit/http_test.ts @@ -2668,6 +2668,61 @@ Deno.test( }, ); +Deno.test("proxy with fetch", async () => { + const listener = Deno.listen({ port: listenPort }); + const deferred = Promise.withResolvers<void>(); + + const server = Deno.serve({ port: listenPort + 1 }, (_req) => { + return new Response("Hello world"); + }); + + let httpConn: Deno.HttpConn; + async function handleHttp(conn: Deno.Conn) { + httpConn = Deno.serveHttp(conn); + for await (const e of httpConn) { + await e.respondWith(serve(e.request)); + break; + } + } + + async function serve(req: Request) { + return await fetch(`http://localhost:${listenPort + 1}/`, req); + } + + const originServer = (async () => { + for await (const conn of listener) { + handleHttp(conn); + break; + } + })(); + + const proxiedRequest = (async () => { + const conn = await Deno.connect({ port: listenPort }); + const payload = new TextEncoder().encode( + "POST /api/sessions HTTP/1.1\x0d\x0aConnection: keep-alive\x0d\x0aContent-Length: 2\x0d\x0a\x0d\x0a{}", + ); + const n = await conn.write(payload); + assertEquals(n, 76); + const buf = new Uint8Array(1000); + const nread = await conn.read(buf); + assertEquals(nread, 150); + const respText = new TextDecoder().decode(buf); + assert(respText.includes("HTTP/1.1 200 OK")); + assert(respText.includes("content-type: text/plain;charset=UTF-8")); + assert(respText.includes("vary: Accept-Encoding")); + assert(respText.includes("content-length: 11")); + assert(respText.includes("Hello world")); + conn.close(); + deferred.resolve(); + })(); + await proxiedRequest; + await originServer; + await deferred.promise; + await server.shutdown(); + await server.finished; + httpConn!.close(); +}); + function chunkedBodyReader(h: Headers, r: BufReader): Deno.Reader { // Based on https://tools.ietf.org/html/rfc2616#section-19.4.6 const tp = new TextProtoReader(r); |