Use more permissive validation in request mock (#58557)

This commit is contained in:
Josh Dover 2020-02-26 12:45:09 -07:00 committed by GitHub
parent 4cb9bc4472
commit abb2c0fd4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,12 +28,13 @@ import {
LifecycleResponseFactory,
RouteMethod,
KibanaResponseFactory,
RouteValidationSpec,
} from './router';
import { OnPreResponseToolkit } from './lifecycle/on_pre_response';
import { OnPostAuthToolkit } from './lifecycle/on_post_auth';
import { OnPreAuthToolkit } from './lifecycle/on_pre_auth';
interface RequestFixtureOptions {
interface RequestFixtureOptions<P = any, Q = any, B = any> {
headers?: Record<string, string>;
params?: Record<string, any>;
body?: Record<string, any>;
@ -42,9 +43,14 @@ interface RequestFixtureOptions {
method?: RouteMethod;
socket?: Socket;
routeTags?: string[];
validation?: {
params?: RouteValidationSpec<P>;
query?: RouteValidationSpec<Q>;
body?: RouteValidationSpec<B>;
};
}
function createKibanaRequestMock({
function createKibanaRequestMock<P = any, Q = any, B = any>({
path = '/path',
headers = { accept: 'something/html' },
params = {},
@ -53,10 +59,11 @@ function createKibanaRequestMock({
method = 'get',
socket = new Socket(),
routeTags,
}: RequestFixtureOptions = {}) {
validation = {},
}: RequestFixtureOptions<P, Q, B> = {}) {
const queryString = stringify(query, { sort: false });
return KibanaRequest.from(
return KibanaRequest.from<P, Q, B>(
createRawRequestMock({
headers,
params,
@ -76,9 +83,9 @@ function createKibanaRequestMock({
},
}),
{
params: schema.object({}, { allowUnknowns: true }),
body: schema.object({}, { allowUnknowns: true }),
query: schema.object({}, { allowUnknowns: true }),
params: validation.params || schema.any(),
body: validation.body || schema.any(),
query: validation.query || schema.any(),
}
);
}