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

92 lines
2.0 KiB
JavaScript
Raw Normal View History

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-02-24 12:34:07 +01:00
test ('detect requests on root', (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-01-03 19:13:21 +01:00
2020-02-24 12:34:07 +01:00
test ('detect requests on root.subfolder', (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);
});
test ('detect requests on subfolder', (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-01-03 19:13:21 +01:00
2020-02-24 12:34:07 +01:00
test ('build requests with subdirectory', (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-01-03 19:13:21 +01:00
});