Skip to content

Commit

Permalink
fix(router): throw when cannot parse a url
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Jul 21, 2016
1 parent 44709e0 commit 27b87ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
15 changes: 12 additions & 3 deletions modules/@angular/router/src/url_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ function matchUrlQueryParamValue(str: string): string {
}

class UrlParser {
constructor(private remaining: string) {}
private remaining: string;
constructor(private url: string) { this.remaining = url; }

peekStartsWith(str: string): boolean { return this.remaining.startsWith(str); }

Expand Down Expand Up @@ -422,7 +423,16 @@ class UrlParser {
const segments: {[key: string]: UrlSegment} = {};
this.capture('(');
while (!this.peekStartsWith(')') && this.remaining.length > 0) {
let path = matchPathWithParams(this.remaining);
const path = matchPathWithParams(this.remaining);

const next = this.remaining[path.length];

// if is is not one of these characters, then the segment was unescaped
// or the group was not closed
if (next !== '/' && next !== ')' && next !== ';') {
throw new Error(`Cannot parse url '${this.url}'`);
}

let outletName: string;
if (path.indexOf(':') > -1) {
outletName = path.substr(0, path.indexOf(':'));
Expand All @@ -435,7 +445,6 @@ class UrlParser {
const children = this.parseSegmentChildren();
segments[outletName] = Object.keys(children).length === 1 ? children[PRIMARY_OUTLET] :
new UrlSegment([], children);

if (this.peekStartsWith('//')) {
this.capture('//');
}
Expand Down
12 changes: 12 additions & 0 deletions modules/@angular/router/test/url_serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ describe('url serializer', () => {
expect(url.serialize(tree)).toEqual('/one#two');
});


it('should parse empty fragment', () => {
const tree = url.parse('/one#');
expect(tree.fragment).toEqual('');
Expand Down Expand Up @@ -183,6 +184,17 @@ describe('url serializer', () => {
expect(url.serialize(tree)).toEqual(u);
});
});

describe('error handling', () => {
it('should throw when invalid characters inside children', () => {
expect(() => url.parse('/one/(left#one)'))
.toThrowError('Cannot parse url \'/one/(left#one)\'');
});

it('should throw when missing closing )', () => {
expect(() => url.parse('/one/(left')).toThrowError('Cannot parse url \'/one/(left\'');
});
});
});

function expectSegment(segment: UrlSegment, expected: string, hasChildren: boolean = false): void {
Expand Down

0 comments on commit 27b87ef

Please sign in to comment.