Skip to content

Commit 29744ea

Browse files
authored
Merge pull request #398 from postmanlabs/someshkoli-fix/quoteOption
Adds additional option to curl codegen
2 parents 2fadad3 + 464da98 commit 29744ea

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

codegens/curl/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ Convert function takes three parameters
1313

1414
* `request` - Postman-SDK Request Object
1515

16-
* `options` - options is an object which hsa following properties
16+
* `options` - options is an object which has following properties
1717
* `indentType` - String denoting type of indentation for code snippet. eg: 'Space', 'Tab'
1818
* `indentCount` - The number of indentation characters to add per code level
1919
* `trimRequestBody` - Trim request body fields
2020
* `followRedirect` - Boolean denoting whether to redirect a request
2121
* `requestTimeout` - Integer denoting time after which the request will bail out in milli-seconds
2222
* `multiLine` - Boolean denoting whether to output code snippet with multi line breaks
2323
* `longFormat` - Boolean denoting whether to use longform cURL options in snippet
24+
* `quoteType` - String denoting the quote type to use (single or double) for URL
2425

2526
* `callback` - callback function with first parameter as error and second parameter as string for code snippet
2627

@@ -34,7 +35,8 @@ var request = new sdk.Request('www.google.com'), //using postman sdk to create
3435
trimRequestBody: true,
3536
multiLine: true,
3637
followRedirect: true,
37-
longFormat: true
38+
longFormat: true,
39+
quoteType: 'single'
3840
};
3941
convert(request, options, function(error, snippet) {
4042
if (error) {

codegens/curl/lib/index.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ self = module.exports = {
1515
options = sanitizeOptions(options, self.getOptions());
1616

1717
var indent, trim, headersData, body, redirect, timeout, multiLine,
18-
format, snippet, silent, url;
18+
format, snippet, silent, url, quoteType;
1919

2020
redirect = options.followRedirect;
2121
timeout = options.requestTimeout;
2222
multiLine = options.multiLine;
2323
format = options.longFormat;
2424
trim = options.trimRequestBody;
2525
silent = options.silent;
26+
quoteType = options.quoteType === 'single' ? '\'' : '"';
2627

2728
snippet = silent ? `curl ${form('-s', format)}` : 'curl';
2829
if (redirect) {
@@ -40,10 +41,10 @@ self = module.exports = {
4041
}
4142
url = getUrlStringfromUrlObject(request.url);
4243
if (request.method === 'HEAD') {
43-
snippet += ` ${form('-I', format)} '${url}'`;
44+
snippet += ` ${form('-I', format)} ${quoteType + url + quoteType}`;
4445
}
4546
else {
46-
snippet += ` ${form('-X', format)} ${request.method} '${url}'`;
47+
snippet += ` ${form('-X', format)} ${request.method} ${quoteType + url + quoteType}`;
4748
}
4849

4950
if (request.body && !request.headers.has('Content-Type')) {
@@ -186,6 +187,15 @@ self = module.exports = {
186187
description: 'Set a character used to mark the continuation of a statement on the next line ' +
187188
'(generally, \\ for OSX/Linux, ^ for Windows)'
188189
},
190+
{
191+
name: 'Quote Type',
192+
id: 'quoteType',
193+
availableOptions: ['single', 'double'],
194+
type: 'string',
195+
default: 'single',
196+
description: 'String denoting the quote type to use (single or double) for URL ' +
197+
'(Use double quotes when running curl in cmd.exe and single quotes for the rest)'
198+
},
189199
{
190200
name: 'Set request timeout',
191201
id: 'requestTimeout',

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('Convert for different types of request', function () {
1111
followRedirect: true,
1212
longFormat: true,
1313
silent: true,
14-
lineContinuationCharacter: '\\'
14+
lineContinuationCharacter: '\\',
15+
quoteType: 'single'
1516
};
1617

1718
runNewmanTest(convert, options, testConfig);

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

+42
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,48 @@ describe('curl convert function', function () {
3333
}
3434
});
3535
});
36+
it('should return snippet with url in single quote(\')', function () {
37+
request = new sdk.Request({
38+
'method': 'POST',
39+
'header': [],
40+
'body': {
41+
'mode': 'raw',
42+
'raw': ''
43+
}
44+
});
45+
options = {
46+
quoteType: 'single'
47+
};
48+
convert(request, options, function (error, snippet) {
49+
if (error) {
50+
expect.fail(null, null, error);
51+
}
52+
53+
snippetArray = snippet.split(' ');
54+
expect(snippetArray[4][0]).to.equal('\'');
55+
});
56+
});
57+
it('should return snippet with url in double quote(")', function () {
58+
request = new sdk.Request({
59+
'method': 'POST',
60+
'header': [],
61+
'body': {
62+
'mode': 'raw',
63+
'raw': ''
64+
}
65+
});
66+
options = {
67+
quoteType: 'double'
68+
};
69+
convert(request, options, function (error, snippet) {
70+
if (error) {
71+
expect.fail(null, null, error);
72+
}
73+
74+
snippetArray = snippet.split(' ');
75+
expect(snippetArray[4][0]).to.equal('"');
76+
});
77+
});
3678

3779
it('should parse header with string value properly', function () {
3880
request = new sdk.Request({

test/codegen/structure.test.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ const expectedOptions = {
6767
type: 'boolean',
6868
default: false,
6969
description: 'Modifies code snippet to incorporate ES6 (EcmaScript) features'
70+
},
71+
quoteType: {
72+
name: 'Quote Type',
73+
type: 'string',
74+
default: 'single',
75+
description: 'String denoting the quote type to use (single or double) for URL ' +
76+
'(Use double quotes when running curl in cmd.exe and single quotes for the rest)'
7077
}
7178
},
7279
// Standard array of ids that should be used for options ids. Any new option should be updated here.
@@ -83,7 +90,8 @@ const expectedOptions = {
8390
'lineContinuationCharacter',
8491
'protocol',
8592
'useMimeType',
86-
'ES6_enabled'
93+
'ES6_enabled',
94+
'quoteType'
8795
],
8896
CODEGEN_ABS_PATH = `./codegens/${codegen}`;
8997
describe('Code-gen repository ' + codegen, function () {

0 commit comments

Comments
 (0)