Skip to content

Commit 0ebddcf

Browse files
authored
add a test interceptors do not have access to request body (#70929)
1 parent 9499417 commit 0ebddcf

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed

src/core/server/http/integration_tests/lifecycle.test.ts

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import supertest from 'supertest';
2121
import request from 'request';
22+
import { schema } from '@kbn/config-schema';
2223

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

223224
await supertest(innerServer.listener).get('/').expect(200, { customField: 'undefined' });
224225
});
226+
227+
it('has no access to request body', async () => {
228+
const { registerOnPreAuth, server: innerServer, createRouter } = await server.setup(setupDeps);
229+
const router = createRouter('/');
230+
let requestBody = null;
231+
registerOnPreAuth((req, res, t) => {
232+
requestBody = req.body;
233+
return t.next();
234+
});
235+
236+
router.post(
237+
{
238+
path: '/',
239+
validate: {
240+
body: schema.object({
241+
term: schema.string(),
242+
}),
243+
},
244+
},
245+
(context, req, res) => res.ok({ body: req.body.term })
246+
);
247+
248+
await server.start();
249+
250+
await supertest(innerServer.listener)
251+
.post('/')
252+
.send({
253+
term: 'foo',
254+
})
255+
.expect(200, 'foo');
256+
257+
expect(requestBody).toStrictEqual({});
258+
});
225259
});
226260

227261
describe('OnPostAuth', () => {
@@ -356,6 +390,39 @@ describe('OnPostAuth', () => {
356390

357391
await supertest(innerServer.listener).get('/').expect(200, { customField: 'undefined' });
358392
});
393+
394+
it('has no access to request body', async () => {
395+
const { registerOnPostAuth, server: innerServer, createRouter } = await server.setup(setupDeps);
396+
const router = createRouter('/');
397+
let requestBody = null;
398+
registerOnPostAuth((req, res, t) => {
399+
requestBody = req.body;
400+
return t.next();
401+
});
402+
403+
router.post(
404+
{
405+
path: '/',
406+
validate: {
407+
body: schema.object({
408+
term: schema.string(),
409+
}),
410+
},
411+
},
412+
(context, req, res) => res.ok({ body: req.body.term })
413+
);
414+
415+
await server.start();
416+
417+
await supertest(innerServer.listener)
418+
.post('/')
419+
.send({
420+
term: 'foo',
421+
})
422+
.expect(200, 'foo');
423+
424+
expect(requestBody).toStrictEqual({});
425+
});
359426
});
360427

361428
describe('Auth', () => {
@@ -852,10 +919,43 @@ describe('Auth', () => {
852919

853920
await supertest(innerServer.listener).get('/').expect(200, { customField: 'undefined' });
854921
});
922+
923+
it('has no access to request body', async () => {
924+
const { registerAuth, server: innerServer, createRouter } = await server.setup(setupDeps);
925+
const router = createRouter('/');
926+
let requestBody = null;
927+
registerAuth((req, res, t) => {
928+
requestBody = req.body;
929+
return t.authenticated({});
930+
});
931+
932+
router.post(
933+
{
934+
path: '/',
935+
validate: {
936+
body: schema.object({
937+
term: schema.string(),
938+
}),
939+
},
940+
},
941+
(context, req, res) => res.ok({ body: req.body.term })
942+
);
943+
944+
await server.start();
945+
946+
await supertest(innerServer.listener)
947+
.post('/')
948+
.send({
949+
term: 'foo',
950+
})
951+
.expect(200, 'foo');
952+
953+
expect(requestBody).toStrictEqual({});
954+
});
855955
});
856956

857957
describe('OnPreResponse', () => {
858-
it('supports registering response inceptors', async () => {
958+
it('supports registering response interceptors', async () => {
859959
const { registerOnPreResponse, server: innerServer, createRouter } = await server.setup(
860960
setupDeps
861961
);
@@ -1001,4 +1101,39 @@ describe('OnPreResponse', () => {
10011101

10021102
await supertest(innerServer.listener).get('/').expect(200);
10031103
});
1104+
1105+
it('has no access to request body', async () => {
1106+
const { registerOnPreResponse, server: innerServer, createRouter } = await server.setup(
1107+
setupDeps
1108+
);
1109+
const router = createRouter('/');
1110+
let requestBody = null;
1111+
registerOnPreResponse((req, res, t) => {
1112+
requestBody = req.body;
1113+
return t.next();
1114+
});
1115+
1116+
router.post(
1117+
{
1118+
path: '/',
1119+
validate: {
1120+
body: schema.object({
1121+
term: schema.string(),
1122+
}),
1123+
},
1124+
},
1125+
(context, req, res) => res.ok({ body: req.body.term })
1126+
);
1127+
1128+
await server.start();
1129+
1130+
await supertest(innerServer.listener)
1131+
.post('/')
1132+
.send({
1133+
term: 'foo',
1134+
})
1135+
.expect(200, 'foo');
1136+
1137+
expect(requestBody).toStrictEqual({});
1138+
});
10041139
});

0 commit comments

Comments
 (0)