-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
rest.server.unit.ts
215 lines (193 loc) · 6.92 KB
/
rest.server.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright IBM Corp. 2019,2020. 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 {Binding, Context} from '@loopback/context';
import {Application} from '@loopback/core';
import {anOperationSpec} from '@loopback/openapi-spec-builder';
import {expect} from '@loopback/testlab';
import {
RequestBodyParserOptions,
RestBindings,
RestComponent,
RestServer,
RestServerConfig,
Route,
} from '../../..';
describe('RestServer', () => {
describe('"bindElement" binding', () => {
it('returns a function for creating new bindings', async () => {
const ctx = await givenRequestContext();
const bindElement = await ctx.get(RestBindings.BIND_ELEMENT);
const binding = bindElement('foo').to('bar');
expect(binding).to.be.instanceOf(Binding);
expect(ctx.getSync('foo')).to.equal('bar');
});
});
describe('"getFromContext" binding', () => {
it('returns a function for getting a value from the context', async () => {
const ctx = await givenRequestContext();
const getFromContext = await ctx.get(RestBindings.GET_FROM_CONTEXT);
ctx.bind('foo').to('bar');
expect(await getFromContext('foo')).to.equal('bar');
});
});
describe('"invokeMethod" binding', () => {
it('returns a function for invoking a route handler', async () => {
function greet() {
return 'Hello world';
}
const route = new Route(
'get',
'/greet',
anOperationSpec().build(),
greet,
);
const ctx = await givenRequestContext();
const invokeMethod = await ctx.get(
RestBindings.SequenceActions.INVOKE_METHOD,
);
const result = await invokeMethod(route, []);
expect(result).to.equal('Hello world');
});
});
describe('configuration', () => {
it('uses http port 3000 by default', async () => {
const app = new Application();
app.component(RestComponent);
const server = await app.getServer(RestServer);
expect(server.getSync(RestBindings.PORT)).to.equal(3000);
});
it('uses undefined http host by default', async () => {
const app = new Application();
app.component(RestComponent);
const server = await app.getServer(RestServer);
const host = await server.get(RestBindings.HOST);
expect(host).to.be.undefined();
});
it('can set port 0', async () => {
const app = new Application({
rest: {port: 0},
});
app.component(RestComponent);
const server = await app.getServer(RestServer);
expect(server.getSync(RestBindings.PORT)).to.equal(0);
});
it('honors host/port', async () => {
const app = new Application({
rest: {port: 4000, host: 'my-host'},
});
app.component(RestComponent);
const server = await app.getServer(RestServer);
expect(server.getSync(RestBindings.PORT)).to.equal(4000);
expect(server.getSync(RestBindings.HOST)).to.equal('my-host');
});
it('honors unix socket path', async () => {
const path = '/var/run/loopback.sock';
const app = new Application({
rest: {path},
});
app.component(RestComponent);
const server = await app.getServer(RestServer);
expect(server.getSync(RestBindings.PATH)).to.equal(path);
});
it('honors named pipe path', async () => {
const path = '\\\\.\\pipe\\loopback';
const app = new Application({
rest: {path},
});
app.component(RestComponent);
const server = await app.getServer(RestServer);
expect(server.getSync(RestBindings.PATH)).to.equal(path);
});
it('honors basePath in config', async () => {
const app = new Application({
rest: {port: 0, basePath: '/api'},
});
app.component(RestComponent);
const server = await app.getServer(RestServer);
expect(server.getSync(RestBindings.BASE_PATH)).to.equal('/api');
});
it('honors basePath via api', async () => {
const app = new Application({
rest: {port: 0},
});
app.component(RestComponent);
const server = await app.getServer(RestServer);
server.basePath('/api');
expect(server.getSync(RestBindings.BASE_PATH)).to.equal('/api');
});
it('rejects basePath if request handler is created', async () => {
const app = new Application({
rest: {port: 0},
});
app.component(RestComponent);
const server = await app.getServer(RestServer);
expect(() => {
// Force the `getter` function to be triggered by referencing
// `server.requestHandler` so that the servers has `requestHandler`
// populated to prevent `basePath` to be set.
if (server.requestHandler != null) {
server.basePath('/api');
}
}).to.throw(
/Base path cannot be set as the request handler has been created/,
);
});
it('honors requestBodyParser options in config', async () => {
const parserOptions: RequestBodyParserOptions = {json: {limit: '1mb'}};
const app = new Application({
rest: {requestBodyParser: parserOptions},
});
app.component(RestComponent);
const server = await app.getServer(RestServer);
expect(server.getSync(RestBindings.REQUEST_BODY_PARSER_OPTIONS)).to.equal(
parserOptions,
);
});
describe('express settings', () => {
class TestRestServer extends RestServer {
constructor(application: Application, config: RestServerConfig) {
super(application, config);
this._setupRequestHandlerIfNeeded();
}
get expressApp() {
return this._expressApp;
}
}
it('honors expressSettings', () => {
const app = new Application();
const server = new TestRestServer(app, {
expressSettings: {
'x-powered-by': false,
env: 'production',
},
});
const expressApp = server.expressApp;
expect(expressApp.get('x-powered-by')).to.equal(false);
expect(expressApp.get('env')).to.equal('production');
// `extended` is the default setting by Express
expect(expressApp.get('query parser')).to.equal('extended');
expect(expressApp.get('not set')).to.equal(undefined);
});
it('honors strict', () => {
const app = new Application();
const server = new TestRestServer(app, {
router: {
strict: true,
},
});
const expressApp = server.expressApp;
expect(expressApp.get('strict routing')).to.equal(true);
});
});
});
async function givenRequestContext() {
const app = new Application();
app.component(RestComponent);
const server = await app.getServer(RestServer);
const requestContext = new Context(server);
requestContext.bind(RestBindings.Http.CONTEXT).to(requestContext);
return requestContext;
}
});