Skip to content

Commit

Permalink
[FIX] cURL code snippet contains encoded value for special char + ins…
Browse files Browse the repository at this point in the history
…tead of char itself (#735)

* IMPORT-1337 do not encode + in query param

* included in test

* Added changes and test for all the codegens
  • Loading branch information
aman-v-singh committed May 2, 2024
1 parent 13a946d commit 7dcc69b
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 17 deletions.
3 changes: 2 additions & 1 deletion codegens/curl/lib/util.js
Expand Up @@ -214,7 +214,7 @@ var self = module.exports = {
},

/**
* Encode param except the following characters- [,{,},],%
* Encode param except the following characters- [,{,},],%,+
*
* @param {String} param
* @returns {String}
Expand All @@ -225,6 +225,7 @@ var self = module.exports = {
.replace(/%7B/g, '{')
.replace(/%5D/g, ']')
.replace(/%7D/g, '}')
.replace(/%2B/g, '+')
.replace(/%25/g, '%')
.replace(/'/g, '%27');
},
Expand Down
6 changes: 3 additions & 3 deletions codegens/curl/test/unit/convert.test.js
Expand Up @@ -676,12 +676,12 @@ describe('curl convert function', function () {

it('should not encode unresolved query params and ' +
'encode every other query param, both present together', function () {
rawUrl = 'https://postman-echo.com/get?key1={{value}}&key2=\'a b c\'';
rawUrl = 'https://postman-echo.com/get?key1={{value}}&key2=\'a b+c\'';
urlObject = new sdk.Url(rawUrl);
outputUrlString = getUrlStringfromUrlObject(urlObject);
expect(outputUrlString).to.not.include('key1=%7B%7Bvalue%7B%7B');
expect(outputUrlString).to.not.include('key2=\'a b c\'');
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b%20c%27');
expect(outputUrlString).to.not.include('key2=\'a b+c\'');
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b+c%27');
});

it('should not encode query params that are already encoded', function () {
Expand Down
3 changes: 2 additions & 1 deletion codegens/golang/lib/util.js
Expand Up @@ -109,7 +109,7 @@ const self = module.exports = {
},

/**
* Encode param except the following characters- [,{,},],%
* Encode param except the following characters- [,{,},],%,+
*
* @param {String} param
* @returns {String}
Expand All @@ -120,6 +120,7 @@ const self = module.exports = {
.replace(/%7B/g, '{')
.replace(/%5D/g, ']')
.replace(/%7D/g, '}')
.replace(/%2B/g, '+')
.replace(/%25/g, '%')
.replace(/'/g, '%27');
},
Expand Down
13 changes: 12 additions & 1 deletion codegens/golang/test/unit/convert.test.js
@@ -1,6 +1,7 @@
var expect = require('chai').expect,
sdk = require('postman-collection'),
convert = require('../../index').convert;
convert = require('../../index').convert,
getUrlStringfromUrlObject = require('../../lib/util').getUrlStringfromUrlObject;

describe('Golang convert function', function () {
describe('Convert function', function () {
Expand Down Expand Up @@ -253,5 +254,15 @@ describe('Golang convert function', function () {
});
});
});

it('should not encode unresolved query params and ' +
'encode every other query param, both present together', function () {
let rawUrl = 'https://postman-echo.com/get?key1={{value}}&key2=\'a b+c\'',
urlObject = new sdk.Url(rawUrl),
outputUrlString = getUrlStringfromUrlObject(urlObject);
expect(outputUrlString).to.not.include('key1=%7B%7Bvalue%7B%7B');
expect(outputUrlString).to.not.include('key2=\'a b+c\'');
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b+c%27');
});
});
});
3 changes: 2 additions & 1 deletion codegens/java-unirest/lib/parseRequest.js
Expand Up @@ -3,7 +3,7 @@ var _ = require('./lodash'),
sanitize = require('./util').sanitize;

/**
* Encode param except the following characters- [,],%
* Encode param except the following characters- [,],%,+
* Characters { and } are kept encoded because unirest does not support them
*
* @param {String} param
Expand All @@ -13,6 +13,7 @@ function encodeParam (param) {
return encodeURIComponent(param)
.replace(/%5B/g, '[')
.replace(/%5D/g, ']')
.replace(/%2B/g, '+')
.replace(/%25/g, '%')
.replace(/'/g, '%27');
}
Expand Down
6 changes: 3 additions & 3 deletions codegens/java-unirest/test/unit/convert.test.js
Expand Up @@ -500,12 +500,12 @@ describe('java unirest convert function for test collection', function () {

it('should not encode unresolved query params and ' +
'encode every other query param, both present together', function () {
rawUrl = 'https://postman-echo.com/get?key1={{value}}&key2=\'a b c\'';
rawUrl = 'https://postman-echo.com/get?key1={{value}}&key2=\'a b+c\'';
urlObject = new sdk.Url(rawUrl);
outputUrlString = getUrlStringfromUrlObject(urlObject);
expect(outputUrlString).to.include('key1=%7B%7Bvalue%7D%7D');
expect(outputUrlString).to.not.include('key2=\'a b c\'');
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1=%7B%7Bvalue%7D%7D&key2=%27a%20b%20c%27');
expect(outputUrlString).to.not.include('key2=\'a b+c\'');
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1=%7B%7Bvalue%7D%7D&key2=%27a%20b+c%27');
});

it('should discard disabled query params', function () {
Expand Down
3 changes: 2 additions & 1 deletion codegens/js-xhr/lib/util.js
Expand Up @@ -158,7 +158,7 @@ const self = module.exports = {
},

/**
* Encode param except the following characters- [,{,},],%
* Encode param except the following characters- [,{,},],%,+
*
* @param {String} param
* @returns {String}
Expand All @@ -169,6 +169,7 @@ const self = module.exports = {
.replace(/%7B/g, '{')
.replace(/%5D/g, ']')
.replace(/%7D/g, '}')
.replace(/%2B/g, '+')
.replace(/%25/g, '%')
.replace(/'/g, '%27');
},
Expand Down
11 changes: 10 additions & 1 deletion codegens/js-xhr/test/unit/convert.test.js
@@ -1,7 +1,7 @@
var expect = require('chai').expect,
sdk = require('postman-collection'),
sanitize = require('../../lib/util.js').sanitize,

getUrlStringfromUrlObject = require('../../lib/util').getUrlStringfromUrlObject,
convert = require('../../index').convert,
getOptions = require('../../index').getOptions;

Expand Down Expand Up @@ -162,6 +162,15 @@ describe('js-xhr convert function', function () {
it('should trim input string when needed', function () {
expect(sanitize('inputString ', true)).to.equal('inputString');
});
it('should not encode unresolved query params and ' +
'encode every other query param, both present together', function () {
let rawUrl = 'https://postman-echo.com/get?key1={{value}}&key2=\'a b+c\'',
urlObject = new sdk.Url(rawUrl),
outputUrlString = getUrlStringfromUrlObject(urlObject);
expect(outputUrlString).to.not.include('key1=%7B%7Bvalue%7B%7B');
expect(outputUrlString).to.not.include('key2=\'a b+c\'');
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b+c%27');
});
});

describe('POST Form data Request', function () {
Expand Down
3 changes: 2 additions & 1 deletion codegens/libcurl/lib/util.js
Expand Up @@ -160,7 +160,7 @@ const self = module.exports = {
},

/**
* Encode param except the following characters- [,{,},],%
* Encode param except the following characters- [,{,},],%,+
*
* @param {String} param
* @returns {String}
Expand All @@ -171,6 +171,7 @@ const self = module.exports = {
.replace(/%7B/g, '{')
.replace(/%5D/g, ']')
.replace(/%7D/g, '}')
.replace(/%2B/g, '+')
.replace(/%25/g, '%')
.replace(/'/g, '%27');
},
Expand Down
10 changes: 10 additions & 0 deletions codegens/libcurl/test/unit/convert.test.js
Expand Up @@ -2,6 +2,7 @@ var expect = require('chai').expect,
sdk = require('postman-collection'),
convert = require('../../index').convert,
getOptions = require('../../index').getOptions,
getUrlStringfromUrlObject = require('../../lib/util').getUrlStringfromUrlObject,
sanitize = require('../../lib/util').sanitize,
mainCollection = require('../../../../test/codegen/newman/fixtures/basicCollection.json');

Expand Down Expand Up @@ -207,5 +208,14 @@ describe('libcurl convert function', function () {
expect(sanitize('inputString ', true)).to.equal('inputString');
});

it('should not encode unresolved query params and ' +
'encode every other query param, both present together', function () {
let rawUrl = 'https://postman-echo.com/get?key1={{value}}&key2=\'a b+c\'',
urlObject = new sdk.Url(rawUrl),
outputUrlString = getUrlStringfromUrlObject(urlObject);
expect(outputUrlString).to.not.include('key1=%7B%7Bvalue%7B%7B');
expect(outputUrlString).to.not.include('key2=\'a b+c\'');
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b+c%27');
});
});
});
1 change: 1 addition & 0 deletions codegens/ocaml-cohttp/lib/util.js
Expand Up @@ -184,6 +184,7 @@ const self = module.exports = {
.replace(/%7B/g, '{')
.replace(/%5D/g, ']')
.replace(/%7D/g, '}')
.replace(/%2B/g, '+')
.replace(/%25/g, '%')
.replace(/'/g, '%27');
},
Expand Down
11 changes: 11 additions & 0 deletions codegens/ocaml-cohttp/test/unit/convert.test.js
Expand Up @@ -3,6 +3,7 @@ var expect = require('chai').expect,
convert = require('../../index').convert,
getOptions = require('../../index').getOptions,
sanitize = require('../../lib/util').sanitize,
getUrlStringfromUrlObject = require('../../lib/util').getUrlStringfromUrlObject,
mainCollection = require('./fixtures/testcollection/collection.json');

describe('Ocaml unit tests', function () {
Expand Down Expand Up @@ -245,5 +246,15 @@ describe('Ocaml unit tests', function () {
expect(sanitize(123, 'raw', false)).to.equal('');
expect(sanitize('inputString', 123, true)).to.equal('inputString');
});

it('should not encode unresolved query params and ' +
'encode every other query param, both present together', function () {
let rawUrl = 'https://postman-echo.com/get?key1={{value}}&key2=\'a b+c\'',
urlObject = new sdk.Url(rawUrl),
outputUrlString = getUrlStringfromUrlObject(urlObject);
expect(outputUrlString).to.not.include('key1=%7B%7Bvalue%7B%7B');
expect(outputUrlString).to.not.include('key2=\'a b+c\'');
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b+c%27');
});
});
});
1 change: 1 addition & 0 deletions codegens/php-curl/lib/util/sanitize.js
Expand Up @@ -174,6 +174,7 @@ const self = module.exports = {
.replace(/%7B/g, '{')
.replace(/%5D/g, ']')
.replace(/%7D/g, '}')
.replace(/%2B/g, '+')
.replace(/%25/g, '%')
.replace(/'/g, '%27');
},
Expand Down
12 changes: 11 additions & 1 deletion codegens/php-curl/test/unit/converter.test.js
@@ -1,6 +1,7 @@
var expect = require('chai').expect,
sdk = require('postman-collection'),
convert = require('../../lib/index').convert;
convert = require('../../lib/index').convert,
getUrlStringfromUrlObject = require('../../lib/util/sanitize').getUrlStringfromUrlObject;

describe('php-curl converter', function () {
it('should throw an error when callback is not function', function () {
Expand Down Expand Up @@ -85,4 +86,13 @@ describe('php-curl converter', function () {
});
});

it('should not encode unresolved query params and ' +
'encode every other query param, both present together', function () {
let rawUrl = 'https://postman-echo.com/get?key1={{value}}&key2=\'a b+c\'',
urlObject = new sdk.Url(rawUrl),
outputUrlString = getUrlStringfromUrlObject(urlObject);
expect(outputUrlString).to.not.include('key1=%7B%7Bvalue%7B%7B');
expect(outputUrlString).to.not.include('key2=\'a b+c\'');
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b+c%27');
});
});
1 change: 1 addition & 0 deletions codegens/swift/lib/util.js
Expand Up @@ -115,6 +115,7 @@ function encodeParam (param) {
.replace(/%7B/g, '{')
.replace(/%5D/g, ']')
.replace(/%7D/g, '}')
.replace(/%2B/g, '+')
.replace(/%25/g, '%')
.replace(/'/g, '%27');
}
Expand Down
6 changes: 3 additions & 3 deletions codegens/swift/test/unit/convert.test.js
Expand Up @@ -324,12 +324,12 @@ describe('Swift Converter', function () {

it('should not encode unresolved query params and ' +
'encode every other query param, both present together', function () {
rawUrl = 'https://postman-echo.com/get?key1={{value}}&key2=\'a b c\'';
rawUrl = 'https://postman-echo.com/get?key1={{value}}&key2=\'a b+c\'';
urlObject = new sdk.Url(rawUrl);
outputUrlString = getUrlStringfromUrlObject(urlObject);
expect(outputUrlString).to.not.include('key1=%7B%7Bvalue%7B%7B');
expect(outputUrlString).to.not.include('key2=\'a b c\'');
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b%20c%27');
expect(outputUrlString).to.not.include('key2=\'a b+c\'');
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b+c%27');
});

it('should not encode query params that are already encoded', function () {
Expand Down

0 comments on commit 7dcc69b

Please sign in to comment.