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

feat(cli): add responses for PingController.ping() #1755

Merged
merged 1 commit into from
Sep 25, 2018
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
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
import {Request, RestBindings, get} from '@loopback/rest';
import {Request, RestBindings, get, ResponseObject} from '@loopback/rest';
import {inject} from '@loopback/context';

/**
* OpenAPI response for ping()
*/
const PING_RESPONSE: ResponseObject = {
description: 'Ping Response',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
greeting: {type: 'string'},
date: {type: 'string'},
url: {type: 'string'},
headers: {
type: 'object',
patternProperties: {
'^.*$': {type: 'string'},
},
additionalProperties: false,
},
},
},
},
},
};

/**
* A simple controller to bounce back http requests
*/
export class PingController {
constructor(@inject(RestBindings.Http.REQUEST) private req: Request) {}

// Map to `GET /ping`
@get('/ping')
@get('/ping', {
responses: {
'200': PING_RESPONSE,
},
})
ping(): object {
// Reply with a greeting, the current time, the url, and request headers
return {
greeting: 'Hello from LoopBack',
date: new Date(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Client,
createRestAppClient,
givenHttpServerConfig,
expect,
} from '@loopback/testlab';
import {<%= project.applicationName %>} from '../..';

Expand All @@ -25,7 +26,8 @@ describe('PingController', () => {
});

it('invokes GET /ping', async () => {
await client.get('/ping?msg=world').expect(200);
const res = await client.get('/ping?msg=world').expect(200);
expect(res.body).to.containEql({greeting: 'Hello from LoopBack'});
});

function givenAnApplication() {
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/test/integration/generators/app.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ describe('app-generator specific files', () => {
assert.fileContent('src/controllers/ping.controller.ts', /@inject/);
assert.fileContent(
'src/controllers/ping.controller.ts',
/@get\('\/ping'\)/,
/@get\('\/ping'\, \{/,
);
assert.fileContent('src/controllers/ping.controller.ts', /ping\(\)/);
assert.fileContent(
'src/controllers/ping.controller.ts',
/\'\@loopback\/rest\'/,
);
assert.fileContent(
'test/acceptance/ping.controller.acceptance.ts',
/describe\('PingController'/,
);
});
});

Expand Down
1 change: 1 addition & 0 deletions packages/rest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ import * as HttpErrors from 'http-errors';
export {HttpErrors};

export * from '@loopback/openapi-v3';
export * from '@loopback/openapi-v3-types';