Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Extend support for Postman random functions/dynamic variables/replaceIn #92

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore linting for
lib/shim/faker.js
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore prettier for
lib/shim/faker.js
88 changes: 88 additions & 0 deletions example/v2/replaceIn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"info": {
"_postman_id": "00ff76ea-1b0e-4ad7-910a-3eb13a7bfa8e",
"name": "replaceIn",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Post with collectionVariable set with replaceIn",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
"pm.collectionVariables.set(\"email\", pm.variables.replaceIn(\"{{$randomEmail}}\"));",
"pm.collectionVariables.set(\"name\", pm.variables.replaceIn(\"{{$randomFirstName}}\"));",
"pm.collectionVariables.set(\"phone_number\", pm.variables.replaceIn(\"{{$randomPhoneNumber}}\"));",
"pm.collectionVariables.set(\"id\", pm.variables.replaceIn(\"{{$guid}}\"));"
],
"type": "text/javascript"
}
},
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"",
"const resp = pm.response.json();",
"",
"pm.test(\"Check response consistency\", function () {",
" pm.expect(resp.json.email).to.be.equal(pm.collectionVariables.get(\"email\"));",
" pm.expect(resp.json.name).to.be.equal(pm.collectionVariables.get(\"name\"));",
" pm.expect(resp.json.phone_number).to.be.equal(pm.collectionVariables.get(\"phone_number\"));",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"email\": \"{{email}}\",\n \"name\": \"{{name}}\",\n \"phone_number\": \"{{phone_number}}\",\n \"id\" : \"{{id}}\"\n}"
},
"url": {
"raw": "httpbin.org/post",
"host": [
"httpbin",
"org"
],
"path": [
"post"
]
}
},
"response": []
}
],
"variable": [
{
"key": "email",
"value": ""
},
{
"key": "name",
"value": ""
},
{
"key": "phone_number",
"value": ""
},
{
"key": "id",
"value": ""
}
]
}
18 changes: 14 additions & 4 deletions lib/shim/core.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import k6 from 'k6';
import http from 'k6/http';

/* Load dynamic variables */
import { dynamicGenerators, dynamicGeneratorsRegex } from './dynamic.js';

/* Constants */
const undef = void 0; /* eslint-disable-line no-void */

Expand Down Expand Up @@ -161,20 +164,20 @@ const dynamic = {
guid() {
return guid();
},

/* Random integer [0,1000] */
randomInt() {
return Math.floor(Math.random() * 1001);
},

/* Current time as Unix timestamp */
timestamp() {
return Date.now();
},
};

function computeDynamic(name) {
if (dynamic[name]) {
return dynamic[name]();
const functionName = '$' + name;
if (dynamicGenerators[functionName]) {
return dynamicGenerators[functionName].generator();
} else {
throw new Error(`Unsupported dynamic variable: ${name}`);
}
Expand Down Expand Up @@ -537,6 +540,13 @@ const pm = Object.freeze({
requireRequest();
scope.local = scope.local[Write](name, value);
},
replaceIn(template) {
return template.replace(dynamicGeneratorsRegex, function(match) {
// We remove the enclosing {{ }} to extract the generator type like "$randomName"
const generatorType = match.slice(2, -2);
return dynamicGenerators[generatorType].generator();
});
},
}),

collectionVariables: Object.freeze({
Expand Down
Loading