Innovenergy_trunk/frontend/node_modules/testcafe/lib/test-run/request/create-request-options.js

175 lines
28 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRequestOptions = void 0;
const lodash_1 = require("lodash");
const is_stream_1 = __importDefault(require("is-stream"));
const interfaces_1 = require("./interfaces");
const testcafe_hammerhead_1 = require("testcafe-hammerhead");
const content_types_1 = __importDefault(require("../../assets/content-types"));
const http_headers_1 = __importDefault(require("../../utils/http-headers"));
const types_1 = require("../../errors/types");
const runtime_1 = require("../../errors/runtime");
const actions_1 = require("../commands/actions");
const DEFAULT_ACCEPT = { [http_headers_1.default.accept]: `${content_types_1.default.json}, ${content_types_1.default.textPlain}, ${content_types_1.default.all}` };
const METHODS_WITH_CONTENT_TYPE = ['post', 'put', 'patch'];
const DEFAULT_REQUEST_METHOD = 'GET';
const DEFAULT_PROTOCOL = 'http:';
function setContentTypeIfNotExists(headers, value) {
if (!(0, lodash_1.isUndefined)(headers) && (0, lodash_1.isUndefined)(headers[http_headers_1.default.contentType]))
headers[http_headers_1.default.contentType] = value;
}
function typeOf(value) {
if (value === null)
return 'null';
if (value && typeof value === 'object')
return value.constructor.name.toLowerCase();
return typeof value;
}
function transformBody(headers, body) {
if (!body)
return Buffer.from('');
if (typeOf(body) === 'formdata' ||
typeOf(body) === 'file' ||
typeOf(body) === 'blob' ||
(0, lodash_1.isArrayBuffer)(body) ||
(0, lodash_1.isBuffer)(body) ||
(0, is_stream_1.default)(body))
return Buffer.from(body);
else if (ArrayBuffer.isView(body))
return Buffer.from(body.buffer);
else if (body instanceof URLSearchParams) {
setContentTypeIfNotExists(headers, `${content_types_1.default.urlencoded};charset=utf-8`);
return Buffer.from(body.toString());
}
else if ((0, lodash_1.isObject)(body) || headers && headers[http_headers_1.default.contentType] === content_types_1.default.json) {
setContentTypeIfNotExists(headers, content_types_1.default.json);
return Buffer.from(JSON.stringify(body));
}
else if (typeof body === 'string')
setContentTypeIfNotExists(headers, content_types_1.default.textPlain);
return body;
}
function getAuthString(auth) {
return 'Basic ' + Buffer.from(auth.username + ':' + auth.password, 'utf8').toString('base64');
}
function changeHeaderNamesToLowercase(headers) {
const lowerCaseHeaders = {};
Object.keys(headers).forEach(headerName => {
lowerCaseHeaders[headerName.toLowerCase()] = headers[headerName];
});
return lowerCaseHeaders;
}
async function prepareHeaders(headers, currentPageUrl, url, body, testRun, withCredentials, options) {
var _a;
const { host, origin } = url;
const preparedHeaders = Object.assign({}, DEFAULT_ACCEPT, changeHeaderNamesToLowercase(headers));
preparedHeaders[http_headers_1.default.host] = host;
preparedHeaders[http_headers_1.default.origin] = origin;
preparedHeaders[http_headers_1.default.contentLength] = body.length;
if (headers.method && METHODS_WITH_CONTENT_TYPE.includes(String(headers.method)))
preparedHeaders[http_headers_1.default.contentType] = content_types_1.default.urlencoded;
if (options.auth && withCredentials)
preparedHeaders[http_headers_1.default.authorization] = getAuthString(options.auth);
if ((_a = options.proxy) === null || _a === void 0 ? void 0 : _a.auth)
preparedHeaders[http_headers_1.default.proxyAuthorization] = getAuthString(options.proxy.auth);
if (withCredentials) {
const currentPageCookies = testRun.session.cookies.getHeader({
url: currentPageUrl.href,
hostname: currentPageUrl.hostname,
});
if (currentPageCookies)
preparedHeaders[http_headers_1.default.cookie] = currentPageCookies;
}
//NOTE: Additional header to recognize API requests in the hammerhead
preparedHeaders[http_headers_1.default.isApiRequest] = 'true';
return preparedHeaders;
}
async function prepareUrl(testRun, currentPageUrl, url, callsite) {
let preparedUrl;
try {
preparedUrl = url instanceof URL
? url
: new URL(url, currentPageUrl.hostname ? currentPageUrl.origin : void 0);
}
catch (e) {
throw new runtime_1.APIError(callsite, types_1.RUNTIME_ERRORS.requestUrlInvalidValueError, url);
}
return preparedUrl;
}
function prepareSearchParams(url, params) {
if (!params)
return url;
let searchParams;
if (params instanceof URLSearchParams)
searchParams = params;
else {
searchParams = new URLSearchParams();
for (const key in params) {
if (!params[key])
continue;
(0, lodash_1.castArray)(params[key]).forEach(v => {
searchParams.append(key, typeof v === 'object' ? JSON.stringify(v) : String(v));
});
}
}
return `${url}${url.includes('?') ? '&' : '?'}${searchParams.toString()}`;
}
function getProxyUrl(testRun, url, withCredentials) {
return testRun.executeCommand(new actions_1.GetProxyUrlCommand({
url: url,
options: { credentials: withCredentials ? interfaces_1.Credentials.include : interfaces_1.Credentials.omit },
}, testRun, true));
}
async function createRequestOptions(currentPageUrl, testRun, options, callsite) {
options.headers = options.headers || {};
const url = await prepareUrl(testRun, currentPageUrl, options.url, callsite);
const withCredentials = !currentPageUrl.host || (0, testcafe_hammerhead_1.sameOriginCheck)(currentPageUrl.href, url.href) || options.withCredentials || false;
const body = transformBody(options.headers, options.body);
const headers = await prepareHeaders(options.headers, currentPageUrl, url, body, testRun, withCredentials, options);
const proxyUrl = await getProxyUrl(testRun, url.href, withCredentials);
const proxyUrlObj = (0, testcafe_hammerhead_1.parseProxyUrl)(proxyUrl);
let auth = options.auth;
if (!auth && url.username && url.password) {
auth = {
username: url.username,
password: url.password,
};
}
const requestParams = {
method: options.method || DEFAULT_REQUEST_METHOD,
url: proxyUrl,
protocol: DEFAULT_PROTOCOL,
hostname: proxyUrlObj.proxy.hostname,
host: proxyUrlObj.proxy.hostname,
port: proxyUrlObj.proxy.port,
path: prepareSearchParams(proxyUrlObj.partAfterHost, options.params),
auth: auth && withCredentials ? `${auth.username}:${auth.password}` : void 0,
headers: headers,
credentials: withCredentials ? testRun.session.getAuthCredentials() : void 0,
body: body,
disableHttp2: testRun.session.isHttp2Disabled(),
requestTimeout: {
ajax: options.timeout,
page: options.timeout,
},
};
if (options.proxy) {
requestParams.externalProxySettings = {
host: options.proxy.host,
hostname: options.proxy.host,
port: options.proxy.port.toString(),
proxyAuth: options.proxy.auth ? `${options.proxy.auth.username}:${options.proxy.auth.password}` : void 0,
};
requestParams.protocol = url.protocol;
requestParams.host = url.host;
requestParams.hostname = url.hostname;
requestParams.port = url.port;
requestParams.path = prepareSearchParams(url.pathname + url.search, options.params);
}
return new testcafe_hammerhead_1.RequestOptions(requestParams);
}
exports.createRequestOptions = createRequestOptions;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY3JlYXRlLXJlcXVlc3Qtb3B0aW9ucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy90ZXN0LXJ1bi9yZXF1ZXN0L2NyZWF0ZS1yZXF1ZXN0LW9wdGlvbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBQ0EsbUNBTWdCO0FBQ2hCLDBEQUFpQztBQUNqQyw2Q0FLc0I7QUFDdEIsNkRBSzZCO0FBRTdCLCtFQUF1RDtBQUN2RCw0RUFBb0Q7QUFDcEQsOENBQW9EO0FBQ3BELGtEQUFnRDtBQUNoRCxpREFBeUQ7QUFHekQsTUFBTSxjQUFjLEdBQWMsRUFBRSxDQUFDLHNCQUFZLENBQUMsTUFBTSxDQUFDLEVBQUUsR0FBRyx1QkFBYSxDQUFDLElBQUksS0FBSyx1QkFBYSxDQUFDLFNBQVMsS0FBSyx1QkFBYSxDQUFDLEdBQUcsRUFBRSxFQUFFLENBQUM7QUFDdkksTUFBTSx5QkFBeUIsR0FBRyxDQUFDLE1BQU0sRUFBRSxLQUFLLEVBQUUsT0FBTyxDQUFDLENBQUM7QUFDM0QsTUFBTSxzQkFBc0IsR0FBTSxLQUFLLENBQUM7QUFDeEMsTUFBTSxnQkFBZ0IsR0FBWSxPQUFPLENBQUM7QUFFMUMsU0FBUyx5QkFBeUIsQ0FBRSxPQUE0QixFQUFFLEtBQWE7SUFDM0UsSUFBSSxDQUFDLElBQUEsb0JBQVcsRUFBQyxPQUFPLENBQUMsSUFBSSxJQUFBLG9CQUFXLEVBQUMsT0FBTyxDQUFDLHNCQUFZLENBQUMsV0FBVyxDQUFDLEN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