2018-06-28 11:33:37 -05:00
|
|
|
import _ from 'lodash';
|
2021-10-29 10:33:05 -05:00
|
|
|
|
2020-11-15 05:14:09 -05:00
|
|
|
import { DEFAULT_DOMAIN, DEFAULT_PORT, parseAddress } from '../src/cli-utils';
|
2020-08-11 00:21:51 -05:00
|
|
|
|
2017-11-01 11:47:20 -05:00
|
|
|
describe('Parse listen address', () => {
|
2019-08-16 14:20:18 -05:00
|
|
|
const useCases: any[] = [];
|
2018-06-28 15:17:38 -05:00
|
|
|
|
2019-08-16 14:20:18 -05:00
|
|
|
function addTest(uri: string, proto: string | null, host?: string, port?: string) {
|
2019-05-20 01:29:17 -05:00
|
|
|
useCases.push([uri, proto, host, port]);
|
2015-03-28 10:03:36 -05:00
|
|
|
}
|
|
|
|
|
2018-06-28 15:17:38 -05:00
|
|
|
addTest(DEFAULT_PORT, 'http', DEFAULT_DOMAIN, DEFAULT_PORT);
|
|
|
|
addTest(':4873', 'http', DEFAULT_DOMAIN, DEFAULT_PORT);
|
|
|
|
addTest('blah:4873', 'http', 'blah', DEFAULT_PORT);
|
|
|
|
addTest('http://:4873', 'http', DEFAULT_DOMAIN, DEFAULT_PORT);
|
|
|
|
addTest('https::4873', 'https', DEFAULT_DOMAIN, DEFAULT_PORT);
|
|
|
|
addTest('https:blah:4873', 'https', 'blah', DEFAULT_PORT);
|
|
|
|
addTest('https://blah:4873/', 'https', 'blah', DEFAULT_PORT);
|
|
|
|
addTest('[::1]:4873', 'http', '::1', DEFAULT_PORT);
|
|
|
|
addTest('https:[::1]:4873', 'https', '::1', DEFAULT_PORT);
|
2015-03-28 10:03:36 -05:00
|
|
|
|
2017-04-19 14:15:28 -05:00
|
|
|
addTest('unix:/tmp/foo.sock', 'http', '/tmp/foo.sock');
|
|
|
|
addTest('http:unix:foo.sock', 'http', 'foo.sock');
|
|
|
|
addTest('https://unix:foo.sock', 'https', 'foo.sock');
|
2018-06-28 15:17:38 -05:00
|
|
|
addTest('https://unix:foo.sock:34', 'https', 'foo.sock:34');
|
|
|
|
addTest('http://foo.sock:34', 'http', 'foo.sock', '34');
|
2015-04-21 18:36:57 -05:00
|
|
|
|
2017-04-19 14:15:28 -05:00
|
|
|
addTest('blah', null);
|
|
|
|
addTest('blah://4873', null);
|
|
|
|
addTest('https://blah:4873///', null);
|
|
|
|
addTest('unix:1234', 'http', 'unix', '1234'); // not unix socket
|
2019-05-20 01:29:17 -05:00
|
|
|
|
|
|
|
test.each(useCases)(`should parse (%s - %s - %s - %s)`, (uri, proto, host, port) => {
|
2020-11-15 05:14:09 -05:00
|
|
|
const parsed = parseAddress(uri);
|
2019-05-20 01:29:17 -05:00
|
|
|
|
|
|
|
if (_.isNull(proto)) {
|
|
|
|
expect(parsed).toBeNull();
|
|
|
|
} else if (port) {
|
|
|
|
expect(parsed).toEqual({
|
|
|
|
proto,
|
|
|
|
host,
|
2020-03-03 17:59:19 -05:00
|
|
|
port,
|
2019-05-20 01:29:17 -05:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
expect(parsed).toEqual({
|
|
|
|
proto,
|
2020-03-03 17:59:19 -05:00
|
|
|
path: host,
|
2019-05-20 01:29:17 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-04-19 14:15:28 -05:00
|
|
|
});
|