From 37163dc5cca602099df5015bf041552f6e4685fc Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Thu, 30 Jul 2020 16:09:08 -0400 Subject: [PATCH 1/2] Test request aborted observable in a pre auth handler --- .../http/integration_tests/request.test.ts | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/core/server/http/integration_tests/request.test.ts b/src/core/server/http/integration_tests/request.test.ts index 2d018f7f464b5..8ebf96ae3214d 100644 --- a/src/core/server/http/integration_tests/request.test.ts +++ b/src/core/server/http/integration_tests/request.test.ts @@ -23,6 +23,7 @@ import { HttpService } from '../http_service'; import { contextServiceMock } from '../../context/context_service.mock'; import { loggingSystemMock } from '../../logging/logging_system.mock'; import { createHttpServer } from '../test_utils'; +import { schema } from '@kbn/config-schema'; let server: HttpService; @@ -173,6 +174,55 @@ describe('KibanaRequest', () => { expect(completeSpy).toHaveBeenCalledTimes(1); }); + it('if in a preAuthHandler completes after the response is send', async () => { + const { server: innerServer, createRouter, registerOnPreAuth } = await server.setup( + setupDeps + ); + const router = createRouter('/'); + + const nextSpy = jest.fn(); + const completeSpy = jest.fn(); + registerOnPreAuth((req, response, toolkit) => { + req.events.aborted$.subscribe({ + next: nextSpy, + complete: completeSpy, + }); + + return toolkit.next(); + }); + + let error: Error | undefined; + router.post( + { path: '/', validate: { body: schema.any() } }, + async (context, request, res) => { + // Spy should not have been called here + try { + expect(completeSpy).toHaveBeenCalledTimes(0); + } catch (err) { + error = err; + } + + return res.ok({ body: 'ok' }); + } + ); + + await server.start(); + + await supertest(innerServer.listener) + .post('/') + .send({ + data: 'test', + }) + .expect(200); + + expect(nextSpy).toHaveBeenCalledTimes(0); + expect(completeSpy).toHaveBeenCalledTimes(1); + + if (error) { + throw error; + } + }); + it('completes & does not emit when request rejected', async () => { const { server: innerServer, createRouter } = await server.setup(setupDeps); const router = createRouter('/'); From 38f2b546d4848e61eed481470c77f53999495e07 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Thu, 30 Jul 2020 16:10:03 -0400 Subject: [PATCH 2/2] fix request aborted observable --- src/core/server/http/router/request.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/server/http/router/request.ts b/src/core/server/http/router/request.ts index 0e73431fe7c6d..47cd6b1c70937 100644 --- a/src/core/server/http/router/request.ts +++ b/src/core/server/http/router/request.ts @@ -186,7 +186,7 @@ export class KibanaRequest< private getEvents(request: Request): KibanaRequestEvents { const finish$ = merge( - fromEvent(request.raw.req, 'end'), // all data consumed + fromEvent(request.raw.res, 'finish'), // Response is done fromEvent(request.raw.req, 'close') // connection was closed ).pipe(shareReplay(1), first()); return {