2023-08-04 16:10:47 +02:00
|
|
|
import { expect, tap } from '@push.rocks/tapbundle';
|
|
|
|
|
import * as typedserver from '@api.global/typedserver';
|
2019-08-31 23:14:46 +02:00
|
|
|
|
2022-03-24 20:02:58 +01:00
|
|
|
import * as typedrequest from '../ts/index.js';
|
2024-02-25 01:54:01 +01:00
|
|
|
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
|
2019-08-31 23:14:46 +02:00
|
|
|
|
2026-03-03 20:22:01 +00:00
|
|
|
let testServer: typedserver.TypedServer;
|
2024-02-21 18:29:35 +01:00
|
|
|
let testTypedRouter: typedrequest.TypedRouter;
|
2019-09-01 13:17:21 +02:00
|
|
|
let testTypedHandler: typedrequest.TypedHandler<ITestReqRes>;
|
|
|
|
|
|
2020-09-14 08:22:27 +00:00
|
|
|
// lets define an interface
|
2019-09-01 13:17:21 +02:00
|
|
|
interface ITestReqRes {
|
|
|
|
|
method: 'hi';
|
|
|
|
|
request: {
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
|
|
|
|
response: {
|
|
|
|
|
surname: string;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 18:29:35 +01:00
|
|
|
interface ITestStream {
|
|
|
|
|
method: 'handleStream';
|
|
|
|
|
request: {
|
2024-02-25 01:54:01 +01:00
|
|
|
requestStream: typedrequestInterfaces.IVirtualStream;
|
2024-02-21 18:29:35 +01:00
|
|
|
};
|
|
|
|
|
response: {
|
2024-02-25 01:54:01 +01:00
|
|
|
responseStream: typedrequestInterfaces.IVirtualStream;
|
2024-02-21 18:29:35 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-01 13:17:21 +02:00
|
|
|
tap.test('should create a typedHandler', async () => {
|
2020-09-14 08:22:27 +00:00
|
|
|
// lets use the interface in a TypedHandler
|
2020-10-06 15:05:29 +00:00
|
|
|
testTypedHandler = new typedrequest.TypedHandler<ITestReqRes>('hi', async (reqArg) => {
|
2019-09-01 13:17:21 +02:00
|
|
|
return {
|
2020-10-06 15:05:29 +00:00
|
|
|
surname: 'wow',
|
2019-09-01 13:17:21 +02:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
2019-08-31 23:14:46 +02:00
|
|
|
|
|
|
|
|
tap.test('should spawn a server to test with', async () => {
|
2026-03-03 20:22:01 +00:00
|
|
|
testServer = new typedserver.TypedServer({
|
2019-08-31 23:14:46 +02:00
|
|
|
cors: true,
|
|
|
|
|
forceSsl: false,
|
2020-10-06 15:05:29 +00:00
|
|
|
port: 3000,
|
2019-08-31 23:14:46 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('should define a testHandler', async () => {
|
2026-03-03 20:22:01 +00:00
|
|
|
testTypedRouter = testServer.typedrouter;
|
2020-07-26 13:48:57 +00:00
|
|
|
testTypedRouter.addTypedHandler(testTypedHandler);
|
2019-08-31 23:14:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('should start the server', async () => {
|
|
|
|
|
await testServer.start();
|
|
|
|
|
});
|
2019-08-21 14:36:16 +02:00
|
|
|
|
2019-09-01 13:17:21 +02:00
|
|
|
tap.test('should fire a request', async () => {
|
2020-02-11 18:55:07 +00:00
|
|
|
const typedRequest = new typedrequest.TypedRequest<ITestReqRes>(
|
2026-03-03 20:22:01 +00:00
|
|
|
'http://localhost:3000/typedrequest',
|
2020-02-11 18:55:07 +00:00
|
|
|
'hi'
|
|
|
|
|
);
|
2019-09-01 13:17:21 +02:00
|
|
|
const response = await typedRequest.fire({
|
2020-10-06 15:05:29 +00:00
|
|
|
name: 'really',
|
2019-09-01 13:17:21 +02:00
|
|
|
});
|
2020-06-25 23:53:05 +00:00
|
|
|
console.log('this is the response:');
|
2019-09-01 15:39:56 +02:00
|
|
|
console.log(response);
|
2022-03-24 20:02:58 +01:00
|
|
|
expect(response.surname).toEqual('wow');
|
2019-09-01 13:17:21 +02:00
|
|
|
});
|
|
|
|
|
|
2024-02-21 18:29:35 +01:00
|
|
|
tap.test('should allow VirtualStreams', async () => {
|
2024-02-25 01:54:01 +01:00
|
|
|
const newRequestingVS = new typedrequest.VirtualStream();
|
|
|
|
|
const newRespondingVS = new typedrequest.VirtualStream();
|
|
|
|
|
let generatedRequestingVS: typedrequestInterfaces.IVirtualStream;
|
|
|
|
|
let generatedRespondingVS: typedrequestInterfaces.IVirtualStream;
|
2024-02-21 18:29:35 +01:00
|
|
|
testTypedRouter.addTypedHandler(new typedrequest.TypedHandler<ITestStream>('handleStream', async (reqArg) => {
|
|
|
|
|
console.log('hey there');
|
|
|
|
|
console.log(reqArg.requestStream);
|
2024-02-25 01:54:01 +01:00
|
|
|
generatedRequestingVS = reqArg.requestStream;
|
2024-02-21 18:29:35 +01:00
|
|
|
return {
|
2024-02-25 01:54:01 +01:00
|
|
|
responseStream: newRespondingVS,
|
2024-02-21 18:29:35 +01:00
|
|
|
};
|
|
|
|
|
}));
|
|
|
|
|
const typedRequest = new typedrequest.TypedRequest<ITestStream>(
|
2026-03-03 20:22:01 +00:00
|
|
|
'http://localhost:3000/typedrequest',
|
2024-02-21 18:29:35 +01:00
|
|
|
'handleStream'
|
|
|
|
|
);
|
|
|
|
|
const response = await typedRequest.fire({
|
2024-02-25 01:54:01 +01:00
|
|
|
requestStream: newRequestingVS,
|
2024-02-21 18:29:35 +01:00
|
|
|
});
|
|
|
|
|
console.log(response.responseStream);
|
2024-02-25 01:54:01 +01:00
|
|
|
|
|
|
|
|
newRequestingVS.sendData(Buffer.from('hello'));
|
2026-03-03 20:22:01 +00:00
|
|
|
const data: any = await generatedRequestingVS.fetchData();
|
|
|
|
|
// Data may arrive as Uint8Array or as JSON-serialized Buffer {type: "Buffer", data: [...]}
|
|
|
|
|
const resolvedData = data instanceof Uint8Array || Buffer.isBuffer(data)
|
|
|
|
|
? data
|
|
|
|
|
: data?.type === 'Buffer' && Array.isArray(data.data)
|
|
|
|
|
? new Uint8Array(data.data)
|
|
|
|
|
: data;
|
|
|
|
|
const decodedData = new TextDecoder().decode(resolvedData);
|
2024-03-03 10:42:14 +01:00
|
|
|
expect(decodedData).toEqual('hello');
|
2024-10-14 01:42:44 +02:00
|
|
|
await newRequestingVS.close();
|
|
|
|
|
await newRespondingVS.close();
|
2024-02-29 19:50:25 +01:00
|
|
|
});
|
2024-02-21 18:29:35 +01:00
|
|
|
|
2024-02-24 12:20:13 +01:00
|
|
|
tap.test('should end the server', async (toolsArg) => {
|
2024-10-11 02:09:50 +02:00
|
|
|
await toolsArg.delayFor(10000);
|
2019-08-31 23:14:46 +02:00
|
|
|
await testServer.stop();
|
2024-10-11 02:09:50 +02:00
|
|
|
await tap.stopForcefully();
|
2019-08-21 14:37:02 +02:00
|
|
|
});
|
2019-08-21 14:36:16 +02:00
|
|
|
|
2024-05-25 02:15:08 +02:00
|
|
|
export default tap.start();
|