Skip to content
Merged
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
2 changes: 1 addition & 1 deletion codegens/powershell-restmethod/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function parseFormData (body, trim) {
* @param {boolean} trim trim body option
*/
function parseRawBody (body, trim) {
return `$body = "${sanitize(body.toString(), trim)}"\n`;
return `$body = @"\n${sanitize(body.toString(), trim, false)}\n"@\n`;
}

/**
Expand Down
10 changes: 7 additions & 3 deletions codegens/powershell-restmethod/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
*
* @param {String} inputString
* @param {Boolean} [trim] - indicates whether to trim string or not
* @param {Boolean} shouldEscapeNewLine - indicates whether to escape newline
* @returns {String}
*/
function sanitize (inputString, trim) {
function sanitize (inputString, trim, shouldEscapeNewLine = true) {
if (typeof inputString !== 'string') {
return '';
}
inputString = inputString
.replace(/`/g, '``')
.replace(/\$/g, '`$')
.replace(/\\/g, '\`\\')
.replace(/\"/g, '\`\"')
.replace(/\n/g, '\`n');
.replace(/\"/g, '\`\"');

if (shouldEscapeNewLine) {
inputString = inputString.replace(/\n/g, '\`n');
}
return trim ? inputString.trim() : inputString;
}

Expand Down
9 changes: 6 additions & 3 deletions codegens/powershell-restmethod/test/unit/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,16 @@ describe('Powershell-restmethod converter', function () {
expect.fail(null, null, error);
return;
}

const lines = snippet.split('\n');
expect(lines[0]).to
.eql('$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"');
expect(lines[1]).to.eql('$headers.Add("Content-Type", "text/plain")');
expect(lines[3]).to.eql('$body = "Hello world"');
expect(lines[5]).to.eql('$response = Invoke-RestMethod \'https://mockbin.org/request\' -Method \'POST\' -Headers $headers -Body $body -TimeoutSec 10'); // eslint-disable-line max-len
expect(lines[6]).to.eql('$response | ConvertTo-Json');
expect(lines[3]).to.eql('$body = @"');
expect(lines[4]).to.eql('Hello world');
expect(lines[5]).to.eql('"@');
expect(lines[7]).to.eql('$response = Invoke-RestMethod \'https://mockbin.org/request\' -Method \'POST\' -Headers $headers -Body $body -TimeoutSec 10'); // eslint-disable-line max-len
expect(lines[8]).to.eql('$response | ConvertTo-Json');
});
});
});
Expand Down
2 changes: 2 additions & 0 deletions npm/ci-requirements.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash
set -ev; # stop on error

sudo apt-get update

echo "Insalling dependencies required for tests in codegens/libcurl"
sudo apt-get install libcurl4-gnutls-dev

Expand Down