diff --git a/src/identity/interaction/routing/IdInteractionRoute.ts b/src/identity/interaction/routing/IdInteractionRoute.ts index 595c962579..1d4b73807e 100644 --- a/src/identity/interaction/routing/IdInteractionRoute.ts +++ b/src/identity/interaction/routing/IdInteractionRoute.ts @@ -9,11 +9,13 @@ export class IdInteractionRoute implem private readonly base: InteractionRoute; private readonly idName: TId; private readonly ensureSlash: boolean; + private readonly matchRegex: RegExp; public constructor(base: InteractionRoute, idName: TId, ensureSlash = true) { this.base = base; this.idName = idName; this.ensureSlash = ensureSlash; + this.matchRegex = ensureSlash ? /(.*\/)([^/]+)\/$/u : /(.*\/)([^/]+)$/u; } public getPath(parameters?: Record): string { @@ -27,7 +29,7 @@ export class IdInteractionRoute implem } public matchPath(path: string): Record | undefined { - const match = /(.*\/)([^/]+)\/$/u.exec(path); + const match = this.matchRegex.exec(path); if (!match) { return; diff --git a/test/unit/identity/interaction/routing/IdInteractionRoute.test.ts b/test/unit/identity/interaction/routing/IdInteractionRoute.test.ts index 6c35b1d37d..63a20ad420 100644 --- a/test/unit/identity/interaction/routing/IdInteractionRoute.test.ts +++ b/test/unit/identity/interaction/routing/IdInteractionRoute.test.ts @@ -36,6 +36,11 @@ describe('An IdInteractionRoute', (): void => { expect(route.matchPath('http://example.com/1234/')).toEqual({ base: 'base', id: '1234' }); }); + it('can match paths without trailing slash if ensureSlash is false.', async(): Promise => { + route = new IdInteractionRoute<'base', 'id'>(base, idName, false); + expect(route.matchPath('http://example.com/1234')).toEqual({ base: 'base', id: '1234' }); + }); + it('returns undefined if there is no match.', async(): Promise => { expect(route.matchPath('http://example.com/1234')).toBeUndefined(); });