Skip to content

Fix the usage of -X cli option in curl codegen [WIP] #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions codegens/curl/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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';

Expand All @@ -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')) {
Expand Down
40 changes: 40 additions & 0 deletions codegens/curl/lib/util.js
Original file line number Diff line number Diff line change
@@ -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 \\ )
Expand Down Expand Up @@ -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;
}
}
};
121 changes: 121 additions & 0 deletions test/codegen/newman/fixtures/specialCurlCases.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}