Many fixes

This commit is contained in:
2026-09-14 08:00:08 +00:00
parent 93b012592b
commit 2d970d3123
15 changed files with 338 additions and 176 deletions
+33 -13
View File
@@ -10,6 +10,22 @@ import type { AppEnv } from '../../../source/services/transport/transport-router
import { fromExtendedJson, toExtendedJson } from '@xo-cash/utils';
import { Logger } from '../../../source/utils/logger.ts';
import { ServerHost } from '../../../source/services/server-host.ts';
import { AuthSecp256k1 } from '../../../source/auth/auth.ts';
/**
* A mock of the AuthSecp256k1 service
*/
const auth = {
verifySignature: vi.fn().mockResolvedValue(true),
verifyUniqueRequest: vi.fn().mockResolvedValue(true),
assertTimestampFreshness: vi.fn().mockResolvedValue(true),
} as unknown as AuthSecp256k1;
const mockAuthHeaders = {
'x-public-key': 'public-key',
'x-signature': 'signature',
'x-timestamp': '1000',
};
const createApp = async (
routes: RouteDefinition[] | ((broadcaster: Broadcaster) => RouteDefinition[]),
@@ -18,7 +34,9 @@ const createApp = async (
const debug = new Logger('http-transport-test');
const broadcaster = new Broadcaster(debug);
const resolvedRoutes = typeof routes === 'function' ? routes(broadcaster) : routes;
const router = await ApplicationRouter.create([
const router = await ApplicationRouter.create({
auth,
}, [
{
async getRoutes(): Promise<RouteDefinition[]> {
return resolvedRoutes;
@@ -40,18 +58,20 @@ describe('HttpTransportRouter', (): void => {
it('runs normal HTTP through a non-streaming route stream', async (): Promise<void> => {
const app = await createApp([
{
url: '/echo',
url: '/echowtf',
handler: async (stream): Promise<void> => stream.send(stream.body),
},
]);
const value = new Uint8Array([ 1, 2, 3 ]);
const response = await app.request('/echo', {
const request = new Request('http://localhost/echowtf', {
method: 'POST',
headers: { 'content-type': 'application/json' },
headers: { 'content-type': 'application/json', ...mockAuthHeaders },
body: toExtendedJson({ value }),
});
const response = await app.request(request);
expect(response.status).toBe(200);
expect(fromExtendedJson(await response.text())).toEqual({ value });
});
@@ -64,7 +84,7 @@ describe('HttpTransportRouter', (): void => {
},
]);
const response = await app.request('/nothing', { method: 'POST' });
const response = await app.request('/nothing', { method: 'POST', headers: { ...mockAuthHeaders } });
expect(response.status).toBe(204);
expect(await response.text()).toBe('');
@@ -73,7 +93,7 @@ describe('HttpTransportRouter', (): void => {
it('returns normalized errors for non-streaming requests', async (): Promise<void> => {
const app = await createApp([]);
const missing = await app.request('/missing', { method: 'POST' });
const missing = await app.request('/missing', { method: 'POST', headers: { ...mockAuthHeaders } });
expect(missing.status).toBe(404);
expect(await missing.json()).toEqual({
statusCode: 404,
@@ -82,7 +102,7 @@ describe('HttpTransportRouter', (): void => {
const invalid = await app.request('/missing', {
method: 'POST',
headers: { 'content-type': 'application/json' },
headers: { 'content-type': 'application/json', ...mockAuthHeaders },
body: '{',
});
expect(invalid.status).toBe(400);
@@ -102,7 +122,7 @@ describe('HttpTransportRouter', (): void => {
},
]);
const response = await app.request('/items/subscribe', { method: 'POST' });
const response = await app.request('/items/subscribe', { method: 'POST', headers: { ...mockAuthHeaders } });
expect(response.status).toBe(406);
expect(await response.json()).toMatchObject({ statusCode: 406 });
@@ -120,7 +140,7 @@ describe('HttpTransportRouter', (): void => {
const response = await app.request('/items/subscribe', {
method: 'POST',
headers: { accept: 'text/event-stream' },
headers: { accept: 'text/event-stream', ...mockAuthHeaders },
});
const events = await response.text();
@@ -140,7 +160,7 @@ describe('HttpTransportRouter', (): void => {
const response = await app.request('/echo', {
method: 'POST',
headers: { accept: 'text/event-stream' },
headers: { accept: 'text/event-stream', ...mockAuthHeaders },
});
const events = await response.text();
@@ -171,7 +191,7 @@ describe('HttpTransportRouter', (): void => {
const response = await app.request('/items/subscribe', {
method: 'POST',
headers: { accept: 'text/event-stream' },
headers: { accept: 'text/event-stream', ...mockAuthHeaders },
});
const body = response.text();
const completed = vi.fn();
@@ -203,7 +223,7 @@ describe('HttpTransportRouter', (): void => {
const response = await app.request('/items/unsubscribe', {
method: 'POST',
headers: { accept: 'text/event-stream' },
headers: { accept: 'text/event-stream', ...mockAuthHeaders },
});
const events = await response.text();
@@ -224,7 +244,7 @@ describe('HttpTransportRouter', (): void => {
const response = await app.request('/echo', {
method: 'POST',
headers: { 'content-type': 'application/json' },
headers: { 'content-type': 'application/json', ...mockAuthHeaders },
body: JSON.stringify({ value: 'x'.repeat(64) }),
});
+25 -5
View File
@@ -1,11 +1,21 @@
import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { z } from 'zod';
import type { WebSocketServer } from 'ws';
import { ApplicationRouter } from '../../../source/services/router.ts';
import { WsTransportRouter } from '../../../source/services/transport/ws-transport.ts';
import { Logger } from '../../../source/utils/logger.ts';
import { toExtendedJson } from '@xo-cash/utils';
import { AuthSecp256k1 } from '../../../source/auth/auth.ts';
import { toRoutes } from '../../helpers/misc.ts';
/**
* A mock of the AuthSecp256k1 service
*/
const auth = {
verifySignature: vi.fn().mockResolvedValue(true),
verifyUniqueRequest: vi.fn().mockResolvedValue(true),
assertTimestampFreshness: vi.fn().mockResolvedValue(true),
} as unknown as AuthSecp256k1;
describe('WebSocket request decoding', (): void => {
it('decodes the minimal route-agnostic envelope and Extended JSON body', async (): Promise<void> => {
@@ -38,11 +48,21 @@ describe('WebSocket request decoding', (): void => {
describe('WsTransportRouter payload limits', (): void => {
it("configures Hono's ws server with the requested maxPayload", async () => {
const debug = new Logger('ws-transport-test');
const router = await ApplicationRouter.create([]);
const router = await ApplicationRouter.create({
auth,
}, [
toRoutes([
{
url: '/data/write',
handler: async (stream): Promise<void> => {
await stream.send({});
},
},
]),
]);
const transport = new WsTransportRouter(router, debug, 1024);
const wsServer = transport.websocketServer as unknown as WebSocketServer;
expect(wsServer.options.maxPayload).toBe(1024);
expect(transport['wsServer'].options.maxPayload).toBe(1024);
await transport.stop();
});
});