Go from this… Pre-Request Script
// Clear env vars at start of run
names = [ "access_token", "refresh_token", "oid_token", "resumeURL", "loginURL", "auth_code", "opentoken" ];
for (index in names) { if (obj.hasOwnProperty(index)) { pm.environment.set(names[index],"") }};
pm.test(pm.info.requestName + "Environment variables cleared", true);
console.info(pm.info.requestName + "Environment variables cleared");
and these Tests
console.info(pm.info.requestName + "OAuth login test started")
if (pm.response.status == "Found") {
if (pm.response.headers.has("Location")) {
pm.environment.set("resumeURL",pm.response.headers.get("Location"));
pm.test(pm.info.requestName + "Successful initialisation", true);
console.info(pm.info.requestName + "Successful initialisation");
} else {
pm.test(pm.info.requestName + "Request Failed: No Location Header", false);
console.info(pm.info.requestName + "Request Failed: No Location Header");
postman.setNextRequest(null);
}
} else if (pm.response.status == "Service Unavailable") {
pm.test(pm.info.requestName + "Service Unavailable: Is your config deployed?", false);
console.info(pm.info.requestName + "Service Unavailable: Is your config deployed?");
postman.setNextRequest(null);
} else {
pm.test(pm.info.requestName + "Request Failed: No Redirect Found", false);
console.info(pm.info.requestName + "Request Failed: No Redirect Found");
postman.setNextRequest(null);
}
To this…
// Clear env vars at start of run
names = [ "access_token", "refresh_token", "oid_token", "resumeURL", "loginURL", "auth_code", "opentoken" ];
for (index in names) { if (obj.hasOwnProperty(index)) { pm.environment.set(names[index],"") }};
pm.pass("Environment variables cleared")
and this much easier to read and clean code
pm.log("OAuth login test started")
if (pm.response.status == "Found") {
if (pm.response.headers.has("Location")) {
pm.environment.set("resumeURL",pm.response.headers.get("Location"));
pm.pass("Successful initialisation");
} else {
pm.fail(postman, "Request Failed: No Location Header");
}
} else if (pm.response.status == "Service Unavailable") {
pm.fail(postman, "Service Unavailable: Is your config deployed?");
} else {
pm.fail(postman, "Request Failed: No Redirect Found");
}
And get test results like this…
And corresponding console logs like this…
All done with these custom pm functions in the Collection level pre-requests
// Custom PM Commands
// pm.fail
Object.prototype.fail = (postman, msg) => {
pm.test(pm.info.requestName, () => { throw new Error(msg) });
console.info(pm.info.requestName + " | " + msg);
postman.setNextRequest(null);
};
// pm.pass
Object.prototype.pass = (msg) => {
pm.test(pm.info.requestName + " | " + msg , true);
console.info(pm.info.requestName + " | " + msg);
};
// pm.log
Object.prototype.log = (msg) => {
console.info(pm.info.requestName + " | " + msg);
};
// Run collection once to load into Global Env space
Enjoy