typedrequest/test/test.ts

111 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-08-04 14:10:47 +00:00
import { expect, tap } from '@push.rocks/tapbundle';
import * as typedserver from '@api.global/typedserver';
2019-08-31 21:14:46 +00:00
2022-03-24 19:02:58 +00:00
import * as typedrequest from '../ts/index.js';
2024-02-25 00:54:01 +00:00
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
2019-08-31 21:14:46 +00:00
2023-08-04 14:10:47 +00:00
let testServer: typedserver.servertools.Server;
2024-02-21 17:29:35 +00:00
let testTypedRouter: typedrequest.TypedRouter;
2019-09-01 11:17:21 +00:00
let testTypedHandler: typedrequest.TypedHandler<ITestReqRes>;
2020-09-14 08:22:27 +00:00
// lets define an interface
2019-09-01 11:17:21 +00:00
interface ITestReqRes {
method: 'hi';
request: {
name: string;
};
response: {
surname: string;
};
}
2024-02-21 17:29:35 +00:00
interface ITestStream {
method: 'handleStream';
request: {
2024-02-25 00:54:01 +00:00
requestStream: typedrequestInterfaces.IVirtualStream;
2024-02-21 17:29:35 +00:00
};
response: {
2024-02-25 00:54:01 +00:00
responseStream: typedrequestInterfaces.IVirtualStream;
2024-02-21 17:29:35 +00:00
};
}
2019-09-01 11:17:21 +00: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 11:17:21 +00:00
return {
2020-10-06 15:05:29 +00:00
surname: 'wow',
2019-09-01 11:17:21 +00:00
};
});
});
2019-08-31 21:14:46 +00:00
tap.test('should spawn a server to test with', async () => {
2023-08-04 14:10:47 +00:00
testServer = new typedserver.servertools.Server({
2019-08-31 21:14:46 +00:00
cors: true,
forceSsl: false,
2020-10-06 15:05:29 +00:00
port: 3000,
2019-08-31 21:14:46 +00:00
});
});
tap.test('should define a testHandler', async () => {
2024-02-21 17:29:35 +00:00
testTypedRouter = new typedrequest.TypedRouter(); // typed routers can broker typedrequests between handlers
2020-07-26 13:48:57 +00:00
testTypedRouter.addTypedHandler(testTypedHandler);
2020-02-11 18:55:07 +00:00
testServer.addRoute(
'/testroute',
2023-08-04 14:10:47 +00:00
new typedserver.servertools.HandlerTypedRouter(testTypedRouter as any) // the "any" is testspecific, since smartexpress ships with its own version of typedrequest.
2020-02-11 18:55:07 +00:00
);
2019-08-31 21:14:46 +00:00
});
tap.test('should start the server', async () => {
await testServer.start();
});
2019-08-21 12:36:16 +00:00
2019-09-01 11:17:21 +00:00
tap.test('should fire a request', async () => {
2020-02-11 18:55:07 +00:00
const typedRequest = new typedrequest.TypedRequest<ITestReqRes>(
'http://localhost:3000/testroute',
'hi'
);
2019-09-01 11:17:21 +00:00
const response = await typedRequest.fire({
2020-10-06 15:05:29 +00:00
name: 'really',
2019-09-01 11:17:21 +00:00
});
2020-06-25 23:53:05 +00:00
console.log('this is the response:');
2019-09-01 13:39:56 +00:00
console.log(response);
2022-03-24 19:02:58 +00:00
expect(response.surname).toEqual('wow');
2019-09-01 11:17:21 +00:00
});
2024-02-21 17:29:35 +00:00
tap.test('should allow VirtualStreams', async () => {
2024-02-25 00:54:01 +00:00
const newRequestingVS = new typedrequest.VirtualStream();
const newRespondingVS = new typedrequest.VirtualStream();
let generatedRequestingVS: typedrequestInterfaces.IVirtualStream;
let generatedRespondingVS: typedrequestInterfaces.IVirtualStream;
2024-02-21 17:29:35 +00:00
testTypedRouter.addTypedHandler(new typedrequest.TypedHandler<ITestStream>('handleStream', async (reqArg) => {
console.log('hey there');
console.log(reqArg.requestStream);
2024-02-25 00:54:01 +00:00
generatedRequestingVS = reqArg.requestStream;
2024-02-21 17:29:35 +00:00
return {
2024-02-25 00:54:01 +00:00
responseStream: newRespondingVS,
2024-02-21 17:29:35 +00:00
};
}));
const typedRequest = new typedrequest.TypedRequest<ITestStream>(
'http://localhost:3000/testroute',
'handleStream'
);
const response = await typedRequest.fire({
2024-02-25 00:54:01 +00:00
requestStream: newRequestingVS,
2024-02-21 17:29:35 +00:00
});
console.log(response.responseStream);
2024-02-25 00:54:01 +00:00
newRequestingVS.sendData(Buffer.from('hello'));
const data = await generatedRequestingVS.fetchData();
2024-03-03 09:42:14 +00:00
const decodedData = new TextDecoder().decode(data);
expect(decodedData).toEqual('hello');
newRequestingVS.close();
2024-02-29 18:50:25 +00:00
});
2024-02-21 17:29:35 +00:00
2024-02-24 11:20:13 +00:00
tap.test('should end the server', async (toolsArg) => {
2024-02-29 22:55:34 +00:00
await toolsArg.delayFor(1000);
2019-08-31 21:14:46 +00:00
await testServer.stop();
await tap.stopForcefully();
2019-08-21 12:37:02 +00:00
});
2019-08-21 12:36:16 +00:00
2024-05-25 00:15:08 +00:00
export default tap.start();