Repository: postmanlabs/postman-code-generators
Pull request #30 (2019-06-19 09:03:45)

Fixed a bug were query params were ignored if the url provided has no path
Fixed a bug where query params were ignored if the url provided has no path [GitHub#5274]


Patch:
diff --git a/codegens/nodejs-native/lib/parseRequest.js b/codegens/nodejs-native/lib/parseRequest.js
index f970e0f..ce7969a 100644
--- a/codegens/nodejs-native/lib/parseRequest.js
+++ b/codegens/nodejs-native/lib/parseRequest.js
@@ -141,7 +141,8 @@ function parseHost (request, indentString) {
 function parsePath (request, indentString) {
     var pathArray = _.get(request, 'url.path'),
         queryArray = _.get(request.toJSON(), 'url.query'),
-        pathSnippet = indentString + '\'path\': \'/';
+        pathSnippet = indentString + '\'path\': \'/',
+        querySnippet = '';
 
     if (pathArray && pathArray.length) {
         pathSnippet += _.reduce(pathArray, function (accumalator, key) {
@@ -153,28 +154,23 @@ function parsePath (request, indentString) {
             }
             return accumalator;
         }, []).join('/');
+    }
 
-        if (queryArray && queryArray.length) {
-            const queryExists = !(_.every(queryArray, function (element) {
-                return element.disabled && element.disabled === false;
-            }));
-
-            if (queryExists) {
-                pathSnippet += '?' + _.reduce(queryArray, function (accumalator, queryElement) {
-                    if (!queryElement.disabled || _.get(queryElement, 'disabled') === false) {
-                        accumalator.push(`${queryElement.key}=${encodeURIComponent(sanitize(queryElement.value))}`);
-                    }
-                    return accumalator;
-                }, []).join('&');
-            }
-        }
+    if (queryArray && queryArray.length) {
+        const queryExists = !(_.every(queryArray, function (element) {
+            return element.disabled && element.disabled === false;
+        }));
 
-        pathSnippet += '\'';
-    }
-    else {
-        pathSnippet += '\'';
+        if (queryExists) {
+            querySnippet += '?' + _.reduce(queryArray, function (accumalator, queryElement) {
+                if (!queryElement.disabled || _.get(queryElement, 'disabled') === false) {
+                    accumalator.push(`${queryElement.key}=${encodeURIComponent(sanitize(queryElement.value))}`);
+                }
+                return accumalator;
+            }, []).join('&');
+        }
     }
-
+    pathSnippet += querySnippet + '\'';
     return pathSnippet;
 }
 


Test patch:
diff --git a/codegens/nodejs-native/test/unit/fixtures/testcollection/collection.json b/codegens/nodejs-native/test/unit/fixtures/testcollection/collection.json
index 76b5cc6..501fce4 100644
--- a/codegens/nodejs-native/test/unit/fixtures/testcollection/collection.json
+++ b/codegens/nodejs-native/test/unit/fixtures/testcollection/collection.json
@@ -129,6 +129,66 @@
       },
       "response": []
     },
+    {
+      "name": "GET Request with no path and has query params",
+      "event": [
+        {
+          "listen": "test",
+          "script": {
+            "type": "text/javascript",
+            "exec": [
+              "var responseJSON;",
+              "",
+              "try { ",
+              "    responseJSON = JSON.parse(responseBody); ",
+              "    tests['response is valid JSON'] = true;",
+              "}",
+              "catch (e) { ",
+              "    responseJSON = {}; ",
+              "    tests['response is valid JSON'] = false;",
+              "}",
+              "",
+              "tests['response json contains headers'] = _.has(responseJSON, 'headers');",
+              "tests['response json contains args'] = _.has(responseJSON, 'args');",
+              "tests['response json contains url'] = _.has(responseJSON, 'url');",
+              "",
+              "tests['args key contains argument passed as url parameter'] = ('test' in responseJSON.args);",
+              "tests['args passed via request url params has value \"123\"'] = (_.get(responseJSON, 'args.test') === \"123\");"
+            ]
+          }
+        }
+      ],
+      "request": {
+        "method": "GET",
+        "header": [],
+        "body": {},
+        "url": {
+          "raw": "https://89c918b1-f4f8-4812-8e6c-69ecbeeb8409.mock.pstmn.io?query1=1&query2=2",
+          "protocol": "https",
+          "host": [
+            "89c918b1-f4f8-4812-8e6c-69ecbeeb8409",
+            "mock",
+            "pstmn",
+            "io"
+          ],
+          "path": [],
+          "query": [
+            {
+              "key": "query1",
+              "value": "1",
+              "equals": true
+            },
+            {
+              "key": "query2",
+              "value": "2",
+              "equals": true
+            }
+          ]
+        },
+        "description": "The HTTP `GET` request method is meant to retrieve data from a server. The data\nis identified by a unique URI (Uniform Resource Identifier). \n\nA `GET` request can pass parameters to the server using \"Query String \nParameters\". For example, in the following request,\n\n> http://example.com/hi/there?hand=wave\n\nThe parameter \"hand\" has the value \"wave\".\n\nThis endpoint echoes the HTTP headers, request parameters and the complete\nURI requested."
+      },
+      "response": []
+    },
     {
       "name": "POST Raw Text",
       "event": [
diff --git a/codegens/nodejs-native/test/unit/snippet.test.js b/codegens/nodejs-native/test/unit/snippet.test.js
index 1a1d14e..9f668d8 100644
--- a/codegens/nodejs-native/test/unit/snippet.test.js
+++ b/codegens/nodejs-native/test/unit/snippet.test.js
@@ -141,4 +141,43 @@ describe('nodejs-native convert function', function () {
             });
         });
     });
+
+    it('should sustain path variables when request has no path and has query params', function () {
+        var request = new sdk.Request({
+                'method': 'GET',
+                'header': [],
+                'body': {},
+                'url': {
+                    'raw': 'https://89c918b1-f4f8-4812-8e6c-69ecbeeb8409.mock.pstmn.io?query1=1&query2=2',
+                    'protocol': 'https',
+                    'host': [
+                        '89c918b1-f4f8-4812-8e6c-69ecbeeb8409',
+                        'mock',
+                        'pstmn',
+                        'io'
+                    ],
+                    'path': [],
+                    'query': [
+                        {
+                            'key': 'query1',
+                            'value': '1',
+                            'equals': true
+                        },
+                        {
+                            'key': 'query2',
+                            'value': '2',
+                            'equals': true
+                        }
+                    ]
+                }
+            }),
+            options = {};
+        convert(request, options, function (error, snippet) {
+            if (error) {
+                expect.fail(null, null, error);
+            }
+            expect(snippet).to.be.a('string');
+            expect(snippet).to.include('\'path\': \'/?query1=1&query2=2\'');
+        });
+    });
 });