Skip to content

Commit

Permalink
fix(UrlMatcher): isOptional always false for empty parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Krebser committed Feb 15, 2016
1 parent 87f10df commit 4e85db4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,6 @@ function $UrlMatcherFactory() {
type = getType(config, type, location);
var arrayMode = getArrayMode();
type = arrayMode ? type.$asArray(arrayMode, location === "search") : type;
if (type.name === "string" && !arrayMode && location === "path" && config.value === undefined)
config.value = ""; // for 0.2.x; in 0.3.0+ do not automatically default to ""
var isOptional = config.value !== undefined;
var squash = getSquashPolicy(config, isOptional);
var replace = getReplace(config, arrayMode, isOptional, squash);
Expand Down
15 changes: 15 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,22 @@ describe("UrlMatcher", function () {
var m = new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?from&to');
expect(m.exec('/users/123/details/what/thisShouldBeDigits', {})).toBeNull();
});

it("should not use optional regexp for '/'", function () {
var m = new UrlMatcher('/{language:(?:fr|en|de)}');
expect(m.exec('/', {})).toBeNull();
});

it("should work with empty default value", function () {
var m = new UrlMatcher('/foo/:str', { params: { str: { value: "" } } });
expect(m.exec('/foo/', {})).toEqual({ str: "" });
});

it("should work with empty default value for regex", function () {
var m = new UrlMatcher('/foo/{param:(?:foo|bar|)}', { params: { param: { value: "" } } });
expect(m.exec('/foo/', {})).toEqual({ param: "" });
});

it("should treat the URL as already decoded and does not decode it further", function () {
expect(new UrlMatcher('/users/:id').exec('/users/100%25', {})).toEqual({ id: '100%25'});
});
Expand Down

0 comments on commit 4e85db4

Please sign in to comment.