@gatling.io/postman
Version:
Gatling Postman adds support for loading Postman collections in the [Gatling load testing tool](https://gatling.io/).
85 lines (84 loc) • 4.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.postmanProtocol = exports.postman = void 0;
const core_1 = require("@gatling.io/core");
const jvm_1 = require("../jvm");
const runtimeData_1 = require("./runtimeData");
const jvm_2 = require("./jvm");
const utils_1 = require("./utils");
(0, jvm_1.onInit)();
exports.postman = {
fromResource: (collectionPath) => postmanCollectionImpl((0, core_1.readResourceAsString)(collectionPath), {}, {}, undefined)
};
const postmanCollectionImpl = (collection, environment, globals, dataFile) => {
const jsonCollection = (0, utils_1.parseCollection)(collection);
const runtimeDataKey = `postman-runtime-data-coll-${jsonCollection.info._postman_id}`;
const feeder = buildFeeder(dataFile);
const initVariables = (0, core_1.exec)((session) => {
const data = feeder !== undefined ? jvm_2.PostmanDataFile.feed(feeder) : {};
const collectionVariables = jsonCollection?.variable?.reduce((acc, v) => (v.disabled === true ? acc : { ...acc, [v.key]: v.value }), {}) ??
{};
const newJvmSession = runtimeData_1.PostmanRuntimeData.createInSession(session._underlying, runtimeDataKey, globals, collectionVariables, environment, data, {});
return (0, core_1.wrapSession)(newJvmSession);
});
return {
environment: (variables) => postmanCollectionImpl(collection, resolveEnvironment(variables), globals, dataFile),
globals: (variables) => postmanCollectionImpl(collection, environment, resolveEnvironment(variables), dataFile),
csvDataFile: (path) => postmanCollectionImpl(collection, environment, globals, { path, type: "csv" }),
jsonDataFile: (path) => postmanCollectionImpl(collection, environment, globals, { path, type: "json" }),
initVariables,
export: {
id: jsonCollection.info._postman_id,
collection,
environment: JSON.stringify(environment),
globals: JSON.stringify(globals)
},
...postmanCollectionFolder(jsonCollection.info._postman_id, [], jsonCollection, initVariables, runtimeDataKey)
};
};
const buildFeeder = (dataFile) => {
if (dataFile === undefined) {
return undefined;
}
else {
const builder = dataFile?.type === "csv" ? (0, core_1.csv)(dataFile.path) : (0, core_1.jsonFile)(dataFile.path);
return builder.circular()._underlying.asScala().apply();
}
};
const resolveEnvironment = (variables) => typeof variables === "string" ? (0, utils_1.readEnvironmentFromResource)(variables) : variables;
const postmanProtocol = (...collections) => ({
_underlying: jvm_2.PostmanDsl.postmanProtocol(collections.map((c) => c.export))
});
exports.postmanProtocol = postmanProtocol;
const postmanRequest = (requestName, collectionId, itemPath, runtimeDataKey) => (0, core_1.wrapActionBuilder)(jvm_2.PostmanDsl.postmanRequest(requestName, collectionId, itemPath, runtimeDataKey));
const postmanCollectionFolder = (collectionId, currentItemGroupPath, currentItemGroup, initVariables, runtimeDataKey) => ({
folder: (folder) => {
const found = (0, utils_1.findItemGroup)(currentItemGroup, folder);
if (found === undefined) {
throw Error(`Postman folder '${folder}' not found`);
}
const [newItemGroup, newIndex] = found;
return postmanCollectionFolder(collectionId, [...currentItemGroupPath, newIndex], newItemGroup, initVariables, runtimeDataKey);
},
request: (request, overrideName) => {
const name = overrideName !== undefined ? overrideName : request;
const found = (0, utils_1.findItem)(currentItemGroup, request);
if (found === undefined) {
throw Error(`Postman request '${request}' not found`);
}
const [_, itemIndex] = found;
return postmanRequest(name, collectionId, [...currentItemGroupPath, itemIndex], runtimeDataKey);
},
scenario: (name, options) => {
let scn = (0, core_1.scenario)(name).exec(initVariables);
const recursive = options?.recursive || false;
const pauses = options?.pauses;
(0, utils_1.listItems)(currentItemGroup, currentItemGroupPath, recursive).forEach(([item, itemPath], idx) => {
if (pauses !== undefined && idx > 0) {
scn = scn.pause(pauses);
}
scn = scn.exec(postmanRequest(item.name, collectionId, itemPath, runtimeDataKey));
});
return scn;
}
});