150 lines
5.3 KiB
TypeScript
150 lines
5.3 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
|
|
// Source
|
|
import { ApplicationRouter } from '../../source/services/router.ts';
|
|
|
|
// Helpers
|
|
import { createMockAuth, toRoutes } from '../helpers/misc.ts';
|
|
import { TestConnection } from '../helpers/test-connection.ts';
|
|
import { createControlledRequest } from '../helpers/controlled-request.ts';
|
|
|
|
/**
|
|
* A mock of the AuthSecp256k1 service
|
|
*/
|
|
const auth = createMockAuth();
|
|
|
|
describe('ApplicationRouter initialization', (): void => {
|
|
it('rejects duplicate exact paths during startup', async (): Promise<void> => {
|
|
const route = { url: '/echo', handler: (): void => undefined };
|
|
|
|
await expect(ApplicationRouter.create({ auth }, [ toRoutes([ route ]), toRoutes([ route ]) ])).rejects.toThrow('Duplicate application route: /echo');
|
|
});
|
|
|
|
it.each([ 'echo', '/', '/items/:id', '/items?active=true', '/items#active' ])('rejects the invalid route path %s', async (url): Promise<void> => {
|
|
await expect(ApplicationRouter.create({ auth }, [ toRoutes([{ url, handler: (): void => undefined }]) ])).rejects.toThrow('Invalid application route');
|
|
});
|
|
});
|
|
|
|
describe('ApplicationRouter dispatch', (): void => {
|
|
it('binds the connection, body, and request ID to one route stream', async (): Promise<void> => {
|
|
const connection = new TestConnection(false, false);
|
|
const router = await ApplicationRouter.create({ auth }, [
|
|
toRoutes([
|
|
{
|
|
url: '/echo',
|
|
handler: async (stream): Promise<void> => {
|
|
expect(stream.connection).toBe(connection);
|
|
expect(stream.path).toBe('/echo');
|
|
expect(stream.headers).toEqual({ 'x-public-key': 'public-key', 'x-signature': 'signature', 'x-timestamp': '1000' });
|
|
await stream.send(stream.body);
|
|
},
|
|
},
|
|
]),
|
|
]);
|
|
|
|
await router.dispatch(
|
|
{
|
|
path: '/echo',
|
|
body: { value: 1 },
|
|
requestId: '1',
|
|
headers: { 'x-public-key': 'public-key', 'x-signature': 'signature', 'x-timestamp': '1000' },
|
|
},
|
|
connection,
|
|
);
|
|
|
|
expect(connection.messages).toEqual([
|
|
{
|
|
id: '1',
|
|
type: 'response',
|
|
statusCode: 200,
|
|
body: { value: 1 },
|
|
},
|
|
]);
|
|
|
|
await expect(router.dispatch(
|
|
{ path: '/echo/other', body: {}, headers: { 'x-public-key': 'public-key', 'x-signature': 'signature', 'x-timestamp': '1000' } },
|
|
connection,
|
|
)).rejects.toMatchObject({ statusCode: 404 });
|
|
});
|
|
|
|
it('preserves correlation when concurrent requests finish out of order', async (): Promise<void> => {
|
|
const router = await ApplicationRouter.create({ auth }, [
|
|
toRoutes([
|
|
{
|
|
url: '/delayed',
|
|
handler: async (stream): Promise<void> => {
|
|
const { key, signalStarted, released } = stream.body as {
|
|
key: string;
|
|
signalStarted: () => void;
|
|
released: Promise<void>;
|
|
};
|
|
|
|
signalStarted();
|
|
|
|
await released;
|
|
await stream.send({ key });
|
|
},
|
|
},
|
|
]),
|
|
]);
|
|
|
|
const connection = new TestConnection(false, false);
|
|
|
|
const first = createControlledRequest({ router, connection, path: '/delayed', requestId: 'A', body: { key: 'A' } });
|
|
const second = createControlledRequest({ router, connection, path: '/delayed', requestId: 'B', body: { key: 'B' } });
|
|
|
|
expect(connection.messages).toEqual([]);
|
|
|
|
await Promise.all([ first.started, second.started ]);
|
|
|
|
second.release();
|
|
await second.request;
|
|
|
|
expect(connection.messages).toEqual([
|
|
{
|
|
id: 'B',
|
|
type: 'response',
|
|
statusCode: 200,
|
|
body: { key: 'B' },
|
|
},
|
|
]);
|
|
|
|
first.release();
|
|
await first.request;
|
|
|
|
expect(connection.messages).toEqual([
|
|
{
|
|
id: 'B',
|
|
type: 'response',
|
|
statusCode: 200,
|
|
body: { key: 'B' },
|
|
},
|
|
{
|
|
id: 'A',
|
|
type: 'response',
|
|
statusCode: 200,
|
|
body: { key: 'A' },
|
|
},
|
|
]);
|
|
}, 1000);
|
|
|
|
it('propagates route failures without infrastructure-specific cleanup', async (): Promise<void> => {
|
|
const error = new Error('route failed');
|
|
const router = await ApplicationRouter.create({ auth }, [
|
|
toRoutes([
|
|
{
|
|
url: '/failure',
|
|
handler: (): void => {
|
|
throw error;
|
|
},
|
|
},
|
|
]),
|
|
]);
|
|
|
|
await expect(router.dispatch(
|
|
{ path: '/failure', headers: { 'x-public-key': 'public-key', 'x-signature': 'signature', 'x-timestamp': '1000' } },
|
|
new TestConnection(false, false),
|
|
)).rejects.toBe(error);
|
|
});
|
|
});
|