smartguard/test/test.ts
2019-08-07 16:31:53 +02:00

60 lines
1.6 KiB
TypeScript

import { expect, tap } from '@pushrocks/tapbundle';
import * as smartguard from '../ts/index';
import * as smartexpress from '@pushrocks/smartexpress';
import * as smartrequest from '@pushrocks/smartrequest';
let smartexpressInstance: smartexpress.Server;
tap.test('should create a demo smartexpress instance', async () => {
smartexpressInstance = new smartexpress.Server({
cors: true,
forceSsl: false,
defaultAnswer: async () => 'hi there',
port: 3211
});
});
tap.test('should be able to create smartguards for a request', async () => {
interface IRequestGuardData {
req: smartexpress.Request;
res: smartexpress.Response;
}
const ipGuard = new smartguard.Guard<IRequestGuardData>(async (dataArg) => {
console.log('executing ip guard');
if (dataArg) {
console.log('ip guard succeeded');
return true;
} else {
console.log('ip guard failed!');
return false;
}
});
smartexpressInstance.addRoute('/testroute', new smartexpress.Handler('ALL', async (req, res) => {
await smartguard.passGuards({
req,
res
}, [ipGuard]);
console.log('ip guard said ok');
res.status(200);
res.send('hi');
}));
console.log('Got here ok');
});
tap.test('should start server with guards in place', async () => {
await smartexpressInstance.start();
});
tap.test('should execute a request', async () => {
const response = await smartrequest.request('http://localhost:3211/testroute', {
method: 'GET'
});
});
tap.test('should end the demo smartexpress instance', async () => {
await smartexpressInstance.stop();
});
tap.start();