2024-05-30 13:08:09 +00:00
|
|
|
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
|
2022-03-21 20:53:46 +00:00
|
|
|
import * as smartguard from '../ts/index.js';
|
2024-05-30 13:08:09 +00:00
|
|
|
import * as typedserver from '@api.global/typedserver';
|
|
|
|
import * as smartrequest from '@push.rocks/smartrequest';
|
2019-06-18 12:51:13 +00:00
|
|
|
|
2024-08-25 16:03:37 +00:00
|
|
|
let typedserverInstance: typedserver.servertools.Server;
|
2019-06-18 12:51:13 +00:00
|
|
|
|
2019-08-07 14:31:53 +00:00
|
|
|
tap.test('should create a demo smartexpress instance', async () => {
|
2024-08-25 16:03:37 +00:00
|
|
|
typedserverInstance = new typedserver.servertools.Server({
|
2019-08-07 14:31:53 +00:00
|
|
|
cors: true,
|
|
|
|
forceSsl: false,
|
|
|
|
defaultAnswer: async () => 'hi there',
|
2022-03-21 20:53:46 +00:00
|
|
|
port: 3211,
|
2019-08-07 14:31:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('should be able to create smartguards for a request', async () => {
|
|
|
|
interface IRequestGuardData {
|
2024-05-30 13:08:09 +00:00
|
|
|
req: typedserver.Request;
|
|
|
|
res: typedserver.Response;
|
2019-08-07 14:31:53 +00:00
|
|
|
}
|
2022-03-21 20:53:46 +00:00
|
|
|
const ipGuard = new smartguard.Guard<IRequestGuardData>(async (dataArg) => {
|
2019-08-07 14:31:53 +00:00
|
|
|
console.log('executing ip guard');
|
|
|
|
if (dataArg) {
|
|
|
|
console.log('ip guard succeeded');
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
console.log('ip guard failed!');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-08-25 16:03:37 +00:00
|
|
|
typedserverInstance.addRoute(
|
2019-08-07 14:34:34 +00:00
|
|
|
'/testroute',
|
2024-05-30 13:08:09 +00:00
|
|
|
new typedserver.servertools.Handler('ALL', async (req, res) => {
|
2024-05-30 14:57:18 +00:00
|
|
|
await smartguard.passGuardsOrReject(
|
2019-08-07 14:34:34 +00:00
|
|
|
{
|
|
|
|
req,
|
2022-03-21 20:53:46 +00:00
|
|
|
res,
|
2019-08-07 14:34:34 +00:00
|
|
|
},
|
|
|
|
[ipGuard]
|
|
|
|
);
|
|
|
|
console.log('ip guard said ok');
|
|
|
|
res.status(200);
|
|
|
|
res.send('hi');
|
|
|
|
})
|
|
|
|
);
|
2019-08-07 14:31:53 +00:00
|
|
|
console.log('Got here ok');
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('should start server with guards in place', async () => {
|
2024-08-25 16:03:37 +00:00
|
|
|
await typedserverInstance.start();
|
2019-08-07 14:31:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('should execute a request', async () => {
|
|
|
|
const response = await smartrequest.request('http://localhost:3211/testroute', {
|
2022-03-21 20:53:46 +00:00
|
|
|
method: 'GET',
|
2019-08-07 14:31:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('should end the demo smartexpress instance', async () => {
|
2024-08-25 16:03:37 +00:00
|
|
|
await typedserverInstance.stop();
|
2019-08-07 14:31:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tap.start();
|