|
| 1 | +// Copyright IBM Corp. 2017. All Rights Reserved. |
| 2 | +// Node module: loopback-next-hello-extension |
| 3 | +// This file is licensed under the MIT License. |
| 4 | +// License text available at https://opensource.org/licenses/MIT |
| 5 | + |
| 6 | +import {Application} from '@loopback/core'; |
| 7 | +import {get, RestComponent, RestServer} from '@loopback/rest'; |
| 8 | +import {txIdFromHeader} from '../../..'; |
| 9 | +import {Client, createClientForHandler} from '@loopback/testlab'; |
| 10 | + |
| 11 | +describe('@txIdFromHeader() (acceptance)', () => { |
| 12 | + let app: Application; |
| 13 | + let server: RestServer; |
| 14 | + |
| 15 | + beforeEach(createApp); |
| 16 | + beforeEach(createController); |
| 17 | + beforeEach(getServerFromApp); |
| 18 | + |
| 19 | + it('works with header set', async () => { |
| 20 | + const client: Client = createClientForHandler(server.handleHttp); |
| 21 | + |
| 22 | + await client |
| 23 | + .get('/') |
| 24 | + .set('X-Transaction-Id', 'testid123') |
| 25 | + .expect(200, 'Your id is testid123'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('works without header', async () => { |
| 29 | + const client: Client = createClientForHandler(server.handleHttp); |
| 30 | + |
| 31 | + await client.get('/').expect(200, 'Your id is undefined'); |
| 32 | + }); |
| 33 | + |
| 34 | + async function createApp() { |
| 35 | + app = new Application({ |
| 36 | + components: [RestComponent], |
| 37 | + rest: { |
| 38 | + port: 3000, |
| 39 | + }, |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + function createController() { |
| 44 | + class MyController { |
| 45 | + @get('/') |
| 46 | + test(@txIdFromHeader() txId: string) { |
| 47 | + return `Your id is ${txId}`; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + app.controller(MyController); |
| 52 | + } |
| 53 | + |
| 54 | + async function getServerFromApp() { |
| 55 | + server = await app.getServer(RestServer); |
| 56 | + } |
| 57 | +}); |
0 commit comments