test: Create single function for port generation

This reduces the chances of several integration tests
re-using the same port.
This commit is contained in:
Joachim Van Herwegen 2021-04-16 11:21:50 +02:00
parent d1eadd75e7
commit fa8d406f34
8 changed files with 35 additions and 7 deletions

View File

@ -4,9 +4,10 @@ import fetch from 'cross-fetch';
import type { Initializer } from '../../src/init/Initializer';
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
import { joinFilePath } from '../../src/util/PathUtil';
import { getPort } from '../util/Util';
import { getTestFolder, instantiateFromConfig, removeFolder } from './Config';
const port = 6006;
const port = getPort('DynamicPods');
const baseUrl = `http://localhost:${port}/`;
const rootFilePath = getTestFolder('dynamicPods');
const podConfigJson = joinFilePath(rootFilePath, 'config-pod.json');

View File

@ -4,9 +4,10 @@ import type { BaseHttpServerFactory } from '../../src/server/BaseHttpServerFacto
import type { HttpHandlerInput } from '../../src/server/HttpHandler';
import { HttpHandler } from '../../src/server/HttpHandler';
import { StaticAsyncHandler } from '../util/StaticAsyncHandler';
import { getPort } from '../util/Util';
import { instantiateFromConfig } from './Config';
const port = 6002;
const port = getPort('Middleware');
class SimpleHttpHandler extends HttpHandler {
public async handle(input: HttpHandlerInput): Promise<void> {

View File

@ -2,9 +2,10 @@ import type { Server } from 'http';
import fetch from 'cross-fetch';
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
import { readableToString } from '../../src/util/StreamUtil';
import { getPort } from '../util/Util';
import { instantiateFromConfig } from './Config';
const port = 6003;
const port = getPort('PodCreation');
const baseUrl = `http://localhost:${port}/`;
describe('A server with a pod handler', (): void => {

View File

@ -4,12 +4,14 @@ import type { RedisResourceLocker } from '../../src';
import { joinFilePath } from '../../src';
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
import { describeIf } from '../util/TestHelpers';
import { getPort } from '../util/Util';
import { instantiateFromConfig } from './Config';
/**
* Test the general functionality of the server using a RedisResourceLocker
*/
describeIf('docker', 'A server with a RedisResourceLocker as ResourceLocker', (): void => {
const port = 6008;
const port = getPort('RedisResourceLocker');
const baseUrl = `http://localhost:${port}/`;
let server: Server;
let locker: RedisResourceLocker;

View File

@ -2,9 +2,10 @@ import type { Server } from 'http';
import fetch from 'cross-fetch';
import type { Initializer } from '../../src/init/Initializer';
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
import { getPort } from '../util/Util';
import { instantiateFromConfig } from './Config';
const port = 6004;
const port = getPort('ServerFetch');
const baseUrl = `http://localhost:${port}/`;
// Some tests with real Requests/Responses until the mocking library has been removed from the tests

View File

@ -3,9 +3,10 @@ import fetch from 'cross-fetch';
import type { Initializer } from '../../src/init/Initializer';
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
import type { ResourceStore } from '../../src/storage/ResourceStore';
import { getPort } from '../util/Util';
import { getTestFolder, instantiateFromConfig, removeFolder } from './Config';
const port = 6005;
const port = getPort('Subdomains');
const baseUrl = `http://localhost:${port}/`;
const rootFilePath = getTestFolder('subdomains');

View File

@ -2,9 +2,10 @@ import type { Server } from 'http';
import fetch from 'cross-fetch';
import WebSocket from 'ws';
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
import { getPort } from '../util/Util';
import { instantiateFromConfig } from './Config';
const port = 6001;
const port = getPort('WebSocketsProtocol');
const serverUrl = `http://localhost:${port}/`;
const headers = { forwarded: 'host=example.pod;proto=https' };

View File

@ -9,6 +9,26 @@ import type { HttpHandler } from '../../src/server/HttpHandler';
import type { HttpRequest } from '../../src/server/HttpRequest';
import type { SystemError } from '../../src/util/errors/SystemError';
/* eslint-disable @typescript-eslint/naming-convention */
const portNames = [
'DynamicPods',
'Middleware',
'PodCreation',
'RedisResourceLocker',
'ServerFetch',
'Subdomains',
'WebSocketsProtocol',
] as const;
export function getPort(name: typeof portNames[number]): number {
const idx = portNames.indexOf(name);
// Just in case something doesn't listen to the typings
if (idx < 0) {
throw new Error(`Unknown port name ${name}`);
}
return 6000 + idx;
}
export async function performRequest(
handler: HttpHandler,
requestUrl: URL,