fix(node/http): remoteAddress and remotePort not being set (#21998)

<!--
Before submitting a PR, please read https://deno.com/manual/contributing

1. Give the PR a descriptive title.

  Examples of good title:
    - fix(std/http): Fix race condition in server
    - docs(console): Update docstrings
    - feat(doc): Handle nested reexports

  Examples of bad title:
    - fix #7123
    - update docs
    - fix bugs

2. Ensure there is a related issue and it is referenced in the PR text.
3. Ensure there are tests that cover the changes.
4. Ensure `cargo test` passes.
5. Ensure `./tools/format.js` passes without changing files.
6. Ensure `./tools/lint.js` passes.
7. Open as a draft PR if your work is still in progress. The CI won't
run
   all steps, but you can add '[ci]' to a commit message to force it to.
8. If you would like to run the benchmarks on the CI, add the 'ci-bench'
label.
-->

Ultimately, it came down to using incorrect property names in the
`FakeSocket` constructor. The values are passed under `remoteAddress`,
not `hostname` and `remotePort` instead of `port`.

Fixes https://github.com/denoland/deno/issues/21980
This commit is contained in:
Marvin Hagemeister 2024-01-19 13:09:37 +01:00 committed by GitHub
parent c62615bfe5
commit 59f419bf41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 3 deletions

View file

@ -184,6 +184,30 @@ Deno.test("[node/http] server can respond with 101, 204, 205, 304 status", async
}
});
Deno.test("[node/http] IncomingRequest socket has remoteAddress + remotePort", async () => {
const { promise, resolve } = Promise.withResolvers<void>();
let remoteAddress: string | undefined;
let remotePort: number | undefined;
const server = http.createServer((req, res) => {
remoteAddress = req.socket.remoteAddress;
remotePort = req.socket.remotePort;
res.end();
});
server.listen(async () => {
// deno-lint-ignore no-explicit-any
const port = (server.address() as any).port;
const res = await fetch(
`http://127.0.0.1:${port}/`,
);
await res.arrayBuffer();
assertEquals(remoteAddress, "127.0.0.1");
assertEquals(typeof remotePort, "number");
server.close(() => resolve());
});
await promise;
});
Deno.test("[node/http] request default protocol", async () => {
const deferred1 = Promise.withResolvers<void>();
const deferred2 = Promise.withResolvers<void>();

View file

@ -283,10 +283,16 @@ const kError = Symbol("kError");
const kUniqueHeaders = Symbol("kUniqueHeaders");
class FakeSocket extends EventEmitter {
constructor(opts = {}) {
constructor(
opts: {
encrypted?: boolean | undefined;
remotePort?: number | undefined;
remoteAddress?: string | undefined;
} = {},
) {
super();
this.remoteAddress = opts.hostname;
this.remotePort = opts.port;
this.remoteAddress = opts.remoteAddress;
this.remotePort = opts.remotePort;
this.encrypted = opts.encrypted;
this.writable = true;
this.readable = true;