mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +03:00
Adding aborted event handler (#3916)
* Adding test of aborts request * Adding aborted event handler * Fixing timing of setting rejected flag Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -60,8 +60,10 @@ module.exports = function httpAdapter(config) {
|
|||||||
done();
|
done();
|
||||||
resolvePromise(value);
|
resolvePromise(value);
|
||||||
};
|
};
|
||||||
|
var rejected = false;
|
||||||
var reject = function reject(value) {
|
var reject = function reject(value) {
|
||||||
done();
|
done();
|
||||||
|
rejected = true;
|
||||||
rejectPromise(value);
|
rejectPromise(value);
|
||||||
};
|
};
|
||||||
var data = config.data;
|
var data = config.data;
|
||||||
@@ -269,12 +271,22 @@ module.exports = function httpAdapter(config) {
|
|||||||
|
|
||||||
// make sure the content length is not over the maxContentLength if specified
|
// make sure the content length is not over the maxContentLength if specified
|
||||||
if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) {
|
if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) {
|
||||||
|
// stream.destoy() emit aborted event before calling reject() on Node.js v16
|
||||||
|
rejected = true;
|
||||||
stream.destroy();
|
stream.destroy();
|
||||||
reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
|
reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
|
||||||
config, null, lastRequest));
|
config, null, lastRequest));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
stream.on('aborted', function handlerStreamAborted() {
|
||||||
|
if (rejected) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
stream.destroy();
|
||||||
|
reject(createError('error request aborted', config, 'ERR_REQUEST_ABORTED', lastRequest));
|
||||||
|
});
|
||||||
|
|
||||||
stream.on('error', function handleStreamError(err) {
|
stream.on('error', function handleStreamError(err) {
|
||||||
if (req.aborted) return;
|
if (req.aborted) return;
|
||||||
reject(enhanceError(err, config, null, lastRequest));
|
reject(enhanceError(err, config, null, lastRequest));
|
||||||
|
|||||||
@@ -1003,5 +1003,36 @@ describe('supports http with nodejs', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should throw an error if http server that aborts a chunked request', function (done) {
|
||||||
|
server = http.createServer(function (req, res) {
|
||||||
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||||
|
res.write('chunk 1');
|
||||||
|
setTimeout(function () {
|
||||||
|
res.write('chunk 2');
|
||||||
|
}, 100);
|
||||||
|
setTimeout(function() {
|
||||||
|
res.destroy();
|
||||||
|
}, 200);
|
||||||
|
}).listen(4444, function () {
|
||||||
|
var success = false, failure = false;
|
||||||
|
var error;
|
||||||
|
|
||||||
|
axios.get('http://localhost:4444/aborted', {
|
||||||
|
timeout: 500
|
||||||
|
}).then(function (res) {
|
||||||
|
success = true;
|
||||||
|
}).catch(function (err) {
|
||||||
|
error = err;
|
||||||
|
failure = true;
|
||||||
|
}).finally(function () {
|
||||||
|
assert.strictEqual(success, false, 'request should not succeed');
|
||||||
|
assert.strictEqual(failure, true, 'request should fail');
|
||||||
|
assert.strictEqual(error.code, 'ERR_REQUEST_ABORTED');
|
||||||
|
assert.strictEqual(error.message, 'error request aborted');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user