1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 10:09:20 -05:00
denoland-deno/cli/tests/testdata/run/045_proxy_test.ts

122 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-01-20 02:10:16 -05:00
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { Server } from "../../../../test_util/std/http/server.ts";
import { assertEquals } from "../../../../test_util/std/testing/asserts.ts";
2019-09-24 18:52:01 -04:00
const addr = Deno.args[1] || "localhost:4555";
2019-09-24 18:52:01 -04:00
async function proxyServer() {
const [hostname, p] = addr.split(":");
const port = parseInt(p ?? 4555);
const server = new Server({ hostname, port, handler });
2019-09-24 18:52:01 -04:00
console.log(`Proxy server listening on http://${addr}/`);
await server.listenAndServe();
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(new Uint8Array(await resp.arrayBuffer()), {
status: resp.status,
headers: resp.headers,
});
2019-09-24 18:52:01 -04:00
}
async function testFetch() {
const { code } = await new Deno.Command(Deno.execPath(), {
args: [
"run",
"--quiet",
"--reload",
"--allow-net",
"run/045_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
assertEquals(code, 0);
2019-09-24 18:52:01 -04:00
}
async function testModuleDownload() {
const { code } = 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
assertEquals(code, 0);
2019-09-24 18:52:01 -04:00
}
async function testFetchNoProxy() {
const { code } = await new Deno.Command(Deno.execPath(), {
args: [
"run",
"--quiet",
"--reload",
"--allow-net",
"run/045_proxy_client.ts",
],
env: {
HTTP_PROXY: "http://not.exising.proxy.server",
NO_PROXY: "localhost",
},
}).output();
assertEquals(code, 0);
}
async function testModuleDownloadNoProxy() {
const { code } = 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();
assertEquals(code, 0);
}
async function testFetchProgrammaticProxy() {
const { code } = await new Deno.Command(Deno.execPath(), {
args: [
"run",
"--quiet",
"--reload",
"--allow-net=localhost:4545,localhost:4555",
"--unstable",
"run/045_programmatic_proxy_client.ts",
],
}).output();
assertEquals(code, 0);
}
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);