Skip to content

Commit

Permalink
fix: assume path always ends with optional trailing slash if missing
Browse files Browse the repository at this point in the history
refs InlineManual/player#690
  • Loading branch information
fczbkk committed Jul 17, 2016
1 parent d837e69 commit 0662e33
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export default class extends UrlPart {

get sanitize_replacements () {
return [
// add trailing slash if path is empty
{substring: /^$/, replacement: '\/'},
// add trailing slash if path does not end with asterisk or trailing slash
{substring: /[^/*]$/, replacement: '$&\/'},
// assume trailing slash at the end of path is optional
{substring: /\/$/, replacement: '\\/?'},
{substring: /\/\*$/, replacement: '((\/?)|\/*)'},
Expand Down
6 changes: 6 additions & 0 deletions test/path.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ describe('Path', function() {
expect(path.test('bbb.ccc', path.sanitize('*/*.ccc'))).toBe(false);
});

it('should add trailing slash if missing', function () {
expect(path.test('/', path.sanitize(''))).toBe(true);
expect(path.test('aaa/', path.sanitize('aaa'))).toBe(true);
expect(path.test('aaa/bbb/', path.sanitize('aaa/bbb'))).toBe(true);
});

it('should assume trailing `/` is optional', function() {
expect(path.test('', path.sanitize('/'))).toBe(true);
expect(path.test('aaa', path.sanitize('aaa/'))).toBe(true);
Expand Down
7 changes: 7 additions & 0 deletions test/real-life-examples.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,11 @@ describe('Real life examples', function() {
expect(my_match.test('http://localhost:3000/aaa/bbb')).toBe(true);
});

it('should handle missing trailing slash', function () {
const my_match = new UrlMatch('*://*/ccc');
expect(my_match.test('http://aaa.bbb/ccc')).toBe(true);
expect(my_match.test('http://aaa.bbb/ccc/')).toBe(true);
expect(my_match.test('http://aaa.bbb/ccc/ddd')).toBe(false);
});

});

0 comments on commit 0662e33

Please sign in to comment.