aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/proxy.test.js
diff options
context:
space:
mode:
authorGravatar Ciro Spaciari <ciro.spaciari@gmail.com> 2023-01-23 19:38:40 -0300
committerGravatar GitHub <noreply@github.com> 2023-01-23 14:38:40 -0800
commitefd33c398f2bf16ef865b5b04df4d40b13ed24a4 (patch)
tree12778c39ce63dce6ffa7461d726eeae4826ae471 /test/bun.js/proxy.test.js
parent9a2b586337ed2807c586821ce2402513d9d35b74 (diff)
downloadbun-efd33c398f2bf16ef865b5b04df4d40b13ed24a4.tar.gz
bun-efd33c398f2bf16ef865b5b04df4d40b13ed24a4.tar.zst
bun-efd33c398f2bf16ef865b5b04df4d40b13ed24a4.zip
enhancement(fetch): Merge parameters from request parameter with the second parameter for fetch, move verbose and proxy options to second parameter, add non-TLS tests for fetch (#1862)
* initial steps for proxy-server * added http_proxy in fetch, move 3rd argument to 3nd argument options, add some non-TLS proxy tests * some changes * use only 1 buffer for url+proxy, merge headers on fetch * initial steps * change back to override headers instead of merging in fetch * fix build response.zig * fix conditional in merged headers on fetch * updated with main and make proxy disabled if null is passed Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'test/bun.js/proxy.test.js')
-rw-r--r--test/bun.js/proxy.test.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/test/bun.js/proxy.test.js b/test/bun.js/proxy.test.js
new file mode 100644
index 000000000..32b21f7e8
--- /dev/null
+++ b/test/bun.js/proxy.test.js
@@ -0,0 +1,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");
+ });
+ }
+}); \ No newline at end of file