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.js
2020-03-26 14:05:15 +01:00

128 lines
3.1 KiB
JavaScript

/*
* 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
*/
'use strict';
const test = require ('ava');
const requestor = require ('../index');
const mock = {
registered: {},
post (path, handler) {
this.registered[`post-${path}`] = handler;
},
get (path, handler) {
this.registered[`get-${path}`] = handler;
},
put (path, handler) {
this.registered[`put-${path}`] = handler;
},
delete (path, handler) {
this.registered[`delete-${path}`] = handler;
},
all (path, handler) {
this.registered[`all-${path}`] = handler;
}
};
test ('detect requests on root', async (t) => {
mock.registered = {};
requestor (mock, './test_files/root');
const keys = [
'all-/',
'delete-/',
'get-/',
'post-/',
'put-/'
];
t.deepEqual (Object.keys (mock.registered), keys);
const res = await Promise.all (
Object.values (mock.registered)
.map ((val) => val ())
);
t.is (res.filter ((val) => val === 'dummy').length, keys.length);
});
test ('detect requests on root.subfolder', async (t) => {
mock.registered = {};
requestor (mock, './test_files/root.sub');
const keys = [
'all-/sub/',
'delete-/sub/',
'get-/sub/',
'post-/sub/',
'put-/sub/'
];
t.deepEqual (Object.keys (mock.registered), keys);
const res = await Promise.all (
Object.values (mock.registered)
.map ((val) => val ())
);
t.is (res.filter ((val) => val === 'dummy').length, keys.length);
});
test ('detect requests on subfolder', async (t) => {
mock.registered = {};
requestor (mock, './test_files/sub');
const keys = [
'all-/sub/',
'all-/sub/root/',
'delete-/sub/',
'get-/sub/',
'get-/sub/lv1/lv2/lv3/',
'post-/sub/',
'put-/sub/'
];
t.deepEqual (Object.keys (mock.registered), keys);
const res = await Promise.all (
Object.values (mock.registered)
.map ((val) => val ())
);
t.is (res.filter ((val) => val === 'dummy').length, keys.length);
});
test ('build requests with subdirectory', async (t) => {
mock.registered = {};
requestor (mock, './test_files/sub', { subdir: 'test' });
const keys = [
'all-/test/sub/',
'all-/test/sub/root/',
'delete-/test/sub/',
'get-/test/sub/',
'get-/test/sub/lv1/lv2/lv3/',
'post-/test/sub/',
'put-/test/sub/'
];
t.deepEqual (Object.keys (mock.registered), keys);
const res = await Promise.all (
Object.values (mock.registered)
.map ((val) => val ())
);
t.is (res.filter ((val) => val === 'dummy').length, keys.length);
});
test ('should rethrow', async (t) => {
mock.registered = {};
requestor (mock, './test_files/throw');
const [ func ] = Object.values (mock.registered);
await t.throwsAsync (func);
});
test ('should not rethrow', async (t) => {
mock.registered = {};
requestor (mock, './test_files/throw', { rethrow: false });
const [ func ] = Object.values (mock.registered);
await t.notThrowsAsync (func);
});