2020-03-25 17:04:52 +01:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-01-30 10:51:05 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-02-24 12:59:16 +01:00
|
|
|
const test = require ('ava');
|
2020-01-03 19:13:21 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-26 13:31:07 +01:00
|
|
|
test ('detect requests on root', async (t) => {
|
2020-02-24 12:59:16 +01:00
|
|
|
mock.registered = {};
|
|
|
|
requestor (mock, './test_files/root');
|
2020-02-24 12:34:07 +01:00
|
|
|
const keys = [
|
2020-02-24 12:59:16 +01:00
|
|
|
'all-/',
|
2020-02-24 12:34:07 +01:00
|
|
|
'delete-/',
|
2020-02-24 12:59:16 +01:00
|
|
|
'get-/',
|
|
|
|
'post-/',
|
|
|
|
'put-/'
|
2020-02-24 12:34:07 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
t.deepEqual (Object.keys (mock.registered), keys);
|
2020-03-26 13:31:07 +01:00
|
|
|
const res = await Promise.all (
|
|
|
|
Object.values (mock.registered)
|
|
|
|
.map ((val) => val ())
|
|
|
|
);
|
|
|
|
t.is (res.filter ((val) => val === 'dummy').length, keys.length);
|
2020-02-24 12:34:07 +01:00
|
|
|
});
|
2020-01-03 19:13:21 +01:00
|
|
|
|
2020-03-26 13:31:07 +01:00
|
|
|
test ('detect requests on root.subfolder', async (t) => {
|
2020-02-24 12:59:16 +01:00
|
|
|
mock.registered = {};
|
|
|
|
requestor (mock, './test_files/root.sub');
|
2020-02-24 12:34:07 +01:00
|
|
|
const keys = [
|
2020-02-24 12:59:16 +01:00
|
|
|
'all-/sub/',
|
2020-02-24 12:34:07 +01:00
|
|
|
'delete-/sub/',
|
2020-02-24 12:59:16 +01:00
|
|
|
'get-/sub/',
|
|
|
|
'post-/sub/',
|
|
|
|
'put-/sub/'
|
2020-02-24 12:34:07 +01:00
|
|
|
];
|
2020-01-03 19:13:21 +01:00
|
|
|
|
2020-02-24 12:34:07 +01:00
|
|
|
t.deepEqual (Object.keys (mock.registered), keys);
|
2020-03-26 13:31:07 +01:00
|
|
|
const res = await Promise.all (
|
|
|
|
Object.values (mock.registered)
|
|
|
|
.map ((val) => val ())
|
|
|
|
);
|
|
|
|
t.is (res.filter ((val) => val === 'dummy').length, keys.length);
|
2020-02-24 12:34:07 +01:00
|
|
|
});
|
|
|
|
|
2020-03-26 13:31:07 +01:00
|
|
|
test ('detect requests on subfolder', async (t) => {
|
2020-02-24 12:59:16 +01:00
|
|
|
mock.registered = {};
|
|
|
|
requestor (mock, './test_files/sub');
|
2020-02-24 12:34:07 +01:00
|
|
|
const keys = [
|
|
|
|
'all-/sub/',
|
2020-02-24 12:59:16 +01:00
|
|
|
'all-/sub/root/',
|
|
|
|
'delete-/sub/',
|
|
|
|
'get-/sub/',
|
2020-02-24 12:34:07 +01:00
|
|
|
'get-/sub/lv1/lv2/lv3/',
|
2020-02-24 12:59:16 +01:00
|
|
|
'post-/sub/',
|
|
|
|
'put-/sub/'
|
2020-02-24 12:34:07 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
t.deepEqual (Object.keys (mock.registered), keys);
|
2020-03-26 13:31:07 +01:00
|
|
|
const res = await Promise.all (
|
|
|
|
Object.values (mock.registered)
|
|
|
|
.map ((val) => val ())
|
|
|
|
);
|
|
|
|
t.is (res.filter ((val) => val === 'dummy').length, keys.length);
|
2020-02-24 12:34:07 +01:00
|
|
|
});
|
2020-01-03 19:13:21 +01:00
|
|
|
|
2020-03-26 13:31:07 +01:00
|
|
|
test ('build requests with subdirectory', async (t) => {
|
2020-02-24 12:59:16 +01:00
|
|
|
mock.registered = {};
|
|
|
|
requestor (mock, './test_files/sub', { subdir: 'test' });
|
2020-02-24 12:34:07 +01:00
|
|
|
const keys = [
|
|
|
|
'all-/test/sub/',
|
2020-02-24 12:59:16 +01:00
|
|
|
'all-/test/sub/root/',
|
|
|
|
'delete-/test/sub/',
|
|
|
|
'get-/test/sub/',
|
2020-02-24 12:34:07 +01:00
|
|
|
'get-/test/sub/lv1/lv2/lv3/',
|
2020-02-24 12:59:16 +01:00
|
|
|
'post-/test/sub/',
|
|
|
|
'put-/test/sub/'
|
2020-02-24 12:34:07 +01:00
|
|
|
];
|
2020-01-03 19:13:21 +01:00
|
|
|
|
2020-02-24 12:34:07 +01:00
|
|
|
t.deepEqual (Object.keys (mock.registered), keys);
|
2020-03-26 13:31:07 +01:00
|
|
|
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);
|
2020-01-03 19:13:21 +01:00
|
|
|
});
|