Skip to content

Commit

Permalink
Use more permissive validation in request mock (#58557)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover authored Feb 26, 2020
1 parent 4cb9bc4 commit abb2c0f
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/core/server/http/http_server.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand All @@ -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 = {},
Expand All @@ -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,
Expand All @@ -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(),
}
);
}
Expand Down

0 comments on commit abb2c0f

Please sign in to comment.