From fb33cb66192392aca8d631786fafdfc6520dd666 Mon Sep 17 00:00:00 2001 From: Jean-Louis Leysens Date: Wed, 19 Aug 2020 15:50:08 +0200 Subject: [PATCH] fix type issue and jest tests --- .../server/__tests__/proxy_route/mocks.ts | 47 ++++++++++++++----- .../__tests__/proxy_route/params.test.ts | 2 +- .../proxy_route/proxy_fallback.test.ts | 14 +++--- src/plugins/console/server/routes/index.ts | 2 +- 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/plugins/console/server/__tests__/proxy_route/mocks.ts b/src/plugins/console/server/__tests__/proxy_route/mocks.ts index 160c2d90cbf1f..c11d658b2e718 100644 --- a/src/plugins/console/server/__tests__/proxy_route/mocks.ts +++ b/src/plugins/console/server/__tests__/proxy_route/mocks.ts @@ -23,22 +23,45 @@ jest.mock('../../lib/proxy_request', () => ({ import { duration } from 'moment'; import { ProxyConfigCollection } from '../../lib'; -import { ProxyHandlerDependencies } from '../../routes/api/console/proxy/create_handler'; -import { coreMock } from '../../../../../core/server/mocks'; +import { RouteDependencies, ProxyDependencies } from '../../routes'; +import { EsLegacyConfigService, SpecDefinitionsService } from '../../services'; +import { coreMock, httpServiceMock } from '../../../../../core/server/mocks'; -export const getProxyRouteHandlerDeps = ({ - proxyConfigCollection = new ProxyConfigCollection([]), - pathFilters = [/.*/], - readLegacyESConfig = () => ({ +const defaultProxyValue = Object.freeze({ + readLegacyESConfig: async () => ({ requestTimeout: duration(30000), customHeaders: {}, requestHeadersWhitelist: [], hosts: ['http://localhost:9200'], }), - log = coreMock.createPluginInitializerContext().logger.get(), -}: Partial): ProxyHandlerDependencies => ({ - proxyConfigCollection, - pathFilters, - readLegacyESConfig, - log, + pathFilters: [/.*/], + proxyConfigCollection: new ProxyConfigCollection([]), }); + +interface MockDepsArgument extends Partial> { + proxy: Partial; +} + +export const getProxyRouteHandlerDeps = ({ + proxy = defaultProxyValue, + log = coreMock.createPluginInitializerContext().logger.get(), + router = httpServiceMock.createSetupContract().createRouter(), +}: MockDepsArgument): RouteDependencies => { + const services: RouteDependencies['services'] = { + esLegacyConfigService: new EsLegacyConfigService(), + specDefinitionService: new SpecDefinitionsService(), + }; + + return { + services, + router, + proxy: + defaultProxyValue !== proxy + ? { + ...defaultProxyValue, + ...proxy, + } + : defaultProxyValue, + log, + }; +}; diff --git a/src/plugins/console/server/__tests__/proxy_route/params.test.ts b/src/plugins/console/server/__tests__/proxy_route/params.test.ts index 1ab9c3ae789cc..8f61f95827a23 100644 --- a/src/plugins/console/server/__tests__/proxy_route/params.test.ts +++ b/src/plugins/console/server/__tests__/proxy_route/params.test.ts @@ -36,7 +36,7 @@ describe('Console Proxy Route', () => { describe('no matches', () => { it('rejects with 403', async () => { handler = createHandler( - getProxyRouteHandlerDeps({ pathFilters: [/^\/foo\//, /^\/bar\//] }) + getProxyRouteHandlerDeps({ proxy: { pathFilters: [/^\/foo\//, /^\/bar\//] } }) ); const { status } = await handler( diff --git a/src/plugins/console/server/__tests__/proxy_route/proxy_fallback.test.ts b/src/plugins/console/server/__tests__/proxy_route/proxy_fallback.test.ts index b226bad11a01a..fc5233d0f833d 100644 --- a/src/plugins/console/server/__tests__/proxy_route/proxy_fallback.test.ts +++ b/src/plugins/console/server/__tests__/proxy_route/proxy_fallback.test.ts @@ -38,12 +38,14 @@ describe('Console Proxy Route', () => { const handler = createHandler( getProxyRouteHandlerDeps({ - readLegacyESConfig: () => ({ - requestTimeout: duration(30000), - customHeaders: {}, - requestHeadersWhitelist: [], - hosts: ['http://localhost:9201', 'http://localhost:9202', 'http://localhost:9203'], - }), + proxy: { + readLegacyESConfig: async () => ({ + requestTimeout: duration(30000), + customHeaders: {}, + requestHeadersWhitelist: [], + hosts: ['http://localhost:9201', 'http://localhost:9202', 'http://localhost:9203'], + }), + }, }) ); diff --git a/src/plugins/console/server/routes/index.ts b/src/plugins/console/server/routes/index.ts index fd105389660ea..cbd1cef7b36e3 100644 --- a/src/plugins/console/server/routes/index.ts +++ b/src/plugins/console/server/routes/index.ts @@ -27,7 +27,7 @@ import { registerEsConfigRoute } from './api/console/es_config'; import { registerProxyRoute } from './api/console/proxy'; import { registerSpecDefinitionsRoute } from './api/console/spec_definitions'; -interface ProxyDependencies { +export interface ProxyDependencies { readLegacyESConfig: () => Promise; pathFilters: RegExp[]; proxyConfigCollection: ProxyConfigCollection;