Files
bun.sh/test/js/node/http/node-http-proxy.js
Meghan Denny f4404a55db misc tidyings from another branch (#24406)
pulled out of https://github.com/oven-sh/bun/pull/21809

- brings the ASAN behavior on linux closer in sync with macos
- fixes some tests to also pass in node

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-11-05 15:28:28 -08:00

80 lines
2.1 KiB
JavaScript

import assert from "node:assert";
import { exampleSite } from "node-harness";
import { createServer, request } from "node:http";
export async function run() {
const { promise, resolve, reject } = Promise.withResolvers();
await using server = exampleSite("http");
const proxyServer = createServer(function (req, res) {
// Use URL object instead of deprecated url.parse
const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
const options = {
protocol: parsedUrl.protocol,
hostname: parsedUrl.hostname,
port: parsedUrl.port,
path: parsedUrl.pathname + parsedUrl.search,
method: req.method,
headers: req.headers,
};
const proxyRequest = request(options, function (proxyResponse) {
res.writeHead(proxyResponse.statusCode, proxyResponse.headers);
proxyResponse.pipe(res); // Use pipe instead of manual data handling
});
proxyRequest.on("error", error => {
console.error("Proxy Request Error:", error);
res.writeHead(500);
res.end("Proxy Error");
});
req.pipe(proxyRequest); // Use pipe instead of manual data handling
});
proxyServer.listen(0, "localhost", async () => {
const address = proxyServer.address();
const options = {
protocol: "http:",
hostname: "localhost",
port: address.port,
path: "/", // Change path to /
headers: {
Host: server.url.host,
"accept-encoding": "identity",
},
};
const req = request(options, res => {
let data = "";
res.on("data", chunk => {
data += chunk;
});
res.on("end", () => {
try {
assert.strictEqual(res.statusCode, 200);
assert(data.length > 0);
assert(data.includes("This domain is for use in illustrative examples in documents"));
resolve();
} catch (err) {
reject(err);
}
});
});
req.on("error", err => {
reject(err);
});
req.end();
});
await promise;
proxyServer.close();
}
if (import.meta.main) {
run().catch(console.error);
}