This repository has been archived on 2020-08-13. You can view files and clone it, but cannot push or open issues or pull requests.
requestor/test/main.ts
2020-04-09 21:12:36 +02:00

48 lines
1.2 KiB
TypeScript

/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Requestor which is released under BSD-3-Clause.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, March 2020
*/
/* eslint-disable max-classes-per-file */
import test from 'ava';
import { Application } from 'express';
import { http } from '@scode/consts';
import { load_handlers, Handler, Transaction } from '../lib/index';
class MockServer implements Application {
public registered: Record<string, Function> = {};
}
class MockHandler extends Handler {
public get path (): string {
return '/mock';
}
private handle_all (t: Transaction): void {
t.status = http.status_im_a_teapot;
t.finalize ('foo');
}
public constructor () {
super ();
this.register_handler (this.handle_all);
}
}
test ('detect requests on root', async (t) => {
const server = (new MockServer);
load_handlers (server, [ (new MockHandler) ]);
const keys = [ 'all-mock' ];
t.deepEqual (Object.keys (server.registered), keys);
const res = await Promise.all (
Object.values (server.registered)
.map ((val) => val ())
);
t.is (res.filter ((val) => val === 'foo').length, keys.length);
});