Skip to content

Commit

Permalink
chore($state): validate rejection of bad params
Browse files Browse the repository at this point in the history
  • Loading branch information
nateabele committed Apr 11, 2014
1 parent d48505c commit c27a0ee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function UrlMatcher(pattern, caseInsensitiveMatch) {
function addParameter(id, type) {
if (!/^\w+(-+\w+)*$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'");
if (params[id]) throw new Error("Duplicate parameter name '" + id + "' in pattern '" + pattern + "'");
params[id] = type;
params[id] = angular.isNumber(type) ? new Type() : type;
}

function quoteRegExp(string) {
Expand Down Expand Up @@ -238,7 +238,7 @@ UrlMatcher.prototype.validates = function (params) {
result = result && self.params[key].is(val);
});
return result;
}
};

/**
* @ngdoc function
Expand Down
27 changes: 27 additions & 0 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ describe('state', function () {
}
}
})
.state('badParam', {
url: "/bad/{param:int}"
})

.state('first', { url: '^/first/subpath' })
.state('second', { url: '^/second' })
Expand Down Expand Up @@ -712,6 +715,7 @@ describe('state', function () {
'about.person.item',
'about.sidebar',
'about.sidebar.item',
'badParam',
'dynamicController',
'first',
'home',
Expand Down Expand Up @@ -779,6 +783,29 @@ describe('state', function () {
expect($state.current.name).toBe('');
}));

describe("typed parameter handling", function() {

it('should initialize parameters without a hacky empty test', inject(function ($urlMatcherFactory, $state) {
new UrlMatcher("");
}));

it('should ignore bad url parameters', inject(function ($state, $rootScope, $location, $urlMatcherFactory) {
$location.path("/bad/5");
$rootScope.$broadcast("$locationChangeSuccess");
$rootScope.$apply();
expect($state.current.name).toBe("badParam");

$state.transitionTo("about");
$rootScope.$apply();
expect($state.current.name).toBe('about');

$location.path("/bad/foo");
$rootScope.$broadcast("$locationChangeSuccess");
$rootScope.$apply();
expect($state.current.name).toBe("about");
}));
});

it('should revert to last known working url on state change failure', inject(function ($state, $rootScope, $location, $q) {
$state.transitionTo("about");
$q.flush();
Expand Down

0 comments on commit c27a0ee

Please sign in to comment.