interceptors icon indicating copy to clipboard operation
interceptors copied to clipboard

GZIP doesn't work with got

Open mikicho opened this issue 2 years ago • 2 comments

const { ClientRequestInterceptor } = require('@mswjs/interceptors/ClientRequest')
const got = require('got')
const zlib = require('zlib')

const interceptor = new ClientRequestInterceptor({
  name: 'my-interceptor',
})
interceptor.apply();
interceptor.on('request', ({ request }) => {
  const compressed = zlib.gzipSync('hello')
  request.respondWith(new Response(compressed, {
    status: 200, headers: {
      'X-Transfer-Length': String(compressed.length),
      'Content-Length': undefined,
      'Content-Encoding': 'gzip',
    }
  }))
});

(async function () {
  console.log(1);
  console.log(await got('http://example.test/foo'))
  console.log(2);
})()

Specifically, got exits in this line with error code 0 (ok). I couldn't catch the error (even with a try-catch), so I'm unsure what's happening.

It does work with native http.

May be related to: #308

mikicho avatar Sep 24 '23 10:09 mikicho

I investigated it further. It seems like we don't emit a readable event. The for await (const chunk of stream) in got waits for a readable event, which is never emitted. I suspect it is connected to the response clones, or maybe the ReadableStream in the createResponse

mikicho avatar Sep 28 '23 18:09 mikicho

This should be fixed by #604.

kettanaito avatar Aug 13 '24 14:08 kettanaito