2018-06-28 18:33:37 +02:00
|
|
|
import path from 'path';
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
2019-05-20 07:53:47 +02:00
|
|
|
import startServer from '../../../../src';
|
|
|
|
import config from '../../partials/config';
|
|
|
|
import {DEFAULT_DOMAIN, DEFAULT_PORT, DEFAULT_PROTOCOL} from '../../../../src/lib/constants';
|
|
|
|
import {getListListenAddresses} from '../../../../src/lib/cli/utils';
|
2019-06-20 15:10:26 +02:00
|
|
|
import {parseConfigFile} from '../../../../src/lib/utils';
|
2018-01-17 22:29:00 +01:00
|
|
|
|
2019-05-20 07:53:47 +02:00
|
|
|
const logger = require('../../../../src/lib/logger');
|
2018-09-22 12:54:21 +02:00
|
|
|
|
2019-05-20 07:53:47 +02:00
|
|
|
jest.mock('../../../../src/lib/logger', () => ({
|
2018-09-22 12:54:21 +02:00
|
|
|
setup: jest.fn(),
|
|
|
|
logger: {
|
|
|
|
child: jest.fn(),
|
|
|
|
warn: jest.fn(),
|
2019-08-10 13:38:06 +02:00
|
|
|
trace: jest.fn(),
|
|
|
|
debug: jest.fn(),
|
2018-09-22 12:54:21 +02:00
|
|
|
error: jest.fn(),
|
|
|
|
fatal: jest.fn()
|
|
|
|
}
|
|
|
|
}));
|
2018-01-17 22:29:00 +01:00
|
|
|
|
|
|
|
describe('startServer via API', () => {
|
|
|
|
|
2019-06-19 09:00:48 +02:00
|
|
|
const parseConfigurationFile = (name) => {
|
2019-06-20 15:10:26 +02:00
|
|
|
return parseConfigFile(path.join(__dirname, `../../partials/config/yaml/${name}.yaml`));
|
2019-06-19 09:00:48 +02:00
|
|
|
};
|
|
|
|
|
2018-01-17 22:29:00 +01:00
|
|
|
describe('startServer launcher', () => {
|
2018-09-22 12:54:21 +02:00
|
|
|
test('should provide all HTTP server data', async (done) => {
|
2018-01-17 22:29:00 +01:00
|
|
|
const store = path.join(__dirname, 'partials/store');
|
2018-06-28 18:33:37 +02:00
|
|
|
const serverName = 'verdaccio-test';
|
|
|
|
const version = '1.0.0';
|
|
|
|
const port = '6000';
|
2018-01-17 22:29:00 +01:00
|
|
|
|
2019-02-24 23:20:25 +01:00
|
|
|
await startServer(config(), port, store, version, serverName,
|
2018-01-17 22:29:00 +01:00
|
|
|
(webServer, addrs, pkgName, pkgVersion) => {
|
|
|
|
expect(webServer).toBeDefined();
|
|
|
|
expect(addrs).toBeDefined();
|
2018-09-22 12:54:21 +02:00
|
|
|
expect(addrs.proto).toBe(DEFAULT_PROTOCOL);
|
|
|
|
expect(addrs.host).toBe(DEFAULT_DOMAIN);
|
2018-06-28 18:33:37 +02:00
|
|
|
expect(addrs.port).toBe(port);
|
2018-01-17 22:29:00 +01:00
|
|
|
expect(pkgName).toBeDefined();
|
|
|
|
expect(pkgVersion).toBeDefined();
|
2018-06-28 18:33:37 +02:00
|
|
|
expect(pkgVersion).toBe(version);
|
|
|
|
expect(pkgName).toBe(serverName);
|
2018-01-17 22:29:00 +01:00
|
|
|
done();
|
2019-07-16 08:40:01 +02:00
|
|
|
});
|
2018-01-17 22:29:00 +01:00
|
|
|
});
|
|
|
|
|
2019-06-19 09:00:48 +02:00
|
|
|
test('should set keepAliveTimeout to 0 seconds', async (done) => {
|
|
|
|
const store = path.join(__dirname, 'partials/store');
|
|
|
|
const serverName = 'verdaccio-test';
|
|
|
|
const version = '1.0.0';
|
|
|
|
const port = '6100';
|
|
|
|
|
|
|
|
await startServer(config(parseConfigurationFile('server/keepalivetimeout-0')), port, store, version, serverName,
|
|
|
|
(webServer, addrs, pkgName, pkgVersion) => {
|
|
|
|
expect(webServer).toBeDefined();
|
|
|
|
expect(webServer.keepAliveTimeout).toBeDefined();
|
|
|
|
expect(webServer.keepAliveTimeout).toBe(0);
|
|
|
|
expect(addrs).toBeDefined();
|
|
|
|
expect(addrs.proto).toBe(DEFAULT_PROTOCOL);
|
|
|
|
expect(addrs.host).toBe(DEFAULT_DOMAIN);
|
|
|
|
expect(addrs.port).toBe(port);
|
|
|
|
expect(pkgName).toBeDefined();
|
|
|
|
expect(pkgVersion).toBeDefined();
|
|
|
|
expect(pkgVersion).toBe(version);
|
|
|
|
expect(pkgName).toBe(serverName);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should set keepAliveTimeout to 60 seconds', async (done) => {
|
|
|
|
const store = path.join(__dirname, 'partials/store');
|
|
|
|
const serverName = 'verdaccio-test';
|
|
|
|
const version = '1.0.0';
|
|
|
|
const port = '6200';
|
|
|
|
|
|
|
|
await startServer(config(parseConfigurationFile('server/keepalivetimeout-60')), port, store, version, serverName,
|
|
|
|
(webServer, addrs, pkgName, pkgVersion) => {
|
|
|
|
expect(webServer).toBeDefined();
|
|
|
|
expect(webServer.keepAliveTimeout).toBeDefined();
|
|
|
|
expect(webServer.keepAliveTimeout).toBe(60000);
|
|
|
|
expect(addrs).toBeDefined();
|
|
|
|
expect(addrs.proto).toBe(DEFAULT_PROTOCOL);
|
|
|
|
expect(addrs.host).toBe(DEFAULT_DOMAIN);
|
|
|
|
expect(addrs.port).toBe(port);
|
|
|
|
expect(pkgName).toBeDefined();
|
|
|
|
expect(pkgVersion).toBeDefined();
|
|
|
|
expect(pkgVersion).toBe(version);
|
|
|
|
expect(pkgName).toBe(serverName);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should set keepAliveTimeout to 5 seconds per default', async (done) => {
|
|
|
|
const store = path.join(__dirname, 'partials/store');
|
|
|
|
const serverName = 'verdaccio-test';
|
|
|
|
const version = '1.0.0';
|
|
|
|
const port = '6300';
|
|
|
|
|
|
|
|
await startServer(config(parseConfigurationFile('server/keepalivetimeout-undefined')), port, store, version, serverName,
|
|
|
|
(webServer, addrs, pkgName, pkgVersion) => {
|
|
|
|
expect(webServer).toBeDefined();
|
|
|
|
expect(webServer.keepAliveTimeout).toBeDefined();
|
|
|
|
expect(webServer.keepAliveTimeout).toBe(5000);
|
|
|
|
expect(addrs).toBeDefined();
|
|
|
|
expect(addrs.proto).toBe(DEFAULT_PROTOCOL);
|
|
|
|
expect(addrs.host).toBe(DEFAULT_DOMAIN);
|
|
|
|
expect(addrs.port).toBe(port);
|
|
|
|
expect(pkgName).toBeDefined();
|
|
|
|
expect(pkgVersion).toBeDefined();
|
|
|
|
expect(pkgVersion).toBe(version);
|
|
|
|
expect(pkgName).toBe(serverName);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-22 12:54:21 +02:00
|
|
|
test('should provide all HTTPS server fails', async (done) => {
|
|
|
|
const store = path.join(__dirname, 'partials/store');
|
|
|
|
const serverName = 'verdaccio-test';
|
|
|
|
const version = '1.0.0';
|
|
|
|
const address = 'https://www.domain.com:443';
|
|
|
|
const realProcess = process;
|
|
|
|
|
2019-02-24 23:20:25 +01:00
|
|
|
const conf = config();
|
2018-09-22 12:54:21 +02:00
|
|
|
conf.https = {};
|
|
|
|
// save process to catch exist
|
|
|
|
const exitMock = jest.fn();
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-09-22 12:54:21 +02:00
|
|
|
global.process = { ...realProcess, exit: exitMock };
|
|
|
|
await startServer(conf, address, store, version, serverName, () => {
|
2018-12-16 21:09:03 +01:00
|
|
|
expect(logger.logger.fatal).toHaveBeenCalled();
|
2018-09-22 12:54:21 +02:00
|
|
|
expect(logger.logger.fatal).toHaveBeenCalledTimes(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
expect(exitMock).toHaveBeenCalledWith(2);
|
|
|
|
// restore process
|
|
|
|
global.process = realProcess;
|
|
|
|
});
|
|
|
|
|
2018-04-21 18:36:06 +02:00
|
|
|
test('should fails if config is missing', async () => {
|
|
|
|
try {
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-04-21 18:36:06 +02:00
|
|
|
await startServer();
|
|
|
|
} catch (e) {
|
|
|
|
expect(e.message).toEqual('config file must be an object');
|
|
|
|
}
|
2018-01-17 22:29:00 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getListListenAddresses test', () => {
|
|
|
|
|
2018-09-22 12:54:21 +02:00
|
|
|
test('should return no address if a single address is wrong', () => {
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-09-22 12:54:21 +02:00
|
|
|
const addrs = getListListenAddresses("wrong");
|
|
|
|
|
|
|
|
expect(_.isArray(addrs)).toBeTruthy();
|
|
|
|
expect(addrs).toHaveLength(0);
|
2018-01-17 22:29:00 +01:00
|
|
|
});
|
|
|
|
|
2018-09-22 12:54:21 +02:00
|
|
|
test('should return no address if a two address are wrong', () => {
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-09-22 12:54:21 +02:00
|
|
|
const addrs = getListListenAddresses(["wrong", "same-wrong"]);
|
2018-01-17 22:29:00 +01:00
|
|
|
|
|
|
|
expect(_.isArray(addrs)).toBeTruthy();
|
2018-09-22 12:54:21 +02:00
|
|
|
expect(addrs).toHaveLength(0);
|
2018-01-17 22:29:00 +01:00
|
|
|
});
|
|
|
|
|
2018-09-22 12:54:21 +02:00
|
|
|
test('should return a list of 1 address provided', () => {
|
2018-01-17 22:29:00 +01:00
|
|
|
const addrs = getListListenAddresses(null, '1000');
|
|
|
|
|
|
|
|
expect(_.isArray(addrs)).toBeTruthy();
|
2018-09-22 12:54:21 +02:00
|
|
|
expect(addrs).toHaveLength(1);
|
2018-01-17 22:29:00 +01:00
|
|
|
});
|
|
|
|
|
2018-09-22 12:54:21 +02:00
|
|
|
test('should return a list of 2 address provided', () => {
|
|
|
|
const addrs = getListListenAddresses(null, ['1000', '2000']);
|
|
|
|
|
|
|
|
expect(_.isArray(addrs)).toBeTruthy();
|
|
|
|
expect(addrs).toHaveLength(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(`should return by default ${DEFAULT_PORT}`, () => {
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-09-22 12:54:21 +02:00
|
|
|
const [addrs] = getListListenAddresses();
|
|
|
|
|
|
|
|
expect(addrs.proto).toBe(DEFAULT_PROTOCOL);
|
|
|
|
expect(addrs.host).toBe(DEFAULT_DOMAIN);
|
|
|
|
expect(addrs.port).toBe(DEFAULT_PORT);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return default proto, host and custom port', () => {
|
|
|
|
const initPort = '1000';
|
|
|
|
const [addrs] = getListListenAddresses(null, initPort);
|
|
|
|
|
|
|
|
expect(addrs.proto).toEqual(DEFAULT_PROTOCOL);
|
|
|
|
expect(addrs.host).toEqual(DEFAULT_DOMAIN);
|
|
|
|
expect(addrs.port).toEqual(initPort);
|
|
|
|
});
|
2018-01-17 22:29:00 +01:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|