2022-01-20 16:10:16 +09:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2022-01-02 14:04:40 +01:00
|
|
|
import { Server } from "../../../test_util/std/http/server.ts";
|
2021-08-11 10:20:47 -04:00
|
|
|
import { assertEquals } from "../../../test_util/std/testing/asserts.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() {
|
2019-09-25 00:52:01 +02:00
|
|
|
const c = Deno.run({
|
2020-07-11 18:29:55 +02:00
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"run",
|
|
|
|
"--quiet",
|
|
|
|
"--reload",
|
|
|
|
"--allow-net",
|
|
|
|
"045_proxy_client.ts",
|
|
|
|
],
|
2019-09-25 00:52:01 +02:00
|
|
|
stdout: "piped",
|
|
|
|
env: {
|
2020-03-29 04:03:49 +11:00
|
|
|
HTTP_PROXY: `http://${addr}`,
|
|
|
|
},
|
2019-09-25 00:52:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const status = await c.status();
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
2021-08-05 13:08:58 +02:00
|
|
|
async function testModuleDownload() {
|
2019-09-25 00:52:01 +02:00
|
|
|
const http = Deno.run({
|
2020-03-22 03:14:18 +05:30
|
|
|
cmd: [
|
2019-09-25 00:52:01 +02:00
|
|
|
Deno.execPath(),
|
2020-04-07 20:54:47 +05:30
|
|
|
"cache",
|
2020-07-11 18:29:55 +02:00
|
|
|
"--reload",
|
|
|
|
"--quiet",
|
2021-08-11 10:20:47 -04:00
|
|
|
"http://localhost:4545/045_mod.ts",
|
2019-09-25 00:52:01 +02:00
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
env: {
|
2020-03-29 04:03:49 +11:00
|
|
|
HTTP_PROXY: `http://${addr}`,
|
|
|
|
},
|
2019-09-25 00:52:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const httpStatus = await http.status();
|
|
|
|
assertEquals(httpStatus.code, 0);
|
|
|
|
http.close();
|
|
|
|
}
|
|
|
|
|
2021-08-05 13:08:58 +02:00
|
|
|
async function testFetchNoProxy() {
|
2020-08-15 22:48:29 +09:00
|
|
|
const c = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"run",
|
|
|
|
"--quiet",
|
|
|
|
"--reload",
|
|
|
|
"--allow-net",
|
|
|
|
"045_proxy_client.ts",
|
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
env: {
|
|
|
|
HTTP_PROXY: "http://not.exising.proxy.server",
|
|
|
|
NO_PROXY: "localhost",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const status = await c.status();
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
2021-08-05 13:08:58 +02:00
|
|
|
async function testModuleDownloadNoProxy() {
|
2020-08-15 22:48:29 +09:00
|
|
|
const http = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"cache",
|
|
|
|
"--reload",
|
|
|
|
"--quiet",
|
2021-08-11 10:20:47 -04:00
|
|
|
"http://localhost:4545/045_mod.ts",
|
2020-08-15 22:48:29 +09:00
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
env: {
|
|
|
|
HTTP_PROXY: "http://not.exising.proxy.server",
|
|
|
|
NO_PROXY: "localhost",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const httpStatus = await http.status();
|
|
|
|
assertEquals(httpStatus.code, 0);
|
|
|
|
http.close();
|
|
|
|
}
|
|
|
|
|
2021-08-05 13:08:58 +02:00
|
|
|
async function testFetchProgrammaticProxy() {
|
2021-06-22 12:21:57 +09:00
|
|
|
const c = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"run",
|
|
|
|
"--quiet",
|
|
|
|
"--reload",
|
|
|
|
"--allow-net=localhost:4545,localhost:4555",
|
|
|
|
"--unstable",
|
|
|
|
"045_programmatic_proxy_client.ts",
|
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
});
|
|
|
|
const status = await c.status();
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
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);
|