interceptors icon indicating copy to clipboard operation
interceptors copied to clipboard

should call callback before response event

Open mikicho opened this issue 1 year ago • 2 comments

Node.js behaviour:

const http = require('http');

const req = http.get('http://nowhere.com/', res => {
  console.log('response event');
})
req.on('finish', () => console.log('finish'))
req.end('', null, () => console.log('callback'))

This prints:

finish callback response event

Mocked behavior:

const { ClientRequestInterceptor } = require('@mswjs/interceptors/ClientRequest')
const http = require('http');

const interceptor = new ClientRequestInterceptor({
  name: 'my-interceptor',
})
interceptor.apply();
interceptor.on('request', async ({request}) => {
  request.respondWith(new Response())
});

const req = http.request('http://nowhere.com/', res => {
  console.log('response event');
})
req.on('finish', () => console.log('finish'))
req.end('', null, () => console.log('callback'))

Prints:

finish response event callback

I can open a PR that pass the callback to the respondWIth and put it after the finish event.

mikicho avatar Sep 29 '23 17:09 mikicho

Hi, @mikicho. Can you please describe the expected behavior here? Is this the callback order that you find wrong? That seems to be correct (in your example). Are we not printing some of this? I suppose the .end() callback never gets called for bypassed requests?

kettanaito avatar Oct 07 '23 17:10 kettanaito

Sorry for the incomplete description. I have updated it.

mikicho avatar Dec 22 '23 12:12 mikicho