70 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-01-03 14:51:07 +01:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Auth-Server-Helper which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, January 2021
*/
2021-01-06 22:43:03 +01:00
/* eslint-disable no-console */
2020-12-30 17:21:56 +01:00
import http from 'http';
2021-01-06 22:43:03 +01:00
import ks from '../lib/KeyStore';
2020-12-30 17:21:56 +01:00
2021-01-01 14:14:19 +01:00
export class Response extends http.IncomingMessage {
2020-12-30 17:21:56 +01:00
body?: string;
}
2021-01-01 14:14:19 +01:00
export function get (
2021-01-03 14:51:07 +01:00
// eslint-disable-next-line default-param-last
headers: http.OutgoingHttpHeaders = {},
body?: string
2020-12-30 17:21:56 +01:00
): Promise<Response> {
return new Promise ((resolve) => {
2021-01-03 14:51:07 +01:00
const req = http.request ('http://localhost:3000', {
headers,
method: typeof body === 'string' ? 'POST' : 'GET'
}, (res: Response) => {
let data = '';
2020-12-30 17:21:56 +01:00
res.on ('data', (d) => {
2021-01-03 14:51:07 +01:00
data += d;
2020-12-30 17:21:56 +01:00
});
res.on ('end', () => {
2021-01-03 14:51:07 +01:00
res.body = data;
2020-12-30 17:21:56 +01:00
resolve (res);
});
});
2021-01-03 14:51:07 +01:00
if (typeof body === 'string')
req.write (body);
req.end ();
2020-12-30 17:21:56 +01:00
});
}
2021-01-01 14:14:19 +01:00
export function modify_signature (signature: string): string {
const dec = signature.split ('.');
dec[1] = '';
return dec.join ('.');
}
2021-01-06 22:43:03 +01:00
/* eslint-disable dot-notation */
export function assert_keystore_state (): void {
if (Object.keys (ks['_keys']).length !== 0) {
console.warn ('keystore gc not running!');
ks['_keys'] = {};
}
}
/* eslint-enable dot-notation */
export function flush_routine (install_clock = true):void {
if (install_clock) {
jasmine.clock ()
.install ();
}
jasmine.clock ()
.mockDate (new Date);
jasmine.clock ()
.tick (30 * 24 * 60 * 60 * 1000);
if (install_clock) {
jasmine.clock ()
.uninstall ();
}
}