mirror of
https://github.com/denoland/deno.git
synced 2024-11-15 16:43:44 -05:00
4f1b1903cf
This commit adds new options to unstable "Deno.createHttpClient" API. "proxy" and "basicAuth" options were added that allow to use custom proxy when client instance is passed to "fetch" API.
142 lines
3.2 KiB
TypeScript
142 lines
3.2 KiB
TypeScript
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
import { serve, ServerRequest } from "../../test_util/std/http/server.ts";
|
|
import { assertEquals } from "../../test_util/std/testing/asserts.ts";
|
|
|
|
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 proxyAuthorization = req.headers.get("proxy-authorization");
|
|
if (proxyAuthorization) {
|
|
console.log(`proxy-authorization: ${proxyAuthorization}`);
|
|
req.headers.delete("proxy-authorization");
|
|
}
|
|
const resp = await fetch(req.url, {
|
|
method: req.method,
|
|
headers: req.headers,
|
|
});
|
|
req.respond({
|
|
status: resp.status,
|
|
body: new Uint8Array(await resp.arrayBuffer()),
|
|
headers: resp.headers,
|
|
});
|
|
}
|
|
|
|
async function testFetch(): Promise<void> {
|
|
const c = Deno.run({
|
|
cmd: [
|
|
Deno.execPath(),
|
|
"run",
|
|
"--quiet",
|
|
"--reload",
|
|
"--allow-net",
|
|
"045_proxy_client.ts",
|
|
],
|
|
stdout: "piped",
|
|
env: {
|
|
HTTP_PROXY: `http://${addr}`,
|
|
},
|
|
});
|
|
|
|
const status = await c.status();
|
|
assertEquals(status.code, 0);
|
|
c.close();
|
|
}
|
|
|
|
async function testModuleDownload(): Promise<void> {
|
|
const http = Deno.run({
|
|
cmd: [
|
|
Deno.execPath(),
|
|
"cache",
|
|
"--reload",
|
|
"--quiet",
|
|
"http://localhost:4545/test_util/std/examples/colors.ts",
|
|
],
|
|
stdout: "piped",
|
|
env: {
|
|
HTTP_PROXY: `http://${addr}`,
|
|
},
|
|
});
|
|
|
|
const httpStatus = await http.status();
|
|
assertEquals(httpStatus.code, 0);
|
|
http.close();
|
|
}
|
|
|
|
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/test_util/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();
|
|
}
|
|
|
|
async function testFetchProgrammaticProxy(): Promise<void> {
|
|
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();
|
|
}
|
|
|
|
proxyServer();
|
|
await testFetch();
|
|
await testModuleDownload();
|
|
await testFetchNoProxy();
|
|
await testModuleDownloadNoProxy();
|
|
await testFetchProgrammaticProxy();
|
|
Deno.exit(0);
|