mswjs / interceptors

Low-level network interception library.
https://npm.im/@mswjs/interceptors
MIT License
535 stars 120 forks source link

should call callback before response event #459

Closed mikicho closed 1 month ago

mikicho commented 11 months ago

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.

kettanaito commented 10 months ago

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?

mikicho commented 8 months ago

Sorry for the incomplete description. I have updated it.