Skip to content

Commit 4a1ed73

Browse files
committed
Merge pull request #215 from nateabele/master
UrlMatcher should throw error when route contains capture groups
2 parents 4ffd28a + 03cd75a commit 4a1ed73

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/urlMatcherFactory.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ UrlMatcher.prototype.exec = function (path, searchParams) {
161161
nPath = this.segments.length-1,
162162
values = {}, i;
163163

164+
if (nPath !== m.length - 1) throw new Error("Unbalanced capture group in route '" + this.source + "'");
165+
164166
for (i=0; i<nPath; i++) values[params[i]] = decodeURIComponent(m[i+1]);
165167
for (/**/; i<nTotal; i++) values[params[i]] = searchParams[params[i]];
166168

test/urlMatcherFactorySpec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ describe("UrlMatcher", function () {
4040
.toBeNull();
4141
});
4242

43+
it('.exec() throws on unbalanced capture list', function () {
44+
var shouldThrow = {
45+
"/url/{matchedParam:([a-z]+)}/child/{childParam}": '/url/someword/child/childParam',
46+
"/url/{matchedParam:([a-z]+)}/child/{childParam}?foo": '/url/someword/child/childParam'
47+
};
48+
49+
angular.forEach(shouldThrow, function(url, route) {
50+
expect(function() { new UrlMatcher(route).exec(url, {}); }).toThrow(
51+
"Unbalanced capture group in route '" + route + "'"
52+
);
53+
});
54+
55+
var shouldPass = {
56+
"/url/{matchedParam:[a-z]+}/child/{childParam}": '/url/someword/child/childParam',
57+
"/url/{matchedParam:[a-z]+}/child/{childParam}?foo": '/url/someword/child/childParam'
58+
};
59+
60+
angular.forEach(shouldPass, function(url, route) {
61+
expect(function() { new UrlMatcher(route).exec(url, {}); }).not.toThrow();
62+
});
63+
});
64+
4365
it(".format() reconstitutes the URL", function () {
4466
expect(
4567
new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?from')

0 commit comments

Comments
 (0)