Skip to content

Instantly share code, notes, and snippets.

@DannyDainton
Created May 16, 2024 16:36
Show Gist options
  • Save DannyDainton/dd098b730a643f2d2be9b544953ccb5b to your computer and use it in GitHub Desktop.
Save DannyDainton/dd098b730a643f2d2be9b544953ccb5b to your computer and use it in GitHub Desktop.
A list of my reusable functions that I use in Postman's Package Library feature.
/**
* Create a random string length between the speficied min / max values
* @param {Number} minValue - The min value of the string length
* @param {Number} maxValue - The max value of the string length
*/
const randomString = function (minValue, maxValue, dataSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') {
const lodash = require('lodash');
if (!minValue) {
minValue = 10;
maxValue = 10;
}
if (!maxValue) {
maxValue = minValue;
}
let length = lodash.random(minValue, maxValue),
randomText = "";
for (let i = 0; i < length; i++)
randomText += dataSet.charAt(Math.floor(Math.random() * dataSet.length));
return randomText;
}
/**
* Create a random alphanumeric string between the speficied min / max values
* @param {number} minValue - The min value of the string length
* @param {number} maxValue - The max value of the string length
* @param {string} dataSet - Optional. The base dataset to create the alphanumeric string
*/
const randomAlpha = function (minValue, maxValue, dataSet = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') {
return randomString(minValue, maxValue, dataSet);
}
/**
* Converts a date string into the format 'DD/MM/YYYY'
* @param {string} date - The date value to be converted
*/
const formatDate = function (date, format = 'DD/MM/YYYY') {
let moment = require('moment');
return moment(date).format(format);
}
/**
* Converts a Unix Timestamp to the format 'DD/MM/YYYY'
* @param {number} time - the timestamp in unix format
*/
const convertTimestamp = function (time, format = 'DD/MM/YYYY') {
let moment = require('moment');
return moment.unix(time).format(format);
}
/**
* Checks if a given date is after another date
* @param {string} baseDate - The base date to check
* @param {string} comparisonDate - The comparison date to check against
* @returns {Boolean} Returns true if the date is before the given date
* @example .isDateBefore("2004-04-04T04:04:04", "2006-04-04T04:04:04")
*/
const isDateBefore = function (baseDate, comparisonDate) {
let moment = require('moment');
return moment(baseDate).isBefore(comparisonDate);
}
/**
* Checks if a given date is after another date
* @param {string} baseDate - The base date to check
* @param {string} comparisonDate - The comparison date to check against
* @returns {Boolean} Returns true if the date is after the given date
* @example .isDateAfter("2004-04-04T04:04:04", "2006-04-04T04:04:04")
*/
const isDateAfter = function (baseDate, comparisonDate) {
let moment = require('moment');
return moment(baseDate).isAfter(comparisonDate);
}
/**
* Checks if the API response is returned in less than a given value
* @param {number} ms - Expected response time is milliseconds
*/
const checkResponseTime = (ms) => {
pm.test(`Response time should be less than ${ms}`, () => {
pm.expect(pm.response.responseTime).lt(ms);
});
}
/**
* Checks if the API response has a given status code
* @param {number} code - Expected response status code
* @param {string} message - Optional assertion failure message
*/
const checkStatusCode = (code, message = "Status code does not match") => {
pm.test(`Status code should be ${code}`, () => {
pm.expect(pm.response.code, message).to.equal(code);
});
}
/**
* Clears the Environment variables which match a specific prefix value
* @param {string} prefix - The prefix given to a variable that will be cleared after the function is run.
* @example .cleanupEnvVars("temp_")
*/
const cleanupEnvVars = (prefix) => {
const lodash = require('lodash');
const clean = lodash.keys(pm.environment.toObject())
lodash.each(clean, (arrItem) => {
if (arrItem.startsWith(`${prefix}`)) {
pm.environment.unset(arrItem)
}
})
}
module.exports = { randomString, randomAlpha, formatDate, convertTimestamp, isDateBefore, isDateAfter, checkResponseTime, checkStatusCode, cleanupEnvVars }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment