2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2019-10-04 14:49:32 -04:00
|
|
|
import { serve, ServerRequest } from "../../std/http/server.ts";
|
|
|
|
import { assertEquals } from "../../std/testing/asserts.ts";
|
2019-09-24 18:52:01 -04:00
|
|
|
|
|
|
|
const addr = Deno.args[1] || "127.0.0.1:4555";
|
|
|
|
|
|
|
|
async function proxyServer(): Promise<void> {
|
|
|
|
const server = serve(addr);
|
|
|
|
|
|
|
|
console.log(`Proxy server listening on http://${addr}/`);
|
|
|
|
for await (const req of server) {
|
|
|
|
proxyRequest(req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function proxyRequest(req: ServerRequest): Promise<void> {
|
|
|
|
console.log(`Proxy request to: ${req.url}`);
|
|
|
|
const resp = await fetch(req.url, {
|
|
|
|
method: req.method,
|
2020-03-28 13:03:49 -04:00
|
|
|
headers: req.headers,
|
2019-09-24 18:52:01 -04:00
|
|
|
});
|
2020-07-11 12:29:55 -04:00
|
|
|
req.respond({
|
|
|
|
status: resp.status,
|
|
|
|
body: new Uint8Array(await resp.arrayBuffer()),
|
|
|
|
headers: resp.headers,
|
|
|
|
});
|
2019-09-24 18:52:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function testFetch(): Promise<void> {
|
|
|
|
const c = Deno.run({
|
2020-07-11 12:29:55 -04:00
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"run",
|
|
|
|
"--quiet",
|
|
|
|
"--reload",
|
|
|
|
"--allow-net",
|
|
|
|
"045_proxy_client.ts",
|
|
|
|
],
|
2019-09-24 18:52:01 -04:00
|
|
|
stdout: "piped",
|
|
|
|
env: {
|
2020-03-28 13:03:49 -04:00
|
|
|
HTTP_PROXY: `http://${addr}`,
|
|
|
|
},
|
2019-09-24 18:52:01 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const status = await c.status();
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function testModuleDownload(): Promise<void> {
|
|
|
|
const http = Deno.run({
|
2020-03-21 17:44:18 -04:00
|
|
|
cmd: [
|
2019-09-24 18:52:01 -04:00
|
|
|
Deno.execPath(),
|
2020-04-07 11:24:47 -04:00
|
|
|
"cache",
|
2020-07-11 12:29:55 -04:00
|
|
|
"--reload",
|
|
|
|
"--quiet",
|
2020-03-28 13:03:49 -04:00
|
|
|
"http://localhost:4545/std/examples/colors.ts",
|
2019-09-24 18:52:01 -04:00
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
env: {
|
2020-03-28 13:03:49 -04:00
|
|
|
HTTP_PROXY: `http://${addr}`,
|
|
|
|
},
|
2019-09-24 18:52:01 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const httpStatus = await http.status();
|
|
|
|
assertEquals(httpStatus.code, 0);
|
|
|
|
http.close();
|
|
|
|
}
|
|
|
|
|
2020-08-15 09:48:29 -04:00
|
|
|
async function testFetchNoProxy(): Promise<void> {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function testModuleDownloadNoProxy(): Promise<void> {
|
|
|
|
const http = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"cache",
|
|
|
|
"--reload",
|
|
|
|
"--quiet",
|
|
|
|
"http://localhost:4545/std/examples/colors.ts",
|
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
env: {
|
|
|
|
HTTP_PROXY: "http://not.exising.proxy.server",
|
|
|
|
NO_PROXY: "localhost",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const httpStatus = await http.status();
|
|
|
|
assertEquals(httpStatus.code, 0);
|
|
|
|
http.close();
|
|
|
|
}
|
|
|
|
|
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();
|
2019-10-27 09:04:42 -04:00
|
|
|
Deno.exit(0);
|