Skip to content

Commit

Permalink
fix type issue and jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Aug 19, 2020
1 parent 06e6396 commit fb33cb6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
47 changes: 35 additions & 12 deletions src/plugins/console/server/__tests__/proxy_route/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>): ProxyHandlerDependencies => ({
proxyConfigCollection,
pathFilters,
readLegacyESConfig,
log,
pathFilters: [/.*/],
proxyConfigCollection: new ProxyConfigCollection([]),
});

interface MockDepsArgument extends Partial<Omit<RouteDependencies, 'proxy'>> {
proxy: Partial<ProxyDependencies>;
}

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,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
}),
},
})
);

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/console/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ESConfigForProxy>;
pathFilters: RegExp[];
proxyConfigCollection: ProxyConfigCollection;
Expand Down

0 comments on commit fb33cb6

Please sign in to comment.