diff --git a/src/path.js b/src/path.js index ba13988..bd114d3 100644 --- a/src/path.js +++ b/src/path.js @@ -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: '((\/?)|\/*)'}, diff --git a/test/path.spec.js b/test/path.spec.js index 0f4144e..6cace39 100644 --- a/test/path.spec.js +++ b/test/path.spec.js @@ -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); diff --git a/test/real-life-examples.spec.js b/test/real-life-examples.spec.js index 6cc2704..1fed9e1 100644 --- a/test/real-life-examples.spec.js +++ b/test/real-life-examples.spec.js @@ -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); + }); + });