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 1 commit
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
4 changes: 3 additions & 1 deletion src/interceptors/ClientRequest/MockHttpSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ export class MockHttpSocket extends MockSocket {
return
}

this.responseType = 'mock'
// First, emit all the connection events
// to emulate a successful connection.
this.mockConnect()
this.responseType = 'mock'

// Flush the write buffer to trigger write callbacks
// if it hasn't been flushed already (e.g. someone started reading request stream).
Expand Down Expand Up @@ -374,6 +374,7 @@ export class MockHttpSocket extends MockSocket {
credentials: 'same-origin',
// @ts-expect-error Undocumented Fetch property.
duplex: canHaveBody ? 'half' : undefined,
// @ts-ignore
body: canHaveBody ? (Readable.toWeb(this.requestStream!) as any) : null,
})

Expand Down Expand Up @@ -435,6 +436,7 @@ export class MockHttpSocket extends MockSocket {
}

const response = new Response(
// @ts-ignore
canHaveBody ? (Readable.toWeb(this.responseStream!) as any) : null,
{
status,
Expand Down
13 changes: 13 additions & 0 deletions src/interceptors/Socket/MockSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ export class MockSocket extends net.Socket {
return this
}

public address(): {} | net.AddressInfo {
// @ts-ignore
if (this.responseType === 'mock') {
return {
address: '::1', // should be this.connectionOptions.host?
family: 'IPv4',
port: 1,
}
}

return super.address()
Copy link
Member

Choose a reason for hiding this comment

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

This will have incorrect (empty?) value for bypass scenarios because MockHttpSocket has no connection information.

I proposed to infer this from the original socket but now I wonder, socket.address() is generally available throughout the socket's life-cycle, yes? Like, you can grab the address at any point in time after constructing the socket. Hmm.

}

public write(...args: Array<unknown>): boolean {
const [chunk, encoding, callback] = normalizeWriteArgs(args as WriteArgs)
this.options.write(chunk, encoding, callback)
Expand Down
37 changes: 37 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,40 @@ 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 req = http.get('http://example.com')
req.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: '::1',
family: 'IPv4',
port: 1,
})
});


it.only('returns real socket address', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Oops, accidental it.only committed! Sorry, don't have an eslint for this.

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

await expect(connectPromise).resolves.toEqual({
address: '::1',
family: 'IPv4',
port: 2,
})
});
Loading