diff --git a/packages/cli/src/services/__tests__/url.service.test.ts b/packages/cli/src/services/__tests__/url.service.test.ts new file mode 100644 index 0000000000..c89d62789c --- /dev/null +++ b/packages/cli/src/services/__tests__/url.service.test.ts @@ -0,0 +1,43 @@ +import type { GlobalConfig } from '@n8n/config'; +import { mock } from 'jest-mock-extended'; + +import config from '@/config'; + +import { UrlService } from '../url.service'; + +describe('UrlService', () => { + beforeEach(() => { + process.env.WEBHOOK_URL = undefined; + config.load(config.default); + }); + + describe('getInstanceBaseUrl', () => { + it('should set URL from N8N_EDITOR_BASE_URL', () => { + config.set('editorBaseUrl', 'https://example.com/'); + process.env.WEBHOOK_URL = undefined; + const urlService = new UrlService(mock()); + expect(urlService.getInstanceBaseUrl()).toBe('https://example.com'); + }); + + it('should set URL from WEBHOOK_URL', () => { + config.set('editorBaseUrl', ''); + process.env.WEBHOOK_URL = 'https://example.com/'; + const urlService = new UrlService(mock()); + expect(urlService.getInstanceBaseUrl()).toBe('https://example.com'); + }); + + it('should trim quotes when setting URL from N8N_EDITOR_BASE_URL', () => { + config.set('editorBaseUrl', '"https://example.com"'); + process.env.WEBHOOK_URL = undefined; + const urlService = new UrlService(mock()); + expect(urlService.getInstanceBaseUrl()).toBe('https://example.com'); + }); + + it('should trim quotes when setting URL from WEBHOOK_URL', () => { + config.set('editorBaseUrl', ''); + process.env.WEBHOOK_URL = '"https://example.com/"'; + const urlService = new UrlService(mock()); + expect(urlService.getInstanceBaseUrl()).toBe('https://example.com'); + }); + }); +}); diff --git a/packages/cli/src/services/url.service.ts b/packages/cli/src/services/url.service.ts index 43b53f28ad..f9d3fcdbbd 100644 --- a/packages/cli/src/services/url.service.ts +++ b/packages/cli/src/services/url.service.ts @@ -14,7 +14,7 @@ export class UrlService { /** Returns the base URL of the webhooks */ getWebhookBaseUrl() { - let urlBaseWebhook = process.env.WEBHOOK_URL ?? this.baseUrl; + let urlBaseWebhook = this.trimQuotes(process.env.WEBHOOK_URL) || this.baseUrl; if (!urlBaseWebhook.endsWith('/')) { urlBaseWebhook += '/'; } @@ -23,7 +23,7 @@ export class UrlService { /** Return the n8n instance base URL without trailing slash */ getInstanceBaseUrl(): string { - const n8nBaseUrl = config.getEnv('editorBaseUrl') || this.getWebhookBaseUrl(); + const n8nBaseUrl = this.trimQuotes(config.getEnv('editorBaseUrl')) || this.getWebhookBaseUrl(); return n8nBaseUrl.endsWith('/') ? n8nBaseUrl.slice(0, n8nBaseUrl.length - 1) : n8nBaseUrl; } @@ -36,4 +36,9 @@ export class UrlService { } return `${protocol}://${host}:${port}${path}`; } + + /** Remove leading and trailing double quotes from a URL. */ + private trimQuotes(url?: string) { + return url?.replace(/^["]+|["]+$/g, '') ?? ''; + } }