From e799c2857c81f7656811aaf6a248432fe88e8169 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Thu, 5 Sep 2024 14:13:06 +0900 Subject: fix(ext/http): do not set localhost to hostname unnecessarily (#24777) This commit changes when to cause the hostname substition of `0.0.0.0` -> `localhost`. Currently we substitute `localhost` to the hostname on windows before calling `options.onListen`, which prevents the users to do more advanced thing using hostname string like https://github.com/denoland/std/issues/5558. This PR changes it not to substitute it when the user provide `onListen` callback. closes #24776 unblocks https://github.com/denoland/std/issues/5558 --- tests/unit/serve_test.ts | 16 ++++++++++++++++ tests/unit_node/net_test.ts | 1 + 2 files changed, 17 insertions(+) (limited to 'tests') diff --git a/tests/unit/serve_test.ts b/tests/unit/serve_test.ts index fde6c7bee..19a8ccad1 100644 --- a/tests/unit/serve_test.ts +++ b/tests/unit/serve_test.ts @@ -4191,3 +4191,19 @@ Deno.test({ 'Operation `"op_net_listen_unix"` not supported on non-unix platforms.', ); }); + +Deno.test({ + name: "onListen callback gets 0.0.0.0 hostname as is", +}, async () => { + const { promise, resolve } = Promise.withResolvers<{ hostname: string }>(); + + const server = Deno.serve({ + handler: (_) => new Response("ok"), + hostname: "0.0.0.0", + port: 0, + onListen: resolve, + }); + const { hostname } = await promise; + assertEquals(hostname, "0.0.0.0"); + await server.shutdown(); +}); diff --git a/tests/unit_node/net_test.ts b/tests/unit_node/net_test.ts index f49ff0ef0..708d06386 100644 --- a/tests/unit_node/net_test.ts +++ b/tests/unit_node/net_test.ts @@ -79,6 +79,7 @@ Deno.test("[node/net] net.connect().unref() works", async () => { port: 0, // any available port will do handler: () => new Response("hello"), onListen: async ({ port, hostname }) => { + hostname = Deno.build.os === "windows" ? "localhost" : hostname; const { stdout, stderr } = await new Deno.Command(Deno.execPath(), { args: [ "eval", -- cgit v1.2.3