/
rest.component.unit.ts
100 lines (89 loc) · 3.24 KB
/
rest.component.unit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright IBM Corp. 2019. 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 {BoundValue, Context, Provider, inject} from '@loopback/context';
import {
Application,
Component,
CoreBindings,
ProviderMap,
} from '@loopback/core';
import {expect, stubExpressContext} from '@loopback/testlab';
import {
HttpHandler,
LogError,
Request,
RestBindings,
RestComponent,
RestComponentConfig,
RestServer,
} from '../..';
import {aRestServerConfig} from '../helpers';
const SequenceActions = RestBindings.SequenceActions;
describe('RestComponent', () => {
describe('Providers', () => {
describe('Default implementations are bound', () => {
let app: Application;
let comp: Component;
before(async () => {
app = new Application();
app.component(RestComponent);
// Stub constructor requirements for some providers.
app.bind(RestBindings.Http.CONTEXT).to(new Context());
app
.bind(RestBindings.HANDLER)
.to(new HttpHandler(app, aRestServerConfig()));
comp = await app.get<Component>('components.RestComponent');
});
it('', async () => {
for (const key in comp.providers || {}) {
it(key, async () => {
const result = await app.get(key);
const expected: Provider<BoundValue> = new comp.providers![key]();
expect(result).to.deepEqual(expected.value());
});
}
});
});
describe('LOG_ERROR', () => {
it('matches expected argument signature', async () => {
const app = new Application();
app.component(RestComponent);
const server = await app.getServer(RestServer);
const logError = await server.get<LogError>(SequenceActions.LOG_ERROR);
expect(logError.length).to.equal(3); // (err, statusCode, request)
});
// tslint:disable-next-line:max-line-length
it('can be customized by extending RestComponent', async () => {
let lastLog: string = 'logError() was not called';
class CustomRestComponent extends RestComponent {
providers: ProviderMap = {
[RestBindings.SequenceActions.LOG_ERROR.key]: CustomLogger,
};
constructor(
@inject(CoreBindings.APPLICATION_INSTANCE) application: Application,
@inject(CoreBindings.APPLICATION_CONFIG)
config?: RestComponentConfig,
) {
super(application, config);
}
}
class CustomLogger implements Provider<LogError> {
value() {
return (err: Error, statusCode: number, request: Request) => {
lastLog = `${request.url} ${statusCode} ${err.message}`;
};
}
}
const app = new Application();
app.component(CustomRestComponent);
const server = await app.getServer(RestServer);
const logError = await server.get<LogError>(SequenceActions.LOG_ERROR);
const expressContext = stubExpressContext({url: '/'});
logError(new Error('test-error'), 400, expressContext.request);
expect(lastLog).to.equal('/ 400 test-error');
});
});
});
});