typedrequest/test/test.ts

112 lines
3.3 KiB
TypeScript

import { expect, tap } from '@push.rocks/tapbundle';
import * as typedserver from '@api.global/typedserver';
import * as typedrequest from '../ts/index.js';
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
let testServer: typedserver.servertools.Server;
let testTypedRouter: typedrequest.TypedRouter;
let testTypedHandler: typedrequest.TypedHandler<ITestReqRes>;
// lets define an interface
interface ITestReqRes {
method: 'hi';
request: {
name: string;
};
response: {
surname: string;
};
}
interface ITestStream {
method: 'handleStream';
request: {
requestStream: typedrequestInterfaces.IVirtualStream;
};
response: {
responseStream: typedrequestInterfaces.IVirtualStream;
};
}
tap.test('should create a typedHandler', async () => {
// lets use the interface in a TypedHandler
testTypedHandler = new typedrequest.TypedHandler<ITestReqRes>('hi', async (reqArg) => {
return {
surname: 'wow',
};
});
});
tap.test('should spawn a server to test with', async () => {
testServer = new typedserver.servertools.Server({
cors: true,
forceSsl: false,
port: 3000,
});
});
tap.test('should define a testHandler', async () => {
testTypedRouter = new typedrequest.TypedRouter(); // typed routers can broker typedrequests between handlers
testTypedRouter.addTypedHandler(testTypedHandler);
testServer.addRoute(
'/testroute',
new typedserver.servertools.HandlerTypedRouter(testTypedRouter as any) // the "any" is testspecific, since smartexpress ships with its own version of typedrequest.
);
});
tap.test('should start the server', async () => {
await testServer.start();
});
tap.test('should fire a request', async () => {
const typedRequest = new typedrequest.TypedRequest<ITestReqRes>(
'http://localhost:3000/testroute',
'hi'
);
const response = await typedRequest.fire({
name: 'really',
});
console.log('this is the response:');
console.log(response);
expect(response.surname).toEqual('wow');
});
tap.test('should allow VirtualStreams', async () => {
const newRequestingVS = new typedrequest.VirtualStream();
const newRespondingVS = new typedrequest.VirtualStream();
let generatedRequestingVS: typedrequestInterfaces.IVirtualStream;
let generatedRespondingVS: typedrequestInterfaces.IVirtualStream;
testTypedRouter.addTypedHandler(new typedrequest.TypedHandler<ITestStream>('handleStream', async (reqArg) => {
console.log('hey there');
console.log(reqArg.requestStream);
generatedRequestingVS = reqArg.requestStream;
return {
responseStream: newRespondingVS,
};
}));
const typedRequest = new typedrequest.TypedRequest<ITestStream>(
'http://localhost:3000/testroute',
'handleStream'
);
const response = await typedRequest.fire({
requestStream: newRequestingVS,
});
console.log(response.responseStream);
newRequestingVS.sendData(Buffer.from('hello'));
const data = await generatedRequestingVS.fetchData();
const decodedData = new TextDecoder().decode(data);
expect(decodedData).toEqual('hello');
await newRequestingVS.close();
await newRespondingVS.close();
});
tap.test('should end the server', async (toolsArg) => {
await toolsArg.delayFor(10000);
await testServer.stop();
await tap.stopForcefully();
});
export default tap.start();