0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

refactor: unit test parse listeners

This commit is contained in:
Juan Picado @jotadeveloper 2018-06-28 22:17:38 +02:00
parent 45110ddb7b
commit 83b64bd798
No known key found for this signature in database
GPG key ID: 18AC54485952D158
5 changed files with 35 additions and 25 deletions

View file

@ -13,6 +13,7 @@ import {parse_address} from './utils';
import type {Callback} from '@verdaccio/types';
import type {$Application} from 'express';
import {DEFAULT_PORT} from './constants';
const logger = require('./logger');
@ -37,7 +38,7 @@ export function getListListenAddresses(argListen: string, configListen: mixed) {
} else if (configListen) {
addresses = [configListen];
} else {
addresses = ['4873'];
addresses = [DEFAULT_PORT];
}
addresses = addresses.map(function(addr) {
const parsedAddr = parse_address(addr);

View file

@ -1,5 +1,8 @@
// @flow
export const DEFAULT_PORT = '4873';
export const DEFAULT_DOMAIN = 'localhost';
export const HEADERS = {
JSON: 'application/json',
JSON_CHARSET: 'application/json; charset=utf-8',

View file

@ -1,16 +1,16 @@
// @flow
import {generateGravatarUrl} from '../utils/user';
import _ from 'lodash';
import fs from 'fs';
import assert from 'assert';
import semver from 'semver';
import YAML from 'js-yaml';
import URL from 'url';
import fs from 'fs';
import _ from 'lodash';
import asciidoctor from 'asciidoctor.js';
import createError from 'http-errors';
import marked from 'marked';
import {HTTP_STATUS, API_ERROR} from './constants';
import {HTTP_STATUS, API_ERROR, DEFAULT_PORT, DEFAULT_DOMAIN} from './constants';
import {generateGravatarUrl} from '../utils/user';
import type {Package} from '@verdaccio/types';
import type {$Request} from 'express';
@ -219,8 +219,8 @@ function parse_address(urlAddress: any) {
if (urlPattern) {
return {
proto: urlPattern[2] || 'http',
host: urlPattern[6] || urlPattern[7] || 'localhost',
port: urlPattern[8] || '4873',
host: urlPattern[6] || urlPattern[7] || DEFAULT_DOMAIN,
port: urlPattern[8] || DEFAULT_PORT,
};
}

View file

@ -4,6 +4,7 @@ import _ from 'lodash';
import startServer from '../../../src/index';
import {getListListenAddresses} from '../../../src/lib/bootstrap';
import config from '../partials/config/index';
import {DEFAULT_DOMAIN, DEFAULT_PORT} from '../../../src/lib/constants';
require('../../../src/lib/logger').setup([]);
@ -42,12 +43,12 @@ describe('startServer via API', () => {
});
describe('getListListenAddresses test', () => {
test('should return by default 4873', () => {
test(`should return by default ${DEFAULT_PORT}`, () => {
const addrs = getListListenAddresses()[0];
expect(addrs.proto).toBe('http');
expect(addrs.host).toBe('localhost');
expect(addrs.port).toBe('4873');
expect(addrs.host).toBe(DEFAULT_DOMAIN);
expect(addrs.port).toBe(DEFAULT_PORT);
});
test('should return a list of address and no cli argument provided', () => {

View file

@ -1,39 +1,44 @@
import _ from 'lodash';
import {parse_address as parse} from '../../../src/lib/utils';
import {DEFAULT_DOMAIN, DEFAULT_PORT} from '../../../src/lib/constants';
describe('Parse listen address', () => {
function addTest(uri, proto, host, port) {
test(uri, () => {
test(`should parse ${uri}`, () => {
const parsed = parse(uri);
if (_.isNull(proto)) {
expect(parse(uri)).toBeNull();
expect(parsed).toBeNull();
} else if (port) {
expect(parse(uri)).toEqual({
expect(parsed).toEqual({
proto,
host,
port,
});
} else {
expect(parse(uri)).toEqual({
expect(parsed).toEqual({
proto,
host,
path: host,
});
}
});
}
addTest('4873', 'http', 'localhost', '4873');
addTest(':4873', 'http', 'localhost', '4873');
addTest('blah:4873', 'http', 'blah', '4873');
addTest('http://:4873', 'http', 'localhost', '4873');
addTest('https::4873', 'https', 'localhost', '4873');
addTest('https:blah:4873', 'https', 'blah', '4873');
addTest('https://blah:4873/', 'https', 'blah', '4873');
addTest('[::1]:4873', 'http', '::1', '4873');
addTest('https:[::1]:4873', 'https', '::1', '4873');
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);
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');
addTest('https://unix:foo.sock:34', 'https', 'foo.sock:34');
addTest('http://foo.sock:34', 'http', 'foo.sock', '34');
addTest('blah', null);
addTest('blah://4873', null);