mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
+19
-3
@@ -13,6 +13,8 @@ var Buffer = require('buffer').Buffer;
|
|||||||
module.exports = function httpAdapter(resolve, reject, config) {
|
module.exports = function httpAdapter(resolve, reject, config) {
|
||||||
var data = config.data;
|
var data = config.data;
|
||||||
var headers = config.headers;
|
var headers = config.headers;
|
||||||
|
var timer;
|
||||||
|
var aborted = false;
|
||||||
|
|
||||||
// Set User-Agent (required by some servers)
|
// Set User-Agent (required by some servers)
|
||||||
// Only set header if it hasn't been set in config
|
// Only set header if it hasn't been set in config
|
||||||
@@ -57,6 +59,12 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
|||||||
// Create the request
|
// Create the request
|
||||||
var transport = parsed.protocol === 'https:' ? https : http;
|
var transport = parsed.protocol === 'https:' ? https : http;
|
||||||
var req = transport.request(options, function handleResponse(res) {
|
var req = transport.request(options, function handleResponse(res) {
|
||||||
|
if (aborted) return;
|
||||||
|
|
||||||
|
// Response has been received so kill timer that handles request timeout
|
||||||
|
clearTimeout(timer);
|
||||||
|
timer = null;
|
||||||
|
|
||||||
// uncompress the response body transparently if required
|
// uncompress the response body transparently if required
|
||||||
var stream = res;
|
var stream = res;
|
||||||
switch (res.headers['content-encoding']) {
|
switch (res.headers['content-encoding']) {
|
||||||
@@ -103,13 +111,21 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
|||||||
|
|
||||||
// Handle errors
|
// Handle errors
|
||||||
req.on('error', function handleRequestError(err) {
|
req.on('error', function handleRequestError(err) {
|
||||||
|
if (aborted) return;
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle request timeout
|
// Handle request timeout
|
||||||
req.setTimeout(config.timeout, function handleRequestTimeout() {
|
if (config.timeout && !timer) {
|
||||||
req.abort();
|
timer = setTimeout(function handleRequestTimeout() {
|
||||||
});
|
var err = new Error('timeout of ' + config.timeout + 'ms exceeded');
|
||||||
|
err.timeout = config.timeout;
|
||||||
|
err.code = 'ECONNABORTED';
|
||||||
|
req.abort();
|
||||||
|
reject(err);
|
||||||
|
aborted = true;
|
||||||
|
}, config.timeout);
|
||||||
|
}
|
||||||
|
|
||||||
// Send the request
|
// Send the request
|
||||||
req.end(data);
|
req.end(data);
|
||||||
|
|||||||
@@ -11,6 +11,34 @@ module.exports = {
|
|||||||
callback();
|
callback();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
testTimeout: function (test) {
|
||||||
|
server = http.createServer(function (req, res) {
|
||||||
|
setTimeout(function () {
|
||||||
|
res.end();
|
||||||
|
}, 1000);
|
||||||
|
}).listen(4444, function () {
|
||||||
|
var success = false, failure = false;
|
||||||
|
var error;
|
||||||
|
|
||||||
|
axios.get('http://localhost:4444/', {
|
||||||
|
timeout: 250
|
||||||
|
}).then(function (res) {
|
||||||
|
success = true;
|
||||||
|
}).catch(function (res) {
|
||||||
|
error = res;
|
||||||
|
failure = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(function () {
|
||||||
|
test.equal(success, false, 'request should not succeed');
|
||||||
|
test.equal(failure, true, 'request should fail');
|
||||||
|
test.equal(error.code, 'ECONNABORTED');
|
||||||
|
test.equal(error.message, 'timeout of 250ms exceeded');
|
||||||
|
test.done();
|
||||||
|
}, 300);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
testJSON: function (test) {
|
testJSON: function (test) {
|
||||||
var data = {
|
var data = {
|
||||||
firstName: 'Fred',
|
firstName: 'Fred',
|
||||||
|
|||||||
Reference in New Issue
Block a user