Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add "address()" on mock Socket #549

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/interceptors/ClientRequest/MockHttpSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class MockHttpSocket extends MockSocket {
*/
public passthrough(): void {
const socket = this.createConnection()
this.address = socket.address.bind(socket)

// Flush the buffered "socket.write()" calls onto
// the original socket instance (i.e. write request body).
Expand Down Expand Up @@ -205,6 +206,13 @@ export class MockHttpSocket extends MockSocket {
return
}

// Return fake address information for the socket.
this.address = () => ({
address: '127.0.0.1',
family: 'IPv4',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's any info in connectionOptions that we can use to set the family? 🤔

Yeah, there's .family and .host. Do you think we should respect those?

this.address = () => {
  return {
    address: this.connectionOptions?.host || '127.0.0.1',
    family: this.connectionOptions?.family || 'IPv6',
    port: this.connectionOptions?.port || 0
  }
}

We can also add automated tests for the cases when explicit connection options are set. Granted, this is likely better tested in the unit tests on MockHttpSocket. You can leave the existing http.* tests as-is, those are useful too!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the address info resolved in the socket. I didn't verfiy this, but the connectionOptions includes only the port.
Also, please note the host !== address.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Let's infer the port only for now then?

port: 0,
})

// First, emit all the connection events
// to emulate a successful connection.
this.mockConnect()
Expand Down Expand Up @@ -298,7 +306,7 @@ export class MockHttpSocket extends MockSocket {
}

private mockConnect(): void {
this.emit('lookup', null, '::1', 6, this.connectionOptions.host)
this.emit('lookup', null, '127.0.0.1', 4, this.connectionOptions.host)
Copy link
Member

@kettanaito kettanaito Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think setting this.address should be done in mockConnect() for mocked responses. This will couple the .address() function withe actual address we emit in the lookup even so those two stay consistent (right now, they are not).

const address = {
  host: '...',
  family: '...',
  port: '...'
}
this.address = () => address

this.emit('lookup', null, address.host, address.family, this.connectionOptions.host)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful! I'm ashamed that I didn't think about it myself.😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You think of a ton of things that never crossed my mind 🙌 The power of collaboration.

this.emit('connect')
this.emit('ready')

Expand Down
38 changes: 38 additions & 0 deletions test/modules/http/compliance/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,41 @@ it('mocks response to a non-existing host', async () => {
expect(await text()).toBe('howdy, john')
expect(requestListener).toHaveBeenCalledTimes(1)
})

it('returns mocked socket address', async () => {
interceptor.on('request', async ({ request }) => {
request.respondWith(new Response())
})

const connectPromise = new DeferredPromise<object>()
const request = http.get('http://example.com')
request.once('socket', socket => {
socket.once('connect', () => {
connectPromise.resolve(socket.address())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to also have a test for socket.address() before the connect is emitted. I believe it should still be set?

Copy link
Contributor Author

@mikicho mikicho Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the lookup happens after the socket event is emitted.

request.once('socket', socket => {
  console.log(socket.address()); // prints empty object {}
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is correct. ClientRequest emits socket as soon as the socket is assigned, then the socket goes through its life-cycle, emitting lookup and connect.

})
})

await expect(connectPromise).resolves.toEqual({
address: '127.0.0.1',
family: 'IPv4',
port: 0,
})
});


it('returns real socket address', async () => {
const connectPromise = new DeferredPromise<object>()
const request = http.get(httpServer.http.url('/user'))
request.once('socket', socket => {
socket.once('connect', () => {
connectPromise.resolve(socket.address())
})
})

await waitForClientRequest(request);
await expect(connectPromise).resolves.toEqual({
address: '127.0.0.1',
family: 'IPv4',
port: expect.any(Number),
})
});
Loading