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