From 85a544bbd621a7e8ffdd46f8d136316be783002a Mon Sep 17 00:00:00 2001 From: Fredrik Strand Oseberg Date: Mon, 3 May 2021 12:28:59 +0200 Subject: [PATCH] Feat/format base path (#828) * chore: update changelog * feat: add formatBaseUri helper * feat: call formatBaseUri on server options * feat: call formatBaseUri on user options * fix: update test * fix: disable consistent return --- CHANGELOG.md | 4 ++++ src/lib/create-config.ts | 18 +++++++++++++-- src/lib/util/format-base-uri.test.ts | 32 +++++++++++++++++++++++++++ src/lib/util/format-base-uri.ts | 19 ++++++++++++++++ src/test/config/create-config.test.ts | 2 +- 5 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 src/lib/util/format-base-uri.test.ts create mode 100644 src/lib/util/format-base-uri.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index d0c459a4aa..67590fe656 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,10 @@ - fix: change default admin password - fix: add types for node-fetch +# 4.0.0-alpha.5 + +- chore: update frontend + # 4.0.0-alpha.4 - feat: add option for LOG_LEVEL (#803) diff --git a/src/lib/create-config.ts b/src/lib/create-config.ts index 9244458cab..5cb9bbee1f 100644 --- a/src/lib/create-config.ts +++ b/src/lib/create-config.ts @@ -16,6 +16,7 @@ import { } from './types/option'; import { getDefaultLogProvider, LogLevel, validateLogProvider } from './logger'; import { defaultCustomAuthDenyAll } from './default-custom-auth-deny-all'; +import { formatBaseUri } from './util/format-base-uri'; const safeToUpper = (s: string) => (s ? s.toUpperCase() : s); @@ -82,7 +83,7 @@ const defaultServerOption: IServerOption = { pipe: undefined, host: process.env.HTTP_HOST, port: safeNumber(process.env.HTTP_PORT || process.env.PORT, 4242), - baseUriPath: process.env.BASE_URI_PATH || '', + baseUriPath: formatBaseUri(process.env.BASE_URI_PATH), unleashUrl: process.env.UNLEASH_URL || 'http://localhost:4242', serverMetrics: true, keepAliveTimeout: 60 * 1000, @@ -136,8 +137,21 @@ const removeUndefinedKeys = (o: object): object => return a; }, {}); +const formatServerOptions = ( + serverOptions?: Partial, +): Partial | undefined => { + if (!serverOptions) return; + + /* eslint-disable-next-line */ + return { + ...serverOptions, + baseUriPath: formatBaseUri(serverOptions.baseUriPath), + }; +}; + export function createConfig(options: IUnleashOptions): IUnleashConfig { let extraDbOptions = {}; + if (options.databaseUrl) { extraDbOptions = parse(options.databaseUrl); } else if (process.env.DATABASE_URL) { @@ -161,7 +175,7 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { const server: IServerOption = mergeAll([ defaultServerOption, - options.server, + formatServerOptions(options.server), ]); const versionCheck: IVersionOption = mergeAll([ diff --git a/src/lib/util/format-base-uri.test.ts b/src/lib/util/format-base-uri.test.ts new file mode 100644 index 0000000000..be7e689349 --- /dev/null +++ b/src/lib/util/format-base-uri.test.ts @@ -0,0 +1,32 @@ +import { formatBaseUri } from './format-base-uri'; +import test from 'ava'; + +test('formatBaseUri returns the correct path when the path is the right format', t => { + const result = formatBaseUri('/hosted'); + t.true(result === '/hosted'); +}); + +test('formatBaseUri returns the correct path when the path lacking initial slash', t => { + const result = formatBaseUri('hosted'); + t.true(result === '/hosted'); +}); + +test('formatBaseUri returns the correct path when the path has both initial and trailing slash', t => { + const result = formatBaseUri('/hosted/'); + t.true(result === '/hosted'); +}); + +test('formatBaseUri returns the correct path when the path has only trailing slash', t => { + const result = formatBaseUri('hosted/'); + t.true(result === '/hosted'); +}); + +test('formatBaseUri returns empty string when called without input', t => { + const result = formatBaseUri(undefined); + t.true(result === ''); +}); + +test('formatBaseUri handles levels of paths', t => { + const result = formatBaseUri('hosted/multi/path'); + t.true(result === '/hosted/multi/path'); +}); diff --git a/src/lib/util/format-base-uri.ts b/src/lib/util/format-base-uri.ts new file mode 100644 index 0000000000..328dfc528b --- /dev/null +++ b/src/lib/util/format-base-uri.ts @@ -0,0 +1,19 @@ +export const formatBaseUri = (input: string): string => { + if (!input) return ''; + const firstChar = input[0]; + const lastChar = input[input.length - 1]; + + if (firstChar === '/' && lastChar === '/') { + return input.substr(0, input.length - 1); + } + + if (firstChar !== '/' && lastChar === '/') { + return `/${input.substr(0, input.length - 1)}`; + } + + if (firstChar !== '/') { + return `/${input}`; + } + + return input; +}; diff --git a/src/test/config/create-config.test.ts b/src/test/config/create-config.test.ts index 937ecb8278..57b6b42842 100644 --- a/src/test/config/create-config.test.ts +++ b/src/test/config/create-config.test.ts @@ -67,7 +67,7 @@ test('should allow setting pool size', t => { }); test('Can set baseUriPath', t => { - const baseUriPath = 'some'; + const baseUriPath = '/some'; const config = createConfig({ server: { baseUriPath } }); t.is(config.server.baseUriPath, baseUriPath); });