typedrequest/test/test.ts

68 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-08-21 12:36:16 +00:00
import { expect, tap } from '@pushrocks/tapbundle';
2019-08-31 21:14:46 +00:00
import * as smartexpress from '@pushrocks/smartexpress';
2022-03-24 19:02:58 +00:00
import * as typedrequest from '../ts/index.js';
2019-08-31 21:14:46 +00:00
let testServer: smartexpress.Server;
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;
};
}
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 () => {
testServer = new smartexpress.Server({
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 () => {
2020-09-14 08:22:27 +00:00
const 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',
2020-09-14 08:28:07 +00:00
new smartexpress.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
});
2019-08-31 21:14:46 +00:00
tap.test('should end the server', async () => {
await testServer.stop();
2019-08-21 12:37:02 +00:00
});
2019-08-21 12:36:16 +00:00
2019-08-21 12:37:02 +00:00
tap.start();