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 2bfd50e commit ecff179
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
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';

0 comments on commit ecff179

Please sign in to comment.