Skip to content

Commit 112ce0d

Browse files
bartlomiejury
authored andcommitted
test: add HTTP_PROXY tests (#2977)
1 parent a497f87 commit 112ce0d

File tree

6 files changed

+99
-5
lines changed

6 files changed

+99
-5
lines changed

cli/flags.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ pub struct DenoFlags {
6262

6363
static ENV_VARIABLES_HELP: &str = "ENVIRONMENT VARIABLES:
6464
DENO_DIR Set deno's base directory
65-
NO_COLOR Set to disable color";
65+
NO_COLOR Set to disable color
66+
HTTP_PROXY Set proxy address for HTTP requests (module downloads, fetch)
67+
HTTPS_PROXY Set proxy address for HTTPS requests (module downloads, fetch)";
6668

6769
fn add_run_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
6870
app

cli/tests/045_proxy_client.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
2+
async function main(): Promise<void> {
3+
const res = await fetch("http://deno.land/welcome.ts");
4+
console.log(`Response http: ${await res.text()}`);
5+
}
6+
7+
main();

cli/tests/045_proxy_test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
2+
import {
3+
serve,
4+
ServerRequest
5+
} from "../../js/deps/https/deno.land/std/http/server.ts";
6+
import { assertEquals } from "../../js/deps/https/deno.land/std/testing/asserts.ts";
7+
8+
const addr = Deno.args[1] || "127.0.0.1:4555";
9+
10+
async function proxyServer(): Promise<void> {
11+
const server = serve(addr);
12+
13+
console.log(`Proxy server listening on http://${addr}/`);
14+
for await (const req of server) {
15+
proxyRequest(req);
16+
}
17+
}
18+
19+
async function proxyRequest(req: ServerRequest): Promise<void> {
20+
console.log(`Proxy request to: ${req.url}`);
21+
const resp = await fetch(req.url, {
22+
method: req.method,
23+
headers: req.headers
24+
});
25+
req.respond(resp);
26+
}
27+
28+
async function testFetch(): Promise<void> {
29+
const c = Deno.run({
30+
args: [
31+
Deno.execPath(),
32+
"--no-prompt",
33+
"--reload",
34+
"--allow-net",
35+
"045_proxy_client.ts"
36+
],
37+
stdout: "piped",
38+
env: {
39+
HTTP_PROXY: `http://${addr}`
40+
}
41+
});
42+
43+
const status = await c.status();
44+
assertEquals(status.code, 0);
45+
c.close();
46+
}
47+
48+
async function testModuleDownload(): Promise<void> {
49+
const http = Deno.run({
50+
args: [
51+
Deno.execPath(),
52+
"--no-prompt",
53+
"--reload",
54+
"fetch",
55+
"http://deno.land/welcome.ts"
56+
],
57+
stdout: "piped",
58+
env: {
59+
HTTP_PROXY: `http://${addr}`
60+
}
61+
});
62+
63+
const httpStatus = await http.status();
64+
assertEquals(httpStatus.code, 0);
65+
http.close();
66+
}
67+
68+
async function main(): Promise<void> {
69+
proxyServer();
70+
await testFetch();
71+
await testModuleDownload();
72+
Deno.exit(0);
73+
}
74+
75+
main();

cli/tests/045_proxy_test.ts.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Proxy server listening on [WILDCARD]
2+
Proxy request to: http://deno.land/welcome.ts
3+
Proxy request to: http://deno.land/welcome.ts

cli/tests/integration_tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ itest!(_044_bad_resource {
317317
exit_code: 1,
318318
});
319319

320+
itest!(_045_proxy {
321+
args: "run --allow-net --allow-env --allow-run --reload 045_proxy_test.ts",
322+
output: "045_proxy_test.ts.out",
323+
});
324+
320325
itest!(async_error {
321326
exit_code: 1,
322327
args: "run --reload async_error.ts",

website/manual.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,8 @@ SUBCOMMANDS:
712712
ENVIRONMENT VARIABLES:
713713
DENO_DIR Set deno's base directory
714714
NO_COLOR Set to disable color
715+
HTTP_PROXY Set proxy address for HTTP requests (module downloads, fetch)
716+
HTTPS_PROXY Set proxy address for HTTPS requests (module downloads, fetch)
715717
```
716718

717719
### Environmental variables
@@ -876,12 +878,12 @@ $ deno install awesome_cli https://example.com/awesome/cli.ts
876878

877879
## Proxies
878880

879-
Deno supports proxies.
881+
Deno supports proxies for module downloads and `fetch` API.
880882

881-
`HTTP_PROXY` and `HTTPS_PROXY` environmental variables are used to configure
882-
them.
883+
Proxy configuration is read from environmental variables: `HTTP_PROXY` and
884+
`HTTPS_PROXY`.
883885

884-
For Windows if environmental variables are not found Deno will fall back to
886+
In case of Windows if environmental variables are not found Deno falls back to
885887
reading proxies from registry.
886888

887889
## Import maps

0 commit comments

Comments
 (0)