summaryrefslogtreecommitdiff
path: root/cli/tests/045_proxy_test.ts
blob: 873d3a0c171ae4989e76aaf07d4b58562aff2b2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { serve, ServerRequest } from "../../std/http/server.ts";
import { assertEquals } from "../../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 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/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/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();
}

proxyServer();
await testFetch();
await testModuleDownload();
await testFetchNoProxy();
await testModuleDownloadNoProxy();
Deno.exit(0);