From f9a1c04679d40060fe4a993b833e1e3824cc32a9 Mon Sep 17 00:00:00 2001 From: Anudeep Date: Tue, 30 Apr 2024 21:56:01 +0530 Subject: [PATCH] use data maps --- package-lock.json | 4 ++-- package.json | 2 +- src/exports/stash.d.ts | 2 +- src/helpers/requestProcessor.js | 20 +++++++++++++++++--- src/models/Spec.d.ts | 4 ++++ src/models/Spec.js | 16 +++++++++++----- src/models/Tosser.js | 2 +- test/component/useDataMap.spec.js | 30 ++++++++++++++++++++++++++++++ 8 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 test/component/useDataMap.spec.js diff --git a/package-lock.json b/package-lock.json index a473a32..e96dab3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "pactum", - "version": "3.6.6", + "version": "3.6.8", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "pactum", - "version": "3.6.6", + "version": "3.6.8", "license": "MIT", "dependencies": { "@exodus/schemasafe": "^1.3.0", diff --git a/package.json b/package.json index 261c8c7..a1e57c8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/exports/stash.d.ts b/src/exports/stash.d.ts index b454707..06c631a 100644 --- a/src/exports/stash.d.ts +++ b/src/exports/stash.d.ts @@ -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 }); */ diff --git a/src/helpers/requestProcessor.js b/src/helpers/requestProcessor.js index 4cea1b9..a4aff2d 100644 --- a/src/helpers/requestProcessor.js +++ b/src/helpers/requestProcessor.js @@ -1,4 +1,5 @@ const { URL } = require('url'); +const stash = require('../exports/stash'); const processor = require('./dataProcessor'); const helper = require('./helper'); const config = require('../config'); @@ -6,7 +7,8 @@ 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); @@ -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; @@ -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') { @@ -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(); diff --git a/src/models/Spec.d.ts b/src/models/Spec.d.ts index f4ab9a9..ff7c1b4 100644 --- a/src/models/Spec.d.ts +++ b/src/models/Spec.d.ts @@ -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 diff --git a/src/models/Spec.js b/src/models/Spec.js index 192497a..8b25463 100644 --- a/src/models/Spec.js +++ b/src/models/Spec.js @@ -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 || {}; @@ -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; @@ -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; } @@ -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; } diff --git a/src/models/Tosser.js b/src/models/Tosser.js index 3ee8556..d5fca2c 100644 --- a/src/models/Tosser.js +++ b/src/models/Tosser.js @@ -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 diff --git a/test/component/useDataMap.spec.js b/test/component/useDataMap.spec.js new file mode 100644 index 0000000..7c91826 --- /dev/null +++ b/test/component/useDataMap.spec.js @@ -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}' + }); + }); + +}); \ No newline at end of file