diff --git a/src/lib/create-config.test.ts b/src/lib/create-config.test.ts index 89cf169e2e..3f367ed7d6 100644 --- a/src/lib/create-config.test.ts +++ b/src/lib/create-config.test.ts @@ -428,3 +428,37 @@ test('Environment variables for frontend CORS origins takes priority over option delete process.env.UNLEASH_FRONTEND_API_ORIGINS; expect(create()).toEqual(['*']); }); + +test('baseUriPath defaults to the empty string', async () => { + let config = createConfig({}); + expect(config.server.baseUriPath).toBe(''); +}); +test('BASE_URI_PATH defined in env is passed through', async () => { + process.env.BASE_URI_PATH = '/demo'; + let config = createConfig({}); + expect(config.server.baseUriPath).toBe('/demo'); + delete process.env.BASE_URI_PATH; +}); + +test('environment variable takes precedence over configured variable', async () => { + process.env.BASE_URI_PATH = '/demo'; + let config = createConfig({ + server: { + baseUriPath: '/other', + }, + }); + expect(config.server.baseUriPath).toBe('/demo'); + delete process.env.BASE_URI_PATH; +}); + +test.each(['demo', '/demo', '/demo/'])( + 'Trailing and leading slashes gets normalized for base path %s', + async (path) => { + let config = createConfig({ + server: { + baseUriPath: path, + }, + }); + expect(config.server.baseUriPath).toBe('/demo'); + }, +); diff --git a/src/lib/create-config.ts b/src/lib/create-config.ts index e7575eca19..48fd9f242f 100644 --- a/src/lib/create-config.ts +++ b/src/lib/create-config.ts @@ -231,12 +231,18 @@ const removeUndefinedKeys = (o: object): object => const formatServerOptions = ( serverOptions?: Partial, ): Partial | undefined => { - if (!serverOptions) return; + if (!serverOptions) { + return { + baseUriPath: formatBaseUri(process.env.BASE_URI_PATH), + }; + } /* eslint-disable-next-line */ return { ...serverOptions, - baseUriPath: formatBaseUri(serverOptions.baseUriPath), + baseUriPath: formatBaseUri( + process.env.BASE_URI_PATH || serverOptions.baseUriPath, + ), }; };