Skip to content

Commit

Permalink
feat(cli): add responses for PingController.ping()
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Sep 25, 2018
1 parent 8d105e4 commit 1205b5e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import {Request, RestBindings, get} from '@loopback/rest';
import {inject} from '@loopback/context';
import {ResponseObject} from '@loopback/openapi-v3-types';

/**
* 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
Expand All @@ -8,8 +35,13 @@ 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
1 change: 1 addition & 0 deletions packages/cli/generators/project/templates/package.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"@loopback/core": "<%= project.dependencies['@loopback/core'] -%>",
"@loopback/dist-util": "<%= project.dependencies['@loopback/dist-util'] -%>",
"@loopback/openapi-v3": "<%= project.dependencies['@loopback/openapi-v3'] -%>",
"@loopback/openapi-v3-types": "<%= project.dependencies['@loopback/openapi-v3-types'] -%>",
<% if (project.repositories) { -%>
"@loopback/repository": "<%= project.dependencies['@loopback/repository'] -%>",
<% } -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<% if (project.projectType === 'application') { -%>
"@loopback/core": "<%= project.dependencies['@loopback/core'] -%>",
"@loopback/openapi-v3": "<%= project.dependencies['@loopback/openapi-v3'] -%>",
"@loopback/openapi-v3-types": "<%= project.dependencies['@loopback/openapi-v3-types'] -%>",
"@loopback/repository": "<%= project.dependencies['@loopback/repository'] -%>",
"@loopback/rest": "<%= project.dependencies['@loopback/rest'] -%>"
<% } else { -%>
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

0 comments on commit 1205b5e

Please sign in to comment.