Skip to content

Commit 2e9a4cd

Browse files
authored
Merge pull request #663 from postmanlabs/curl-short-options
Use short options if that option is selected as far as possible
2 parents 4168827 + 2141747 commit 2e9a4cd

File tree

4 files changed

+72
-15
lines changed

4 files changed

+72
-15
lines changed

codegens/curl/lib/index.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,33 +130,43 @@ self = module.exports = {
130130
case 'urlencoded':
131131
_.forEach(body.urlencoded, function (data) {
132132
if (!data.disabled) {
133-
// Using the long form below without considering the longFormat option,
134-
// to generate more accurate and correct snippet
135-
snippet += indent + '--data-urlencode';
136-
snippet += ` ${quoteType}${sanitize(data.key, trim, quoteType)}=` +
137-
`${sanitize(data.value, trim, quoteType)}${quoteType}`;
133+
snippet += indent + (format ? '--data-urlencode' : '-d');
134+
snippet += ` ${quoteType}${sanitize(data.key, trim, quoteType, false, true)}=` +
135+
`${sanitize(data.value, trim, quoteType, false, !format)}${quoteType}`;
138136
}
139137
});
140138
break;
141-
case 'raw':
142-
snippet += indent + `--data-raw ${quoteType}${sanitize(body.raw.toString(), trim, quoteType)}${quoteType}`;
139+
case 'raw': {
140+
let rawBody = body.raw.toString(),
141+
isAsperandPresent = _.includes(rawBody, '@'),
142+
// Use the long option if `@` is present in the request body otherwise follow user setting
143+
optionName = isAsperandPresent ? '--data-raw' : form('-d', format);
144+
snippet += indent + `${optionName} ${quoteType}${sanitize(rawBody, trim, quoteType)}${quoteType}`;
143145
break;
146+
}
144147

145-
case 'graphql':
148+
case 'graphql': {
146149
// eslint-disable-next-line no-case-declarations
147150
let query = body.graphql ? body.graphql.query : '',
148-
graphqlVariables;
151+
graphqlVariables, requestBody, isAsperandPresent, optionName;
149152
try {
150153
graphqlVariables = JSON.parse(body.graphql.variables);
151154
}
152155
catch (e) {
153156
graphqlVariables = {};
154157
}
155-
snippet += indent + `--data-raw ${quoteType}${sanitize(JSON.stringify({
158+
159+
requestBody = JSON.stringify({
156160
query: query,
157161
variables: graphqlVariables
158-
}), trim, quoteType)}${quoteType}`;
162+
});
163+
164+
isAsperandPresent = _.includes(requestBody, '@');
165+
// Use the long option if `@` is present in the request body otherwise follow user setting
166+
optionName = isAsperandPresent ? '--data-raw' : form('-d', format);
167+
snippet += indent + `${optionName} ${quoteType}${sanitize(requestBody, trim, quoteType)}${quoteType}`;
159168
break;
169+
}
160170
case 'formdata':
161171
_.forEach(body.formdata, function (data) {
162172
if (!(data.disabled)) {
@@ -179,7 +189,7 @@ self = module.exports = {
179189
});
180190
break;
181191
case 'file':
182-
snippet += indent + '--data-binary';
192+
snippet += indent + form('-d', format);
183193
snippet += ` ${quoteType}@${sanitize(body[body.mode].src, trim)}${quoteType}`;
184194
break;
185195
default:

codegens/curl/lib/util.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ var self = module.exports = {
99
* @param {Boolean} [trim] - indicates whether to trim string or not
1010
* @param {String} [quoteType] - indicates which quoteType has to be escaped
1111
* @param {Boolean} [backSlash] - indicates whether to escape backslash(\\)
12+
* @param {Boolean} [urlEncode] - indicates whether to url-encode inputString
1213
* @returns {String}
1314
*/
14-
sanitize: function (inputString, trim, quoteType, backSlash) {
15+
sanitize: function (inputString, trim, quoteType, backSlash = false, urlEncode = false) {
1516
if (typeof inputString !== 'string') {
1617
return '';
1718
}
1819

20+
if (urlEncode) {
21+
inputString = encodeURIComponent(inputString);
22+
}
23+
1924
if (backSlash) {
2025
inputString = inputString.replace(/\\/g, '\\\\');
2126
}

codegens/curl/test/newman/newman.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var runNewmanTest = require('../../../../test/codegen/newman/newmanTestUtil').ru
33

44
describe('Convert for different types of request', function () {
55
var testConfig = {compileScript: null, runScript: null, fileName: null},
6-
options = {
6+
options1 = {
77
indentCount: 3,
88
indentType: 'Space',
99
requestTimeout: 200,
@@ -13,7 +13,19 @@ describe('Convert for different types of request', function () {
1313
silent: true,
1414
lineContinuationCharacter: '\\',
1515
quoteType: 'single'
16+
},
17+
options2 = {
18+
indentCount: 3,
19+
indentType: 'Space',
20+
requestTimeout: 200,
21+
multiLine: true,
22+
followRedirect: true,
23+
longFormat: false,
24+
silent: true,
25+
lineContinuationCharacter: '\\',
26+
quoteType: 'single'
1627
};
1728

18-
runNewmanTest(convert, options, testConfig);
29+
runNewmanTest(convert, options1, testConfig);
30+
runNewmanTest(convert, options2, testConfig);
1931
});

codegens/curl/test/unit/convert.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,36 @@ describe('curl convert function', function () {
531531
});
532532
});
533533

534+
it('should longer option for body even if longFormat is disabled if @ character is present', function () {
535+
let request = new sdk.Request({
536+
'method': 'POST',
537+
'header': [],
538+
'body': {
539+
'mode': 'raw',
540+
'raw': '@hello'
541+
},
542+
'url': {
543+
'raw': 'https://postman-echo.com/post',
544+
'protocol': 'https',
545+
'host': [
546+
'postman-echo',
547+
'com'
548+
],
549+
'path': [
550+
'post'
551+
]
552+
}
553+
});
554+
555+
convert(request, { longFormat: false }, function (error, snippet) {
556+
if (error) {
557+
expect.fail(null, null, error);
558+
}
559+
560+
expect(snippet).include('--data-raw');
561+
});
562+
});
563+
534564
describe('getUrlStringfromUrlObject function', function () {
535565
var rawUrl, urlObject, outputUrlString;
536566

0 commit comments

Comments
 (0)