diff --git a/src/fetch/index.js b/src/fetch/index.js index f2abcdf..a6903e7 100644 --- a/src/fetch/index.js +++ b/src/fetch/index.js @@ -61,8 +61,8 @@ const fetch = async (ctx, url, options) => { const err = new AbortError('The operation was aborted.'); // cleanup request /* istanbul ignore else */ - if (req.body instanceof Readable) { - req.body.destroy(err); + if (req.init.body instanceof Readable) { + req.init.body.destroy(err); } throw err; } @@ -83,8 +83,8 @@ const fetch = async (ctx, url, options) => { }); } catch (err) { // cleanup request - if (body instanceof Readable) { - body.destroy(err); + if (initBody instanceof Readable) { + initBody.destroy(err); } /* istanbul ignore next */ if (err instanceof TypeError) { @@ -104,8 +104,8 @@ const fetch = async (ctx, url, options) => { const err = new AbortError('The operation was aborted.'); // cleanup request /* istanbul ignore else */ - if (req.body instanceof Readable) { - req.body.destroy(err); + if (req.init.body instanceof Readable) { + req.init.body.destroy(err); } // propagate error on response stream coreResp.readable.emit('error', err); diff --git a/test/fetch/index.test.js b/test/fetch/index.test.js index 0ffd012..35471e9 100644 --- a/test/fetch/index.test.js +++ b/test/fetch/index.test.js @@ -256,6 +256,22 @@ testParams.forEach((params) => { assert(body.destroyed); }); + it('AbortController works (POST with string body)', async () => { + const controller = new AbortController(); + setTimeout(() => controller.abort(), 5); + const { signal } = controller; + + const method = 'POST'; + const body = 'hello, world!'; + + try { + await fetch(`${baseUrl}/post`, { signal, method, body }); + assert.fail(); + } catch (err) { + assert(err instanceof AbortError); + } + }); + it('AbortController works (dripping response)', async function test() { this.timeout(5000);