nats-micro
Version:
NATS micro compatible extra-lightweight microservice library
116 lines (91 loc) • 3.27 kB
text/typescript
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-empty-function */
import { expect } from 'chai';
import '../common.js';
import { storage } from '../../src/decorators/storage.js';
import {
method, microservice,
Request, Response, middleware,
} from '../../src/index.js';
describe('@middleware decorator', function () {
it('no middleware', async function () {
()
class Test {
()
public method(_req: Request<void>, res: Response<void>): void {
res.sendNoResponse();
}
}
const config = storage.getConfig(new Test());
const methods = config?.methods;
expect(methods).to.exist;
expect(methods).to.have.property('method');
expect(methods!.method.middlewares).to.be.undefined;
expect(methods!.method.postMiddlewares).to.be.undefined;
});
describe('Pre-middleware', function () {
it('single middleware', async function () {
()
class Test {
.pre(() => undefined)
()
public method(_req: Request<void>, res: Response<void>): void {
res.sendNoResponse();
}
}
const config = storage.getConfig(new Test());
const methods = config?.methods;
expect(methods!.method.middlewares).to.have.lengthOf(1);
expect(methods!.method.postMiddlewares).to.have.lengthOf(0);
});
it('multiple middleware', async function () {
()
class MethodTest {
.pre(() => undefined)
.pre(() => undefined)
.pre(() => undefined)
()
public method(_req: Request<void>, res: Response<void>): void {
res.sendNoResponse();
}
}
const config = storage.getConfig(new MethodTest());
const methods = config?.methods;
expect(methods!.method.middlewares).to.have.lengthOf(3);
expect(methods!.method.postMiddlewares).to.have.lengthOf(0);
});
});
describe('Post-middleware', function () {
it('single middleware', async function () {
()
class Test {
.post(() => undefined)
()
public method(_req: Request<void>, res: Response<void>): void {
res.sendNoResponse();
}
}
const config = storage.getConfig(new Test());
const methods = config?.methods;
expect(methods!.method.middlewares).to.have.lengthOf(0);
expect(methods!.method.postMiddlewares).to.have.lengthOf(1);
});
it('multiple middleware', async function () {
()
class MethodTest {
.post(() => undefined)
.post(() => undefined)
.post(() => undefined)
()
public method(_req: Request<void>, res: Response<void>): void {
res.sendNoResponse();
}
}
const config = storage.getConfig(new MethodTest());
const methods = config?.methods;
expect(methods!.method.middlewares).to.have.lengthOf(0);
expect(methods!.method.postMiddlewares).to.have.lengthOf(3);
});
});
});