Skip to content
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
4 changes: 3 additions & 1 deletion packages/rest-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"@loopback/testlab": "^1.0.7",
"@loopback/tslint-config": "^2.0.1",
"@types/ejs": "^2.6.0",
"@types/node": "^10.1.1"
"@types/express": "^4.16.1",
"@types/node": "^10.1.1",
"express": "^4.16.4"
},
"keywords": [
"LoopBack",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: @loopback/rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {
HttpRequestListener,
RestApplication,
RestServer,
RestServerConfig,
} from '@loopback/rest';
import {
Client,
createClientForHandler,
givenHttpServerConfig,
} from '@loopback/testlab';
import * as express from 'express';
import {RestExplorerComponent} from '../..';

describe('REST Explorer mounted as an express router', () => {
let client: Client;
let expressApp: express.Application;
let server: RestServer;
let handler: HttpRequestListener;
beforeEach(givenLoopBackApp);
beforeEach(givenExpressApp);
beforeEach(givenClient);

it('exposes API Explorer at "/api/explorer/"', async () => {
await client
.get('/api/explorer/')
.expect(200)
.expect('content-type', /html/)
.expect(/url\: '\/api\/openapi\.json'\,/);
});

it('redirects from "/api/explorer" to "/api/explorer/"', async () => {
await client
.get('/api/explorer')
.expect(301)
.expect('location', '/api/explorer/');
});

async function givenLoopBackApp(
options: {rest: RestServerConfig} = {rest: {port: 0}},
) {
options.rest = givenHttpServerConfig(options.rest);
const app = new RestApplication(options);
app.component(RestExplorerComponent);
server = await app.getServer(RestServer);
handler = server.requestHandler;
}

/**
* Create an express app that mounts the LoopBack routes to `/api`
*/
function givenExpressApp() {
expressApp = express();
expressApp.use('/api', handler);
}

function givenClient() {
client = createClientForHandler(expressApp);
}
});
12 changes: 10 additions & 2 deletions packages/rest-explorer/src/rest-explorer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ export class ExplorerController {
}

indexRedirect() {
this.response.redirect(301, this.request.url + '/');
let url = this.request.url + '/';
if (this.request.baseUrl && this.request.baseUrl !== '/') {
url = this.request.baseUrl + url;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, this can be simplified using req.originalUrl, see #2508

}
this.response.redirect(301, url);
}

index() {
let openApiSpecUrl = this.openApiSpecUrl;
if (this.request.baseUrl && this.request.baseUrl !== '/') {
openApiSpecUrl = this.request.baseUrl + openApiSpecUrl;
}
const data = {
openApiSpecUrl: this.openApiSpecUrl,
openApiSpecUrl,
};

const homePage = templateFn(data);
Expand Down