refactor(ngMocks): simplify routeToRegExp by assuming path has query/hash stripped off

Closes #16672
This commit is contained in:
George Kalpakas
2018-08-21 15:13:21 +03:00
parent 99ad41fa3f
commit a5537359c5
2 changed files with 21 additions and 14 deletions
+10 -4
View File
@@ -1771,8 +1771,8 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* See {@link ngMock.$httpBackend#when `when`} for more info.
*/
$httpBackend.whenRoute = function(method, url) {
var pathObj = routeToRegExp(url, {caseInsensitiveMatch: true, ignoreTrailingSlashes: true, isUrl: true});
return $httpBackend.when(method, pathObj.regexp, undefined, undefined, pathObj.keys);
var parsed = parseRouteUrl(url);
return $httpBackend.when(method, parsed.regexp, undefined, undefined, parsed.keys);
};
/**
@@ -1955,8 +1955,8 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* See {@link ngMock.$httpBackend#expect `expect`} for more info.
*/
$httpBackend.expectRoute = function(method, url) {
var pathObj = routeToRegExp(url, {caseInsensitiveMatch: true, ignoreTrailingSlashes: true, isUrl: true});
return $httpBackend.expect(method, pathObj.regexp, undefined, undefined, pathObj.keys);
var parsed = parseRouteUrl(url);
return $httpBackend.expect(method, parsed.regexp, undefined, undefined, parsed.keys);
};
@@ -2084,6 +2084,12 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
};
});
}
function parseRouteUrl(url) {
var strippedUrl = stripQueryAndHash(url);
var parseOptions = {caseInsensitiveMatch: true, ignoreTrailingSlashes: true};
return routeToRegExp(strippedUrl, parseOptions);
}
}
function assertArgDefined(args, index, name) {
+11 -10
View File
@@ -3,29 +3,30 @@
/* global routeToRegExp: true */
/**
* @param pathOrUrl {string} path or url
* @param opts {Object} options
* @return {?Object}
* @param {string} path - The path to parse. (It is assumed to have query and hash stripped off.)
* @param {Object} opts - Options.
* @return {Object} - An object containing an array of path parameter names (`keys`) and a regular
* expression (`regexp`) that can be used to identify a matching URL and extract the path
* parameter values.
*
* @description
* Normalizes the given path, returning a regular expression
* and the original path.
* Parses the given path, extracting path parameter names and a regular expression to match URLs.
*
* Inspired by pathRexp in visionmedia/express/lib/utils.js.
* Originally inspired by `pathRexp` in `visionmedia/express/lib/utils.js`.
*/
function routeToRegExp(pathOrUrl, opts) {
function routeToRegExp(path, opts) {
var keys = [];
var pattern = pathOrUrl
var pattern = path
.replace(/([().])/g, '\\$1')
.replace(/(\/)?:(\w+)(\*\?|[?*])?/g, function(_, slash, key, option) {
var optional = option === '?' || option === '*?';
var star = option === '*' || option === '*?';
keys.push({ name: key, optional: optional });
keys.push({name: key, optional: optional});
slash = slash || '';
return (
(optional ? '(?:' + slash : slash + '(?:') +
(opts.isUrl ? (star ? '([^?#]+?)' : '([^/?#]+)') : (star ? '(.+?)' : '([^/]+)')) +
(star ? '(.+?)' : '([^/]+)') +
(optional ? '?)?' : ')')
);
})