Skip to content

test: add proxy tests #2977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cli/tests/045_proxy_client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
async function main(): Promise<void> {
const res = await fetch("http://deno.land/welcome.ts");
console.log(`Response http: ${await res.text()}`);
}

main();
75 changes: 75 additions & 0 deletions cli/tests/045_proxy_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import {
serve,
ServerRequest
} from "../../js/deps/https/deno.land/std/http/server.ts";
import { assertEquals } from "../../js/deps/https/deno.land/std/testing/asserts.ts";

const addr = Deno.args[1] || "0.0.0.0: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,
headers: req.headers
});
req.respond(resp);
}

async function testFetch(): Promise<void> {
const c = Deno.run({
args: [
Deno.execPath(),
"--no-prompt",
"--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({
args: [
Deno.execPath(),
"--no-prompt",
"--reload",
"fetch",
"http://deno.land/welcome.ts"
],
stdout: "piped",
env: {
HTTP_PROXY: `http://${addr}`
}
});

const httpStatus = await http.status();
assertEquals(httpStatus.code, 0);
http.close();
}

async function main(): Promise<void> {
proxyServer();
await testFetch();
await testModuleDownload();
Deno.exit(0);
}

main();
3 changes: 3 additions & 0 deletions cli/tests/045_proxy_test.ts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Proxy server listening on [WILDCARD]
Proxy request to: http://deno.land/welcome.ts
Proxy request to: http://deno.land/welcome.ts
5 changes: 5 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ itest!(_042_dyn_import_evalcontext {
output: "042_dyn_import_evalcontext.ts.out",
});

itest!(_045_proxy {
args: "run --allow-net --allow-env --allow-run --reload 045_proxy_test.ts",
output: "045_proxy_test.ts.out",
});

itest!(async_error {
exit_code: 1,
args: "run --reload async_error.ts",
Expand Down
8 changes: 4 additions & 4 deletions website/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -876,12 +876,12 @@ $ deno install awesome_cli https://example.com/awesome/cli.ts

## Proxies

Deno supports proxies.
Deno supports proxies for module downloads and `fetch` API.

`HTTP_PROXY` and `HTTPS_PROXY` environmental variables are used to configure
them.
Proxy configuration is read from environmental variables: `HTTP_PROXY` and
`HTTPS_PROXY`.

For Windows if environmental variables are not found Deno will fall back to
In case of Windows if environmental variables are not found Deno falls back to
reading proxies from registry.

## Import maps
Expand Down