Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:postmanlabs/postman-code-generat…
Browse files Browse the repository at this point in the history
…ors into fix-unit-tests
  • Loading branch information
aman-v-singh committed Apr 30, 2024
2 parents 5003247 + b5116c1 commit 91046c1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
7 changes: 4 additions & 3 deletions codegens/powershell-restmethod/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var _ = require('./lodash'),
sanitize = require('./util').sanitize,
sanitizeSingleQuotes = require('./util').sanitizeSingleQuotes,
sanitizeOptions = require('./util').sanitizeOptions,
addFormParam = require('./util').addFormParam,
path = require('path');
Expand Down Expand Up @@ -289,12 +290,12 @@ function convert (request, options, callback) {
}

if (_.includes(VALID_METHODS, request.method)) {
codeSnippet += `$response = Invoke-RestMethod '${request.url.toString().replace(/'/g, '\'\'')}' -Method '` +
codeSnippet += `$response = Invoke-RestMethod '${sanitizeSingleQuotes(request.url.toString())}' -Method '` +
`${request.method}' -Headers $headers`;
}
else {
codeSnippet += `$response = Invoke-RestMethod '${request.url.toString().replace(/'/g, '\'\'')}' -CustomMethod ` +
`'${request.method.replace(/'/g, '\'\'')}' -Headers $headers`;
codeSnippet += `$response = Invoke-RestMethod '${sanitizeSingleQuotes(request.url.toString())}' -CustomMethod ` +
`'${sanitizeSingleQuotes(request.method)}' -Headers $headers`;
}
if (bodySnippet !== '') {
codeSnippet += ' -Body $body';
Expand Down
17 changes: 17 additions & 0 deletions codegens/powershell-restmethod/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ function sanitize (inputString, trim, shouldEscapeNewLine = true) {
return trim ? inputString.trim() : inputString;
}

/**
*
* @param {String} inputString - input string
* @returns {String} - sanitized string
*/
function sanitizeSingleQuotes (inputString) {
if (typeof inputString !== 'string') {
return '';
}
inputString = inputString
.replace(/'/g, '\'\'');

return inputString;

}

/**
* sanitizes input options
*
Expand Down Expand Up @@ -126,6 +142,7 @@ function addFormParam (array, key, type, val, disabled, contentType) {

module.exports = {
sanitize: sanitize,
sanitizeSingleQuotes: sanitizeSingleQuotes,
sanitizeOptions: sanitizeOptions,
addFormParam: addFormParam
};
39 changes: 39 additions & 0 deletions codegens/powershell-restmethod/test/unit/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,45 @@ describe('Powershell-restmethod converter', function () {
});
});

it('should generate valid snippet when single quotes in custom request method', function () {
var request = new sdk.Request({
// eslint-disable-next-line quotes
'method': "TEST';DIR;#'",
'header': [],
'url': {
'raw': 'https://postman-echo.com/get?query1=b\'b&query2=c"c',
'protocol': 'https',
'host': [
'postman-echo',
'com'
],
'path': [
'get'
],
'query': [
{
'key': 'query1',
'value': "b'b" // eslint-disable-line quotes
},
{
'key': 'query2',
'value': 'c"c'
}
]
}
});
convert(request, {}, function (error, snippet) {
if (error) {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
// An extra single quote is placed before a single quote to escape a single quote inside a single quoted string
// eslint-disable-next-line quotes
expect(snippet).to.include("-CustomMethod 'TEST'';DIR;#'''");
});
});


it('should generate snippet for form data params with no type key present', function () {
var request = new sdk.Request({
method: 'POST',
Expand Down

0 comments on commit 91046c1

Please sign in to comment.