0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-31 09:14:20 -04:00
denoland-deno/cli/tests/testdata/045_proxy_test.ts

146 lines
3.1 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import {
serve,
ServerRequest,
} from "../../../test_util/std/http/server_legacy.ts";
import { assertEquals } from "../../../test_util/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() {
2019-09-24 18:52:01 -04:00
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) {
2019-09-24 18:52:01 -04:00
console.log(`Proxy request to: ${req.url}`);
const proxyAuthorization = req.headers.get("proxy-authorization");
if (proxyAuthorization) {
console.log(`proxy-authorization: ${proxyAuthorization}`);
req.headers.delete("proxy-authorization");
}
2019-09-24 18:52:01 -04:00
const resp = await fetch(req.url, {
method: req.method,
headers: req.headers,
2019-09-24 18:52:01 -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() {
2019-09-24 18:52:01 -04:00
const c = Deno.run({
cmd: [
Deno.execPath(),
"run",
"--quiet",
"--reload",
"--allow-net",
"045_proxy_client.ts",
],
2019-09-24 18:52:01 -04:00
stdout: "piped",
env: {
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() {
2019-09-24 18:52:01 -04:00
const http = Deno.run({
cmd: [
2019-09-24 18:52:01 -04:00
Deno.execPath(),
"cache",
"--reload",
"--quiet",
"http://localhost:4545/045_mod.ts",
2019-09-24 18:52:01 -04:00
],
stdout: "piped",
env: {
HTTP_PROXY: `http://${addr}`,
},
2019-09-24 18:52:01 -04:00
});
const httpStatus = await http.status();
assertEquals(httpStatus.code, 0);
http.close();
}
async function testFetchNoProxy() {
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() {
const http = Deno.run({
cmd: [
Deno.execPath(),
"cache",
"--reload",
"--quiet",
"http://localhost:4545/045_mod.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();
}
async function testFetchProgrammaticProxy() {
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 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);