aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGravatar dave caruso <me@paperdave.net> 2023-09-29 03:39:26 -0700
committerGravatar GitHub <noreply@github.com> 2023-09-29 03:39:26 -0700
commit6afa78120ac0512bc55d00a8a1562d8f0eafa2c2 (patch)
tree6a413589452d966f42c1e97e39fd11a82308e565 /test
parent6514dcf4cbc63cff89f3cac59598872caf776f0b (diff)
downloadbun-6afa78120ac0512bc55d00a8a1562d8f0eafa2c2.tar.gz
bun-6afa78120ac0512bc55d00a8a1562d8f0eafa2c2.tar.zst
bun-6afa78120ac0512bc55d00a8a1562d8f0eafa2c2.zip
feat(runtime): implement `server.requestIp` + node:http `socket.address()` (#6165)
* [server] requestIp and AnyRequestContext Changed Request.uws_request to the new AnyRequestContext. This allows grabbing the IP from a Request. Unfinished. * [server] basic `requestIp` implementation Currently using uws's requestIpAsText, which always returns a ipv6 string. We should return a `SocketAddress` object to the user instead, which will contain the formatted address string and what type it is. We'll have to use requestIpAsBinary and parse that ourselves. * TypeScript docs, use `bun.String`, return `undefined` instead of `null` if we can't get the ip. * binary address formatting * uws getRemoteAddress binding * remove dead code * working * final touches:sparkles: * I will abide by the results of this poll. --------- Co-authored-by: Parzival-3141 <29632054+Parzival-3141@users.noreply.github.com>
Diffstat (limited to 'test')
-rw-r--r--test/js/bun/http/serve.test.ts36
1 files changed, 36 insertions, 0 deletions
diff --git a/test/js/bun/http/serve.test.ts b/test/js/bun/http/serve.test.ts
index c9fd47475..be271cf33 100644
--- a/test/js/bun/http/serve.test.ts
+++ b/test/js/bun/http/serve.test.ts
@@ -1212,3 +1212,39 @@ it("#5859 arrayBuffer", async () => {
await Bun.write("/tmp/bad", new Uint8Array([0xfd]));
expect(async () => await Bun.file("/tmp/bad").json()).toThrow();
});
+
+it("server.requestIP (v4)", async () => {
+ const server = Bun.serve({
+ port: 0,
+ fetch(req, server) {
+ return Response.json(server.requestIP(req));
+ },
+ hostname: "127.0.0.1",
+ });
+
+ const response = await fetch(`http://${server.hostname}:${server.port}`).then(x => x.json());
+ expect(response).toEqual({
+ address: "127.0.0.1",
+ family: "IPv4",
+ port: expect.any(Number),
+ });
+ server.stop(true);
+});
+
+it("server.requestIP (v6)", async () => {
+ const server = Bun.serve({
+ port: 0,
+ fetch(req, server) {
+ return Response.json(server.requestIP(req));
+ },
+ hostname: "0000:0000:0000:0000:0000:0000:0000:0001",
+ });
+
+ const response = await fetch(`http://localhost:${server.port}`).then(x => x.json());
+ expect(response).toEqual({
+ address: "0000:0000:0000:0000:0000:0000:0000:0001",
+ family: "IPv6",
+ port: expect.any(Number),
+ });
+ server.stop(true);
+});