diff --git a/codegens/curl/lib/index.js b/codegens/curl/lib/index.js index 268815365..8698a7457 100644 --- a/codegens/curl/lib/index.js +++ b/codegens/curl/lib/index.js @@ -2,6 +2,7 @@ var sanitize = require('./util').sanitize, sanitizeOptions = require('./util').sanitizeOptions, getUrlStringfromUrlObject = require('./util').getUrlStringfromUrlObject, addFormParam = require('./util').addFormParam, + addXOption = require('./util').addXOption, form = require('./util').form, _ = require('./lodash'), self; @@ -15,7 +16,7 @@ self = module.exports = { options = sanitizeOptions(options, self.getOptions()); var indent, trim, headersData, body, redirect, timeout, multiLine, - format, snippet, silent, url, quoteType; + format, snippet, silent, url, quoteType, xOption; redirect = options.followRedirect; timeout = options.requestTimeout; @@ -25,6 +26,7 @@ self = module.exports = { silent = options.silent; quoteType = options.quoteType === 'single' ? '\'' : '"'; url = getUrlStringfromUrlObject(request.url, quoteType); + xOption = addXOption(request, options); snippet = silent ? `curl ${form('-s', format)}` : 'curl'; @@ -47,8 +49,11 @@ self = module.exports = { if (request.method === 'HEAD') { snippet += ` ${form('-I', format)} ${quoteType + url + quoteType}`; } + else if (request.method === 'PUT' && !xOption) { + snippet += ` ${form('-T', format)} ${quoteType + url + quoteType}`; + } else { - snippet += ` ${form('-X', format)} ${request.method} ${quoteType + url + quoteType}`; + snippet += ` ${xOption ? `${form('-X', format)} ${request.method}` : ''} ${quoteType + url + quoteType}`; } if (request.body && !request.headers.has('Content-Type')) { diff --git a/codegens/curl/lib/util.js b/codegens/curl/lib/util.js index 30173c116..792a709b6 100644 --- a/codegens/curl/lib/util.js +++ b/codegens/curl/lib/util.js @@ -1,3 +1,10 @@ +const conventionalMethods = [ + 'GET', + 'POST', + 'PUT', + 'HEAD' +]; + var self = module.exports = { /** * sanitizes input string by handling escape characters eg: converts '''' to '\'\'', (" to \" and \ to \\ ) @@ -197,5 +204,38 @@ var self = module.exports = { contentType: contentType }); } + }, + + /** Computes whether -X option should be added or not + * + * @param {Object} request - Request + * @param {Object} options - Options provided by the user + * @returns {Boolean} + */ + addXOption: function (request, options) { + const convMethods = conventionalMethods.includes(request.method), + method = request.method, + followRedirect = options.followRedirect, + followOriginalMethod = request.protocolProfileBehavior.followOriginalHttpMethod, + disableBodyPruning = request.protocolProfileBehavior.disableBodyPruning; + + if (!convMethods) { + return true; + } + // Deviating from normal behavior, we'll need -XGET + else if (disableBodyPruning) { + return true; + } + // check for file upload in case of PUT + else if (method === 'PUT' && !request.body) { + return true; + } + // use -X with -L ONLY if followOriginalHttpMethod is true + else if (followOriginalMethod && followRedirect) { + return true; + } + else { + return false; + } } }; diff --git a/test/codegen/newman/fixtures/specialCurlCases.json b/test/codegen/newman/fixtures/specialCurlCases.json new file mode 100644 index 000000000..7d044c4d8 --- /dev/null +++ b/test/codegen/newman/fixtures/specialCurlCases.json @@ -0,0 +1,121 @@ +{ + "info": { + "_postman_id": "679443d7-3dcf-4943-9502-6e0cd31251c9", + "name": "Test collection for special cURL cases", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "GET request with body", + "protocolProfileBehavior": { + "disableBodyPruning": true, + "followOriginalHttpMethod": true + }, + "request": { + "method": "GET", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"hello\": \"world\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "https://postman-echo.com/get", + "protocol": "https", + "host": [ + "postman-echo", + "com" + ], + "path": [ + "get" + ] + } + }, + "response": [] + }, + { + "name": "New Request", + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "https://postman-echo.com/delete", + "protocol": "https", + "host": [ + "postman-echo", + "com" + ], + "path": [ + "delete" + ] + } + }, + "response": [] + }, + { + "name": "New Request", + "request": { + "method": "OPTIONS", + "header": [], + "url": { + "raw": "https://postman-echo.com/options", + "protocol": "https", + "host": [ + "postman-echo", + "com" + ], + "path": [ + "options" + ] + } + }, + "response": [] + }, + { + "name": "New Request", + "request": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "" + }, + "url": { + "raw": "https://postman-echo.com/patch", + "protocol": "https", + "host": [ + "postman-echo", + "com" + ], + "path": [ + "patch" + ] + } + }, + "response": [] + }, + { + "name": "New Request", + "request": { + "method": "PUT", + "header": [], + "url": { + "raw": "https://postman-echo.com/put", + "protocol": "https", + "host": [ + "postman-echo", + "com" + ], + "path": [ + "put" + ] + } + }, + "response": [] + } + ] +} \ No newline at end of file