[refactor] Rewrite functions using axios

This commit is contained in:
Pavel Ostrovskij
2025-03-31 13:42:00 +03:00
parent 24c441133a
commit d3cc432d0e
5 changed files with 1219 additions and 768 deletions

View File

@ -338,37 +338,13 @@ function addExternalRequestOptions(ctx, uri, isInJwtToken, options) {
return res;
}
function downloadUrlPromise(ctx, uri, optTimeout, optLimit, opt_Authorization, opt_filterPrivate, opt_headers, opt_streamWriter) {
const tenTenantRequestDefaults = ctx.getCfg('services.CoAuthoring.requestDefaults', cfgRequestDefaults);
const maxRedirects = (undefined !== tenTenantRequestDefaults.maxRedirects) ? tenTenantRequestDefaults.maxRedirects : 10;
const followRedirect = (undefined !== tenTenantRequestDefaults.followRedirect) ? tenTenantRequestDefaults.followRedirect : true;
var redirectsFollowed = 0;
const doRequest = (curUrl) => {
return downloadUrlPromiseWithoutRedirect(ctx, curUrl, optTimeout, optLimit, opt_Authorization, opt_filterPrivate, opt_headers, opt_streamWriter)
.catch(err => {
const response = err.response;
if (isRedirectResponse(response)) {
if (followRedirect && redirectsFollowed < maxRedirects) {
let redirectTo = response.headers.location;
if (!/^https?:/.test(redirectTo)) {
redirectTo = url.resolve(curUrl, redirectTo);
}
ctx.logger.debug('downloadUrlPromise redirectsFollowed:%d redirectTo: %s', redirectsFollowed, redirectTo);
redirectsFollowed++;
return doRequest(redirectTo);
}
}
throw err;
});
};
return doRequest(uri);
}
async function downloadUrlPromiseWithoutRedirect(ctx, uri, optTimeout, optLimit, opt_Authorization, opt_filterPrivate, opt_headers, opt_streamWriter) {
async function downloadUrlPromise(ctx, uri, optTimeout, optLimit, opt_Authorization, opt_filterPrivate, opt_headers, opt_streamWriter) {
const tenTenantRequestDefaults = ctx.getCfg('services.CoAuthoring.requestDefaults', cfgRequestDefaults);
const tenTokenOutboxHeader = ctx.getCfg('services.CoAuthoring.token.outbox.header', cfgTokenOutboxHeader);
const tenTokenOutboxPrefix = ctx.getCfg('services.CoAuthoring.token.outbox.prefix', cfgTokenOutboxPrefix);
const sizeLimit = optLimit || Number.MAX_VALUE
const maxRedirects = (undefined !== tenTenantRequestDefaults.maxRedirects) ? tenTenantRequestDefaults.maxRedirects : 10;
const followRedirect = (undefined !== tenTenantRequestDefaults.followRedirect) ? tenTenantRequestDefaults.followRedirect : true;
const sizeLimit = optLimit || Number.MAX_VALUE;
uri = URI.serialize(URI.parse(uri));
const connectionAndInactivity = optTimeout?.connectionAndInactivity ? ms(optTimeout.connectionAndInactivity) : undefined;
const options = config.util.cloneDeep(tenTenantRequestDefaults);
@ -393,16 +369,16 @@ async function downloadUrlPromiseWithoutRedirect(ctx, uri, optTimeout, optLimit,
if (opt_headers) {
Object.assign(headers, opt_headers);
}
const axiosConfig = {
...options,
url: uri,
method: 'GET',
responseType: 'stream',
headers,
maxRedirects: 0,
maxRedirects: followRedirect ? maxRedirects : 0,
timeout: connectionAndInactivity,
validateStatus: () => true,
validateStatus: (status) => status >= 200 && status < 300,
cancelToken: new axios.CancelToken(cancel => {
if (optTimeout?.wholeCycle) {
setTimeout(() => {
@ -415,8 +391,7 @@ async function downloadUrlPromiseWithoutRedirect(ctx, uri, optTimeout, optLimit,
try {
const response = await axios(axiosConfig);
const { status, headers } = response;
if (status !== 200 && status !== 206) {
if (![200, 206].includes(status)) {
const error = new Error(`Error response: statusCode:${status}; headers:${JSON.stringify(headers)};`);
error.statusCode = status;
error.response = response;
@ -427,7 +402,7 @@ async function downloadUrlPromiseWithoutRedirect(ctx, uri, optTimeout, optLimit,
if (contentLength && parseInt(contentLength) > sizeLimit) {
throw new Error('EMSGSIZE: Error response: content-length:' + contentLength);
}
return await processResponseStream(ctx, {
response,
sizeLimit,
@ -440,19 +415,19 @@ async function downloadUrlPromiseWithoutRedirect(ctx, uri, optTimeout, optLimit,
if (axios.isCancel(err)) {
const error = new Error(err.message);
error.code = 'ETIMEDOUT';
throw error;
}
throw error;
}
if (err.response) {
if (opt_streamWriter && !isRedirectResponse(err.response)) {
delete err.response.headers['set-cookie'];
return processErrorResponseStream(err.response, {
sizeLimit,
opt_streamWriter,
timeout: optTimeout?.wholeCycle ? ms(optTimeout.wholeCycle) : null
});
}
if (err.response) {
if (opt_streamWriter && !isRedirectResponse(err.response)) {
delete err.response.headers['set-cookie'];
return processErrorResponseStream(err.response, {
sizeLimit,
opt_streamWriter,
timeout: optTimeout?.wholeCycle ? ms(optTimeout.wholeCycle) : null
});
}
}
throw err;
}
}
@ -554,7 +529,7 @@ async function postRequestPromise(ctx, uri, postData, postDataStream, postDataSi
let connectionAndInactivity = optTimeout && optTimeout.connectionAndInactivity && ms(optTimeout.connectionAndInactivity);
const wholeCycleTimeout = optTimeout?.wholeCycle ? ms(optTimeout.wholeCycle) : undefined;
uri = URI.serialize(URI.parse(uri));
let options = config.util.extendDeep({}, tenTenantRequestDefaults);
let options = { ...tenTenantRequestDefaults };
Object.assign(options, {
method: 'post',
url: uri,
@ -562,11 +537,14 @@ async function postRequestPromise(ctx, uri, postData, postDataStream, postDataSi
validateStatus: (status) => status === 200 || status === 204
});
if (!addExternalRequestOptions(ctx, uri, opt_isInJwtToken, options)) {
throw new Error('Block external request. See externalRequest config options')
throw new Error('Block external request. See externalRequest config options');
}
const protocol = new URL(uri).protocol;
if (!options.httpsAgent && !options.httpAgent) {
const agentOptions = { ...https.globalAgent.options, rejectUnauthorized: tenTenantRequestDefaults.rejectUnauthorized === false? false : true};
const agentOptions = {
...https.globalAgent.options,
rejectUnauthorized: tenTenantRequestDefaults.rejectUnauthorized === false ? false : true
};
if (protocol === 'https:') {
options.httpsAgent = new https.Agent(agentOptions);
} else if (protocol === 'http:') {
@ -616,7 +594,7 @@ async function postRequestPromise(ctx, uri, postData, postDataStream, postDataSi
const err = new Error(error.message);
err.code = 'ETIMEDOUT';
throw err;
}
}
if (error.response) {
const { status, headers, data } = error.response;
const err = new Error(`Error response: statusCode:${status}; headers:${JSON.stringify(headers)}; body:\r\n${data}`);

418
npm-shrinkwrap.json generated
View File

@ -1082,6 +1082,16 @@
"integrity": "sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ==",
"dev": true
},
"accepts": {
"version": "1.3.8",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz",
"integrity": "sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==",
"dev": true,
"requires": {
"mime-types": "~2.1.34",
"negotiator": "0.6.3"
}
},
"ansi-escapes": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.2.tgz",
@ -1127,6 +1137,12 @@
"is-array-buffer": "^3.0.1"
}
},
"array-flatten": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==",
"dev": true
},
"arraybuffer.prototype.slice": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/arraybuffer.prototype.slice/-/arraybuffer.prototype.slice-1.0.2.tgz",
@ -1275,6 +1291,24 @@
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="
},
"body-parser": {
"version": "1.19.0",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz",
"integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==",
"dev": true,
"requires": {
"bytes": "3.1.0",
"content-type": "~1.0.4",
"debug": "2.6.9",
"depd": "~1.1.2",
"http-errors": "1.7.2",
"iconv-lite": "0.4.24",
"on-finished": "~2.3.0",
"qs": "6.7.0",
"raw-body": "2.4.0",
"type-is": "~1.6.17"
}
},
"brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
@ -1311,6 +1345,12 @@
"integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==",
"dev": true
},
"bytes": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
"integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==",
"dev": true
},
"cacheable-lookup": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-7.0.0.tgz",
@ -1443,12 +1483,39 @@
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg=="
},
"content-disposition": {
"version": "0.5.3",
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz",
"integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==",
"dev": true,
"requires": {
"safe-buffer": "5.1.2"
}
},
"content-type": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz",
"integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==",
"dev": true
},
"convert-source-map": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz",
"integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==",
"dev": true
},
"cookie": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz",
"integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==",
"dev": true
},
"cookie-signature": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
"integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==",
"dev": true
},
"create-jest": {
"version": "29.7.0",
"resolved": "https://registry.npmjs.org/create-jest/-/create-jest-29.7.0.tgz",
@ -1525,6 +1592,15 @@
}
}
},
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"dev": true,
"requires": {
"ms": "2.0.0"
}
},
"decompress-response": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-6.0.0.tgz",
@ -1582,6 +1658,18 @@
"object-keys": "^1.1.1"
}
},
"depd": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz",
"integrity": "sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ==",
"dev": true
},
"destroy": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz",
"integrity": "sha512-3NdhDuEXnfun/z7x9GOElY49LoqVHoGScmOKwmxhsS8N5Y+Z8KyPPDnaSzqWgYt/ji4mqwfTS34Htrk0zPIXVg==",
"dev": true
},
"detect-newline": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/detect-newline/-/detect-newline-3.1.0.tgz",
@ -1594,6 +1682,12 @@
"integrity": "sha512-EjePK1srD3P08o2j4f0ExnylqRs5B9tJjcp9t1krH2qRi8CCdsYfwe9JgSLurFBWwq4uOlipzfk5fHNvwFKr8Q==",
"dev": true
},
"ee-first": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==",
"dev": true
},
"electron-to-chromium": {
"version": "1.5.114",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.114.tgz",
@ -1612,6 +1706,12 @@
"integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==",
"dev": true
},
"encodeurl": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz",
"integrity": "sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w==",
"dev": true
},
"eol": {
"version": "0.9.1",
"resolved": "https://registry.npmjs.org/eol/-/eol-0.9.1.tgz",
@ -1697,6 +1797,12 @@
"integrity": "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==",
"dev": true
},
"escape-html": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
"integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==",
"dev": true
},
"escape-string-regexp": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
@ -1708,6 +1814,12 @@
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
"dev": true
},
"etag": {
"version": "1.8.1",
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
"integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==",
"dev": true
},
"execa": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz",
@ -1744,6 +1856,44 @@
"jest-util": "^29.7.0"
}
},
"express": {
"version": "4.17.1",
"resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz",
"integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==",
"dev": true,
"requires": {
"accepts": "~1.3.7",
"array-flatten": "1.1.1",
"body-parser": "1.19.0",
"content-disposition": "0.5.3",
"content-type": "~1.0.4",
"cookie": "0.4.0",
"cookie-signature": "1.0.6",
"debug": "2.6.9",
"depd": "~1.1.2",
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"finalhandler": "~1.1.2",
"fresh": "0.5.2",
"merge-descriptors": "1.0.1",
"methods": "~1.1.2",
"on-finished": "~2.3.0",
"parseurl": "~1.3.3",
"path-to-regexp": "0.1.7",
"proxy-addr": "~2.0.5",
"qs": "6.7.0",
"range-parser": "~1.2.1",
"safe-buffer": "5.1.2",
"send": "0.17.1",
"serve-static": "1.14.1",
"setprototypeof": "1.1.1",
"statuses": "~1.5.0",
"type-is": "~1.6.18",
"utils-merge": "1.0.1",
"vary": "~1.1.2"
}
},
"fast-json-stable-stringify": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz",
@ -1759,6 +1909,21 @@
"bser": "2.1.1"
}
},
"finalhandler": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz",
"integrity": "sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==",
"dev": true,
"requires": {
"debug": "2.6.9",
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"on-finished": "~2.3.0",
"parseurl": "~1.3.3",
"statuses": "~1.5.0",
"unpipe": "~1.0.0"
}
},
"find-up": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz",
@ -1782,6 +1947,18 @@
"resolved": "https://registry.npmjs.org/form-data-encoder/-/form-data-encoder-2.1.4.tgz",
"integrity": "sha512-yDYSgNMraqvnxiEXO4hi88+YZxaHC6QKzb5N84iRCTDeRO7ZALpir/lVmf/uXUhnwUr2O4HU8s/n6x+yNjQkHw=="
},
"forwarded": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
"integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==",
"dev": true
},
"fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
"integrity": "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==",
"dev": true
},
"fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@ -2006,6 +2183,27 @@
"resolved": "https://registry.npmjs.org/http-cache-semantics/-/http-cache-semantics-4.1.1.tgz",
"integrity": "sha512-er295DKPVsV82j5kw1Gjt+ADA/XYHsajl82cGNQG2eyoPkvgUhX+nDIyelzhIWbbsXP39EHcI6l5tYs2FYqYXQ=="
},
"http-errors": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz",
"integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==",
"dev": true,
"requires": {
"depd": "~1.1.2",
"inherits": "2.0.3",
"setprototypeof": "1.1.1",
"statuses": ">= 1.5.0 < 2",
"toidentifier": "1.0.0"
},
"dependencies": {
"inherits": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
"integrity": "sha512-x00IRNXNy63jwGkJmzPigoySHbaqpNuzKbBOmzK+g2OdZpQ9w+sxCN+VSB3ja7IAge2OP2qpfxTjeNcyjmW1uw==",
"dev": true
}
}
},
"http2-wrapper": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/http2-wrapper/-/http2-wrapper-2.2.1.tgz",
@ -2021,6 +2219,15 @@
"integrity": "sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==",
"dev": true
},
"iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
"dev": true,
"requires": {
"safer-buffer": ">= 2.1.2 < 3"
}
},
"import-local": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/import-local/-/import-local-3.2.0.tgz",
@ -2068,6 +2275,12 @@
"side-channel": "^1.0.4"
}
},
"ipaddr.js": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
"integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==",
"dev": true
},
"is-array-buffer": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/is-array-buffer/-/is-array-buffer-3.0.2.tgz",
@ -3522,17 +3735,56 @@
"tmpl": "1.0.5"
}
},
"media-typer": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
"integrity": "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==",
"dev": true
},
"memorystream": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/memorystream/-/memorystream-0.3.1.tgz",
"integrity": "sha512-S3UwM3yj5mtUSEfP41UZmt/0SCoVYUcU1rkXv+BQ5Ig8ndL4sPoJNBUJERafdPb5jjHJGuMgytgKvKIf58XNBw=="
},
"merge-descriptors": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz",
"integrity": "sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w==",
"dev": true
},
"merge-stream": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz",
"integrity": "sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==",
"dev": true
},
"methods": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz",
"integrity": "sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==",
"dev": true
},
"mime": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
"integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==",
"dev": true
},
"mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
"dev": true
},
"mime-types": {
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"dev": true,
"requires": {
"mime-db": "1.52.0"
}
},
"mimic-fn": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz",
@ -3557,12 +3809,24 @@
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.6.tgz",
"integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q=="
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==",
"dev": true
},
"natural-compare": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
"integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==",
"dev": true
},
"negotiator": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
"integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==",
"dev": true
},
"nice-try": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz",
@ -3733,6 +3997,15 @@
"object-keys": "^1.1.1"
}
},
"on-finished": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
"integrity": "sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==",
"dev": true,
"requires": {
"ee-first": "1.1.1"
}
},
"once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
@ -3803,6 +4076,12 @@
"lines-and-columns": "^1.1.6"
}
},
"parseurl": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
"integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==",
"dev": true
},
"path-exists": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
@ -3826,6 +4105,12 @@
"resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz",
"integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw=="
},
"path-to-regexp": {
"version": "0.1.7",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
"integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==",
"dev": true
},
"path-type": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/path-type/-/path-type-3.0.0.tgz",
@ -3900,17 +4185,51 @@
"sisteransi": "^1.0.5"
}
},
"proxy-addr": {
"version": "2.0.7",
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
"integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==",
"dev": true,
"requires": {
"forwarded": "0.2.0",
"ipaddr.js": "1.9.1"
}
},
"pure-rand": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/pure-rand/-/pure-rand-6.1.0.tgz",
"integrity": "sha512-bVWawvoZoBYpp6yIoQtQXHZjmz35RSVHnUOTefl8Vcjr8snTPY1wnpSPMWekcFwbxI6gtmT7rSYPFvz71ldiOA==",
"dev": true
},
"qs": {
"version": "6.7.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
"integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==",
"dev": true
},
"quick-lru": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/quick-lru/-/quick-lru-5.1.1.tgz",
"integrity": "sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA=="
},
"range-parser": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
"integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==",
"dev": true
},
"raw-body": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz",
"integrity": "sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==",
"dev": true,
"requires": {
"bytes": "3.1.0",
"http-errors": "1.7.2",
"iconv-lite": "0.4.24",
"unpipe": "1.0.0"
}
},
"rc": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz",
@ -4023,6 +4342,12 @@
}
}
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==",
"dev": true
},
"safe-regex-test": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/safe-regex-test/-/safe-regex-test-1.0.0.tgz",
@ -4033,11 +4358,46 @@
"is-regex": "^1.1.4"
}
},
"safer-buffer": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
"dev": true
},
"semver": {
"version": "5.7.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.2.tgz",
"integrity": "sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g=="
},
"send": {
"version": "0.17.1",
"resolved": "https://registry.npmjs.org/send/-/send-0.17.1.tgz",
"integrity": "sha512-BsVKsiGcQMFwT8UxypobUKyv7irCNRHk1T0G680vk88yf6LBByGcZJOTJCrTP2xVN6yI+XjPJcNuE3V4fT9sAg==",
"dev": true,
"requires": {
"debug": "2.6.9",
"depd": "~1.1.2",
"destroy": "~1.0.4",
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"fresh": "0.5.2",
"http-errors": "~1.7.2",
"mime": "1.6.0",
"ms": "2.1.1",
"on-finished": "~2.3.0",
"range-parser": "~1.2.1",
"statuses": "~1.5.0"
},
"dependencies": {
"ms": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz",
"integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==",
"dev": true
}
}
},
"sentence-case": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/sentence-case/-/sentence-case-3.0.4.tgz",
@ -4048,6 +4408,18 @@
"upper-case-first": "^2.0.2"
}
},
"serve-static": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.14.1.tgz",
"integrity": "sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg==",
"dev": true,
"requires": {
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"parseurl": "~1.3.3",
"send": "0.17.1"
}
},
"set-function-length": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.1.1.tgz",
@ -4069,6 +4441,12 @@
"has-property-descriptors": "^1.0.0"
}
},
"setprototypeof": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz",
"integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==",
"dev": true
},
"shebang-command": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
@ -4189,6 +4567,12 @@
}
}
},
"statuses": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
"integrity": "sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==",
"dev": true
},
"string-length": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/string-length/-/string-length-4.0.2.tgz",
@ -4338,6 +4722,12 @@
"integrity": "sha512-3f0uOEAQwIqGuWW2MVzYg8fV/QNnc/IpuJNG837rLuczAaLVHslWHZQj4IGiEl5Hs3kkbhwL9Ab7Hrsmuj+Smw==",
"dev": true
},
"toidentifier": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz",
"integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==",
"dev": true
},
"tslib": {
"version": "2.8.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
@ -4355,6 +4745,16 @@
"integrity": "sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==",
"dev": true
},
"type-is": {
"version": "1.6.18",
"resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",
"integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==",
"dev": true,
"requires": {
"media-typer": "0.3.0",
"mime-types": "~2.1.24"
}
},
"typed-array-buffer": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/typed-array-buffer/-/typed-array-buffer-1.0.0.tgz",
@ -4420,6 +4820,12 @@
"resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.1.tgz",
"integrity": "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw=="
},
"unpipe": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
"integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==",
"dev": true
},
"update-browserslist-db": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.1.3.tgz",
@ -4438,6 +4844,12 @@
"tslib": "^2.0.3"
}
},
"utils-merge": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==",
"dev": true
},
"v8-to-istanbul": {
"version": "9.3.0",
"resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.3.0.tgz",
@ -4458,6 +4870,12 @@
"spdx-expression-parse": "^3.0.0"
}
},
"vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==",
"dev": true
},
"visit-values": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/visit-values/-/visit-values-2.0.0.tgz",

View File

@ -11,7 +11,8 @@
"devDependencies": {
"@jest/globals": "29.7.0",
"cross-env": "7.0.3",
"jest": "29.7.0"
"jest": "29.7.0",
"express": "4.17.1"
},
"scripts": {
"perf-expired": "cd ./DocService&& cross-env NODE_ENV=development-windows NODE_CONFIG_DIR=../Common/config node ../tests/perf/checkFileExpire.js",

View File

@ -0,0 +1,772 @@
const { describe, test, expect, beforeAll, afterAll } = require('@jest/globals');
const { Writable, Readable } = require('stream');
const http = require('http');
const express = require('express');
const operationContext = require('../../../Common/sources/operationContext');
const utils = require('../../../Common/sources/utils');
const fs = require('fs').promises;
const path = require('path');
// Create operation context for tests
const ctx = new operationContext.Context();
// Test server setup
let server;
let testServer;
const PORT = 3456;
const BASE_URL = `http://localhost:${PORT}`;
// Helper to create a writable stream for testing
const createMockWriter = () => {
const chunks = [];
return new Writable({
write(chunk, encoding, callback) {
chunks.push(chunk);
callback();
}
});
};
const getStatusCode = (response) => response.statusCode || response.status;
function createMockContext(overrides = {}) {
const defaultCtx = {
getCfg: function(key, _) {
switch (key) {
case 'services.CoAuthoring.requestDefaults':
return {
"headers": {
"User-Agent": "Node.js/6.13",
"Connection": "Keep-Alive"
},
"decompress": true,
"rejectUnauthorized": true,
"followRedirect": false
};
case 'services.CoAuthoring.token.outbox.header':
return "Authorization";
case 'services.CoAuthoring.token.outbox.prefix':
return "Bearer ";
case 'externalRequest.action':
return {
"allow": true,
"blockPrivateIP": false,
"proxyUrl": "",
"proxyUser": {
"username": "",
"password": ""
},
"proxyHeaders": {}
};
case 'services.CoAuthoring.request-filtering-agent':
return {
"allowPrivateIPAddress": false,
"allowMetaIPAddress": false
};
case 'externalRequest.directIfIn':
return {
"allowList": [],
"jwtToken": true
};
default:
return undefined;
}
},
logger: {
debug: function() {},
}
};
// Return a mock context with overridden values if any
return {
...defaultCtx,
getCfg: function(key, _) {
// Return the override if it exists
if (overrides[key]) {
return overrides[key];
}
// Otherwise, return the default behavior
return defaultCtx.getCfg(key, _);
}
};
}
describe('HTTP Request Integration Tests', () => {
beforeAll(async () => {
// Setup test Express server
const app = express();
// Basic endpoint that returns JSON
app.get('/api/data', (req, res) => {
res.json({ success: true });
});
// Endpoint that streams data
app.get('/api/stream', (req, res) => {
res.setHeader('content-type', 'application/octet-stream');
res.setHeader('content-length', '1024');
const buffer = Buffer.alloc(1024);
res.send(buffer);
});
// Endpoint that simulates timeout
app.get('/api/timeout', (req, res) => {
// Never send response to trigger timeout
return;
});
// Endpoint that redirects
app.get('/api/redirect', (req, res) => {
res.redirect('/api/data');
});
// Endpoint that returns error
app.get('/api/error', (req, res) => {
res.status(500).json({ error: 'Internal Server Error' });
});
// POST endpoint
app.post('/api/post', express.json(), (req, res) => {
res.json({ received: req.body });
});
// POST endpoint that times out
app.post('/api/timeout', express.json(), (req, res) => {
// Never send response to trigger timeout
return;
});
app.get('/api/binary', (req, res) => {
// PNG file signature as binary data
const binaryData = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
res.setHeader('content-type', 'image/png');
res.setHeader('content-length', binaryData.length);
res.send(binaryData);
});
// Large file endpoint
app.get('/api/large', (req, res) => {
res.setHeader('content-length', '2097152'); // 2MB
res.setHeader('content-type', 'application/octet-stream');
const buffer = Buffer.alloc(2097152);
res.send(buffer);
});
// Start server
server = http.createServer(app);
await new Promise(resolve => server.listen(PORT, resolve));
});
afterAll(async () => {
// Cleanup server
await new Promise(resolve => server.close(resolve));
});
describe('downloadUrlPromise', () => {
test('successfully downloads JSON data', async () => {
const result = await utils.downloadUrlPromise(
ctx,
`${BASE_URL}/api/data`,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
false,
null,
null
);
expect(result).toBeDefined();
expect(getStatusCode(result.response)).toBe(200);
expect(JSON.parse(result.body.toString())).toEqual({ success: true });
});
test('handles streaming with writer', async () => {
const mockStreamWriter = createMockWriter();
const result = await utils.downloadUrlPromise(
ctx,
`${BASE_URL}/api/stream`,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
false,
null,
mockStreamWriter
);
expect(result).toBeUndefined();
});
test('throws error on timeout', async () => {
await expect(utils.downloadUrlPromise(
ctx,
`${BASE_URL}/api/timeout`,
{ wholeCycle: '1s', connectionAndInactivity: '500ms' },
1024 * 1024,
null,
false,
null,
null
)).rejects.toThrow(/(?:ESOCKETTIMEDOUT|timeout of 500ms exceeded)/);
});
test('throws error on wholeCycle timeout', async () => {
await expect(utils.downloadUrlPromise(
ctx,
`${BASE_URL}/api/timeout`,
{ wholeCycle: '1s', connectionAndInactivity: '5000ms' },
1024 * 1024,
null,
false,
null,
null
)).rejects.toThrow(/(?:ESOCKETTIMEDOUT|ETIMEDOUT: 1s|whole request cycle timeout: 1s)/);
});
test('follows redirects correctly', async () => {
const result = await utils.downloadUrlPromise(
ctx,
`${BASE_URL}/api/redirect`,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
false,
null,
null
);
expect(result).toBeDefined();
expect(getStatusCode(result.response)).toBe(200);
expect(JSON.parse(result.body.toString())).toEqual({ success: true });
});
test(`doesn't follow redirects(maxRedirects=0)`, async () => {
const mockCtx = createMockContext({
'services.CoAuthoring.requestDefaults': {
"headers": {
"User-Agent": "Node.js/6.13",
"Connection": "Keep-Alive"
},
"decompress": true,
"rejectUnauthorized": false,
"followRedirect": true,
"maxRedirects": 0
},
});
try {
const result = await utils.downloadUrlPromise(
mockCtx,
`${BASE_URL}/api/redirect`,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
false,
null,
null
);
// Old implementation path
expect(result).toBeDefined();
expect(getStatusCode(result.response)).toBe(302);
} catch (error) {
// New implementation path (Axios)
expect(error.message).toMatch(/(?:Request failed with status code 302|Error response: statusCode:302)/);
}
});
test(`doesn't follow redirects(followRedirect=false)`, async () => {
const mockCtx = createMockContext({
'services.CoAuthoring.requestDefaults': {
"headers": {
"User-Agent": "Node.js/6.13",
"Connection": "Keep-Alive"
},
"decompress": true,
"rejectUnauthorized": false,
"followRedirect": false,
"maxRedirects": 100
},
});
try {
const result = await utils.downloadUrlPromise(
mockCtx,
`${BASE_URL}/api/redirect`,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
false,
null,
null
);
// Old implementation path
expect(result).toBeDefined();
expect(getStatusCode(result.response)).toBe(302);
} catch (error) {
// New implementation path (Axios)
expect(error.message).toMatch(/(?:Request failed with status code 302|Error response: statusCode:302)/);
}
});
test('throws error on server error', async () => {
await expect(utils.downloadUrlPromise(
ctx,
`${BASE_URL}/api/error`,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
false,
null,
null
)).rejects.toThrow(/(?:Error response: statusCode:500|Request failed with status code 500)/);
});
test('throws error when content-length exceeds limit', async () => {
await expect(utils.downloadUrlPromise(
ctx,
`${BASE_URL}/api/large`,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
false,
null,
null
)).rejects.toThrow('Error response: content-length:2097152');
});
});
test('handles binary data correctly', async () => {
const result = await utils.downloadUrlPromise(
ctx,
`${BASE_URL}/api/binary`,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
false,
null,
null
);
// Expected binary data (PNG file signature)
const expectedData = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
expect(result).toBeDefined();
expect(result.response).toBeDefined();
expect(getStatusCode(result.response)).toBe(200);
expect(result.response.headers['content-type']).toBe('image/png');
// Verify binary data
expect(Buffer.isBuffer(result.body)).toBe(true);
expect(result.body.length).toBe(expectedData.length);
expect(Buffer.compare(result.body, expectedData)).toBe(0);
});
test('handles binary data with stream writer', async () => {
const chunks = [];
const mockStreamWriter = new Writable({
write(chunk, encoding, callback) {
chunks.push(chunk);
callback();
}
});
await utils.downloadUrlPromise(
ctx,
`${BASE_URL}/api/binary`,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
false,
null,
mockStreamWriter
);
// Combine chunks and verify
const receivedData = Buffer.concat(chunks);
const expectedData = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
expect(Buffer.isBuffer(receivedData)).toBe(true);
expect(receivedData.length).toBe(expectedData.length);
expect(Buffer.compare(receivedData, expectedData)).toBe(0);
});
test('block external requests', async () => {
const mockCtx = createMockContext({
'externalRequest.action': {
"allow": false, // Block all external requests
"blockPrivateIP": false,
"proxyUrl": "",
"proxyUser": {
"username": "",
"password": ""
},
"proxyHeaders": {}
}
});
await expect(utils.downloadUrlPromise(
mockCtx,
'https://example.com/test',
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
false,
null,
null
)).rejects.toThrow('Block external request. See externalRequest config options');
});
test('allows request to external url in allowlist', async () => {
const mockCtx = createMockContext({
'externalRequest.action': {
"allow": false, // Block external requests by default
"blockPrivateIP": false,
"proxyUrl": "",
"proxyUser": {
"username": "",
"password": ""
},
"proxyHeaders": {}
},
'externalRequest.directIfIn': {
"allowList": [`${BASE_URL}`], // Allow our test server
"jwtToken": false
}
});
const result = await utils.downloadUrlPromise(
mockCtx,
`${BASE_URL}/api/data`,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
false,
null,
null
);
expect(result).toBeDefined();
expect(getStatusCode(result.response)).toBe(200);
expect(JSON.parse(result.body.toString())).toEqual({ success: true });
});
test('allows request when URL is in JWT token', async () => {
const mockCtx = createMockContext({
'externalRequest.action': {
"allow": false, // Block external requests by default
"blockPrivateIP": false,
"proxyUrl": "",
"proxyUser": {
"username": "",
"password": ""
},
"proxyHeaders": {}
},
'externalRequest.directIfIn': {
"allowList": [], // Empty allowlist
"jwtToken": true // Allow URLs from JWT token
}
});
const result = await utils.downloadUrlPromise(
mockCtx,
`${BASE_URL}/api/data`,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
true, // Indicate URL is from JWT token
null,
null
);
expect(result).toBeDefined();
expect(getStatusCode(result.response)).toBe(200);
expect(JSON.parse(result.body.toString())).toEqual({ success: true });
});
test('request with proxy configuration', async () => {
const mockCtx = createMockContext({
'externalRequest.action': {
"allow": true,
"blockPrivateIP": true,
"proxyUrl": "http://proxy.example.com:8080",
"proxyUser": {
"username": "testuser",
"password": "testpass"
},
"proxyHeaders": {
"X-Custom-Header": "test-value"
}
}
});
try {
await utils.downloadUrlPromise(
mockCtx,
'https://example.com/test',
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
1024 * 1024,
null,
false,
null,
null
);
fail('Expected request to fail');
} catch (error) {
// Different error structures between implementations
const headers = error.config?.headers || error.request?._headers || error._headers;
if (headers) {
expect(headers).toEqual(
expect.objectContaining({
'proxy-authorization': expect.stringContaining('testuser:testpass'),
'X-Custom-Header': 'test-value'
})
);
}
// If headers aren't available, at least verify the error occurred
expect(error).toBeDefined();
}
});
describe('postRequestPromise', () => {
test('successfully posts data', async () => {
const postData = JSON.stringify({ test: 'data' });
const result = await utils.postRequestPromise(
ctx,
`${BASE_URL}/api/post`,
postData,
null,
postData.length,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
null,
false,
{ 'Content-Type': 'application/json' }
);
expect(result).toBeDefined();
expect(result.response.statusCode).toBe(200);
expect(JSON.parse(result.body)).toEqual({ received: { test: 'data' } });
});
test('handles timeout during post', async () => {
const postData = JSON.stringify({ test: 'data' });
await expect(utils.postRequestPromise(
ctx,
`${BASE_URL}/api/timeout`,
postData,
null,
postData.length,
{ wholeCycle: '1s', connectionAndInactivity: '500ms' },
null,
false,
{ 'Content-Type': 'application/json' }
)).rejects.toThrow(/(?:ESOCKETTIMEDOUT|timeout of 500ms exceeded)/);
});
test('handles post with Authorization header', async () => {
const postData = JSON.stringify({ test: 'data' });
const authToken = 'test-auth-token';
const result = await utils.postRequestPromise(
ctx,
`${BASE_URL}/api/post`,
postData,
null,
postData.length,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
authToken,
false,
{ 'Content-Type': 'application/json' }
);
expect(result).toBeDefined();
expect(result.response.statusCode).toBe(200);
expect(JSON.parse(result.body)).toEqual({ received: { test: 'data' } });
});
test('handles post with custom headers', async () => {
const postData = JSON.stringify({ test: 'data' });
const customHeaders = {
'X-Custom-Header': 'test-value',
'Content-Type': 'application/json'
};
const result = await utils.postRequestPromise(
ctx,
`${BASE_URL}/api/post`,
postData,
null,
postData.length,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
null,
false,
customHeaders
);
expect(result).toBeDefined();
expect(result.response.statusCode).toBe(200);
expect(JSON.parse(result.body)).toEqual({ received: { test: 'data' } });
});
test('handles post with stream data', async () => {
const postData = JSON.stringify({ test: 'stream-data' });
const postStream = new Readable({
read() {
this.push(postData);
this.push(null);
}
});
const result = await utils.postRequestPromise(
ctx,
`${BASE_URL}/api/post`,
null,
postStream,
postData.length,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
null,
false,
{ 'Content-Type': 'application/json' }
);
expect(result).toBeDefined();
expect(result.response.statusCode).toBe(200);
expect(JSON.parse(result.body)).toEqual({ received: { test: 'stream-data' } });
});
test('throws error on wholeCycle timeout during post', async () => {
const postData = JSON.stringify({ test: 'data' });
await expect(utils.postRequestPromise(
ctx,
`${BASE_URL}/api/timeout`,
postData,
null,
postData.length,
{ wholeCycle: '1s', connectionAndInactivity: '5s' },
null,
false,
{ 'Content-Type': 'application/json' }
)).rejects.toThrow(/(?:ETIMEDOUT|ESOCKETTIMEDOUT|whole request cycle timeout: 1s|Whole request cycle timeout: 1s)/);
});
test('blocks external post requests when configured', async () => {
const mockCtx = createMockContext({
'externalRequest.action': {
"allow": false,
"blockPrivateIP": false,
"proxyUrl": "",
"proxyUser": {
"username": "",
"password": ""
},
"proxyHeaders": {}
}
});
const postData = JSON.stringify({ test: 'data' });
await expect(utils.postRequestPromise(
mockCtx,
'https://example.com/api/post',
postData,
null,
postData.length,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
null,
false,
{ 'Content-Type': 'application/json' }
)).rejects.toThrow('Block external request. See externalRequest config options');
});
test('allows post request when URL is in JWT token', async () => {
const mockCtx = createMockContext({
'externalRequest.action': {
"allow": false,
"blockPrivateIP": false,
"proxyUrl": "",
"proxyUser": {
"username": "",
"password": ""
},
"proxyHeaders": {}
},
'externalRequest.directIfIn': {
"allowList": [],
"jwtToken": true
}
});
const postData = JSON.stringify({ test: 'data' });
const result = await utils.postRequestPromise(
mockCtx,
`${BASE_URL}/api/post`,
postData,
null,
postData.length,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
null,
true, // Indicate URL is from JWT token
{ 'Content-Type': 'application/json' }
);
expect(result).toBeDefined();
expect(result.response.statusCode).toBe(200);
expect(JSON.parse(result.body)).toEqual({ received: { test: 'data' } });
});
test('handles post with proxy configuration', async () => {
const mockCtx = createMockContext({
'externalRequest.action': {
"allow": true,
"blockPrivateIP": true,
"proxyUrl": "http://proxy.example.com:8080",
"proxyUser": {
"username": "testuser",
"password": "testpass"
},
"proxyHeaders": {
"X-Custom-Proxy-Header": "test-value"
}
}
});
const postData = JSON.stringify({ test: 'data' });
try {
await utils.postRequestPromise(
mockCtx,
'https://example.com/api/post',
postData,
null,
postData.length,
{ wholeCycle: '5s', connectionAndInactivity: '3s' },
null,
false,
{ 'Content-Type': 'application/json' }
);
fail('Expected request to fail');
} catch (error) {
// Different error structures between implementations
const headers = error.config?.headers || error.request?._headers || error._headers;
if (headers) {
expect(headers).toEqual(
expect.objectContaining({
'proxy-authorization': expect.stringContaining('testuser:testpass'),
'X-Custom-Proxy-Header': 'test-value',
'Content-Type': 'application/json'
})
);
}
// If headers aren't available, at least verify the error occurred
expect(error).toBeDefined();
}
});
});
});

View File

@ -1,718 +0,0 @@
// Required modules
const { describe, test, expect, beforeEach, afterAll, jest } = require('@jest/globals');
const { Readable, Writable } = require('stream');
// Setup mocks for axios
const axiosReal = require('axios');
jest.mock('axios');
const axios = require('axios');
const operationContext = require('../../Common/sources/operationContext');
const utils = require('../../Common/sources/utils');
// Assign real CancelToken from the imported axiosReal to the mocked axios
axios.CancelToken = axiosReal.CancelToken;
axios.CancelToken.source = axiosReal.CancelToken.source;
// Create operation context for tests
const ctx = new operationContext.Context();
// Helper functions for creating test streams
const createMockStream = (data) => {
// Convert string to Buffer if it's not already a buffer
const bufferData = Buffer.isBuffer(data) ? data : Buffer.from(data || JSON.stringify({ success: true }), 'utf8');
// Create a Readable stream from buffer data
return Readable.from(bufferData);
};
const createMockWriter = () => {
const chunks = [];
return new Writable({
write(chunk, encoding, callback) {
chunks.push(chunk);
callback();
}
});
};
// Common test parameters
const commonTestParams = {
uri: 'https://example.com/api/data',
timeout: { wholeCycle: '500ms', connectionAndInactivity: '200s' },
limit: 1024 * 1024, // 1MB
authorization: 'token123',
filterPrivate: true,
headers: { 'Accept': 'application/json' }
};
// Creates common parameter assertion
const createParamAssertion = (uri) => {
return expect.objectContaining({
url: uri || commonTestParams.uri,
timeout: commonTestParams.timeout,
maxContentLength: commonTestParams.limit,
responseType: 'stream',
headers: expect.objectContaining({
'Accept': 'application/json',
'Authorization': commonTestParams.authorization
})
});
};
describe('HTTP Request Functionality', () => {
beforeEach(() => {
// Reset all mocks before each test
jest.clearAllMocks();
});
afterAll(() => {
// Clean up all mocks
jest.restoreAllMocks();
});
describe('downloadUrlPromise', () => {
test('properly handles content streaming', async () => {
// Create mock data
const mockData = 'Sample data content';
// Mock successful response with stream
const mockResponse = {
status: 200,
headers: {
'content-type': 'text/plain',
'content-length': String(Buffer.byteLength(mockData, 'utf8'))
},
data: createMockStream(mockData)
};
// Setup axios mock - axios() is used directly in the code, not axios.get()
axios.mockImplementation((config) => {
console.log('Mock axios called with config:', JSON.stringify(config, null, 2));
return Promise.resolve(mockResponse);
});
// Create a proper writable stream for testing
const mockStreamWriter = createMockWriter();
// Test version with stream writer (returns undefined)
const resultWithStreamWriter = await utils.downloadUrlPromise(
ctx,
'https://example.com/file',
commonTestParams.timeout,
commonTestParams.limit,
null,
false,
null,
mockStreamWriter
);
// Verify axios was called
expect(axios).toHaveBeenCalledTimes(1);
// With stream writer, the function returns undefined
expect(resultWithStreamWriter).toBeUndefined();
});
test('returns complete response without stream writer', async () => {
// Create mock data
const mockData = JSON.stringify({ data: 'test content' });
// Mock successful response with stream
const mockResponse = {
status: 200,
headers: {
'content-type': 'application/json',
'content-length': String(Buffer.byteLength(mockData, 'utf8'))
},
data: createMockStream(mockData)
};
// Reset mocks and setup new behavior
jest.clearAllMocks();
axios.mockImplementation(() => Promise.resolve(mockResponse));
// Call function without stream writer
const result = await utils.downloadUrlPromise(
ctx,
'https://example.com/data',
commonTestParams.timeout,
commonTestParams.limit,
null,
false,
null,
null // No stream writer
);
// Verify axios was called
expect(axios).toHaveBeenCalledTimes(1);
// Verify full response object is returned
expect(result).toBeDefined();
expect(result).toHaveProperty('response', mockResponse);
expect(result).toHaveProperty('sha256');
expect(result).toHaveProperty('body');
expect(result.sha256).toMatch(/^[a-f0-9]{64}$/i);
expect(Buffer.isBuffer(result.body)).toBe(true);
});
test('throws error on non-200 status codes', async () => {
// Create error data
const errorData = JSON.stringify({ error: 'Not found' });
// Mock error response with stream
const mockErrorResponse = {
status: 404,
statusText: 'Not Found',
headers: {
'content-type': 'application/json',
'content-length': String(Buffer.byteLength(errorData, 'utf8'))
},
data: createMockStream(errorData)
};
// Reset mocks and setup new behavior for error
jest.clearAllMocks();
axios.mockImplementation(() => Promise.resolve(mockErrorResponse));
// Call function and expect it to throw
await expect(utils.downloadUrlPromise(
ctx,
'https://example.com/not-found',
commonTestParams.timeout,
commonTestParams.limit,
null,
false,
null,
null
)).rejects.toThrow(/Error response: statusCode:404/);
// Verify axios was called
expect(axios).toHaveBeenCalledTimes(1);
});
test('throws error when content-length exceeds limit', async () => {
// Create large data (but mock only returns the header)
const mockResponse = {
status: 200,
headers: {
'content-type': 'application/json',
'content-length': '2097152' // 2MB (greater than 1MB limit)
},
data: createMockStream('{}') // actual data is irrelevant, header check happens first
};
// Reset mocks and setup new behavior
jest.clearAllMocks();
axios.mockImplementation(() => Promise.resolve(mockResponse));
// Call function with a 1MB limit and expect it to throw
await expect(utils.downloadUrlPromise(
ctx,
'https://example.com/large-file',
commonTestParams.timeout,
1024 * 1024, // 1MB limit
null,
false,
null,
null
)).rejects.toThrow(/EMSGSIZE: Error response: content-length/);
// Verify axios was called
expect(axios).toHaveBeenCalledTimes(1);
});
test('follows redirects correctly', async () => {
// Create a counter to track calls
let callCount = 0;
// Mock redirect response
const redirectResponse = {
status: 302,
headers: {
location: 'https://example.com/redirected'
}
};
// Mock success response after redirect
const successData = JSON.stringify({ success: true });
const successResponse = {
status: 200,
headers: {
'content-type': 'application/json',
'content-length': String(Buffer.byteLength(successData, 'utf8'))
},
data: createMockStream(successData)
};
// Reset mocks and implement redirect then success
jest.clearAllMocks();
axios.mockImplementation(() => {
if (callCount === 0) {
callCount++;
// First call - simulate redirect by throwing error with response
const err = new Error('Redirect');
err.response = redirectResponse;
return Promise.reject(err);
} else {
// Second call - return success
return Promise.resolve(successResponse);
}
});
// Call function with original URL
const result = await utils.downloadUrlPromise(
ctx,
'https://example.com/original',
commonTestParams.timeout,
commonTestParams.limit,
null,
false,
null,
null
);
// Verify axios was called twice (once for original, once for redirect)
expect(axios).toHaveBeenCalledTimes(2);
// Verify the result is from the successful redirect
expect(result).toBeDefined();
expect(result).toHaveProperty('response');
expect(result).toHaveProperty('sha256');
expect(result.response.status).toBe(200);
});
test('handles network errors correctly', async () => {
// Reset mocks and implement network error
jest.clearAllMocks();
axios.mockImplementation(() => {
const err = new Error('Network Error');
return Promise.reject(err);
});
// Call function and expect network error
await expect(utils.downloadUrlPromise(
ctx,
'https://example.com/network-error',
commonTestParams.timeout,
commonTestParams.limit,
null,
false,
null,
null
)).rejects.toThrow('Network Error');
// Verify axios was called
expect(axios).toHaveBeenCalledTimes(1);
});
test('handles binary data correctly', async () => {
// Create binary data (simple buffer with pattern of bytes)
const binaryData = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]); // PNG file signature
// Mock successful response with binary stream
const mockResponse = {
status: 200,
headers: {
'content-type': 'image/png',
'content-length': String(binaryData.length)
},
data: createMockStream(binaryData)
};
// Reset mocks and setup for binary data
jest.clearAllMocks();
axios.mockImplementation(() => Promise.resolve(mockResponse));
// Call function without stream writer
const result = await utils.downloadUrlPromise(
ctx,
'https://example.com/image.png',
commonTestParams.timeout,
commonTestParams.limit,
null,
false,
null,
null
);
// Verify axios was called
expect(axios).toHaveBeenCalledTimes(1);
// Verify binary data is preserved correctly
expect(result).toBeDefined();
expect(result).toHaveProperty('body');
expect(Buffer.isBuffer(result.body)).toBe(true);
expect(result.body.length).toBe(binaryData.length);
// Verify the content matches the original binary data
expect(Buffer.compare(result.body, binaryData)).toBe(0);
});
test('handles timeout correctly', async () => {
jest.useFakeTimers();
// Mock a never-resolving request
axios.mockImplementation((config) => {
return new Promise((_, reject) => {
if (config.cancelToken) {
config.cancelToken.promise.then(cancel => {
reject(new axiosReal.Cancel(cancel.message));
});
}
});
});
const promise = utils.downloadUrlPromise(
ctx,
'https://example.com/timeout-test',
{ wholeCycle: '500ms', connectionAndInactivity: '200s' },
1024,
null,
false,
null,
null
);
// Fast-forward exactly 1 second
jest.advanceTimersByTime(1000);
await Promise.resolve(); // Flush pending promises
await expect(promise).rejects.toThrow('ETIMEDOUT: 500ms');
jest.useRealTimers();
});
test('throws an error on max redirects limit reached', async () => {
// Create a counter to track calls
let callCount = 0;
// Mock redirect response
const redirectResponse = {
status: 302,
headers: {
location: 'https://example.com/redirected'
}
};
// Mock success response after redirect
const successData = JSON.stringify({ success: true });
const successResponse = {
status: 200,
headers: {
'content-type': 'application/json',
'content-length': String(Buffer.byteLength(successData, 'utf8'))
},
data: createMockStream(successData)
};
// Reset mocks and implement redirect then success
jest.clearAllMocks();
axios.mockImplementation(() => {
if (callCount < 12) {
callCount++;
// First call - simulate redirect by throwing error with response
const err = new Error('Redirect');
err.response = redirectResponse;
return Promise.reject(err);
} else {
// Second call - return success
return Promise.resolve(successResponse);
}
});
// Call function with original URL
await expect(utils.downloadUrlPromise(
ctx,
'https://example.com/original',
commonTestParams.timeout,
commonTestParams.limit,
null,
false,
null,
null
)).rejects.toThrow('Redirect');
expect(axios).toHaveBeenCalledTimes(11);
});
test('should block external request', async () => {
const addExternalRequestOptionsMock = jest.spyOn(utils, 'addExternalRequestOptions');
addExternalRequestOptionsMock.mockReturnValue(false);
await expect(utils.downloadUrlPromise(
ctx,
'https://example.com/original',
commonTestParams.timeout,
commonTestParams.limit,
null,
false,
null,
null
)).rejects.toThrow('Block external request. See externalRequest config options');
addExternalRequestOptionsMock.mockRestore();
});
test('should throw error on redirect with followRedirect=false', async () => {
let callCount = 0;
// Mock redirect response
const redirectResponse = {
status: 302,
headers: {
location: 'https://example.com/redirected'
}
};
// Mock success response after redirect
const successData = JSON.stringify({ success: true });
const successResponse = {
status: 200,
headers: {
'content-type': 'application/json',
'content-length': String(Buffer.byteLength(successData, 'utf8'))
},
data: createMockStream(successData)
};
// Reset mocks and implement redirect then success
jest.clearAllMocks();
axios.mockImplementation(() => {
if (callCount < 2) {
callCount++;
// First call - simulate redirect by throwing error with response
const err = new Error('Redirect');
err.response = redirectResponse;
return Promise.reject(err);
} else {
// Second call - return success
return Promise.resolve(successResponse);
}
});
const ctx = {
getCfg: function(key, _) {
switch (key) {
case 'services.CoAuthoring.requestDefaults':
return {
"headers": {
"User-Agent": "Node.js/6.13",
"Connection": "Keep-Alive"
},
"decompress": true,
"rejectUnauthorized": true,
"followRedirect": false
}
case 'services.CoAuthoring.token.outbox.header':
return "Authorization";
case 'services.CoAuthoring.token.outbox.prefix':
return "Bearer ";
case 'externalRequest.action':
return {
"allow": true,
"blockPrivateIP": true,
"proxyUrl": "",
"proxyUser": {
"username": "",
"password": ""
},
"proxyHeaders": {
}
};
case 'services.CoAuthoring.request-filtering-agent':
return {
"allowPrivateIPAddress": false,
"allowMetaIPAddress": false
}
case 'externalRequest.directIfIn':
return {
"allowList": [],
"jwtToken": true
}
}
},
logger: {
debug: function() {},
}
}
await expect(utils.downloadUrlPromise(
ctx,
'https://example.com/original',
commonTestParams.timeout,
commonTestParams.limit,
null,
false,
null,
null
)).rejects.toThrow('Redirect');
});
});
describe('postRequestPromise', () => {
test('properly sends post data and returns response', async () => {
// Mock successful response
const mockData = { success: true };
const mockResponse = {
status: 200,
headers: { 'content-type': 'application/json' },
data: mockData
};
// Setup axios mock
axios.mockImplementation(() => Promise.resolve(mockResponse));
// Call the function
const result = await utils.postRequestPromise(
ctx,
'https://example.com/data',
{ key: 'value' },
null,
null,
commonTestParams.timeout,
commonTestParams.authorization,
false,
null
);
// Verify axios was called with the correct configuration
expect(axios).toHaveBeenCalledWith(expect.objectContaining({
method: 'post',
url: 'https://example.com/data',
data: { key: 'value' },
validateStatus: expect.any(Function),
timeout: expect.any(Number),
}));
expect(result).toBeDefined();
expect(result).toHaveProperty('response');
expect(result.response.statusCode).toBe(200);
expect(result.response.body).toBe(mockData);
});
test('handles timeout and cancels request', async () => {
// Mock cancellation
const cancelMessage = 'Whole request cycle timeout: 500ms';
axios.mockImplementation(() => {
const error = new Error(cancelMessage);
error.code = 'ETIMEDOUT';
throw error;
});
// Call function and expect it to throw ETIMEDOUT error
await expect(utils.postRequestPromise(
ctx,
'https://example.com/data',
{ key: 'value' },
null,
null,
commonTestParams.timeout,
commonTestParams.authorization,
false,
null
)).rejects.toThrowError(cancelMessage);
// Verify axios was called
expect(axios).toHaveBeenCalledTimes(1);
});
test('throws error for non-200 status codes', async () => {
// Create mock error data
const errorData = JSON.stringify({ error: 'Not found' });
const mockErrorResponse = {
status: 404,
statusText: 'Not Found',
headers: {
'content-type': 'application/json',
'content-length': String(Buffer.byteLength(errorData, 'utf8'))
},
data: errorData
};
// Setup axios mock
axios.mockImplementation(() => Promise.reject({ response: mockErrorResponse }));
// Call function and expect it to throw error
await expect(utils.postRequestPromise(
ctx,
'https://example.com/not-found',
{ key: 'value' },
null,
null,
commonTestParams.timeout,
commonTestParams.authorization,
false,
null
)).rejects.toThrowError(/Error response: statusCode:404/);
// Verify axios was called
expect(axios).toHaveBeenCalledTimes(1);
});
test('handles post data stream correctly', async () => {
// Mock successful response with stream
const mockData = 'Sample streamed content';
const mockResponse = {
status: 200,
headers: {
'content-type': 'application/json',
'content-length': String(Buffer.byteLength(mockData, 'utf8'))
},
data: mockData
};
// Setup axios mock
axios.mockImplementation(() => Promise.resolve(mockResponse));
// Call the function with postDataStream
const result = await utils.postRequestPromise(
ctx,
'https://example.com/upload',
null,
mockData,
null,
commonTestParams.timeout,
commonTestParams.authorization,
false,
null
);
expect(axios).toHaveBeenCalledWith(expect.objectContaining({
method: 'post',
url: 'https://example.com/upload',
headers: expect.objectContaining({
'Authorization': 'Bearer token123'
}),
data: mockData,
validateStatus: expect.any(Function),
timeout: expect.any(Number),
}));
// Verify result
expect(result).toBeDefined();
expect(result.response.statusCode).toBe(200);
expect(result.response.body).toBe(mockData);
});
test('handles network errors correctly', async () => {
// Mock network error
const networkError = new Error('Network Error');
axios.mockImplementation(() => Promise.reject(networkError));
// Call function and expect it to throw network error
await expect(utils.postRequestPromise(
ctx,
'https://example.com/network-error',
{ key: 'value' },
null,
null,
commonTestParams.timeout,
commonTestParams.authorization,
false,
null
)).rejects.toThrowError('Network Error');
// Verify axios was called
expect(axios).toHaveBeenCalledTimes(1);
});
})
});