66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
|
|
import * as smartguard from '../ts/index.js';
|
|
import * as typedserver from '@api.global/typedserver';
|
|
import * as smartrequest from '@push.rocks/smartrequest';
|
|
|
|
let typedserverInstance: typedserver.servertools.Server;
|
|
|
|
tap.test('should create a demo smartexpress instance', async () => {
|
|
typedserverInstance = new typedserver.servertools.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: typedserver.Request;
|
|
res: typedserver.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;
|
|
}
|
|
});
|
|
|
|
typedserverInstance.addRoute(
|
|
'/testroute',
|
|
new typedserver.servertools.Handler('ALL', async (req, res) => {
|
|
await smartguard.passGuardsOrReject(
|
|
{
|
|
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 typedserverInstance.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 typedserverInstance.stop();
|
|
});
|
|
|
|
tap.start();
|