Skip to content

Instantly share code, notes, and snippets.

@bogdanovna
Last active September 15, 2024 11:31
Show Gist options
  • Save bogdanovna/559b614726b7ece839965f0c88085e4d to your computer and use it in GitHub Desktop.
Save bogdanovna/559b614726b7ece839965f0c88085e4d to your computer and use it in GitHub Desktop.
this code should be saved into collection variables for example as "magic" and will be call with ".eval(pm.collectionVariables.get('magic'))" from postman tests
const magic = {
res_jbody_to_env: [
{
access: ['data', 'access_token']
},
],
req_jbody_to_env: [
{
email: ["email"],
name: ["name"]
},
]
}
eval(pm.collectionVariables.get('magic'));
pm.test(pm.request.name, () => {
const {
res_codes = [200, 201, 202, 203, 204],
res_jbody_to_env,
res_jbody_to_col,
req_jbody_to_env,
req_jbody_to_col,
} = magic;
const actual_res_code = pm.response.code;
if (!res_codes.include(actual_res_code)) {
pm.expect(res_code).to.be.equals(res_code);
return;
}
if (res_jbody_to_env) {
const [source, options] = res_jbody_to_env;
mapping(source, pm.response.json(), "environment", options);
}
if (res_jbody_to_col) {
const [source, options] = res_jbody_to_col;
mapping(source, pm.response.json(), "collectionVariables", options);
}
if (req_jbody_to_env) {
const [source, options] = req_jbody_to_env;
mapping(source, JSON.parse(pm.request.body.raw), "environment", options);
}
if (req_jbody_to_col) {
const [source, options] = req_jbody_to_col;
mapping(
source,
JSON.parse(pm.request.body.raw),
"collectionVariables",
options
);
}
});
function mapping(mapping, source, destination, options = {}) {
if (!mapping) return;
const { override = true } = options;
Object.entries(mapping).forEach(([k, path]) => {
const value = path.reduce((acc, p) => acc[p], source);
if (!override && pm[destination].has(k)) return;
pm[destination].set(k, value);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment