Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('adds bindings to the request context', async () => {
expect(requestCtx.contains(CoreBindings.CONTROLLER_CURRENT));
expect(
requestCtx.getBinding(CoreBindings.CONTROLLER_CURRENT).scope,
).to.equal(BindingScope.SINGLETON);
expect(await requestCtx.get(CoreBindings.CONTROLLER_CLASS)).to.equal(
MyController,
);
expect(
await requestCtx.get(CoreBindings.CONTROLLER_METHOD_NAME),
).to.equal('greet');
expect(await requestCtx.get(RestBindings.OPERATION_SPEC_CURRENT)).to.eql({
'x-controller-name': 'MyController',
'x-operation-name': 'greet',
tags: ['MyController'],
responses: {'200': {description: 'An undocumented response body.'}},
});
});
it('returns undefined for a method decorated with @authenticate.skip', async () => {
const context: Context = new Context();
context.bind(CoreBindings.CONTROLLER_CLASS).to(TestController);
context.bind(CoreBindings.CONTROLLER_METHOD_NAME).to('hello');
context
.bind(CoreBindings.CONTROLLER_METHOD_META)
.toProvider(AuthMetadataProvider);
const authMetadata = await context.get(
CoreBindings.CONTROLLER_METHOD_META,
);
expect(authMetadata).to.be.undefined();
});
it('returns the auth metadata of a controller method', async () => {
const context: Context = new Context();
context.bind(CoreBindings.CONTROLLER_CLASS).to(TestController);
context.bind(CoreBindings.CONTROLLER_METHOD_NAME).to('whoAmI');
context
.bind(CoreBindings.CONTROLLER_METHOD_META)
.toProvider(AuthMetadataProvider);
const authMetadata = await context.get(
CoreBindings.CONTROLLER_METHOD_META,
);
expect(authMetadata).to.be.eql({
strategy: 'my-strategy',
options: {option1: 'value1', option2: 'value2'},
});
});
* is invoked.
*
* Please note the controller class itself can be bound to other scopes,
* such as SINGLETON or TRANSIENT (default) in the application or server
* context.
*
* - SINGLETON: all requests share the same instance of a given controller
* - TRANSIENT: each request has its own instance of a given controller
*/
requestContext
.bind(CoreBindings.CONTROLLER_CURRENT)
.toDynamicValue(() => this._controllerFactory(requestContext))
.inScope(BindingScope.SINGLETON);
requestContext.bind(CoreBindings.CONTROLLER_CLASS).to(this._controllerCtor);
requestContext
.bind(CoreBindings.CONTROLLER_METHOD_NAME)
.to(this._methodName);
requestContext.bind(RestBindings.OPERATION_SPEC_CURRENT).to(this.spec);
}
constructor(
@inject(CoreBindings.CONTROLLER_CLASS, {optional: true})
private readonly controllerClass: Constructor<{}>,
@inject(CoreBindings.CONTROLLER_METHOD_NAME, {optional: true})
private readonly methodName: string,
@config({fromBinding: AuthenticationBindings.COMPONENT})
private readonly options: AuthenticationOptions = {},
) {}
constructor(
@inject(CoreBindings.CONTROLLER_CLASS, {optional: true})
private readonly controllerClass: Constructor<{}>,
@inject(CoreBindings.CONTROLLER_METHOD_NAME, {optional: true})
private readonly methodName: string,
) {}
constructor(
@inject(CoreBindings.CONTROLLER_CLASS, {optional: true}) protected _controllerClass: Constructor<{}>,
@inject(CoreBindings.CONTROLLER_METHOD_NAME, {optional: true}) protected _methodName: string,
) {
super(_controllerClass, _methodName);
}