Skip to content

Commit

Permalink
add a test interceptors do not have access to request body (elastic#7…
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov authored Jul 8, 2020
1 parent ca447de commit 7dcdc85
Showing 1 changed file with 136 additions and 1 deletion.
137 changes: 136 additions & 1 deletion src/core/server/http/integration_tests/lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import supertest from 'supertest';
import request from 'request';
import { schema } from '@kbn/config-schema';

import { ensureRawRequest } from '../router';
import { HttpService } from '../http_service';
Expand Down Expand Up @@ -222,6 +223,39 @@ describe('OnPreAuth', () => {

await supertest(innerServer.listener).get('/').expect(200, { customField: 'undefined' });
});

it('has no access to request body', async () => {
const { registerOnPreAuth, server: innerServer, createRouter } = await server.setup(setupDeps);
const router = createRouter('/');
let requestBody = null;
registerOnPreAuth((req, res, t) => {
requestBody = req.body;
return t.next();
});

router.post(
{
path: '/',
validate: {
body: schema.object({
term: schema.string(),
}),
},
},
(context, req, res) => res.ok({ body: req.body.term })
);

await server.start();

await supertest(innerServer.listener)
.post('/')
.send({
term: 'foo',
})
.expect(200, 'foo');

expect(requestBody).toStrictEqual({});
});
});

describe('OnPostAuth', () => {
Expand Down Expand Up @@ -356,6 +390,39 @@ describe('OnPostAuth', () => {

await supertest(innerServer.listener).get('/').expect(200, { customField: 'undefined' });
});

it('has no access to request body', async () => {
const { registerOnPostAuth, server: innerServer, createRouter } = await server.setup(setupDeps);
const router = createRouter('/');
let requestBody = null;
registerOnPostAuth((req, res, t) => {
requestBody = req.body;
return t.next();
});

router.post(
{
path: '/',
validate: {
body: schema.object({
term: schema.string(),
}),
},
},
(context, req, res) => res.ok({ body: req.body.term })
);

await server.start();

await supertest(innerServer.listener)
.post('/')
.send({
term: 'foo',
})
.expect(200, 'foo');

expect(requestBody).toStrictEqual({});
});
});

describe('Auth', () => {
Expand Down Expand Up @@ -852,10 +919,43 @@ describe('Auth', () => {

await supertest(innerServer.listener).get('/').expect(200, { customField: 'undefined' });
});

it('has no access to request body', async () => {
const { registerAuth, server: innerServer, createRouter } = await server.setup(setupDeps);
const router = createRouter('/');
let requestBody = null;
registerAuth((req, res, t) => {
requestBody = req.body;
return t.authenticated({});
});

router.post(
{
path: '/',
validate: {
body: schema.object({
term: schema.string(),
}),
},
},
(context, req, res) => res.ok({ body: req.body.term })
);

await server.start();

await supertest(innerServer.listener)
.post('/')
.send({
term: 'foo',
})
.expect(200, 'foo');

expect(requestBody).toStrictEqual({});
});
});

describe('OnPreResponse', () => {
it('supports registering response inceptors', async () => {
it('supports registering response interceptors', async () => {
const { registerOnPreResponse, server: innerServer, createRouter } = await server.setup(
setupDeps
);
Expand Down Expand Up @@ -1001,4 +1101,39 @@ describe('OnPreResponse', () => {

await supertest(innerServer.listener).get('/').expect(200);
});

it('has no access to request body', async () => {
const { registerOnPreResponse, server: innerServer, createRouter } = await server.setup(
setupDeps
);
const router = createRouter('/');
let requestBody = null;
registerOnPreResponse((req, res, t) => {
requestBody = req.body;
return t.next();
});

router.post(
{
path: '/',
validate: {
body: schema.object({
term: schema.string(),
}),
},
},
(context, req, res) => res.ok({ body: req.body.term })
);

await server.start();

await supertest(innerServer.listener)
.post('/')
.send({
term: 'foo',
})
.expect(200, 'foo');

expect(requestBody).toStrictEqual({});
});
});

0 comments on commit 7dcdc85

Please sign in to comment.