-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest): allow rest-server to be mounted on a path to express
- Loading branch information
1 parent
f8db27c
commit de8f626
Showing
2 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
packages/rest/src/__tests__/integration/express.integration.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,89 @@ | ||
// 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 {Application} from '@loopback/core'; | ||
import {get} from '@loopback/openapi-v3'; | ||
import { | ||
Client, | ||
createClientForHandler, | ||
expect, | ||
givenHttpServerConfig, | ||
} from '@loopback/testlab'; | ||
import * as express from 'express'; | ||
import {RestComponent} from '../../rest.component'; | ||
import { | ||
HttpRequestListener, | ||
RestServer, | ||
RestServerConfig, | ||
} from '../../rest.server'; | ||
|
||
describe('HttpHandler mounted as an express router', () => { | ||
let client: Client; | ||
beforeEach(givenLoopBackApp); | ||
beforeEach(givenExpressApp); | ||
beforeEach(givenClient); | ||
|
||
context('with a simple HelloWorld controller', () => { | ||
beforeEach(function setupHelloController() { | ||
class HelloController { | ||
@get('/hello') | ||
public async greet(): Promise<string> { | ||
return 'Hello world!'; | ||
} | ||
} | ||
|
||
givenControllerClass(HelloController); | ||
}); | ||
|
||
it('handles simple "GET /hello" requests', () => { | ||
return client | ||
.get('/api/hello') | ||
.expect(200) | ||
.expect('content-type', 'text/plain') | ||
.expect('Hello world!'); | ||
}); | ||
|
||
it('handles openapi.json', async () => { | ||
const res = await client | ||
.get('/api/openapi.json') | ||
.set('Accept', 'application/json') | ||
.expect(200); | ||
expect(res.body.servers[0]).to.eql({url: '/api'}); | ||
}); | ||
}); | ||
|
||
let expressApp: express.Application; | ||
let server: RestServer; | ||
let handler: HttpRequestListener; | ||
|
||
function givenControllerClass( | ||
// tslint:disable-next-line:no-any | ||
ctor: new (...args: any[]) => Object, | ||
) { | ||
server.controller(ctor); | ||
} | ||
|
||
async function givenLoopBackApp( | ||
options: {rest: RestServerConfig} = {rest: {port: 0}}, | ||
) { | ||
options.rest = givenHttpServerConfig(options.rest); | ||
const app = new Application(options); | ||
app.component(RestComponent); | ||
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