Skip to content

Commit

Permalink
Add http-server tests and clean up some deep source issues (#1201)
Browse files Browse the repository at this point in the history
* Refactor common to esm module
  • Loading branch information
thekevinscott authored Oct 10, 2023
1 parent 954656f commit 0c5b486
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 53 deletions.
59 changes: 36 additions & 23 deletions internals/common/src/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chalk from 'chalk';
import { vi } from 'vitest';
import { rimraf } from 'rimraf';
import { isLogLevel, log, parseMessage, setLogLevel, logTypes } from './logger.js';
// import * as mockProcess from 'vitest-mock-process';

vi.mock('rimraf', async () => {
const actual = await vi.importActual("rimraf") as typeof rimraf;
Expand Down Expand Up @@ -56,31 +57,43 @@ describe('logger', () => {
});
});

// describe('log', () => {
// let mockStdout: ReturnType<typeof mockProcess.mockProcessStdout>;
// let mockStderr: ReturnType<typeof mockProcess.mockProcessStderr>;
describe('log', () => {
const origLog = console.log;
const origError = console.error;
// let mockStdout: ReturnType<typeof mockProcess.mockProcessStdout>;
// let mockStderr: ReturnType<typeof mockProcess.mockProcessStderr>;

// beforeEach(() => {
// mockStdout = mockProcess.mockProcessStdout();
// mockStderr = mockProcess.mockProcessStderr();
// });
afterEach(() => {
vi.clearAllMocks();
console.log = origLog;
console.error = origError;
// mockStdout = mockProcess.mockProcessStdout();
// mockStderr = mockProcess.mockProcessStderr();
});

// it('logs if level is greater than valid', () => {
// setLogLevel('info');
// log('warn', ['foo']);
// expect(mockStderr).toHaveBeenCalledWith(logTypes.warn('foo\n'));
// });
it('logs if level is greater than valid', () => {
const mockStderr = vi.fn();
console.error = mockStderr;
setLogLevel('info');
log('warn', ['foo']);
expect(mockStderr).toHaveBeenCalledTimes(1);
expect(mockStderr).toHaveBeenCalledWith(logTypes.warn('foo'));
});

// it('logs if level is equal to valid', () => {
// setLogLevel('info');
// log('info', ['foo']);
// expect(mockStdout).toHaveBeenCalledWith(logTypes.info('foo\n'));
// });
it('logs if level is equal to valid', () => {
const mockStdout = vi.fn();
console.log = mockStdout;
setLogLevel('info');
log('info', ['foo']);
expect(mockStdout).toHaveBeenCalledWith(logTypes.info('foo'));
});

// it('ignores log if below current log level', () => {
// setLogLevel('info');
// log('verbose', ['foo']);
// expect(mockStdout).toHaveBeenCalledTimes(0);
// });
// });
it('ignores log if below current log level', () => {
const mockStdout = vi.fn();
console.log = mockStdout;
setLogLevel('info');
log('verbose', ['foo']);
expect(mockStdout).toHaveBeenCalledTimes(0);
});
});
});
10 changes: 8 additions & 2 deletions internals/http-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@
]
},
"test:run": {
"command": "vitest run --config ./vite.config.ts"
"command": "vitest run --config ./vite.config.ts",
"dependencies": [
"../common:build"
]
},
"test": {
"command": "vitest --config ./vite.config.ts"
"command": "vitest --config ./vite.config.ts",
"dependencies": [
"../common:build"
]
}
},
"scripts": {
Expand Down
29 changes: 29 additions & 0 deletions internals/http-server/src/HttpServer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { vi, } from 'vitest';
import { Server as HTTPServer } from 'http';
import { ERROR_STRING_ADDRESS, ERROR_NO_ADDRESS, getServerPort } from "./HttpServer.js";

describe('getServerPort', () => {
it('throws if no address is returned', () => {
const address = vi.fn().mockImplementation(() => undefined);
expect(() => getServerPort({
address,
} as unknown as HTTPServer)).toThrowError(ERROR_NO_ADDRESS);
});

it('throws if string address is returned', () => {
const address = vi.fn().mockImplementation(() => 'foo');
expect(() => getServerPort({
address,
} as unknown as HTTPServer)).toThrowError(ERROR_STRING_ADDRESS);
});

it('returns valid port', () => {
const port = 123;
const address = vi.fn().mockImplementation(() => ({
port,
}));
expect(getServerPort({
address,
} as unknown as HTTPServer)).toEqual(port);
});
});
32 changes: 5 additions & 27 deletions internals/http-server/src/HttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,18 @@ import handler from 'serve-handler';
import { exists } from '@internals/common/fs';
import { verbose } from '@internals/common/logger';
import { Tunnel } from './Tunnel.js';
import { serverHeaders } from './serverHeaders.js';

const serverHeaders = [
{
"source": "**/*",
"headers": [
{
"key": "Bypass-Tunnel-Reminder",
"value": "true",
},
{
"key": "Access-Control-Allow-Origin",
"value": "*",
},
{
"key": "Access-Control-Allow-Headers",
"value": "Origin, X-Requested-With, Content-Type, Accept, Range",
}
]
}
];
export const ERROR_NO_ADDRESS = 'No address found for server';
export const ERROR_STRING_ADDRESS = 'Address is of type string for server';

export const getServerPort = (server: HTTPServer): number => {
const address = server.address();
if (!address) {
throw new Error('No address found for server');
throw new Error(ERROR_NO_ADDRESS);
}
if (typeof address === 'string') {
throw new Error('Address is of type string for server');
throw new Error(ERROR_STRING_ADDRESS);
}
return address.port;
};
Expand Down Expand Up @@ -121,9 +105,3 @@ export class HttpServer {
]);
}
}

// type StartServer = (port: number, dist?: string) => Promise<{server: Server; url: string; }>;
// export const startServer: StartServer = (port, dist) => {
// const server = new Server(port, dist);
// return server.url;
// }
19 changes: 19 additions & 0 deletions internals/http-server/src/serverHeaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const serverHeaders = [
{
"source": "**/*",
"headers": [
{
"key": "Bypass-Tunnel-Reminder",
"value": "true",
},
{
"key": "Access-Control-Allow-Origin",
"value": "*",
},
{
"key": "Access-Control-Allow-Headers",
"value": "Origin, X-Requested-With, Content-Type, Accept, Range",
}
]
}
];
3 changes: 2 additions & 1 deletion internals/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"wireit": {
"test": {
"dependencies": [
"./common:test:run"
"./common:test:run",
"./http-server:test:run"
]
}
},
Expand Down

0 comments on commit 0c5b486

Please sign in to comment.