Many fixes
This commit is contained in:
@@ -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) }),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user