Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes the unit tests #13

Merged
merged 5 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
indent_size = 2
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/.idea
/node_modules
/src/locales
/coverage
/coverage
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
1 change: 1 addition & 0 deletions language/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ invalid_request: The request task %1% has unknown properties.
response_not_success: 'The response was not success, %1% field was %2%'
response_not_failure: 'The response was not failure, %1% field was %2%'
no_errors_warnings: No errors or warnings found.
impossible_include: Can't include %1%'s export %2%.
10 changes: 6 additions & 4 deletions property/middlewares/max-time.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import MaxTime from '../../src/middlewares/max-time';
import {
process as post,
} from '../../src/middlewares/max-time.js';
import {
expect,
} from 'chai';
import 'mocha';
import Result from '../../src/messaging/result';
import Result from '../../src/messaging/result.js';
import fc from 'fast-check';

describe('middlewares/max-time', () => {
Expand All @@ -25,13 +27,13 @@ describe('middlewares/max-time', () => {
},
};
if (duration > max) {
expect(() => MaxTime.process(response,),)
expect(() => post(response,),)
.to.throw(
`The response time was above ${ max } ns`,
);
return;
}
expect(() => MaxTime.process(response,),).to.not.throw();
expect(() => post(response,),).to.not.throw();
},
);
},),);
Expand Down
4 changes: 2 additions & 2 deletions src/routes/include-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const include = async(
path = path
.replace(/\/\//ug, '/',)
.replace(/\.ts$/u, INCLUDE_EXTENSION,);
const val = await import('file://' + path,)[part];
if (typeof val !== 'function') {
const val = (await import('file://' + path,))[part];
if (typeof val === 'undefined') {
throw new Error(language('impossible_include', path, part,),);
}
if (part === 'default' && isCallable(val,)) {
Expand Down
6 changes: 3 additions & 3 deletions src/routes/validate-tasks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Task from './task.js';
import language from './helper/language.js';
import language from '../helper/language.js';
import {
EMPTY,
} from './constants.js';
import noDuplicateIds from './no-duplicate-ids.js';
} from '../constants.js';
import noDuplicateIds from '../validation/no-duplicate-ids.js';

const executableAmount = (
repetitions: number,
Expand Down
6 changes: 3 additions & 3 deletions src/worker/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import Task from '../routes/task.js';
import {
prepare,
process,
process as post,
} from '../middlewares/middleware.js';
import load from '../routes/middleware-loader.js';

Expand Down Expand Up @@ -45,10 +45,10 @@ const handlePost = async(task: Task, res:Result, callable: Callback,) => {
for (const validator of task.post) {
try {
// eslint-disable-next-line no-await-in-loop
const validatorMiddleware: process = await load(
const validatorMiddleware: post = await load(
validator,
'post',
) as process;
) as post;
validatorMiddleware(res,);
} catch (er) {
callable(buildAnswer(res, er+'', false,),);
Expand Down
18 changes: 9 additions & 9 deletions test/helper/middleware-loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import loader from '../../src/routes/middleware-loader';
import loader from '../../src/routes/middleware-loader.js';
import {
expect,
} from 'chai';
Expand All @@ -15,36 +15,36 @@ const __dirname = url.fileURLToPath(new URL('.', import.meta.url,),);
const basedir = realpathSync(__dirname + '../..',);

describe('helper/middleware-loader', () => {
it('should be a string', () => {
it('should be a function', () => {
expect(loader,).to.be.a('function',);
},);
it('should load by absolute path', async() => {
expect(await loader(__dirname + '../../src/middlewares/cookie',),)
expect(await loader(__dirname + '../../src/middlewares/cookie', 'post',),)
.to.be.a('function',);
},);
it('should load by ^-path', async() => {
expect(await loader('^cookie',),).to.be.a('function',);
expect(await loader('^cookie', 'post',),).to.be.a('function',);
},);
it('should load by ^-path and skip the default key', async() => {
expect(await loader('^encoding',),).to.be.a('function',);
expect(await loader('^encoding', 'pre',),).to.be.a('function',);
},);
it('should load by #-path', async() => {
expect(await loader('#cookie',),).to.be.a('function',);
expect(await loader('#cookie', 'pre',),).to.be.a('function',);
},);
it('should load by $-path', async() => {
try {
await loader('$needle/cookie',);
await loader('$needle/cookie', 'post',);
// eslint-disable-next-line no-unused-expressions
expect(false,).to.be.true;
} catch (e) {
if (sep === '/') {
expect(`${ e }`,).to.equal(`Error: Cannot find module '${ basedir }`
+ '/node_modules/needle/src/middlewares/cookie.ts\' '
+ `imported from ${ basedir }/src/helper/include-default.ts`,);
+ `imported from ${ basedir }/src/routes/include-default.ts`,);
} else {
expect(`${ e }`,).to.equal(`Error: Cannot find module '${ basedir }`
+ '\\node_modules\\needle\\src\\middlewares\\cookie.ts\' '
+ `imported from ${ basedir }\\src\\helper\\include-default.ts`,);
+ `imported from ${ basedir }\\src\\routes\\include-default.ts`,);
}
}
},);
Expand Down
8 changes: 4 additions & 4 deletions test/helper/user-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ describe('helper/user-agent', () => {
it('should match expectations', () => {
expect(userAgent,).to.match(
// eslint-disable-next-line max-len
/^@idrinth-api-bench\/framework\/\d+\.\d+ @idrinth\/api-bench\/\d+\.\d+ needle\/\d+\.\d+$/u,
/^@idrinth-api-bench\/framework\/\d+\.\d+ @idrinth-api-bench\/framework\/\d+\.\d+ needle\/\d+\.\d+$/u,
);
},);
it('should not be root version 0.0', () => {
expect(userAgent,).to.not.match(
// eslint-disable-next-line max-len
/^@idrinth-api-bench\/framework\/0+\.0+ @idrinth\/api-bench\/\d+\.\d+ needle\/\d+\.\d+$/u,
/^@idrinth-api-bench\/framework\/0+\.0+ @idrinth-api-bench\/framework\/\d+\.\d+ needle\/\d+\.\d+$/u,
);
},);
it('should not be needle version 0.0', () => {
expect(userAgent,).to.not.match(
// eslint-disable-next-line max-len
/^@idrinth-api-bench\/framework\/\d+\.\d+ @idrinth\/api-bench\/\d+\.\d+ needle\/0\.0$/u,
/^@idrinth-api-bench\/framework\/\d+\.\d+ @idrinth-api-bench\/framework\/\d+\.\d+ needle\/0\.0$/u,
);
},);
it('should not be api-bench version 0.0', () => {
expect(userAgent,).to.not.match(
// eslint-disable-next-line max-len
/^@idrinth-api-bench\/framework\/\d+\.\d+ @idrinth\/api-bench\/0\.0 needle\/\d+\.\d+$/u,
/^@idrinth-api-bench\/framework\/\d+\.\d+ @idrinth-api-bench\/framework\/0\.0 needle\/\d+\.\d+$/u,
);
},);
},);
32 changes: 16 additions & 16 deletions test/middlewares/access-token.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import Access from '../../src/middlewares/access-token';
import {
prepare as pre,
process as post,
} from '../../src/middlewares/access-token.js';
import {
expect,
} from 'chai';
import 'mocha';
import Request from '../../src/routes/request';
import Result from '../../src/messaging/result';
import store from '../../src/store/store';
import Request from '../../src/routes/request.js';
import Result from '../../src/messaging/result.js';
import store from '../../src/store/store.js';

describe('middlewares/csrf-header', () => {
before(store.clean,);
after(store.clean,);
it('should be a class', () => {
expect(Access,).to.be.a('function',);
},);
it('should have a static method prepare', () => {
expect(Access.prepare,).to.be.a('function',);
expect(pre,).to.be.a('function',);
},);
it('should have a static method process', () => {
expect(Access.process,).to.be.a('function',);
expect(post,).to.be.a('function',);
},);
it('should not set token by default', () => {
expect(Access.prepare(<Request>{},),).to.deep.equal({},);
expect(pre(<Request>{},),).to.deep.equal({},);
},);
it('should get token by default', () => {
expect(() => Access.process(<Result><unknown>{
expect(() => post(<Result><unknown>{
response: {
headers: {
'content-type': 'application/json',
Expand All @@ -33,26 +33,26 @@ describe('middlewares/csrf-header', () => {
},),).to.not.throw();
},);
it('should set token it has', () => {
expect(Access.prepare(<Request>{},),).to.deep.equal({
expect(pre(<Request>{},),).to.deep.equal({
headers: {
'authorization': 'Bearer 11',
},
},);
},);
it('should change no token if the response is incomplete', () => {
expect(() => Access.process(<Result><unknown>{
expect(() => post(<Result><unknown>{
response: {},
},),).to.not.throw();
},);
it('should set token it has', () => {
expect(Access.prepare(<Request>{},),).to.deep.equal({
expect(pre(<Request>{},),).to.deep.equal({
headers: {
'authorization': 'Bearer 11',
},
},);
},);
it('should get refresh-token by default', () => {
expect(() => Access.process(<Result><unknown>{
expect(() => post(<Result><unknown>{
response: {
headers: {
'content-type': 'application/json',
Expand All @@ -62,7 +62,7 @@ describe('middlewares/csrf-header', () => {
},),).to.not.throw();
},);
it('should set all tokens it has', () => {
expect(Access.prepare(<Request>{
expect(pre(<Request>{
body: '%refresh-token-middleware%%access-token-middleware%',
},),).to.deep.equal({
headers: {
Expand Down
30 changes: 15 additions & 15 deletions test/middlewares/cookie.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import Cookie from '../../src/middlewares/cookie';
import {
prepare as pre,
process as post,
} from '../../src/middlewares/cookie.js';
import {
expect,
} from 'chai';
import 'mocha';
import Request from '../../src/routes/request';
import Result from '../../src/messaging/result';
import store from '../../src/store/store';
import Request from '../../src/routes/request.js';
import Result from '../../src/messaging/result.js';
import store from '../../src/store/store.js';

describe('middlewares/cookie', () => {
before(store.clean,);
after(store.clean,);
it('should be a class', () => {
expect(Cookie,).to.be.a('function',);
},);
it('should have a static method prepare', () => {
expect(Cookie.prepare,).to.be.a('function',);
expect(pre,).to.be.a('function',);
},);
it('should have a static method process', () => {
expect(Cookie.process,).to.be.a('function',);
expect(post,).to.be.a('function',);
},);
it('should not set cookies by default', () => {
expect(Cookie.prepare(<Request>{},),).to.deep.equal({
expect(pre(<Request>{},),).to.deep.equal({
cookies: {},
},);
},);
it('should get cookies by default', () => {
expect(() => Cookie.process(<Result><unknown>{
expect(() => post(<Result><unknown>{
response: {
cookies: {
abc: 'def',
Expand All @@ -34,26 +34,26 @@ describe('middlewares/cookie', () => {
},),).to.not.throw();
},);
it('should set cookies it has', () => {
expect(Cookie.prepare(<Request>{},),).to.deep.equal({
expect(pre(<Request>{},),).to.deep.equal({
cookies: {
abc: 'def',
},
},);
},);
it('should change no cookies if there are none', () => {
expect(() => Cookie.process(<Result><unknown>{
expect(() => post(<Result><unknown>{
response: {
cookies: {},
},
},),).to.not.throw();
},);
it('should change no cookies if the response is incomplete', () => {
expect(() => Cookie.process(<Result><unknown>{
expect(() => post(<Result><unknown>{
response: {},
},),).to.not.throw();
},);
it('should set cookies it has', () => {
expect(Cookie.prepare(<Request>{},),).to.deep.equal({
expect(pre(<Request>{},),).to.deep.equal({
cookies: {
abc: 'def',
},
Expand Down
Loading
Loading