From 8b01efe7257e733268b3d13f96da963b7486ed9b Mon Sep 17 00:00:00 2001 From: Anudeep Date: Sat, 19 Nov 2022 17:56:59 +0530 Subject: [PATCH] feat: support duplicate query params --- package-lock.json | 2 +- package.json | 2 +- src/helpers/helper.js | 25 ++++++++++++------------- src/models/Spec.js | 9 ++++++++- test/component/withQueryParams.spec.js | 18 ++++++++++++++++++ 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index e38b241..8073c42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "pactum", - "version": "3.2.4", + "version": "3.3.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index ff942e2..d02a9e8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pactum", - "version": "3.2.4", + "version": "3.3.0", "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/helpers/helper.js b/src/helpers/helper.js index 858bcc8..4b9e102 100644 --- a/src/helpers/helper.js +++ b/src/helpers/helper.js @@ -24,22 +24,21 @@ const helper = { return (typeof value === 'string' && value); }, - getPlainQuery(query) { - let plain_query = ''; - if (typeof query === 'object') { - for (const key in query) { - if (plain_query !== '') { - plain_query = plain_query + '&'; - } - const value = query[key]; - if (typeof value === 'undefined') { - plain_query = plain_query + `${key}`; - } else { - plain_query = plain_query + `${key}=${query[key]}`; + getPlainQuery(query = {}) { + const values = []; + for (const key in query) { + const value = query[key]; + if (typeof value === 'undefined') { + values.push(key); + } else if (Array.isArray(value)) { + for (const current_value of value) { + values.push(`${key}=${current_value}`); } + } else { + values.push(`${key}=${value}`); } } - return plain_query; + return values.join('&'); }, isValidObject(value) { diff --git a/src/models/Spec.js b/src/models/Spec.js index bf90af7..0f37e3d 100644 --- a/src/models/Spec.js +++ b/src/models/Spec.js @@ -148,7 +148,14 @@ class Spec { if (!helper.isValidString(key)) { throw new PactumRequestError('`key` is required'); } - this._request.queryParams[key] = value; + 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); + } else { + this._request.queryParams[key] = value; + } } else { if (!helper.isValidObject(key) || Object.keys(key).length === 0) { throw new PactumRequestError('`params` are required'); diff --git a/test/component/withQueryParams.spec.js b/test/component/withQueryParams.spec.js index a73d79e..11c1538 100644 --- a/test/component/withQueryParams.spec.js +++ b/test/component/withQueryParams.spec.js @@ -64,6 +64,24 @@ describe('withQueryParams', () => { .withQueryParams('id', '1') .expectStatus(200); }); + + it('with duplicate query params', async () => { + await pactum.spec() + .useInteraction({ + strict: false, + request: { + method: 'GET', + path: '/api/query' + }, + response: { + status: 200 + } + }) + .get('http://localhost:9393/api/query') + .withQueryParams('filters', 'name=test') + .withQueryParams('filters', 'total>10') + .expectStatus(200); + }); });