mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
chore(tests): fixed tests to pass in node v19 and v20 with keep-alive enabled; (#6021)
This commit is contained in:
@@ -15,7 +15,7 @@ jobs:
|
|||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
node-version: [12.x, 14.x, 16.x, 18.x]
|
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
|||||||
@@ -580,7 +580,7 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
|
|||||||
}
|
}
|
||||||
response.data = responseData;
|
response.data = responseData;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
reject(AxiosError.from(err, null, config, response.request, response));
|
return reject(AxiosError.from(err, null, config, response.request, response));
|
||||||
}
|
}
|
||||||
settle(resolve, reject, response);
|
settle(resolve, reject, response);
|
||||||
});
|
});
|
||||||
|
|||||||
+77
-77
@@ -58,14 +58,15 @@ const LOCAL_SERVER_URL = 'http://localhost:4444';
|
|||||||
|
|
||||||
const SERVER_HANDLER_STREAM_ECHO = (req, res) => req.pipe(res);
|
const SERVER_HANDLER_STREAM_ECHO = (req, res) => req.pipe(res);
|
||||||
|
|
||||||
function startHTTPServer(options) {
|
function startHTTPServer(handlerOrOptions, options) {
|
||||||
|
|
||||||
const {handler, useBuffering = false, rate = undefined, port = 4444} = typeof options === 'function' ? {
|
const {handler, useBuffering = false, rate = undefined, port = 4444, keepAlive = 1000} =
|
||||||
handler: options
|
Object.assign(typeof handlerOrOptions === 'function' ? {
|
||||||
} : options || {};
|
handler: handlerOrOptions
|
||||||
|
} : handlerOrOptions || {}, options);
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
http.createServer(handler || async function (req, res) {
|
const server = http.createServer(handler || async function (req, res) {
|
||||||
try {
|
try {
|
||||||
req.headers['content-length'] && res.setHeader('content-length', req.headers['content-length']);
|
req.headers['content-length'] && res.setHeader('content-length', req.headers['content-length']);
|
||||||
|
|
||||||
@@ -93,9 +94,21 @@ function startHTTPServer(options) {
|
|||||||
}).listen(port, function (err) {
|
}).listen(port, function (err) {
|
||||||
err ? reject(err) : resolve(this);
|
err ? reject(err) : resolve(this);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
server.keepAliveTimeout = keepAlive;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const stopHTTPServer = async (server, timeout = 10000) => {
|
||||||
|
if (server) {
|
||||||
|
if (typeof server.closeAllConnections === 'function') {
|
||||||
|
server.closeAllConnections();
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.race([new Promise(resolve => server.close(resolve)), setTimeoutAsync(timeout)]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const handleFormData = (req) => {
|
const handleFormData = (req) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const form = new formidable.IncomingForm();
|
const form = new formidable.IncomingForm();
|
||||||
@@ -131,16 +144,12 @@ function generateReadableStream(length = 1024 * 1024, chunkSize = 10 * 1024, sle
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('supports http with nodejs', function () {
|
describe('supports http with nodejs', function () {
|
||||||
|
afterEach(async function () {
|
||||||
|
await Promise.all([stopHTTPServer(server), stopHTTPServer(proxy)]);
|
||||||
|
|
||||||
|
server = null;
|
||||||
|
proxy = null;
|
||||||
|
|
||||||
afterEach(function () {
|
|
||||||
if (server) {
|
|
||||||
server.close();
|
|
||||||
server = null;
|
|
||||||
}
|
|
||||||
if (proxy) {
|
|
||||||
proxy.close();
|
|
||||||
proxy = null;
|
|
||||||
}
|
|
||||||
delete process.env.http_proxy;
|
delete process.env.http_proxy;
|
||||||
delete process.env.https_proxy;
|
delete process.env.https_proxy;
|
||||||
delete process.env.no_proxy;
|
delete process.env.no_proxy;
|
||||||
@@ -382,53 +391,57 @@ describe('supports http with nodejs', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support beforeRedirect and proxy with redirect', function (done) {
|
it('should support beforeRedirect and proxy with redirect', async () => {
|
||||||
var requestCount = 0;
|
let requestCount = 0;
|
||||||
var totalRedirectCount = 5;
|
let totalRedirectCount = 5;
|
||||||
server = http.createServer(function (req, res) {
|
|
||||||
|
server = await startHTTPServer(function (req, res) {
|
||||||
requestCount += 1;
|
requestCount += 1;
|
||||||
if (requestCount <= totalRedirectCount) {
|
if (requestCount <= totalRedirectCount) {
|
||||||
res.setHeader('Location', 'http://localhost:4444');
|
res.setHeader('Location', 'http://localhost:4444');
|
||||||
res.writeHead(302);
|
res.writeHead(302);
|
||||||
}
|
}
|
||||||
res.end();
|
res.end();
|
||||||
}).listen(4444, function () {
|
}, {port: 4444});
|
||||||
var proxyUseCount = 0;
|
|
||||||
proxy = http.createServer(function (request, response) {
|
|
||||||
proxyUseCount += 1;
|
|
||||||
var parsed = url.parse(request.url);
|
|
||||||
var opts = {
|
|
||||||
host: parsed.hostname,
|
|
||||||
port: parsed.port,
|
|
||||||
path: parsed.path
|
|
||||||
};
|
|
||||||
|
|
||||||
http.get(opts, function (res) {
|
let proxyUseCount = 0;
|
||||||
response.writeHead(res.statusCode, res.headers);
|
proxy = await startHTTPServer(function (req, res) {
|
||||||
res.on('data', function (data) {
|
proxyUseCount += 1;
|
||||||
response.write(data)
|
const targetUrl = new URL(req.url, 'http://' + req.headers.host);
|
||||||
});
|
const opts = {
|
||||||
res.on('end', function () {
|
host: targetUrl.hostname,
|
||||||
response.end();
|
port: targetUrl.port,
|
||||||
});
|
path: targetUrl.path,
|
||||||
});
|
method: req.method
|
||||||
}).listen(4000, function () {
|
};
|
||||||
var configBeforeRedirectCount = 0;
|
|
||||||
axios.get('http://localhost:4444/', {
|
const request = http.get(opts, function (response) {
|
||||||
proxy: {
|
res.writeHead(response.statusCode, response.headers);
|
||||||
host: 'localhost',
|
stream.pipeline(response, res, () => {});
|
||||||
port: 4000
|
|
||||||
},
|
|
||||||
maxRedirects: totalRedirectCount,
|
|
||||||
beforeRedirect: function (options) {
|
|
||||||
configBeforeRedirectCount += 1;
|
|
||||||
}
|
|
||||||
}).then(function (res) {
|
|
||||||
assert.equal(totalRedirectCount, configBeforeRedirectCount, 'should invoke config.beforeRedirect option on every redirect');
|
|
||||||
assert.equal(totalRedirectCount + 1, proxyUseCount, 'should go through proxy on every redirect');
|
|
||||||
done();
|
|
||||||
}).catch(done);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
request.on('error', (err) => {
|
||||||
|
console.warn('request error', err);
|
||||||
|
res.statusCode = 500;
|
||||||
|
res.end();
|
||||||
|
})
|
||||||
|
|
||||||
|
}, {port: 4000});
|
||||||
|
|
||||||
|
let configBeforeRedirectCount = 0;
|
||||||
|
|
||||||
|
await axios.get('http://localhost:4444/', {
|
||||||
|
proxy: {
|
||||||
|
host: 'localhost',
|
||||||
|
port: 4000
|
||||||
|
},
|
||||||
|
maxRedirects: totalRedirectCount,
|
||||||
|
beforeRedirect: function (options) {
|
||||||
|
configBeforeRedirectCount += 1;
|
||||||
|
}
|
||||||
|
}).then(function (res) {
|
||||||
|
assert.equal(totalRedirectCount, configBeforeRedirectCount, 'should invoke config.beforeRedirect option on every redirect');
|
||||||
|
assert.equal(totalRedirectCount + 1, proxyUseCount, 'should go through proxy on every redirect');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -666,31 +679,18 @@ describe('supports http with nodejs', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support max content length', function (done) {
|
it('should support max content length', async function () {
|
||||||
var str = Array(100000).join('ж');
|
server = await startHTTPServer(function (req, res) {
|
||||||
|
|
||||||
server = http.createServer(function (req, res) {
|
|
||||||
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
|
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
|
||||||
res.end(str);
|
res.end(Array(5000).join('#'));
|
||||||
}).listen(4444, function () {
|
}, {port: 4444});
|
||||||
var success = false, failure = false, error;
|
|
||||||
|
|
||||||
axios.get('http://localhost:4444/', {
|
await assert.rejects(() => {
|
||||||
maxContentLength: 2000
|
return axios.get('http://localhost:4444/', {
|
||||||
}).then(function (res) {
|
maxContentLength: 2000,
|
||||||
success = true;
|
maxRedirects: 0
|
||||||
}).catch(function (err) {
|
})
|
||||||
error = err;
|
},/maxContentLength size of 2000 exceeded/);
|
||||||
failure = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
setTimeout(function () {
|
|
||||||
assert.equal(success, false, 'request should not succeed');
|
|
||||||
assert.equal(failure, true, 'request should fail');
|
|
||||||
assert.equal(error.message, 'maxContentLength size of 2000 exceeded');
|
|
||||||
done();
|
|
||||||
}, 100);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support max content length for redirected', function (done) {
|
it('should support max content length for redirected', function (done) {
|
||||||
@@ -711,7 +711,7 @@ describe('supports http with nodejs', function () {
|
|||||||
var success = false, failure = false, error;
|
var success = false, failure = false, error;
|
||||||
|
|
||||||
axios.get('http://localhost:4444/one', {
|
axios.get('http://localhost:4444/one', {
|
||||||
maxContentLength: 2000
|
maxContentLength: 2000,
|
||||||
}).then(function (res) {
|
}).then(function (res) {
|
||||||
success = true;
|
success = true;
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
|
|||||||
@@ -20,8 +20,9 @@ describe('Server-Side Request Forgery (SSRF)', () => {
|
|||||||
fail = true;
|
fail = true;
|
||||||
res.end('rm -rf /');
|
res.end('rm -rf /');
|
||||||
}).listen(EVIL_PORT);
|
}).listen(EVIL_PORT);
|
||||||
|
|
||||||
proxy = http.createServer(function (req, res) {
|
proxy = http.createServer(function (req, res) {
|
||||||
if (req.url === 'http://localhost:' + EVIL_PORT + '/') {
|
if (new URL(req.url, 'http://' + req.headers.host).toString() === 'http://localhost:' + EVIL_PORT + '/') {
|
||||||
return res.end(JSON.stringify({
|
return res.end(JSON.stringify({
|
||||||
msg: 'Protected',
|
msg: 'Protected',
|
||||||
headers: req.headers,
|
headers: req.headers,
|
||||||
@@ -35,8 +36,10 @@ describe('Server-Side Request Forgery (SSRF)', () => {
|
|||||||
server.close();
|
server.close();
|
||||||
proxy.close();
|
proxy.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('obeys proxy settings when following redirects', async () => {
|
it('obeys proxy settings when following redirects', async () => {
|
||||||
location = 'http://localhost:' + EVIL_PORT;
|
location = 'http://localhost:' + EVIL_PORT;
|
||||||
|
|
||||||
let response = await axios({
|
let response = await axios({
|
||||||
method: "get",
|
method: "get",
|
||||||
url: "http://www.google.com/",
|
url: "http://www.google.com/",
|
||||||
|
|||||||
Reference in New Issue
Block a user