diff --git a/__tests__/parseServerURI.defaultPort.unit.test.ts b/__tests__/parseServerURI.defaultPort.unit.test.ts new file mode 100644 index 000000000..cdfc61839 --- /dev/null +++ b/__tests__/parseServerURI.defaultPort.unit.test.ts @@ -0,0 +1,23 @@ +import parseServerURI from '@app/uris/parseServerURI'; + +test.each([ + ['https://example.com:443/', 'https://example.com:443'], + ['https://example.com:443/path', 'https://example.com:443'], + ['https://example.com:443?x=1', 'https://example.com:443'], + ['http://localhost:80/', 'http://localhost:80'], +])( + 'Tests that the parser keeps an explicit default port when a path or query follows it. %s', + (uri, canonical) => { + expect(parseServerURI(uri)).toEqual({ + kind: 'canonicalUri', + uri: canonical, + }); + }, +); + +test('Tests that the parser keeps port 9067 when the path ends with a default port.', () => { + expect(parseServerURI('https://example.com/a:443')).toEqual({ + kind: 'canonicalUri', + uri: 'https://example.com:9067', + }); +}); diff --git a/app/uris/parseServerURI.ts b/app/uris/parseServerURI.ts index cc35ab8de..1f8958ec5 100644 --- a/app/uris/parseServerURI.ts +++ b/app/uris/parseServerURI.ts @@ -22,6 +22,9 @@ export type ServerUriErrorKey = 'uris.baduri' | 'uris.error-http-not-allowed'; export type ParseServerUriResult = { kind: 'canonicalUri'; uri: string } | ErrorKeyed; +const authority = (uri: string): string => + uri.replace(/^[^/]*\/\//, '').split(/[/?#]/)[0]; + const parseServerURI = (uri: string): ParseServerUriResult => { if (!uri) { return { kind: 'error', errorKey: 'uris.baduri' }; @@ -52,14 +55,14 @@ const parseServerURI = (uri: string): ParseServerUriResult => { // I need to verify if the URI have a standard port like `443` or `80` if ( parsedUri.protocol === GlobalConst.http && - uri.endsWith(':' + GlobalConst.port80) + authority(uri).endsWith(':' + GlobalConst.port80) ) { // looking for 80 port = GlobalConst.port80; } if ( parsedUri.protocol === GlobalConst.https && - uri.endsWith(':' + GlobalConst.port443) + authority(uri).endsWith(':' + GlobalConst.port443) ) { // looking for 443 port = GlobalConst.port443;