typedrequest/test/test.ts

59 lines
1.4 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';
2019-08-21 12:37:02 +00:00
import * as typedrequest from '../ts/index';
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>;
interface ITestReqRes {
method: 'hi';
request: {
name: string;
};
response: {
surname: string;
};
}
tap.test('should create a typedHandler', async () => {
testTypedHandler = new typedrequest.TypedHandler<ITestReqRes>('hi', async (reqArg) => {
return {
surname: 'wow'
};
});
});
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,
port: 3000
});
});
tap.test('should define a testHandler', async () => {
2019-09-01 11:17:21 +00:00
testServer.addRoute('/testroute', new smartexpress.Handler('POST', async (req, res) => {
console.log(req.body);
res.json(await testTypedHandler.addResponse(req.body));
}));
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 () => {
const typedRequest = new typedrequest.TypedRequest<ITestReqRes>('http://localhost:3000/testroute', 'hi');
const response = await typedRequest.fire({
name: 'really'
});
expect(response.surname).to.equal('wow');
});
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();