Skip to content

Commit

Permalink
refactor: apply feedback
Browse files Browse the repository at this point in the history
apply server route in test, relocate test RE: integration

Signed-off-by: Douglas McConnachie <[email protected]>
  • Loading branch information
dougal83 committed Apr 18, 2020
1 parent 619d62f commit cce2478
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 189 deletions.
160 changes: 159 additions & 1 deletion packages/rest/src/__tests__/integration/rest.server.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
// License text available at https://opensource.org/licenses/MIT

import {Application} from '@loopback/core';
import {anOpenApiSpec, anOperationSpec} from '@loopback/openapi-spec-builder';
import {
aComponentsSpec,
anOpenApiSpec,
anOperationSpec,
} from '@loopback/openapi-spec-builder';
import {
createClientForHandler,
createRestAppClient,
Expand Down Expand Up @@ -875,6 +879,160 @@ paths:
await server.stop();
});

it('disables consolidator if openApiSpec.consolidate option is set to false', async () => {
const options = {openApiSpec: {consolidate: false}};
const server = await givenAServer({rest: options});

const EXPECTED_SPEC = anOpenApiSpec()
.withOperation(
'get',
'/',
anOperationSpec().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.build();

server.route('get', '/', EXPECTED_SPEC.paths['/'].get, () => {});

await server.start();
const spec = await server.getApiSpec();
expect(spec).to.eql(EXPECTED_SPEC);
await server.stop();
});

it('runs consolidator if openApiSpec.consolidate option is set to true', async () => {
const options = {openApiSpec: {consolidate: true}};
const server = await givenAServer({rest: options});

const EXPECTED_SPEC = anOpenApiSpec()
.withOperation(
'get',
'/',
anOperationSpec().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/loopback.example',
},
},
},
}),
)
.withComponents(
aComponentsSpec().withSchema('loopback.example', {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
}),
)
.build();

server.route(
'get',
'/',
anOperationSpec()
.withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
})
.build(),
() => {},
);

await server.start();
const spec = await server.getApiSpec();
expect(spec).to.eql(EXPECTED_SPEC);
await server.stop();
});

it('runs consolidator if openApiSpec.consolidate option is undefined', async () => {
const options = {openApiSpec: {consolidate: undefined}};
const server = await givenAServer({rest: options});

const EXPECTED_SPEC = anOpenApiSpec()
.withOperation(
'get',
'/',
anOperationSpec().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/loopback.example',
},
},
},
}),
)
.withComponents(
aComponentsSpec().withSchema('loopback.example', {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
}),
)
.build();

server.route(
'get',
'/',
anOperationSpec()
.withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
})
.build(),
() => {},
);

await server.start();
const spec = await server.getApiSpec();
expect(spec).to.eql(EXPECTED_SPEC);
await server.stop();
});

it('registers controller routes under routes.*', async () => {
const server = await givenAServer();
server.controller(DummyController);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,16 @@
// License text available at https://opensource.org/licenses/MIT

import {Application, createBindingFromClass} from '@loopback/core';
import {
aComponentsSpec,
anOpenApiSpec,
anOperationSpec,
} from '@loopback/openapi-spec-builder';
import {get, OpenApiSpec, post, requestBody} from '@loopback/openapi-v3';
import {anOpenApiSpec, anOperationSpec} from '@loopback/openapi-spec-builder';
import {get, post, requestBody} from '@loopback/openapi-v3';
import {model, property} from '@loopback/repository';
import {
expect,
givenHttpServerConfig,
validateApiSpec,
} from '@loopback/testlab';
import {expect, validateApiSpec} from '@loopback/testlab';
import {
createControllerFactoryForClass,
RestComponent,
RestServer,
RestServerConfig,
} from '../../..';
import {RestBindings, RestTags} from '../../../keys';
import {RestTags} from '../../../keys';
import {ConsolidationEnhancer} from '../../../spec-enhancers/consolidate.spec-enhancer';
import {TestInfoSpecEnhancer} from './fixtures/info.spec.extension';

Expand Down Expand Up @@ -408,181 +399,6 @@ describe('RestServer.getApiSpec()', () => {
expect(spec.info).to.eql(EXPECTED_SPEC_INFO);
});

context('options', () => {
afterEach(async () => server.stop());
it('disables consolidator if consolidate is set to false', async () => {
await givenAppWithRestConfig({
rest: {openApiSpec: {consolidate: false}},
});

const INPUT_SPEC = anOpenApiSpec()
.withOperation(
'get',
'/',
anOperationSpec().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.build();

server.api(INPUT_SPEC);

server.OASEnhancer.spec = await server.get<OpenApiSpec>(
RestBindings.API_SPEC,
);
await server.OASEnhancer.applyEnhancerByName('consolidate');
const spec = await server.get<OpenApiSpec>(RestBindings.API_SPEC);
expect(spec).to.eql(INPUT_SPEC);
});

it('runs consolidator if consolidate is set to true', async () => {
await givenAppWithRestConfig({
rest: {openApiSpec: {consolidate: true}},
});

server.api(
anOpenApiSpec()
.withOperation(
'get',
'/',
anOperationSpec().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.build(),
);

const EXPECTED_SPEC = anOpenApiSpec()
.withOperation(
'get',
'/',
anOperationSpec().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/loopback.example',
},
},
},
}),
)
.withComponents(
aComponentsSpec().withSchema('loopback.example', {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
}),
)
.build();

server.OASEnhancer.spec = await server.get<OpenApiSpec>(
RestBindings.API_SPEC,
);
await server.OASEnhancer.applyEnhancerByName('consolidate');
const spec = await server.get<OpenApiSpec>(RestBindings.API_SPEC);
expect(spec).to.eql(EXPECTED_SPEC);
});

it('runs consolidator if consolidate is undefined', async () => {
await givenAppWithRestConfig({
rest: {openApiSpec: {consolidate: undefined}},
});

server.api(
anOpenApiSpec()
.withOperation(
'get',
'/',
anOperationSpec().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.build(),
);

const EXPECTED_SPEC = anOpenApiSpec()
.withOperation(
'get',
'/',
anOperationSpec().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/loopback.example',
},
},
},
}),
)
.withComponents(
aComponentsSpec().withSchema('loopback.example', {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
}),
)
.build();

server.OASEnhancer.spec = await server.get<OpenApiSpec>(
RestBindings.API_SPEC,
);
await server.OASEnhancer.applyEnhancerByName('consolidate');
const spec = await server.get<OpenApiSpec>(RestBindings.API_SPEC);
expect(spec).to.eql(EXPECTED_SPEC);
});
});

async function givenAppWithRestConfig(options: {rest: RestServerConfig}) {
options.rest = givenHttpServerConfig(options.rest);
app = new Application(options);
app.component(RestComponent);
server = await app.getServer(RestServer);
await server.start();
}
async function givenApplication() {
app = new Application();
app.component(RestComponent);
Expand Down

0 comments on commit cce2478

Please sign in to comment.