forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server.mocks.ts
129 lines (117 loc) · 3.4 KB
/
http_server.mocks.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
/*
* 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 { Request } from 'hapi';
import { merge } from 'lodash';
import querystring from 'querystring';
import { schema } from '@kbn/config-schema';
import { KibanaRequest, LifecycleResponseFactory, RouteMethod, ResponseFactory } from './router';
interface RequestFixtureOptions {
headers?: Record<string, string>;
params?: Record<string, any>;
body?: Record<string, any>;
query?: Record<string, any>;
path?: string;
method?: RouteMethod;
}
function createKibanaRequestMock({
path = '/path',
headers = { accept: 'something/html' },
params = {},
body = {},
query = {},
method = 'get',
}: RequestFixtureOptions = {}) {
const queryString = querystring.stringify(query);
return KibanaRequest.from(
{
headers,
params,
query,
payload: body,
path,
method,
url: {
path,
query: queryString,
search: queryString ? `?${queryString}` : queryString,
},
route: { settings: {} },
raw: {
req: {},
},
} as any,
{
params: schema.object({}, { allowUnknowns: true }),
body: schema.object({}, { allowUnknowns: true }),
query: schema.object({}, { allowUnknowns: true }),
}
);
}
type DeepPartial<T> = T extends any[]
? DeepPartialArray<T[number]>
: T extends object
? DeepPartialObject<T>
: T;
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface DeepPartialArray<T> extends Array<DeepPartial<T>> {}
type DeepPartialObject<T> = { [P in keyof T]+?: DeepPartial<T[P]> };
function createRawRequestMock(customization: DeepPartial<Request> = {}) {
return merge(
{},
{
headers: {},
path: '/',
route: { settings: {} },
raw: {
req: {
url: '/',
},
},
},
customization
) as Request;
}
const createResponseFactoryMock = (): jest.Mocked<ResponseFactory> => ({
ok: jest.fn(),
accepted: jest.fn(),
noContent: jest.fn(),
custom: jest.fn(),
redirected: jest.fn(),
badRequest: jest.fn(),
unauthorized: jest.fn(),
forbidden: jest.fn(),
notFound: jest.fn(),
conflict: jest.fn(),
internalError: jest.fn(),
});
const createLifecycleResponseFactoryMock = (): jest.Mocked<LifecycleResponseFactory> => ({
redirected: jest.fn(),
badRequest: jest.fn(),
unauthorized: jest.fn(),
forbidden: jest.fn(),
notFound: jest.fn(),
conflict: jest.fn(),
internalError: jest.fn(),
});
export const httpServerMock = {
createKibanaRequest: createKibanaRequestMock,
createRawRequest: createRawRequestMock,
createResponseFactory: createResponseFactoryMock,
createLifecycleResponseFactory: createLifecycleResponseFactoryMock,
};