diff --git a/src/common/servers/serverManager.test.js b/src/common/servers/serverManager.test.js index 72213e95..4f0a53eb 100644 --- a/src/common/servers/serverManager.test.js +++ b/src/common/servers/serverManager.test.js @@ -13,7 +13,7 @@ jest.mock('common/config', () => ({ jest.mock('common/utils/url', () => ({ parseURL: jest.fn(), isInternalURL: jest.fn(), - getFormattedPathName: (pathname) => (pathname.length ? pathname : '/'), + getFormattedPathName: (pathname) => (pathname.endsWith('/') ? pathname : `${pathname}/`), })); jest.mock('common/utils/util', () => ({ isVersionGreaterThanOrEqualTo: jest.fn(), @@ -128,6 +128,11 @@ describe('common/servers/serverManager', () => { expect(serverManager.lookupViewByURL(inputURL)).toStrictEqual({id: 'view-2', url: new URL('http://server-2.com/subpath')}); }); + it('should not match a server where the subpaths are substrings of each other ', () => { + const inputURL = new URL('http://server-2.com/subpath2'); + expect(serverManager.lookupViewByURL(inputURL)).toBe(undefined); + }); + it('should match the correct server with a subpath - base view', () => { const inputURL = new URL('http://server-2.com/subpath/server'); expect(serverManager.lookupViewByURL(inputURL)).toStrictEqual({id: 'view-2', url: new URL('http://server-2.com/subpath')}); diff --git a/src/common/servers/serverManager.ts b/src/common/servers/serverManager.ts index 1a957322..f6d5896b 100644 --- a/src/common/servers/serverManager.ts +++ b/src/common/servers/serverManager.ts @@ -123,7 +123,7 @@ export class ServerManager extends EventEmitter { } const server = this.getAllServers().find((server) => { return isInternalURL(parsedURL, server.url, ignoreScheme) && - getFormattedPathName(parsedURL.pathname).startsWith(server.url.pathname); + getFormattedPathName(parsedURL.pathname).startsWith(getFormattedPathName(server.url.pathname)); }); if (!server) { return undefined; @@ -134,7 +134,7 @@ export class ServerManager extends EventEmitter { views. filter((view) => view && view.type !== TAB_MESSAGING). forEach((view) => { - if (getFormattedPathName(parsedURL.pathname).startsWith(view.url.pathname)) { + if (getFormattedPathName(parsedURL.pathname).startsWith(getFormattedPathName(view.url.pathname))) { selectedView = view; } });