1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00
denoland-deno/tests/specs/run/045_proxy/proxy_test.ts

125 lines
2.9 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
const addr = Deno.args[1] || "localhost:4555";
2019-09-24 18:52:01 -04:00
function proxyServer() {
const [hostname, p] = addr.split(":");
const port = parseInt(p ?? 4555);
Deno.serve({ hostname, port }, handler);
2019-09-24 18:52:01 -04:00
}
async function handler(req: Request): Promise<Response> {
2019-09-24 18:52:01 -04:00
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");
}
2019-09-24 18:52:01 -04:00
const resp = await fetch(req.url, {
method: req.method,
headers: headers,
2019-09-24 18:52:01 -04:00
});
return new Response(await resp.bytes(), {
status: resp.status,
headers: resp.headers,
});
2019-09-24 18:52:01 -04:00
}
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",
],
2019-09-24 18:52:01 -04:00
env: {
HTTP_PROXY: `http://${addr}`,
},
}).output();
2019-09-24 18:52:01 -04:00
assertSuccessOutput(output);
2019-09-24 18:52:01 -04:00
}
async function testModuleDownload() {
const output = await new Deno.Command(Deno.execPath(), {
args: [
"cache",
"--reload",
"--quiet",
"http://localhost:4545/run/045_mod.ts",
2019-09-24 18:52:01 -04:00
],
env: {
HTTP_PROXY: `http://${addr}`,
},
}).output();
2019-09-24 18:52:01 -04:00
assertSuccessOutput(output);
2019-09-24 18:52:01 -04:00
}
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",
"programmatic_proxy_client.ts",
],
}).output();
assertSuccessOutput(output);
}
2019-10-27 09:04:42 -04:00
proxyServer();
await testFetch();
await testModuleDownload();
await testFetchNoProxy();
await testModuleDownloadNoProxy();
await testFetchProgrammaticProxy();
2019-10-27 09:04:42 -04:00
Deno.exit(0);