48 lines
1.2 KiB
TypeScript
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);
|
|
});
|