diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-05-14 12:47:57 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-14 16:47:57 +0000 |
commit | 432792a46c71b402408e1bed0affdb1c3a90d8c6 (patch) | |
tree | 22d5f90349acc8388a28351bd95ddc5f9e952c88 /tests/specs/run/045_proxy/proxy_test.ts | |
parent | c6189e2070ac31006920e210e616c0d9dd159b7c (diff) |
chore: 045_proxy output stdout & stderr on failure (#23810)
Part of https://github.com/denoland/deno/issues/23624
Diffstat (limited to 'tests/specs/run/045_proxy/proxy_test.ts')
-rw-r--r-- | tests/specs/run/045_proxy/proxy_test.ts | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/tests/specs/run/045_proxy/proxy_test.ts b/tests/specs/run/045_proxy/proxy_test.ts new file mode 100644 index 000000000..d3386f0d7 --- /dev/null +++ b/tests/specs/run/045_proxy/proxy_test.ts @@ -0,0 +1,129 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +import { Server } from "../../../util/std/http/server.ts"; + +const addr = Deno.args[1] || "localhost:4555"; + +async function proxyServer() { + const [hostname, p] = addr.split(":"); + const port = parseInt(p ?? 4555); + const server = new Server({ hostname, port, handler }); + console.log(`Proxy server listening on http://${addr}/`); + await server.listenAndServe(); +} + +async function handler(req: Request): Promise<Response> { + console.log(`Proxy request to: ${req.url}`); + const headers = new Headers(req.headers); + const proxyAuthorization = headers.get("proxy-authorization"); + if (proxyAuthorization) { + console.log(`proxy-authorization: ${proxyAuthorization}`); + headers.delete("proxy-authorization"); + } + const resp = await fetch(req.url, { + method: req.method, + headers: headers, + }); + return new Response(new Uint8Array(await resp.arrayBuffer()), { + status: resp.status, + headers: resp.headers, + }); +} + +function assertSuccessOutput(output: Deno.CommandOutput) { + if (output.code !== 0) { + console.error("STDOUT", new TextDecoder().decode(output.stdout)); + console.error("STDERR", new TextDecoder().decode(output.stderr)); + throw new Error(`Expected exit code 0, was ${output.code}`); + } +} + +async function testFetch() { + const output = await new Deno.Command(Deno.execPath(), { + args: [ + "run", + "--quiet", + "--reload", + "--allow-net", + "proxy_client.ts", + ], + env: { + HTTP_PROXY: `http://${addr}`, + }, + }).output(); + + assertSuccessOutput(output); +} + +async function testModuleDownload() { + const output = await new Deno.Command(Deno.execPath(), { + args: [ + "cache", + "--reload", + "--quiet", + "http://localhost:4545/run/045_mod.ts", + ], + env: { + HTTP_PROXY: `http://${addr}`, + }, + }).output(); + + assertSuccessOutput(output); +} + +async function testFetchNoProxy() { + const output = await new Deno.Command(Deno.execPath(), { + args: [ + "run", + "--quiet", + "--reload", + "--allow-net", + "proxy_client.ts", + ], + env: { + HTTP_PROXY: "http://not.exising.proxy.server", + NO_PROXY: "localhost", + }, + }).output(); + + assertSuccessOutput(output); +} + +async function testModuleDownloadNoProxy() { + const output = await new Deno.Command(Deno.execPath(), { + args: [ + "cache", + "--reload", + "--quiet", + "http://localhost:4545/run/045_mod.ts", + ], + env: { + HTTP_PROXY: "http://not.exising.proxy.server", + NO_PROXY: "localhost", + }, + }).output(); + + assertSuccessOutput(output); +} + +async function testFetchProgrammaticProxy() { + const output = await new Deno.Command(Deno.execPath(), { + args: [ + "run", + "--quiet", + "--reload", + "--allow-net=localhost:4545,localhost:4555", + "--unstable", + "programmatic_proxy_client.ts", + ], + }).output(); + + assertSuccessOutput(output); +} + +proxyServer(); +await testFetch(); +await testModuleDownload(); +await testFetchNoProxy(); +await testModuleDownloadNoProxy(); +await testFetchProgrammaticProxy(); +Deno.exit(0); |