Skip to content

Commit

Permalink
fix: add response object for ping
Browse files Browse the repository at this point in the history
Signed-off-by: Raymond Feng <[email protected]>
  • Loading branch information
raymondfeng committed Sep 24, 2018
1 parent 74bfccb commit f1cc2bb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
29 changes: 28 additions & 1 deletion src/controllers/ping.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,41 @@ import {Request, RestBindings} from '@loopback/rest';
import {get} from '@loopback/openapi-v3';
import {inject} from '@loopback/context';

const PING_RESPONSE = {
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 {
return {
greeting: 'Hello from LoopBack',
Expand Down
23 changes: 21 additions & 2 deletions test/acceptance/ping.controller.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {createClientForHandler, supertest} from '@loopback/testlab';
import {createClientForHandler, supertest, expect} from '@loopback/testlab';
import {RestServer} from '@loopback/rest';
import {ShoppingApplication} from '../..';

Expand All @@ -30,7 +30,26 @@ 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'});
});

it('exposes OpenAPI spec at /openapi.json', async () => {
const res = await client.get('/openapi.json').expect(200);
expect(res.body).to.containDeep({
paths: {
'/ping': {
get: {
'x-controller-name': 'PingController',
'x-operation-name': 'ping',
tags: ['PingController'],
responses: {
'200': {description: 'Return value of PingController.ping'},
},
},
},
},
});
});

function givenAnApplication() {
Expand Down

0 comments on commit f1cc2bb

Please sign in to comment.