Skip to content

Commit 91046c1

Browse files
committed
Merge branch 'develop' of github.com:postmanlabs/postman-code-generators into fix-unit-tests
2 parents 5003247 + b5116c1 commit 91046c1

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

codegens/powershell-restmethod/lib/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var _ = require('./lodash'),
22
sanitize = require('./util').sanitize,
3+
sanitizeSingleQuotes = require('./util').sanitizeSingleQuotes,
34
sanitizeOptions = require('./util').sanitizeOptions,
45
addFormParam = require('./util').addFormParam,
56
path = require('path');
@@ -289,12 +290,12 @@ function convert (request, options, callback) {
289290
}
290291

291292
if (_.includes(VALID_METHODS, request.method)) {
292-
codeSnippet += `$response = Invoke-RestMethod '${request.url.toString().replace(/'/g, '\'\'')}' -Method '` +
293+
codeSnippet += `$response = Invoke-RestMethod '${sanitizeSingleQuotes(request.url.toString())}' -Method '` +
293294
`${request.method}' -Headers $headers`;
294295
}
295296
else {
296-
codeSnippet += `$response = Invoke-RestMethod '${request.url.toString().replace(/'/g, '\'\'')}' -CustomMethod ` +
297-
`'${request.method.replace(/'/g, '\'\'')}' -Headers $headers`;
297+
codeSnippet += `$response = Invoke-RestMethod '${sanitizeSingleQuotes(request.url.toString())}' -CustomMethod ` +
298+
`'${sanitizeSingleQuotes(request.method)}' -Headers $headers`;
298299
}
299300
if (bodySnippet !== '') {
300301
codeSnippet += ' -Body $body';

codegens/powershell-restmethod/lib/util.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ function sanitize (inputString, trim, shouldEscapeNewLine = true) {
2424
return trim ? inputString.trim() : inputString;
2525
}
2626

27+
/**
28+
*
29+
* @param {String} inputString - input string
30+
* @returns {String} - sanitized string
31+
*/
32+
function sanitizeSingleQuotes (inputString) {
33+
if (typeof inputString !== 'string') {
34+
return '';
35+
}
36+
inputString = inputString
37+
.replace(/'/g, '\'\'');
38+
39+
return inputString;
40+
41+
}
42+
2743
/**
2844
* sanitizes input options
2945
*
@@ -126,6 +142,7 @@ function addFormParam (array, key, type, val, disabled, contentType) {
126142

127143
module.exports = {
128144
sanitize: sanitize,
145+
sanitizeSingleQuotes: sanitizeSingleQuotes,
129146
sanitizeOptions: sanitizeOptions,
130147
addFormParam: addFormParam
131148
};

codegens/powershell-restmethod/test/unit/convert.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,45 @@ describe('Powershell-restmethod converter', function () {
498498
});
499499
});
500500

501+
it('should generate valid snippet when single quotes in custom request method', function () {
502+
var request = new sdk.Request({
503+
// eslint-disable-next-line quotes
504+
'method': "TEST';DIR;#'",
505+
'header': [],
506+
'url': {
507+
'raw': 'https://postman-echo.com/get?query1=b\'b&query2=c"c',
508+
'protocol': 'https',
509+
'host': [
510+
'postman-echo',
511+
'com'
512+
],
513+
'path': [
514+
'get'
515+
],
516+
'query': [
517+
{
518+
'key': 'query1',
519+
'value': "b'b" // eslint-disable-line quotes
520+
},
521+
{
522+
'key': 'query2',
523+
'value': 'c"c'
524+
}
525+
]
526+
}
527+
});
528+
convert(request, {}, function (error, snippet) {
529+
if (error) {
530+
expect.fail(null, null, error);
531+
}
532+
expect(snippet).to.be.a('string');
533+
// An extra single quote is placed before a single quote to escape a single quote inside a single quoted string
534+
// eslint-disable-next-line quotes
535+
expect(snippet).to.include("-CustomMethod 'TEST'';DIR;#'''");
536+
});
537+
});
538+
539+
501540
it('should generate snippet for form data params with no type key present', function () {
502541
var request = new sdk.Request({
503542
method: 'POST',

0 commit comments

Comments
 (0)