summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorTomofumi Chiba <tomofumi.chiba@gmail.com>2021-06-22 12:21:57 +0900
committerGitHub <noreply@github.com>2021-06-22 05:21:57 +0200
commit4f1b1903cfadeeba24e1b0448879fe12682effb9 (patch)
tree5adf8bd7af8b86052ef555ca7f6ae221515baec0 /cli
parent580c9f9ef02f8e8226437137867d3edeb9241b5e (diff)
feat(fetch): add programmatic proxy (#10907)
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.
Diffstat (limited to 'cli')
-rw-r--r--cli/dts/lib.deno.unstable.d.ts18
-rw-r--r--cli/tests/045_programmatic_proxy_client.ts16
-rw-r--r--cli/tests/045_proxy_test.ts24
-rw-r--r--cli/tests/045_proxy_test.ts.out2
4 files changed, 59 insertions, 1 deletions
diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts
index 828211c5b..1d01a748e 100644
--- a/cli/dts/lib.deno.unstable.d.ts
+++ b/cli/dts/lib.deno.unstable.d.ts
@@ -1089,6 +1089,17 @@ declare namespace Deno {
/** A certificate authority to use when validating TLS certificates. Certificate data must be PEM encoded.
*/
caData?: string;
+ proxy?: Proxy;
+ }
+
+ export interface Proxy {
+ url: string;
+ basicAuth?: BasicAuth;
+ }
+
+ export interface BasicAuth {
+ username: string;
+ password: string;
}
/** **UNSTABLE**: New API, yet to be vetted.
@@ -1096,7 +1107,12 @@ declare namespace Deno {
*
* ```ts
* const client = Deno.createHttpClient({ caData: await Deno.readTextFile("./ca.pem") });
- * const req = await fetch("https://myserver.com", { client });
+ * const response = await fetch("https://myserver.com", { client });
+ * ```
+ *
+ * ```ts
+ * const client = Deno.createHttpClient({ proxy: { url: "http://myproxy.com:8080" } });
+ * const response = await fetch("https://myserver.com", { client });
* ```
*/
export function createHttpClient(
diff --git a/cli/tests/045_programmatic_proxy_client.ts b/cli/tests/045_programmatic_proxy_client.ts
new file mode 100644
index 000000000..50884407d
--- /dev/null
+++ b/cli/tests/045_programmatic_proxy_client.ts
@@ -0,0 +1,16 @@
+// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+
+const client = Deno.createHttpClient({
+ proxy: {
+ url: "http://localhost:4555",
+ basicAuth: { username: "username", password: "password" },
+ },
+});
+
+const res = await fetch(
+ "http://localhost:4545/test_util/std/examples/colors.ts",
+ { client },
+);
+console.log(`Response http: ${await res.text()}`);
+
+client.close();
diff --git a/cli/tests/045_proxy_test.ts b/cli/tests/045_proxy_test.ts
index c7ba5e967..6e338f4fc 100644
--- a/cli/tests/045_proxy_test.ts
+++ b/cli/tests/045_proxy_test.ts
@@ -15,6 +15,11 @@ async function proxyServer(): Promise<void> {
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,
@@ -110,9 +115,28 @@ async function testModuleDownloadNoProxy(): Promise<void> {
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);
diff --git a/cli/tests/045_proxy_test.ts.out b/cli/tests/045_proxy_test.ts.out
index 4b07438ec..4957c9307 100644
--- a/cli/tests/045_proxy_test.ts.out
+++ b/cli/tests/045_proxy_test.ts.out
@@ -2,3 +2,5 @@ Proxy server listening on [WILDCARD]
Proxy request to: http://localhost:4545/test_util/std/examples/colors.ts
Proxy request to: http://localhost:4545/test_util/std/examples/colors.ts
Proxy request to: http://localhost:4545/test_util/std/fmt/colors.ts
+Proxy request to: http://localhost:4545/test_util/std/examples/colors.ts
+proxy-authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=