Skip to content

Commit

Permalink
use data maps (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep authored Apr 30, 2024
1 parent f027565 commit ea9686d
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pactum",
"version": "3.6.7",
"version": "3.6.8",
"description": "REST API Testing Tool for all levels in a Test Pyramid",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/exports/stash.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function addDataTemplate(templates: object): void;
export function addDataTemplate(templates: object[]): void;

/**
*
*
* @example
* stash.addDataTemplate('USER:NEW', { name: 'john', age: 28 });
*/
Expand Down
20 changes: 17 additions & 3 deletions src/helpers/requestProcessor.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const { URL } = require('url');
const stash = require('../exports/stash');
const processor = require('./dataProcessor');
const helper = require('./helper');
const config = require('../config');
const fd = require('../plugins/form.data')

const requestProcessor = {

process(request) {
process(request, local_data_maps) {
process_local_data_maps(local_data_maps);
processor.processMaps();
processor.processTemplates();
request = processor.processData(request);
Expand All @@ -26,6 +28,18 @@ const requestProcessor = {

};

/**
*
* @param {any[]} local_data_maps
*/
function process_local_data_maps(local_data_maps) {
if (local_data_maps) {
for (const local_data_map of local_data_maps) {
stash.addDataMap(local_data_map.key, local_data_map.value);
}
}
}

function setBaseUrl(request) {
if (config.request.baseUrl && request.url && !request.url.startsWith('http')) {
request.url = config.request.baseUrl + request.url;
Expand Down Expand Up @@ -107,7 +121,7 @@ function setMultiPartFormData(request) {
let multi_part_form_data;
for (let i = 0; i < request._multi_parts.length; i++) {
const { key, value, options } = request._multi_parts[i];
if (key instanceof FormData) {
if (key instanceof FormData) {
multi_part_form_data = key;
} else {
if (typeof multi_part_form_data === 'undefined') {
Expand All @@ -120,7 +134,7 @@ function setMultiPartFormData(request) {
multi_part_form_data.append(form_key, key[form_key], options);
}
}

}
}
request.data = multi_part_form_data.getBuffer();
Expand Down
4 changes: 4 additions & 0 deletions src/models/Spec.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ declare class Spec {
useInteraction(interaction: Interaction): Spec;
useInteraction(handler: string, data?: any): Spec;

useDataMap(map: object): Spec;
useDataMap(maps: object[]): Spec;
useDataMap(key: string, value: any): Spec;

/**
* The GET method requests a representation of the specified resource.
* @see https://pactumjs.github.io/guides/api-testing.html
Expand Down
16 changes: 11 additions & 5 deletions src/models/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Spec {
this.interactions = [];
this._wait = null;
this._save = null;
this._data_maps = [];
this._specHandlerData = data;
hr.spec(name, data, this);
this._opts = opts || {};
Expand Down Expand Up @@ -69,6 +70,11 @@ class Spec {
return this;
}

useDataMap(key, value) {
this._data_maps.push({ key, value });
return this;
}

get(url) {
validateRequestUrl(this._request, url);
this._request.url = url;
Expand Down Expand Up @@ -157,10 +163,10 @@ class Spec {
throw new PactumRequestError('`key` is required');
}
if (Object.keys(this._request.queryParams).includes(key)) {
if (!Array.isArray(this._request.queryParams[key])) {
this._request.queryParams[key] = [this._request.queryParams[key]];
}
this._request.queryParams[key].push(value);
if (!Array.isArray(this._request.queryParams[key])) {
this._request.queryParams[key] = [this._request.queryParams[key]];
}
this._request.queryParams[key].push(value);
} else {
this._request.queryParams[key] = value;
}
Expand Down Expand Up @@ -430,7 +436,7 @@ class Spec {
expectJsonMatchStrictAt(...args) { return this.expectJsonMatchStrict(...args); }

expectJsonSnapshot(name, value) {
typeof name === 'string' ? this._expect.jsonSnapshots.push({ name, value }): this._expect.jsonSnapshots.push({ value: name });
typeof name === 'string' ? this._expect.jsonSnapshots.push({ name, value }) : this._expect.jsonSnapshots.push({ value: name });
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/models/Tosser.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Tosser {
async toss() {
try {
this.spec.start = Date.now().toString();
this.request = requestProcessor.process(this.request);
this.request = requestProcessor.process(this.request, this.spec._data_maps);
await this.setState();
await this.addInteractionsToServer();
// get interactions to check for background property
Expand Down
30 changes: 30 additions & 0 deletions test/component/useDataMap.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { spec } = require('../../src/index');

describe('useDataMap', () => {

it('with key and value', async () => {
await spec()
.useDataMap('USE_DATA_MAP_PATH', '/default/get')
.useInteraction('default get')
.get('http://localhost:9393/default/get')
.expectStatus(200)
.expectJson({
method: 'GET',
path: '$M{USE_DATA_MAP_PATH}'
});
});

it('with multiple key and value', async () => {
await spec()
.useDataMap('USE_DATA_MAP_METHOD', 'GET')
.useDataMap('USE_DATA_MAP_PATH', '/default/get')
.useInteraction('default get')
.get('http://localhost:9393/default/get')
.expectStatus(200)
.expectJson({
method: '$M{USE_DATA_MAP_METHOD}',
path: '$M{USE_DATA_MAP_PATH}'
});
});

});

0 comments on commit ea9686d

Please sign in to comment.