Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions __tests__/parseServerURI.defaultPort.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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',
});
});
7 changes: 5 additions & 2 deletions app/uris/parseServerURI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export type ServerUriErrorKey = 'uris.baduri' | 'uris.error-http-not-allowed';
export type ParseServerUriResult =
{ kind: 'canonicalUri'; uri: string } | ErrorKeyed<ServerUriErrorKey>;

const authority = (uri: string): string =>
uri.replace(/^[^/]*\/\//, '').split(/[/?#]/)[0];

const parseServerURI = (uri: string): ParseServerUriResult => {
if (!uri) {
return { kind: 'error', errorKey: 'uris.baduri' };
Expand Down Expand Up @@ -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;
Expand Down
Loading