aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/proxy.test.js
blob: 32b21f7e876b4c48565516da5c42f26b36bcbf48 (plain) (blame)
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
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
import { gc } from "./gc";

let proxy, server;

// TODO: Proxy with TLS requests

beforeAll(()=> {
  proxy = Bun.serve({
    async fetch(request) {
      // if is not an proxy connection just drop it
      if (!request.headers.has("proxy-connection")) {
        return new Response("Bad Request", { status: 400 });
      }
      
      // simple http proxy
      if (request.url.startsWith("http://")) {
        return await fetch(request.url, { method: request.method, body: await request.text() });
      }
      
      // no TLS support here
      return new Response("Bad Request", { status: 400 });
  
    },
    port: 54321,
  });  
  server = Bun.serve({
    async fetch(request) {
      if (request.method === "POST"){
        const text = await request.text();
        return new Response(text,{ status: 200 });
      }
        return new Response("Hello, World",{ status: 200 });
    },
    port: 54322,
  });  
});

afterAll(() => {
  server.stop();
  proxy.stop();
});

describe("proxy", () => {
  const requests = [
    [ new Request("http://localhost:54322"), "fetch() GET with non-TLS Proxy", "http://localhost:54321"],
    [ new Request("http://localhost:54322", { method: "POST", body: "Hello, World" }), "fetch() POST with non-TLS Proxy", "http://localhost:54321"]
  ];
  for (let [ request, name, proxy ] of requests) {
    gc();
    it(name, async () => {
      gc();
      const response = await fetch(request, { verbose: true, proxy });
      gc();
      const text = await response.text();
      gc();
      expect(text).toBe("Hello, World");
    });
  }
});