Skip to content

Commit c27a0ee

Browse files
committed
chore($state): validate rejection of bad params
1 parent d48505c commit c27a0ee

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/urlMatcherFactory.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function UrlMatcher(pattern, caseInsensitiveMatch) {
7979
function addParameter(id, type) {
8080
if (!/^\w+(-+\w+)*$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'");
8181
if (params[id]) throw new Error("Duplicate parameter name '" + id + "' in pattern '" + pattern + "'");
82-
params[id] = type;
82+
params[id] = angular.isNumber(type) ? new Type() : type;
8383
}
8484

8585
function quoteRegExp(string) {
@@ -238,7 +238,7 @@ UrlMatcher.prototype.validates = function (params) {
238238
result = result && self.params[key].is(val);
239239
});
240240
return result;
241-
}
241+
};
242242

243243
/**
244244
* @ngdoc function

test/stateSpec.js

+27
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ describe('state', function () {
9191
}
9292
}
9393
})
94+
.state('badParam', {
95+
url: "/bad/{param:int}"
96+
})
9497

9598
.state('first', { url: '^/first/subpath' })
9699
.state('second', { url: '^/second' })
@@ -712,6 +715,7 @@ describe('state', function () {
712715
'about.person.item',
713716
'about.sidebar',
714717
'about.sidebar.item',
718+
'badParam',
715719
'dynamicController',
716720
'first',
717721
'home',
@@ -779,6 +783,29 @@ describe('state', function () {
779783
expect($state.current.name).toBe('');
780784
}));
781785

786+
describe("typed parameter handling", function() {
787+
788+
it('should initialize parameters without a hacky empty test', inject(function ($urlMatcherFactory, $state) {
789+
new UrlMatcher("");
790+
}));
791+
792+
it('should ignore bad url parameters', inject(function ($state, $rootScope, $location, $urlMatcherFactory) {
793+
$location.path("/bad/5");
794+
$rootScope.$broadcast("$locationChangeSuccess");
795+
$rootScope.$apply();
796+
expect($state.current.name).toBe("badParam");
797+
798+
$state.transitionTo("about");
799+
$rootScope.$apply();
800+
expect($state.current.name).toBe('about');
801+
802+
$location.path("/bad/foo");
803+
$rootScope.$broadcast("$locationChangeSuccess");
804+
$rootScope.$apply();
805+
expect($state.current.name).toBe("about");
806+
}));
807+
});
808+
782809
it('should revert to last known working url on state change failure', inject(function ($state, $rootScope, $location, $q) {
783810
$state.transitionTo("about");
784811
$q.flush();

0 commit comments

Comments
 (0)