-
Notifications
You must be signed in to change notification settings - Fork 972
/
Copy pathhttp_service.test.ts
311 lines (256 loc) · 8.72 KB
/
http_service.test.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { mockHttpServer } from './http_service.test.mocks';
import { noop } from 'lodash';
import { BehaviorSubject } from 'rxjs';
import { REPO_ROOT } from '@osd/dev-utils';
import { getEnvOptions } from '../config/mocks';
import { HttpService } from '.';
import { HttpConfigType, config } from './http_config';
import { httpServerMock } from './http_server.mocks';
import { ConfigService, Env } from '../config';
import { loggingSystemMock } from '../logging/logging_system.mock';
import { contextServiceMock } from '../context/context_service.mock';
import { config as cspConfig } from '../csp';
const logger = loggingSystemMock.create();
const env = Env.createDefault(REPO_ROOT, getEnvOptions());
const coreId = Symbol();
const createConfigService = (value: Partial<HttpConfigType> = {}) => {
const configService = new ConfigService(
{
getConfig$: () =>
new BehaviorSubject({
server: value,
}),
},
env,
logger
);
configService.setSchema(config.path, config.schema);
configService.setSchema(cspConfig.path, cspConfig.schema);
return configService;
};
const contextSetup = contextServiceMock.createSetupContract();
const setupDeps = {
context: contextSetup,
};
const fakeHapiServer = {
start: noop,
stop: noop,
route: noop,
};
afterEach(() => {
jest.clearAllMocks();
});
test('creates and sets up http server', async () => {
const configService = createConfigService({
host: 'example.org',
port: 1234,
});
const httpServer = {
isListening: () => false,
setup: jest.fn().mockReturnValue({ server: fakeHapiServer }),
start: jest.fn(),
stop: jest.fn(),
};
mockHttpServer.mockImplementation(() => httpServer);
const service = new HttpService({ coreId, configService, env, logger });
expect(mockHttpServer.mock.instances.length).toBe(1);
expect(httpServer.setup).not.toHaveBeenCalled();
await service.setup(setupDeps);
expect(httpServer.setup).toHaveBeenCalled();
expect(httpServer.start).not.toHaveBeenCalled();
await service.start();
expect(httpServer.start).toHaveBeenCalled();
});
test('spins up notReady server until started if configured with `autoListen:true`', async () => {
const configService = createConfigService();
const httpServer = {
isListening: () => false,
setup: jest.fn().mockReturnValue({}),
start: jest.fn(),
stop: jest.fn(),
};
const notReadyHapiServer = {
start: jest.fn(),
stop: jest.fn(),
route: jest.fn(),
};
mockHttpServer
.mockImplementationOnce(() => httpServer)
.mockImplementationOnce(() => ({
setup: () => ({ server: notReadyHapiServer }),
}));
const service = new HttpService({
coreId,
configService,
env: Env.createDefault(REPO_ROOT, getEnvOptions()),
logger,
});
await service.setup(setupDeps);
const mockResponse: any = {
code: jest.fn().mockImplementation(() => mockResponse),
header: jest.fn().mockImplementation(() => mockResponse),
};
const mockResponseToolkit = {
response: jest.fn().mockReturnValue(mockResponse),
};
const [[{ handler }]] = notReadyHapiServer.route.mock.calls;
const response503 = await handler(httpServerMock.createRawRequest(), mockResponseToolkit);
expect(response503).toBe(mockResponse);
expect({
body: mockResponseToolkit.response.mock.calls,
code: mockResponse.code.mock.calls,
header: mockResponse.header.mock.calls,
}).toMatchSnapshot('503 response');
await service.start();
expect(httpServer.start).toBeCalledTimes(1);
expect(notReadyHapiServer.stop).toBeCalledTimes(1);
});
test('logs error if already set up', async () => {
const configService = createConfigService();
const httpServer = {
isListening: () => true,
setup: jest.fn().mockReturnValue({ server: fakeHapiServer }),
start: noop,
stop: noop,
};
mockHttpServer.mockImplementation(() => httpServer);
const service = new HttpService({ coreId, configService, env, logger });
await service.setup(setupDeps);
expect(loggingSystemMock.collect(logger).warn).toMatchSnapshot();
});
test('stops http server', async () => {
const configService = createConfigService();
const httpServer = {
isListening: () => false,
setup: jest.fn().mockReturnValue({ server: fakeHapiServer }),
start: noop,
stop: jest.fn(),
};
mockHttpServer.mockImplementation(() => httpServer);
const service = new HttpService({ coreId, configService, env, logger });
await service.setup(setupDeps);
await service.start();
expect(httpServer.stop).toHaveBeenCalledTimes(0);
await service.stop();
expect(httpServer.stop).toHaveBeenCalledTimes(1);
});
test('stops not ready server if it is running', async () => {
const configService = createConfigService();
const mockHapiServer = {
start: jest.fn(),
stop: jest.fn(),
route: jest.fn(),
};
const httpServer = {
isListening: () => false,
setup: jest.fn().mockReturnValue({ server: mockHapiServer }),
start: noop,
stop: jest.fn(),
};
mockHttpServer.mockImplementation(() => httpServer);
const service = new HttpService({ coreId, configService, env, logger });
await service.setup(setupDeps);
await service.stop();
expect(mockHapiServer.stop).toHaveBeenCalledTimes(1);
});
test('register route handler', async () => {
const configService = createConfigService();
const registerRouterMock = jest.fn();
const httpServer = {
isListening: () => false,
setup: jest
.fn()
.mockReturnValue({ server: fakeHapiServer, registerRouter: registerRouterMock }),
start: noop,
stop: noop,
};
mockHttpServer.mockImplementation(() => httpServer);
const service = new HttpService({ coreId, configService, env, logger });
const { createRouter } = await service.setup(setupDeps);
const router = createRouter('/foo');
expect(registerRouterMock).toHaveBeenCalledTimes(1);
expect(registerRouterMock).toHaveBeenLastCalledWith(router);
});
test('returns http server contract on setup', async () => {
const configService = createConfigService();
const httpServer = { server: fakeHapiServer, options: { someOption: true } };
mockHttpServer.mockImplementation(() => ({
isListening: () => false,
setup: jest.fn().mockReturnValue(httpServer),
stop: noop,
}));
const service = new HttpService({ coreId, configService, env, logger });
const setupContract = await service.setup(setupDeps);
expect(setupContract).toMatchObject(httpServer);
expect(setupContract).toMatchObject({
createRouter: expect.any(Function),
});
});
test('does not start http server if process is dev cluster master', async () => {
const configService = createConfigService();
const httpServer = {
isListening: () => false,
setup: jest.fn().mockReturnValue({}),
start: jest.fn(),
stop: noop,
};
mockHttpServer.mockImplementation(() => httpServer);
const service = new HttpService({
coreId,
configService,
env: Env.createDefault(REPO_ROOT, getEnvOptions({ isDevClusterMaster: true })),
logger,
});
await service.setup(setupDeps);
await service.start();
expect(httpServer.start).not.toHaveBeenCalled();
});
test('does not start http server if configured with `autoListen:false`', async () => {
const configService = createConfigService({
autoListen: false,
});
const httpServer = {
isListening: () => false,
setup: jest.fn().mockReturnValue({}),
start: jest.fn(),
stop: noop,
};
mockHttpServer.mockImplementation(() => httpServer);
const service = new HttpService({
coreId,
configService,
env: Env.createDefault(REPO_ROOT, getEnvOptions()),
logger,
});
await service.setup(setupDeps);
await service.start();
expect(httpServer.start).not.toHaveBeenCalled();
});