Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('infers array parameter type with `any`', () => {
class MyController {
@get('/greet')
greet(
@param.array('names', 'query', {type: 'string'})
names: // eslint-disable-next-line @typescript-eslint/no-explicit-any
any,
) {}
}
const actualSpec = getControllerSpec(MyController);
const expectedSpec = anOperationSpec()
.withOperationName('greet')
.withControllerName('MyController')
.withResponse(200, {description: 'Return value of MyController.greet'})
.withParameter({
name: 'names',
schema: {
type: 'array',
items: {
type: 'string',
},
},
in: 'query',
})
.build();
expect(actualSpec.paths['/greet']['get']).to.eql(expectedSpec);
function givenNumberOfRoutes(base: string, num: number) {
const spec = anOpenApiSpec();
let i = 0;
while (i < num) {
// Add 1/4 paths with vars
if (i % 4 === 0) {
spec.withOperationReturningString(
'get',
`${base}/group${i}/{version}`,
`greet${i}`,
);
} else {
spec.withOperationReturningString(
'get',
`${base}/group${i}/version_${i}`,
`greet${i}`,
);
}
it('returns spec defined via @api()', () => {
const expectedSpec = anOpenApiSpec()
.withOperationReturningString('get', '/greet', 'greet')
.build();
@api(expectedSpec)
class MyController {
greet() {
return 'Hello world!';
}
}
const actualSpec = getControllerSpec(MyController);
expect(actualSpec).to.eql(expectedSpec);
});
it('preserves routes specified in app.api()', () => {
function status() {}
server.api(
anOpenApiSpec()
.withOperation('get', '/status', {
'x-operation': status,
responses: {},
})
.build(),
);
function greet() {}
server.route('get', '/greet', {responses: {}}, greet);
const spec = server.getApiSpec();
expect(spec.paths).to.eql({
'/greet': {
get: {
responses: {},
},
it('finds simple "GET /hello/world" endpoint', () => {
const spec = anOpenApiSpec()
.withOperationReturningString('get', '/hello/{msg}', 'greet')
.withOperationReturningString('get', '/hello/world', 'greetWorld')
.build();
class TestController {}
const table = givenRoutingTable();
table.registerController(spec, TestController);
const request = givenRequest({
method: 'get',
url: '/hello/world',
});
const route = table.find(request);
expect(route)
it('honors basePath for routes', async () => {
givenApplication();
restApp.basePath('/api');
restApp.route('get', '/status', anOperationSpec().build(), () => ({
running: true,
}));
await restApp.start();
client = createRestAppClient(restApp);
await client.get('/api/status').expect(200, {running: true});
});
it('allows children to override parent REST parameters', () => {
const operationSpec = anOperationSpec()
.withStringResponse()
.build();
class Parent {
@get('/greet', operationSpec)
greet(@param.query.string('msg') msg: string) {
return `Parent: ${msg}`;
}
}
class Child extends Parent {
greet(@param.query.string('message') msg: string) {
return `Child: ${msg}`;
}
}
it('returns a function for invoking a route handler', async () => {
function greet() {
return 'Hello world';
}
const route = new Route(
'get',
'/greet',
anOperationSpec().build(),
greet,
);
const ctx = await givenRequestContext();
const invokeMethod = await ctx.get(
RestBindings.SequenceActions.INVOKE_METHOD,
);
const result = await invokeMethod(route, []);
expect(result).to.equal('Hello world');
});
});
it('emits all media types for request body', () => {
const expectedOpSpec = anOperationSpec()
.withRequestBody({
description: 'Any object value.',
required: true,
content: {
'application/json': {
schema: {type: 'object'},
},
'application/x-www-form-urlencoded': {
schema: {type: 'object'},
},
},
})
.withResponse(200, {
content: {
'application/json': {
schema: {
it('implements toString', () => {
const spec = anOperationSpec().build();
const route = new Route('get', '/greet', spec, () => {});
expect(route.toString()).to.equal('Route - get /greet');
expect(new RouteSource(route).toString()).to.equal('get /greet');
});
});