Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rest-explorer): allow explorer for a rest app to be mounted to ex…
…press This is a follow-up to #2387
- Loading branch information
1 parent
d2f658c
commit 5d2db07
Showing
3 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/rest-explorer/src/__tests__/acceptance/rest-explorer.express.acceptance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters