Skip to content

Commit

Permalink
Test for not-inline handler (need to check IRouter for elastic#47047)
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Dec 4, 2019
1 parent 1b2165c commit 336844e
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/core/server/http/http_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ import supertest from 'supertest';

import { ByteSizeValue, schema } from '@kbn/config-schema';
import { HttpConfig } from './http_config';
import { Router, RouteValidationError, RouteValidator } from './router';
import {
Router,
RouteValidationError,
RouteValidator,
KibanaRequest,
KibanaResponseFactory,
} from './router';
import { loggingServiceMock } from '../logging/logging_service.mock';
import { HttpServer } from './http_server';
import { Readable } from 'stream';
import { RequestHandlerContext } from 'kibana/server';

const cookieOptions = {
name: 'sid',
Expand Down Expand Up @@ -326,6 +333,56 @@ test('valid body with validate function', async () => {
});
});

// https://github.com/elastic/kibana/issues/47047
test('not inline handler', async () => {
const router = new Router('/foo', logger, enhanceWithContext);

const handler = (
context: RequestHandlerContext,
req: KibanaRequest<unknown, unknown, { bar: string; baz: number }>,
res: KibanaResponseFactory
) => {
const body = {
bar: req.body.bar.toUpperCase(),
baz: req.body.baz.toString(),
};

return res.ok({ body });
};

router.post(
{
path: '/',
validate: {
body: new RouteValidator(({ bar, baz } = {}) => {
if (typeof bar === 'string' && typeof baz === 'number') {
return { value: { bar, baz } };
} else {
return { error: new RouteValidationError('Wrong payload', ['body']) };
}
}),
},
},
handler
);

const { registerRouter, server: innerServer } = await server.setup(config);
registerRouter(router);

await server.start();

await supertest(innerServer.listener)
.post('/foo/')
.send({
bar: 'test',
baz: 123,
})
.expect(200)
.then(res => {
expect(res.body).toEqual({ bar: 'TEST', baz: '123' });
});
});

test('invalid body', async () => {
const router = new Router('/foo', logger, enhanceWithContext);

Expand Down

0 comments on commit 336844e

Please sign in to comment.