From e80d3f171bfe8741f29b1229f6561113030aa2c9 Mon Sep 17 00:00:00 2001 From: Aman Singh <121886615+aman-v-singh@users.noreply.github.com> Date: Tue, 30 Apr 2024 13:47:38 +0530 Subject: [PATCH 1/3] IMPORT-1337 do not encode + in query param --- codegens/curl/lib/util.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/codegens/curl/lib/util.js b/codegens/curl/lib/util.js index c9b53af4d..16eadc039 100644 --- a/codegens/curl/lib/util.js +++ b/codegens/curl/lib/util.js @@ -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} @@ -226,7 +226,8 @@ var self = module.exports = { .replace(/%5D/g, ']') .replace(/%7D/g, '}') .replace(/%25/g, '%') - .replace(/'/g, '%27'); + .replace(/'/g, '%27') + .replace(/%2B/g, '+'); }, /** From c386332158eaf0bd1e6598b5f4d5eb66e893b8b5 Mon Sep 17 00:00:00 2001 From: Aman Singh <121886615+aman-v-singh@users.noreply.github.com> Date: Tue, 30 Apr 2024 16:22:26 +0530 Subject: [PATCH 2/3] included in test --- codegens/curl/lib/util.js | 4 ++-- codegens/curl/test/unit/convert.test.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/codegens/curl/lib/util.js b/codegens/curl/lib/util.js index 16eadc039..c10640085 100644 --- a/codegens/curl/lib/util.js +++ b/codegens/curl/lib/util.js @@ -225,9 +225,9 @@ var self = module.exports = { .replace(/%7B/g, '{') .replace(/%5D/g, ']') .replace(/%7D/g, '}') + .replace(/%2B/g, '+') .replace(/%25/g, '%') - .replace(/'/g, '%27') - .replace(/%2B/g, '+'); + .replace(/'/g, '%27'); }, /** diff --git a/codegens/curl/test/unit/convert.test.js b/codegens/curl/test/unit/convert.test.js index be9d709c7..12f4ae687 100644 --- a/codegens/curl/test/unit/convert.test.js +++ b/codegens/curl/test/unit/convert.test.js @@ -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.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b+c%27'); }); it('should not encode query params that are already encoded', function () { From df6e88c6570067253d98fcfa3de1a3e023645c2c Mon Sep 17 00:00:00 2001 From: Aman Singh <121886615+aman-v-singh@users.noreply.github.com> Date: Tue, 30 Apr 2024 18:41:18 +0530 Subject: [PATCH 3/3] Added changes and test for all the codegens --- codegens/curl/test/unit/convert.test.js | 2 +- codegens/golang/lib/util.js | 3 ++- codegens/golang/test/unit/convert.test.js | 13 ++++++++++++- codegens/java-unirest/lib/parseRequest.js | 3 ++- codegens/java-unirest/test/unit/convert.test.js | 6 +++--- codegens/js-xhr/lib/util.js | 3 ++- codegens/js-xhr/test/unit/convert.test.js | 11 ++++++++++- codegens/libcurl/lib/util.js | 3 ++- codegens/libcurl/test/unit/convert.test.js | 10 ++++++++++ codegens/ocaml-cohttp/lib/util.js | 1 + codegens/ocaml-cohttp/test/unit/convert.test.js | 11 +++++++++++ codegens/php-curl/lib/util/sanitize.js | 1 + codegens/php-curl/test/unit/converter.test.js | 12 +++++++++++- codegens/swift/lib/util.js | 1 + codegens/swift/test/unit/convert.test.js | 6 +++--- 15 files changed, 72 insertions(+), 14 deletions(-) diff --git a/codegens/curl/test/unit/convert.test.js b/codegens/curl/test/unit/convert.test.js index 12f4ae687..81362faf5 100644 --- a/codegens/curl/test/unit/convert.test.js +++ b/codegens/curl/test/unit/convert.test.js @@ -680,7 +680,7 @@ describe('curl convert function', function () { 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.not.include('key2=\'a b+c\''); expect(outputUrlString).to.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b+c%27'); }); diff --git a/codegens/golang/lib/util.js b/codegens/golang/lib/util.js index 005da12a7..fae5ac768 100644 --- a/codegens/golang/lib/util.js +++ b/codegens/golang/lib/util.js @@ -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} @@ -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'); }, diff --git a/codegens/golang/test/unit/convert.test.js b/codegens/golang/test/unit/convert.test.js index 4859c85e6..9644429bd 100644 --- a/codegens/golang/test/unit/convert.test.js +++ b/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 () { @@ -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'); + }); }); }); diff --git a/codegens/java-unirest/lib/parseRequest.js b/codegens/java-unirest/lib/parseRequest.js index 6e6053bea..1c16c66e2 100644 --- a/codegens/java-unirest/lib/parseRequest.js +++ b/codegens/java-unirest/lib/parseRequest.js @@ -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 @@ -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'); } diff --git a/codegens/java-unirest/test/unit/convert.test.js b/codegens/java-unirest/test/unit/convert.test.js index 02ff19310..cd9a2c700 100644 --- a/codegens/java-unirest/test/unit/convert.test.js +++ b/codegens/java-unirest/test/unit/convert.test.js @@ -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 () { diff --git a/codegens/js-xhr/lib/util.js b/codegens/js-xhr/lib/util.js index 525862b57..94fb47ac7 100644 --- a/codegens/js-xhr/lib/util.js +++ b/codegens/js-xhr/lib/util.js @@ -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} @@ -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'); }, diff --git a/codegens/js-xhr/test/unit/convert.test.js b/codegens/js-xhr/test/unit/convert.test.js index c36f2226d..c972d8656 100644 --- a/codegens/js-xhr/test/unit/convert.test.js +++ b/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; @@ -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 () { diff --git a/codegens/libcurl/lib/util.js b/codegens/libcurl/lib/util.js index ddc8cfce0..e81f6187d 100644 --- a/codegens/libcurl/lib/util.js +++ b/codegens/libcurl/lib/util.js @@ -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} @@ -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'); }, diff --git a/codegens/libcurl/test/unit/convert.test.js b/codegens/libcurl/test/unit/convert.test.js index 5ff1e9630..57b33f02c 100644 --- a/codegens/libcurl/test/unit/convert.test.js +++ b/codegens/libcurl/test/unit/convert.test.js @@ -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'); @@ -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'); + }); }); }); diff --git a/codegens/ocaml-cohttp/lib/util.js b/codegens/ocaml-cohttp/lib/util.js index 7cd812e17..e23d4c5be 100644 --- a/codegens/ocaml-cohttp/lib/util.js +++ b/codegens/ocaml-cohttp/lib/util.js @@ -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'); }, diff --git a/codegens/ocaml-cohttp/test/unit/convert.test.js b/codegens/ocaml-cohttp/test/unit/convert.test.js index 0d80296c8..cd242f032 100644 --- a/codegens/ocaml-cohttp/test/unit/convert.test.js +++ b/codegens/ocaml-cohttp/test/unit/convert.test.js @@ -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 () { @@ -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'); + }); }); }); diff --git a/codegens/php-curl/lib/util/sanitize.js b/codegens/php-curl/lib/util/sanitize.js index 6755abf3d..fab55ebee 100644 --- a/codegens/php-curl/lib/util/sanitize.js +++ b/codegens/php-curl/lib/util/sanitize.js @@ -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'); }, diff --git a/codegens/php-curl/test/unit/converter.test.js b/codegens/php-curl/test/unit/converter.test.js index 068b425fa..cf07d0f03 100644 --- a/codegens/php-curl/test/unit/converter.test.js +++ b/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 () { @@ -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'); + }); }); diff --git a/codegens/swift/lib/util.js b/codegens/swift/lib/util.js index 4465a5d81..0a83261ab 100644 --- a/codegens/swift/lib/util.js +++ b/codegens/swift/lib/util.js @@ -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'); } diff --git a/codegens/swift/test/unit/convert.test.js b/codegens/swift/test/unit/convert.test.js index 5be4966fe..82650b1b4 100644 --- a/codegens/swift/test/unit/convert.test.js +++ b/codegens/swift/test/unit/convert.test.js @@ -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 () {