From 061bf13057458cd873933778450bcaea4640f999 Mon Sep 17 00:00:00 2001 From: shreys7 Date: Tue, 12 Nov 2019 15:13:36 +0530 Subject: [PATCH 01/11] Added JSON.stringify code part in js-fetch snippet --- codegens/js-fetch/lib/index.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/codegens/js-fetch/lib/index.js b/codegens/js-fetch/lib/index.js index e842abf4f..58a4c46d2 100644 --- a/codegens/js-fetch/lib/index.js +++ b/codegens/js-fetch/lib/index.js @@ -60,9 +60,22 @@ function parseFormData (body, trim) { * * @param {Object} body Raw body data * @param {boolean} trim trim body option + * @param {String} contentType Content type of the body being sent */ -function parseRawBody (body, trim) { - var bodySnippet = `var raw = "${sanitize(body.toString(), trim)}";\n`; +function parseRawBody (body, trim, contentType) { + var bodySnippet = 'var raw = '; + if (contentType === 'application/json') { + try { + let jsonBody = JSON.parse(body); + bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody)});\n`; + } + catch (error) { + bodySnippet += `"${sanitize(body.toString(), trim)}";\n`; + } + } + else { + bodySnippet += `"${sanitize(body.toString(), trim)}";\n`; + } return bodySnippet; } @@ -81,14 +94,15 @@ function parseFileData () { * * @param {Object} body body object from request. * @param {boolean} trim trim body option + * @param {String} contentType Content type of the body being sent */ -function parseBody (body, trim) { +function parseBody (body, trim, contentType) { if (!_.isEmpty(body)) { switch (body.mode) { case 'urlencoded': return parseURLEncodedBody(body.urlencoded, trim); case 'raw': - return parseRawBody(body.raw, trim); + return parseRawBody(body.raw, trim, contentType); case 'formdata': return parseFormData(body.formdata, trim); /* istanbul ignore next */ @@ -201,7 +215,7 @@ function convert (request, options, callback) { headerSnippet = parseHeaders(headers); body = request.body && request.body.toJSON(); - bodySnippet = parseBody(body, trim); + bodySnippet = parseBody(body, trim, request.headers.get('Content-Type')); optionsSnippet = `var requestOptions = {\n${indent}`; optionsSnippet += `method: '${request.method}',\n${indent}`; From e9afd2767a03b0910382465c6f1a7b94d10a1140 Mon Sep 17 00:00:00 2001 From: shreys7 Date: Tue, 12 Nov 2019 15:14:28 +0530 Subject: [PATCH 02/11] Added JSON.stringify code part in js-jquery snippet --- codegens/js-jquery/lib/js-jquery.js | 3 ++- codegens/js-jquery/lib/util/parseBody.js | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/codegens/js-jquery/lib/js-jquery.js b/codegens/js-jquery/lib/js-jquery.js index 0a9b58481..73e4a31ec 100644 --- a/codegens/js-jquery/lib/js-jquery.js +++ b/codegens/js-jquery/lib/js-jquery.js @@ -140,7 +140,8 @@ self = module.exports = { }); } jQueryCode += `${getHeaders(request, indent)}`; - jQueryCode += `${parseBody(request.toJSON(), options.trimRequestBody, indent)}};\n\n`; + jQueryCode += `${parseBody(request.toJSON(), options.trimRequestBody, indent, + request.headers.get('Content-Type'))}};\n\n`; jQueryCode += `$.ajax(settings).done(function (response) {\n${indent}console.log(response);\n});`; return callback(null, jQueryCode); diff --git a/codegens/js-jquery/lib/util/parseBody.js b/codegens/js-jquery/lib/util/parseBody.js index 53c0512ea..b3dc50a0b 100644 --- a/codegens/js-jquery/lib/util/parseBody.js +++ b/codegens/js-jquery/lib/util/parseBody.js @@ -7,9 +7,10 @@ var _ = require('../lodash'), * @param {Object} request - postman SDK-request object * @param {Boolean} trimRequestBody - whether to trim request body fields * @param {String} indentation - used for indenting snippet's structure + * @param {String} contentType Content type of the body being sent * @returns {String} - request body */ -module.exports = function (request, trimRequestBody, indentation) { +module.exports = function (request, trimRequestBody, indentation, contentType) { // used to check whether body is present in the request and return accordingly if (request.body) { var requestBody = '', @@ -19,8 +20,21 @@ module.exports = function (request, trimRequestBody, indentation) { switch (request.body.mode) { case 'raw': if (!_.isEmpty(request.body[request.body.mode])) { - requestBody += `${indentation}"data": ` + + if (contentType === 'application/json') { + // eslint-disable-next-line max-depth + try { + let jsonBody = JSON.parse(request.body[request.body.mode]); + requestBody += `${indentation}"data": JSON.stringify(${JSON.stringify(jsonBody)}),\n`; + } + catch (error) { + requestBody += `${indentation}"data": ` + `${sanitize(request.body[request.body.mode], request.body.mode, trimRequestBody)},\n`; + } + } + else { + requestBody += `${indentation}"data": ` + + `${sanitize(request.body[request.body.mode], request.body.mode, trimRequestBody)},\n`; + } } return requestBody; case 'urlencoded': From f643a2c04422070cd2c5ad9056f3386d77892cea Mon Sep 17 00:00:00 2001 From: shreys7 Date: Tue, 12 Nov 2019 15:14:47 +0530 Subject: [PATCH 03/11] Added JSON.stringify code part in js-xhr snippet --- codegens/js-xhr/lib/index.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/codegens/js-xhr/lib/index.js b/codegens/js-xhr/lib/index.js index f34ee446a..6302951e3 100644 --- a/codegens/js-xhr/lib/index.js +++ b/codegens/js-xhr/lib/index.js @@ -25,10 +25,22 @@ function parseURLEncodedBody (body) { * * @param {*} body Raw body data * @param {*} trim trim body option + * @param {String} contentType Content type of the body being sent */ -function parseRawBody (body, trim) { - var bodySnippet; - bodySnippet = `var data = "${sanitize(body.toString(), trim)}";\n`; +function parseRawBody (body, trim, contentType) { + var bodySnippet = 'var data = '; + if (contentType === 'application/json') { + try { + let jsonBody = JSON.parse(body); + bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody)});\n`; + } + catch (error) { + bodySnippet += `"${sanitize(body.toString(), trim)}";\n`; + } + } + else { + bodySnippet += `"${sanitize(body.toString(), trim)}";\n`; + } return bodySnippet; } @@ -76,14 +88,15 @@ function parseFile () { * * @param {*} body body object from request. * @param {*} trim trim body option + * @param {String} contentType Content type of the body being sent */ -function parseBody (body, trim) { +function parseBody (body, trim, contentType) { if (!_.isEmpty(body)) { switch (body.mode) { case 'urlencoded': return parseURLEncodedBody(body.urlencoded, trim); case 'raw': - return parseRawBody(body.raw, trim); + return parseRawBody(body.raw, trim, contentType); case 'formdata': return parseFormData(body.formdata, trim); case 'file': @@ -174,7 +187,8 @@ function convert (request, options, callback) { indent = indent.repeat(options.indentCount); trim = options.trimRequestBody; - bodySnippet = request.body && !_.isEmpty(request.body.toJSON()) ? parseBody(request.body.toJSON(), trim, indent) : ''; + bodySnippet = request.body && !_.isEmpty(request.body.toJSON()) ? parseBody(request.body.toJSON(), trim, + request.headers.get('Content-Type')) : ''; codeSnippet += bodySnippet + '\n'; From 818b6a7ac5793e5cb68df74e24e303fd07acbf96 Mon Sep 17 00:00:00 2001 From: shreys7 Date: Tue, 12 Nov 2019 15:15:21 +0530 Subject: [PATCH 04/11] Added JSON.stringify code part in nodejs-native snippet --- codegens/nodejs-native/lib/parseRequest.js | 12 +++++++++++- codegens/nodejs-native/lib/request.js | 4 +++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/codegens/nodejs-native/lib/parseRequest.js b/codegens/nodejs-native/lib/parseRequest.js index 5fe8244ce..067783dda 100644 --- a/codegens/nodejs-native/lib/parseRequest.js +++ b/codegens/nodejs-native/lib/parseRequest.js @@ -70,11 +70,21 @@ function generateMultipartFormData (requestbody) { * @param {Object} requestbody - json object for body of request * @param {String} indentString - string for indentation * @param {Boolean} trimBody - indicates whether to trim body fields or not + * @param {String} contentType Content type of the body being sent */ -function parseBody (requestbody, indentString, trimBody) { +function parseBody (requestbody, indentString, trimBody, contentType) { if (requestbody) { switch (requestbody.mode) { case 'raw': + if (contentType === 'application/json') { + try { + let jsonBody = JSON.parse(requestbody[requestbody.mode]); + return `JSON.stringify(${JSON.stringify(jsonBody)})`; + } + catch (error) { + return ` ${JSON.stringify(requestbody[requestbody.mode])}`; + } + } return ` ${JSON.stringify(requestbody[requestbody.mode])}`; case 'formdata': return generateMultipartFormData(requestbody); diff --git a/codegens/nodejs-native/lib/request.js b/codegens/nodejs-native/lib/request.js index b3c371af8..ec61e2ea0 100644 --- a/codegens/nodejs-native/lib/request.js +++ b/codegens/nodejs-native/lib/request.js @@ -41,7 +41,8 @@ function makeSnippet (request, indentString, options) { * } */ if (request.body && request.body[request.body.mode]) { - postData.push(parseRequest.parseBody(request.body.toJSON(), indentString, options.trimRequestBody)); + postData.push(parseRequest.parseBody(request.body.toJSON(), indentString, options.trimRequestBody, + request.headers.get('Content-Type'))); } if (request.body && request.body.mode === 'file' && !request.headers.has('Content-Type')) { request.addHeader({ @@ -106,6 +107,7 @@ function makeSnippet (request, indentString, options) { } snippet += 'req.end();'; + console.log('\n' + snippet + '\n'); return snippet; } From fa85544caef9810203dc2c3e9c8be4c77e4ff7bf Mon Sep 17 00:00:00 2001 From: shreys7 Date: Tue, 12 Nov 2019 15:15:35 +0530 Subject: [PATCH 05/11] Added JSON.stringify code part in nodejs-request snippet --- codegens/nodejs-request/lib/parseRequest.js | 12 +++++++++++- codegens/nodejs-request/lib/request.js | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/codegens/nodejs-request/lib/parseRequest.js b/codegens/nodejs-request/lib/parseRequest.js index 51cc7e0cb..b5dc7829d 100644 --- a/codegens/nodejs-request/lib/parseRequest.js +++ b/codegens/nodejs-request/lib/parseRequest.js @@ -62,11 +62,21 @@ function extractFormData (dataArray, indentString, trimBody) { * @param {Object} requestbody - json object for body of request * @param {String} indentString - string for indentation * @param {Boolean} trimBody - indicates whether to trim body fields or not + * @param {String} contentType Content type of the body being sent */ -function parseBody (requestbody, indentString, trimBody) { +function parseBody (requestbody, indentString, trimBody, contentType) { if (requestbody) { switch (requestbody.mode) { case 'raw': + if (contentType === 'application/json') { + try { + let jsonBody = JSON.parse(requestbody[requestbody.mode]); + return `body: JSON.stringify(${JSON.stringify(jsonBody)})\n`; + } + catch (error) { + return `body: ${JSON.stringify(requestbody[requestbody.mode])}\n`; + } + } return `body: ${JSON.stringify(requestbody[requestbody.mode])}\n`; case 'formdata': return `formData: {\n${extractFormData(requestbody[requestbody.mode], indentString, trimBody)}` + diff --git a/codegens/nodejs-request/lib/request.js b/codegens/nodejs-request/lib/request.js index cdfe89c93..c6c79367f 100644 --- a/codegens/nodejs-request/lib/request.js +++ b/codegens/nodejs-request/lib/request.js @@ -49,7 +49,8 @@ function makeSnippet (request, indentString, options) { if (request.body && request.body[request.body.mode]) { optionsArray.push( - indentString + parseRequest.parseBody(request.body.toJSON(), indentString, options.trimRequestBody) + indentString + parseRequest.parseBody(request.body.toJSON(), indentString, options.trimRequestBody, + request.headers.get('Content-Type')) ); } if (options.requestTimeout) { From ce45fd0bf8aa21010f783e37b009d70311c36253 Mon Sep 17 00:00:00 2001 From: shreys7 Date: Tue, 12 Nov 2019 15:15:44 +0530 Subject: [PATCH 06/11] Added JSON.stringify code part in nodejs-unirest snippet --- codegens/nodejs-unirest/lib/parseRequest.js | 12 +++++++++++- codegens/nodejs-unirest/lib/unirest.js | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/codegens/nodejs-unirest/lib/parseRequest.js b/codegens/nodejs-unirest/lib/parseRequest.js index ca0faeeae..ad9234718 100644 --- a/codegens/nodejs-unirest/lib/parseRequest.js +++ b/codegens/nodejs-unirest/lib/parseRequest.js @@ -53,12 +53,22 @@ function parseFormdata (bodyArray, indentString, trimBody) { * @param {Object} requestbody - json object representing body of request * @param {String} indentString - string required for indentation * @param {Boolean} trimBody - indicates whether to trim body fields or not + * @param {String} contentType Content type of the body being sent * @returns {String} - code snippet for adding body in request */ -function parseBody (requestbody, indentString, trimBody) { +function parseBody (requestbody, indentString, trimBody, contentType) { if (requestbody) { switch (requestbody.mode) { case 'raw': + if (contentType === 'application/json') { + try { + let jsonBody = JSON.parse(requestbody[requestbody.mode]); + return `${indentString}.send(JSON.stringify(${JSON.stringify(jsonBody)}))\n`; + } + catch (error) { + return indentString + '.send(' + JSON.stringify(requestbody[requestbody.mode]) + ')\n'; + } + } return indentString + '.send(' + JSON.stringify(requestbody[requestbody.mode]) + ')\n'; case 'urlencoded': return parseFormdata(requestbody[requestbody.mode], indentString, trimBody); diff --git a/codegens/nodejs-unirest/lib/unirest.js b/codegens/nodejs-unirest/lib/unirest.js index 2e81810ad..52c370c97 100644 --- a/codegens/nodejs-unirest/lib/unirest.js +++ b/codegens/nodejs-unirest/lib/unirest.js @@ -25,7 +25,8 @@ function makeSnippet (request, indentString, options) { snippet += parseRequest.parseHeader(request, indentString); if (request.body) { - snippet += parseRequest.parseBody(request.body.toJSON(), indentString, options.trimRequestBody); + snippet += parseRequest.parseBody(request.body.toJSON(), indentString, options.trimRequestBody, + request.headers.get('Content-Type')); } if (options.requestTimeout) { snippet += indentString + `.timeout(${options.requestTimeout})`; From ae27cf132f90abb932137b32f4da1e2a504d4272 Mon Sep 17 00:00:00 2001 From: shreys7 Date: Tue, 12 Nov 2019 15:20:31 +0530 Subject: [PATCH 07/11] Fixed js-jquery tests --- codegens/js-jquery/test/unit/fixtures/snippetFixtures.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegens/js-jquery/test/unit/fixtures/snippetFixtures.json b/codegens/js-jquery/test/unit/fixtures/snippetFixtures.json index aa4858c53..aaef22193 100644 --- a/codegens/js-jquery/test/unit/fixtures/snippetFixtures.json +++ b/codegens/js-jquery/test/unit/fixtures/snippetFixtures.json @@ -7,7 +7,7 @@ "Resolve URL (Quotes + Special Characters) Copy": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%3Fa%3D%21@%24%5E*%28%29_-%60%2526%26b%3D%2C./%27%3B%5B%5D%7D%7B%5C%22%3A/%3F%3E%3C%7C%7C%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B", "POST Raw Text": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22application/x-www-form-urlencoded%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20%22Duis%20posuere%20augue%20vel%20cursus%20pharetra.%20In%20luctus%20a%20ex%20nec%20pretium.%20Praesent%20neque%20quam%2C%20tincidunt%20nec%20leo%20eget%2C%20rutrum%20vehicula%20magna.%5CnMaecenas%20consequat%20elementum%20elit%2C%20id%20semper%20sem%20tristique%20et.%20Integer%20pulvinar%20enim%20quis%20consectetur%20interdum%20volutpat.%21@%23%24%25%5E%26*%28%29+POL%3A%7D%2C%27%27%3B%2C%5B%3B%5B%3B%22%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B", "POST urlencoded data with disabled entries": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22application/x-www-form-urlencoded%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%221%22%3A%20%22%27a%27%22%2C%0A%20%20%20%20%20%20%20%20%222%22%3A%20%22%5C%22b%5C%22%22%0A%20%20%20%20%7D%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B", - "POST json with raw": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22application/json%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20%22%7B%5Cn%20%20%5C%22json%5C%22%3A%20%5C%22Test-Test%21@%23%24%25%5E%26*%28%29+POL%3A%7D%2C%27%27%3B%2C%5B%3B%5B%3B%3A%3E%5C%22%5Cn%7D%22%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B", + "POST json with raw": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22application/json%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20JSON.stringify%28%7B%22json%22%3A%22Test-Test%21@%23%24%25%5E%26*%28%29+POL%3A%7D%2C%27%27%3B%2C%5B%3B%5B%3B%3A%3E%22%7D%29%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B", "POST javascript with raw": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22application/javascript%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20%22var%20val%20%3D%206%3B%5Cnconsole.log%28val%29%3B%22%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B", "POST text/xml with raw": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22text/xml%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20%22%3Cxml%3E%5Cn%5CtTest%20Test%21@%23%24%25%5E%26*%28%29+POL%3A%7D%2C%27%27%3B%2C%5B%3B%5B%3B%5Cn%3C/xml%3E%22%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B", "POST text/html with raw": "var%20settings%20%3D%20%7B%0A%20%20%20%20%22url%22%3A%20%22https%3A//postman-echo.com/post%22%2C%0A%20%20%20%20%22method%22%3A%20%22POST%22%2C%0A%20%20%20%20%22timeout%22%3A%20100%2C%0A%20%20%20%20%22headers%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22text/html%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22data%22%3A%20%22%3Chtml%3E%5Cn%20%20Test%20Test%20%21@%23%24%25%5E%26*%28%29+POL%3A%7D%2C%27%27%3B%2C%5B%3B%5B%3B%5Cn%3C/html%3E%22%2C%0A%7D%3B%0A%0A%24.ajax%28settings%29.done%28function%20%28response%29%20%7B%0A%20%20%20%20console.log%28response%29%3B%0A%7D%29%3B", From 91e9fbf2823277589a93c99163ad1c79479f2ffd Mon Sep 17 00:00:00 2001 From: shreys7 Date: Wed, 13 Nov 2019 12:19:10 +0530 Subject: [PATCH 08/11] Added unit tests for JSON.stringify fix to check if it was actually added in the snippet being generated --- codegens/js-fetch/test/unit/convert.test.js | 34 +++++++++++++++++++ .../js-jquery/test/unit/converter.test.js | 33 ++++++++++++++++++ codegens/js-xhr/test/unit/convert.test.js | 33 ++++++++++++++++++ .../nodejs-native/test/unit/snippet.test.js | 33 ++++++++++++++++++ .../nodejs-request/test/unit/snippet.test.js | 34 +++++++++++++++++++ .../nodejs-unirest/test/unit/snippet.test.js | 34 +++++++++++++++++++ 6 files changed, 201 insertions(+) diff --git a/codegens/js-fetch/test/unit/convert.test.js b/codegens/js-fetch/test/unit/convert.test.js index d18f3436b..dd1686902 100644 --- a/codegens/js-fetch/test/unit/convert.test.js +++ b/codegens/js-fetch/test/unit/convert.test.js @@ -146,6 +146,40 @@ describe('js-fetch convert function for test collection', function () { '" value_containing_whitespaces ");'); }); }); + + it('should include JSON.stringify in the snippet for raw json bodies', function () { + var request = new sdk.Request({ + 'method': 'POST', + 'header': [ + { + 'key': 'Content-Type', + 'value': 'application/json' + } + ], + 'body': { + 'mode': 'raw', + 'raw': '{\n "json": "Test-Test"\n}' + }, + 'url': { + 'raw': 'https://postman-echo.com/post', + 'protocol': 'https', + 'host': [ + 'postman-echo', + 'com' + ], + 'path': [ + 'post' + ] + } + }); + convert(request, {}, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.include('var raw = JSON.stringify({"json":"Test-Test"})'); + }); + }); }); describe('getOptions function', function () { diff --git a/codegens/js-jquery/test/unit/converter.test.js b/codegens/js-jquery/test/unit/converter.test.js index 62bc9395e..ecc8288d6 100644 --- a/codegens/js-jquery/test/unit/converter.test.js +++ b/codegens/js-jquery/test/unit/converter.test.js @@ -99,4 +99,37 @@ describe('jQuery converter', function () { expect(snippet).to.include('"key_containing_whitespaces": " value_containing_whitespaces "'); }); }); + it('should include JSON.stringify in the snippet for raw json bodies', function () { + var request = new sdk.Request({ + 'method': 'POST', + 'header': [ + { + 'key': 'Content-Type', + 'value': 'application/json' + } + ], + 'body': { + 'mode': 'raw', + 'raw': '{\n "json": "Test-Test"\n}' + }, + 'url': { + 'raw': 'https://postman-echo.com/post', + 'protocol': 'https', + 'host': [ + 'postman-echo', + 'com' + ], + 'path': [ + 'post' + ] + } + }); + convert(request, {}, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.include('"data": JSON.stringify({"json":"Test-Test"})'); + }); + }); }); diff --git a/codegens/js-xhr/test/unit/convert.test.js b/codegens/js-xhr/test/unit/convert.test.js index 9aeed4552..8a4e1665c 100644 --- a/codegens/js-xhr/test/unit/convert.test.js +++ b/codegens/js-xhr/test/unit/convert.test.js @@ -34,6 +34,39 @@ describe('js-xhr convert function', function () { '" value_containing_whitespaces ")'); }); }); + it('should include JSON.stringify in the snippet for raw json bodies', function () { + var request = new sdk.Request({ + 'method': 'POST', + 'header': [ + { + 'key': 'Content-Type', + 'value': 'application/json' + } + ], + 'body': { + 'mode': 'raw', + 'raw': '{\n "json": "Test-Test"\n}' + }, + 'url': { + 'raw': 'https://postman-echo.com/post', + 'protocol': 'https', + 'host': [ + 'postman-echo', + 'com' + ], + 'path': [ + 'post' + ] + } + }); + convert(request, {}, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.include('var data = JSON.stringify({"json":"Test-Test"})'); + }); + }); describe('Sanitize function', function () { it('should return empty string when input is not a string type', function () { diff --git a/codegens/nodejs-native/test/unit/snippet.test.js b/codegens/nodejs-native/test/unit/snippet.test.js index cd479984b..393a1c7da 100644 --- a/codegens/nodejs-native/test/unit/snippet.test.js +++ b/codegens/nodejs-native/test/unit/snippet.test.js @@ -94,4 +94,37 @@ describe('nodejs-native convert function', function () { expect(snippet).to.include('\'key_containing_whitespaces\': \' value_containing_whitespaces \''); }); }); + it('should include JSON.stringify in the snippet for raw json bodies', function () { + var request = new sdk.Request({ + 'method': 'POST', + 'header': [ + { + 'key': 'Content-Type', + 'value': 'application/json' + } + ], + 'body': { + 'mode': 'raw', + 'raw': '{\n "json": "Test-Test"\n}' + }, + 'url': { + 'raw': 'https://postman-echo.com/post', + 'protocol': 'https', + 'host': [ + 'postman-echo', + 'com' + ], + 'path': [ + 'post' + ] + } + }); + convert(request, {}, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.include('var postData = JSON.stringify({"json":"Test-Test"})'); + }); + }); }); diff --git a/codegens/nodejs-request/test/unit/snippet.test.js b/codegens/nodejs-request/test/unit/snippet.test.js index 8706b128a..17803f81c 100644 --- a/codegens/nodejs-request/test/unit/snippet.test.js +++ b/codegens/nodejs-request/test/unit/snippet.test.js @@ -273,6 +273,40 @@ describe('nodejs-request convert function', function () { }); }); + it('should include JSON.stringify in the snippet for raw json bodies', function () { + var request = new sdk.Request({ + 'method': 'POST', + 'header': [ + { + 'key': 'Content-Type', + 'value': 'application/json' + } + ], + 'body': { + 'mode': 'raw', + 'raw': '{\n "json": "Test-Test"\n}' + }, + 'url': { + 'raw': 'https://postman-echo.com/post', + 'protocol': 'https', + 'host': [ + 'postman-echo', + 'com' + ], + 'path': [ + 'post' + ] + } + }); + convert(request, {}, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.include('body: JSON.stringify({"json":"Test-Test"})'); + }); + }); + describe('getOptions function', function () { it('should return an array of specific options', function () { diff --git a/codegens/nodejs-unirest/test/unit/snippet.test.js b/codegens/nodejs-unirest/test/unit/snippet.test.js index 57469f301..6a04e5bd7 100644 --- a/codegens/nodejs-unirest/test/unit/snippet.test.js +++ b/codegens/nodejs-unirest/test/unit/snippet.test.js @@ -109,6 +109,40 @@ describe('nodejs unirest convert function', function () { expect(snippet).to.include('\'key_containing_whitespaces\': \' value_containing_whitespaces \''); }); }); + + it('should include JSON.stringify in the snippet for raw json bodies', function () { + var request = new sdk.Request({ + 'method': 'POST', + 'header': [ + { + 'key': 'Content-Type', + 'value': 'application/json' + } + ], + 'body': { + 'mode': 'raw', + 'raw': '{\n "json": "Test-Test"\n}' + }, + 'url': { + 'raw': 'https://postman-echo.com/post', + 'protocol': 'https', + 'host': [ + 'postman-echo', + 'com' + ], + 'path': [ + 'post' + ] + } + }); + convert(request, {}, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.include('.send(JSON.stringify({"json":"Test-Test"}))'); + }); + }); }); describe('getOptions function', function () { From 90efd6e56403e71f501add06b92cbae49c0c139b Mon Sep 17 00:00:00 2001 From: shreys7 Date: Wed, 13 Nov 2019 21:53:55 +0530 Subject: [PATCH 09/11] Removed unnecessary console logs --- codegens/nodejs-native/lib/request.js | 1 - 1 file changed, 1 deletion(-) diff --git a/codegens/nodejs-native/lib/request.js b/codegens/nodejs-native/lib/request.js index ec61e2ea0..3af555336 100644 --- a/codegens/nodejs-native/lib/request.js +++ b/codegens/nodejs-native/lib/request.js @@ -107,7 +107,6 @@ function makeSnippet (request, indentString, options) { } snippet += 'req.end();'; - console.log('\n' + snippet + '\n'); return snippet; } From a1fe667202c09a2aec5b8a4ef2c08afe033dedde Mon Sep 17 00:00:00 2001 From: shreys7 Date: Fri, 15 Nov 2019 13:30:01 +0530 Subject: [PATCH 10/11] Fixed indent problems in js-xhr --- codegens/js-xhr/lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegens/js-xhr/lib/index.js b/codegens/js-xhr/lib/index.js index 1cc97ad6c..1dac5710a 100644 --- a/codegens/js-xhr/lib/index.js +++ b/codegens/js-xhr/lib/index.js @@ -215,7 +215,7 @@ function convert (request, options, callback) { trim = options.trimRequestBody; bodySnippet = request.body && !_.isEmpty(request.body.toJSON()) ? parseBody(request.body.toJSON(), trim, - request.headers.get('Content-Type')) : ''; + indent, request.headers.get('Content-Type')) : ''; codeSnippet += bodySnippet + '\n'; From c1662cc37d2c0996536391ad2a862c3537697ae7 Mon Sep 17 00:00:00 2001 From: shreys7 Date: Fri, 15 Nov 2019 14:28:32 +0530 Subject: [PATCH 11/11] Fixed nodejs-native tests --- codegens/nodejs-native/lib/request.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/codegens/nodejs-native/lib/request.js b/codegens/nodejs-native/lib/request.js index 26ef18e1d..73b7a8d86 100644 --- a/codegens/nodejs-native/lib/request.js +++ b/codegens/nodejs-native/lib/request.js @@ -16,7 +16,7 @@ function makeSnippet (request, indentString, options) { var nativeModule = (request.url.protocol === 'http' ? 'http' : 'https'), snippet = `var ${nativeModule} = require('${nativeModule}');\n`, optionsArray = [], - postData; + postData = ''; if (options.followRedirect) { snippet = `var ${nativeModule} = require('follow-redirects').${nativeModule};\n`; @@ -41,8 +41,8 @@ function makeSnippet (request, indentString, options) { * } */ if (request.body && request.body[request.body.mode]) { - postData.push(parseRequest.parseBody(request.body.toJSON(), indentString, options.trimRequestBody, - request.headers.get('Content-Type'))); + postData += parseRequest.parseBody(request.body.toJSON(), indentString, options.trimRequestBody, + request.headers.get('Content-Type')); } if (request.body && !request.headers.has('Content-Type')) { if (request.body.mode === 'file') {