Repository: pactumjs/pactum (js)

Issue:
Query param without value
### Discussed in https://github.com/pactumjs/pactum/discussions/231

<div type='discussions-op-text'>

<sup>Originally posted by **crystalfp** October 30, 2022</sup>
If I want to pass a boolean query parameter to a GET request like:
`http://localhost/api/users?refresh` that is, without passing a value to the query parameter, I should do this:

```js
await spec()
  .get(endpointUsers)
  .withQueryParams("refresh", "")
  .expectStatus(200)
  .toss();
```

That call generates the following GET: `http://localhost/api/users?refresh=`

Do you think could be a good idea to modify ` .withQueryParams()` to accept the key only and generate the original GET? I know, it is a minor feature, but cost almost nothing to implement.

Thanks for considering!
mario</div>

Pull request:
feat: support query params without values
closes #235 

Patch:
diff --git a/src/helpers/helper.js b/src/helpers/helper.js
index 917a42b..858bcc8 100644
--- a/src/helpers/helper.js
+++ b/src/helpers/helper.js
@@ -25,16 +25,21 @@ const helper = {
   },
 
   getPlainQuery(query) {
-    let plainQuery = '';
+    let plain_query = '';
     if (typeof query === 'object') {
-      for (const prop in query) {
-        if (plainQuery !== '') {
-          plainQuery = plainQuery + '&';
+      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]}`;
         }
-        plainQuery = plainQuery + `${prop}=${query[prop]}`;
       }
     }
-    return plainQuery;
+    return plain_query;
   },
 
   isValidObject(value) {
diff --git a/src/models/Spec.d.ts b/src/models/Spec.d.ts
index 5fb95cb..f46d2c5 100644
--- a/src/models/Spec.d.ts
+++ b/src/models/Spec.d.ts
@@ -120,7 +120,7 @@ declare class Spec {
    * adds query params to the request url - /comments?user=snow&sort=asc
    * @see https://pactumjs.github.io/api/requests/withQueryParams.html
    */
-  withQueryParams(key: string, value: any): Spec;
+  withQueryParams(key: string, value?: any): Spec;
   withQueryParams(params: object): Spec;
 
   /**
diff --git a/src/models/Spec.js b/src/models/Spec.js
index bc70b43..bf90af7 100644
--- a/src/models/Spec.js
+++ b/src/models/Spec.js
@@ -148,9 +148,6 @@ class Spec {
       if (!helper.isValidString(key)) {
         throw new PactumRequestError('`key` is required');
       }
-      if (value === undefined || value === null) {
-        throw new PactumRequestError('`value` is required');
-      }
       this._request.queryParams[key] = value;
     } else {
       if (!helper.isValidObject(key) || Object.keys(key).length === 0) {


Test patch:
diff --git a/test/component/withQueryParams.spec.js b/test/component/withQueryParams.spec.js
new file mode 100644
index 0000000..a73d79e
--- /dev/null
+++ b/test/component/withQueryParams.spec.js
@@ -0,0 +1,70 @@
+const pactum = require('../../src/index');
+
+describe('withQueryParams', () => {
+
+  describe('should process', () => {
+    
+    it('without value', async () => {
+      await pactum.spec()
+        .useInteraction({
+          request: {
+            method: 'GET',
+            path: '/api/query',
+            queryParams: {
+              refresh: ''
+            }
+          },
+          response: {
+            status: 200
+          }
+        })
+        .get('http://localhost:9393/api/query')
+        .withQueryParams('refresh')
+        .expectStatus(200);
+    });
+
+    it('with multiple query params without values', async () => {
+      await pactum.spec()
+        .useInteraction({
+          request: {
+            method: 'GET',
+            path: '/api/query',
+            queryParams: {
+              refresh: '',
+              id: ''
+            }
+          },
+          response: {
+            status: 200
+          }
+        })
+        .get('http://localhost:9393/api/query')
+        .withQueryParams('refresh')
+        .withQueryParams('id')
+        .expectStatus(200);
+    });
+
+    it('with multiple query params where one without value', async () => {
+      await pactum.spec()
+        .useInteraction({
+          request: {
+            method: 'GET',
+            path: '/api/query',
+            queryParams: {
+              refresh: '',
+              id: '1'
+            }
+          },
+          response: {
+            status: 200
+          }
+        })
+        .get('http://localhost:9393/api/query')
+        .withQueryParams('refresh')
+        .withQueryParams('id', '1')
+        .expectStatus(200);
+    });
+  
+  });
+  
+});
\ No newline at end of file
diff --git a/test/unit/spec.spec.js b/test/unit/spec.spec.js
index 69f5a7d..6736a99 100644
--- a/test/unit/spec.spec.js
+++ b/test/unit/spec.spec.js
@@ -26,28 +26,6 @@ describe('Spec', () => {
     expect(err.toString()).equals('Error: `key` is required');
   });
 
-  it('withQueryParams - no value', () => {
-    let err;
-    try {
-      const spec = new Spec();
-      spec.withQueryParams('some');
-    } catch (error) {
-      err = error;
-    }
-    expect(err.toString()).equals('Error: `value` is required');
-  });
-
-  it('withQueryParams - no value', () => {
-    let err;
-    try {
-      const spec = new Spec();
-      spec.withQueryParams('some', null);
-    } catch (error) {
-      err = error;
-    }
-    expect(err.toString()).equals('Error: `value` is required');
-  });
-
   it('withQueryParams - null', async () => {
     let err;
     try {