Skip to content

Commit

Permalink
Merge pull request #243 from pactumjs/242-support-duplicate-query-params
Browse files Browse the repository at this point in the history
feat: support duplicate query params
  • Loading branch information
ASaiAnudeep authored Nov 19, 2022
2 parents 1fd6da3 + 8b01efe commit 360a4f7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion 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.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",
Expand Down
25 changes: 12 additions & 13 deletions src/helpers/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion src/models/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
18 changes: 18 additions & 0 deletions test/component/withQueryParams.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});

Expand Down

0 comments on commit 360a4f7

Please sign in to comment.