[Issue#18] - Consistent parsing of all template variables

This commit is contained in:
abhijitkane
2019-05-15 01:21:53 +05:30
parent 23c8ca148b
commit a2bb828f17
2 changed files with 17 additions and 17 deletions

View File

@@ -89,23 +89,14 @@ module.exports = {
* @returns {string} string after replacing /{pet}/ with /:pet/ * @returns {string} string after replacing /{pet}/ with /:pet/
*/ */
fixPathVariablesInUrl: function (url) { fixPathVariablesInUrl: function (url) {
/* // All complicated logic removed
The first .replace: // This simply replaces all instances of {text} with {{text}}
Handling path templating in request url if any // text cannot have any of these 3 chars: /{}
only replace /{pet}<end-of-string> or /{pet}/ // and {text} cannot be followed by a }
https://regex101.com/r/Yqm2As/2 // {{text}} will not be converted
// https://regex101.com/r/9N1520/1
The second .replace:
we also need to replace the scheme variables
{scheme}://api.com
needs to become
{{scheme}}://api.com
and scheme is already set as a collection variable
*/
return url return url
.replace(/\/\{([a-zA-Z0-9\-\_]+)\}(\/|$)/g, '/:$1$2') .replace(/(\{[^\/\{\}]+\})(?!\})/g, '{$1}');
.replace(/^\{([a-zA-Z0-9\-\_]+)\}\:/, '{{$1}}:');
}, },
/** /**

View File

@@ -1558,7 +1558,16 @@ describe('UTILITY FUNCTION TESTS ', function () {
describe('fixPathVariablesInUrl function', function() { describe('fixPathVariablesInUrl function', function() {
it('should convert a url with scheme and path variables', function(done) { it('should convert a url with scheme and path variables', function(done) {
var convertedUrl = Utils.fixPathVariablesInUrl('{scheme}://developer.uspto.gov/{path0}/segment/{path1}'); var convertedUrl = Utils.fixPathVariablesInUrl('{scheme}://developer.uspto.gov/{path0}/segment/{path1}');
expect(convertedUrl).to.equal('{{scheme}}://developer.uspto.gov/:path0/segment/:path1'); expect(convertedUrl).to.equal('{{scheme}}://developer.uspto.gov/{{path0}}/segment/{{path1}}');
expect(Utils.fixPathVariablesInUrl('{{a}}')).to.equal('{{a}}');
expect(Utils.fixPathVariablesInUrl('{{a}}://{b}.com/{pathvar}/{morevar}'))
.to.equal('{{a}}://{{b}}.com/{{pathvar}}/{{morevar}}');
expect(Utils.fixPathVariablesInUrl('{protocol}://{host}:{port}/{contextpath}/{restapi}'))
.to.equal('{{protocol}}://{{host}}:{{port}}/{{contextpath}}/{{restapi}}');
done(); done();
}); });
}); });