2022-01-20 02:10:16 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2022-09-19 10:32:21 -04:00
|
|
|
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
|
|
|
|
2022-01-02 08:04:40 -05:00
|
|
|
const addr = Deno.args[1] || "localhost:4555";
|
2019-09-24 18:52:01 -04:00
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function proxyServer() {
|
2022-01-02 08:04:40 -05:00
|
|
|
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}/`);
|
2022-01-02 08:04:40 -05:00
|
|
|
await server.listenAndServe();
|
2019-09-24 18:52:01 -04:00
|
|
|
}
|
|
|
|
|
2022-01-02 08:04:40 -05:00
|
|
|
async function handler(req: Request): Promise<Response> {
|
2019-09-24 18:52:01 -04:00
|
|
|
console.log(`Proxy request to: ${req.url}`);
|
2022-01-02 08:04:40 -05:00
|
|
|
const headers = new Headers(req.headers);
|
|
|
|
const proxyAuthorization = headers.get("proxy-authorization");
|
2021-06-21 23:21:57 -04:00
|
|
|
if (proxyAuthorization) {
|
|
|
|
console.log(`proxy-authorization: ${proxyAuthorization}`);
|
2022-01-02 08:04:40 -05:00
|
|
|
headers.delete("proxy-authorization");
|
2021-06-21 23:21:57 -04:00
|
|
|
}
|
2019-09-24 18:52:01 -04:00
|
|
|
const resp = await fetch(req.url, {
|
|
|
|
method: req.method,
|
2022-01-02 08:04:40 -05:00
|
|
|
headers: headers,
|
2019-09-24 18:52:01 -04:00
|
|
|
});
|
2022-01-02 08:04:40 -05:00
|
|
|
return new Response(new Uint8Array(await resp.arrayBuffer()), {
|
2020-07-11 12:29:55 -04:00
|
|
|
status: resp.status,
|
|
|
|
headers: resp.headers,
|
|
|
|
});
|
2019-09-24 18:52:01 -04:00
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function testFetch() {
|
2022-12-02 08:43:17 -05:00
|
|
|
const { code } = await new Deno.Command(Deno.execPath(), {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: [
|
2020-07-11 12:29:55 -04:00
|
|
|
"run",
|
|
|
|
"--quiet",
|
|
|
|
"--reload",
|
|
|
|
"--allow-net",
|
2022-09-19 10:32:21 -04:00
|
|
|
"run/045_proxy_client.ts",
|
2020-07-11 12:29:55 -04:00
|
|
|
],
|
2019-09-24 18:52:01 -04:00
|
|
|
env: {
|
2020-03-28 13:03:49 -04:00
|
|
|
HTTP_PROXY: `http://${addr}`,
|
|
|
|
},
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2019-09-24 18:52:01 -04:00
|
|
|
|
2022-07-18 09:16:12 -04:00
|
|
|
assertEquals(code, 0);
|
2019-09-24 18:52:01 -04:00
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function testModuleDownload() {
|
2022-12-02 08:43:17 -05:00
|
|
|
const { code } = await new Deno.Command(Deno.execPath(), {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: [
|
2020-04-07 11:24:47 -04:00
|
|
|
"cache",
|
2020-07-11 12:29:55 -04:00
|
|
|
"--reload",
|
|
|
|
"--quiet",
|
2022-09-19 10:32:21 -04:00
|
|
|
"http://localhost:4545/run/045_mod.ts",
|
2019-09-24 18:52:01 -04:00
|
|
|
],
|
|
|
|
env: {
|
2020-03-28 13:03:49 -04:00
|
|
|
HTTP_PROXY: `http://${addr}`,
|
|
|
|
},
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2019-09-24 18:52:01 -04:00
|
|
|
|
2022-07-18 09:16:12 -04:00
|
|
|
assertEquals(code, 0);
|
2019-09-24 18:52:01 -04:00
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function testFetchNoProxy() {
|
2022-12-02 08:43:17 -05:00
|
|
|
const { code } = await new Deno.Command(Deno.execPath(), {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: [
|
2020-08-15 09:48:29 -04:00
|
|
|
"run",
|
|
|
|
"--quiet",
|
|
|
|
"--reload",
|
|
|
|
"--allow-net",
|
2022-09-19 10:32:21 -04:00
|
|
|
"run/045_proxy_client.ts",
|
2020-08-15 09:48:29 -04:00
|
|
|
],
|
|
|
|
env: {
|
|
|
|
HTTP_PROXY: "http://not.exising.proxy.server",
|
|
|
|
NO_PROXY: "localhost",
|
|
|
|
},
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2020-08-15 09:48:29 -04:00
|
|
|
|
2022-07-18 09:16:12 -04:00
|
|
|
assertEquals(code, 0);
|
2020-08-15 09:48:29 -04:00
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function testModuleDownloadNoProxy() {
|
2022-12-02 08:43:17 -05:00
|
|
|
const { code } = await new Deno.Command(Deno.execPath(), {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: [
|
2020-08-15 09:48:29 -04:00
|
|
|
"cache",
|
|
|
|
"--reload",
|
|
|
|
"--quiet",
|
2022-09-19 10:32:21 -04:00
|
|
|
"http://localhost:4545/run/045_mod.ts",
|
2020-08-15 09:48:29 -04:00
|
|
|
],
|
|
|
|
env: {
|
|
|
|
HTTP_PROXY: "http://not.exising.proxy.server",
|
|
|
|
NO_PROXY: "localhost",
|
|
|
|
},
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2020-08-15 09:48:29 -04:00
|
|
|
|
2022-07-18 09:16:12 -04:00
|
|
|
assertEquals(code, 0);
|
2020-08-15 09:48:29 -04:00
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
async function testFetchProgrammaticProxy() {
|
2022-12-02 08:43:17 -05:00
|
|
|
const { code } = await new Deno.Command(Deno.execPath(), {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: [
|
2021-06-21 23:21:57 -04:00
|
|
|
"run",
|
|
|
|
"--quiet",
|
|
|
|
"--reload",
|
|
|
|
"--allow-net=localhost:4545,localhost:4555",
|
|
|
|
"--unstable",
|
2022-09-19 10:32:21 -04:00
|
|
|
"run/045_programmatic_proxy_client.ts",
|
2021-06-21 23:21:57 -04:00
|
|
|
],
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assertEquals(code, 0);
|
2021-06-21 23:21:57 -04:00
|
|
|
}
|
|
|
|
|
2019-10-27 09:04:42 -04:00
|
|
|
proxyServer();
|
|
|
|
await testFetch();
|
|
|
|
await testModuleDownload();
|
2020-08-15 09:48:29 -04:00
|
|
|
await testFetchNoProxy();
|
|
|
|
await testModuleDownloadNoProxy();
|
2021-06-21 23:21:57 -04:00
|
|
|
await testFetchProgrammaticProxy();
|
2019-10-27 09:04:42 -04:00
|
|
|
Deno.exit(0);
|