From b5d5b5711838fc5a1b63edce4f7fb8dc0a087883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Tue, 8 Apr 2025 12:17:01 +0200 Subject: [PATCH] fix(core): Fix routing for waiting webhooks and forms (#14470) --- packages/cli/src/abstract-server.ts | 4 +- .../cli/test/integration/webhooks.test.ts | 76 +++++++++++++------ 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/packages/cli/src/abstract-server.ts b/packages/cli/src/abstract-server.ts index 82599ce151..016b54cba5 100644 --- a/packages/cli/src/abstract-server.ts +++ b/packages/cli/src/abstract-server.ts @@ -193,13 +193,13 @@ export abstract class AbstractServer { // Register a handler for waiting forms this.app.all( - `/${this.endpointFormWaiting}/:path/{:suffix}`, + `/${this.endpointFormWaiting}/:path{/:suffix}`, createWebhookHandlerFor(Container.get(WaitingForms)), ); // Register a handler for waiting webhooks this.app.all( - `/${this.endpointWebhookWaiting}/:path/{:suffix}`, + `/${this.endpointWebhookWaiting}/:path{/:suffix}`, createWebhookHandlerFor(Container.get(WaitingWebhooks)), ); } diff --git a/packages/cli/test/integration/webhooks.test.ts b/packages/cli/test/integration/webhooks.test.ts index 0895f3caa8..c08b2c7517 100644 --- a/packages/cli/test/integration/webhooks.test.ts +++ b/packages/cli/test/integration/webhooks.test.ts @@ -16,23 +16,31 @@ import { mockInstance } from '@test/mocking'; let agent: SuperAgentTest; describe('WebhookServer', () => { + const liveWebhooks = mockInstance(LiveWebhooks); + const testWebhooks = mockInstance(TestWebhooks); + const waitingWebhooks = mockInstance(WaitingWebhooks); + mockInstance(WaitingForms); mockInstance(ExternalHooks); + const globalConfig = Container.get(GlobalConfig); + + const mockResponse = (data = {}, headers = {}, status = 200) => { + const response = mock(); + response.responseCode = status; + response.data = data; + response.headers = headers; + return response; + }; + + beforeAll(async () => { + const server = new WebhookServer(); + // @ts-expect-error: testWebhooksEnabled is private + server.testWebhooksEnabled = true; + await server.start(); + agent = testAgent(server.app); + }); describe('CORS', () => { const corsOrigin = 'https://example.com'; - const liveWebhooks = mockInstance(LiveWebhooks); - const testWebhooks = mockInstance(TestWebhooks); - mockInstance(WaitingWebhooks); - mockInstance(WaitingForms); - - beforeAll(async () => { - const server = new WebhookServer(); - // @ts-expect-error: testWebhooksEnabled is private - server.testWebhooksEnabled = true; - await server.start(); - agent = testAgent(server.app); - }); - const tests = [ ['webhook', liveWebhooks], ['webhookTest', testWebhooks], @@ -43,8 +51,9 @@ describe('WebhookServer', () => { for (const [key, manager] of tests) { describe(`for ${key}`, () => { + const pathPrefix = globalConfig.endpoints[key]; + it('should handle preflight requests', async () => { - const pathPrefix = Container.get(GlobalConfig).endpoints[key]; manager.getWebhookMethods.mockResolvedValueOnce(['GET']); const response = await agent @@ -58,7 +67,6 @@ describe('WebhookServer', () => { }); it('should handle regular requests', async () => { - const pathPrefix = Container.get(GlobalConfig).endpoints[key]; manager.getWebhookMethods.mockResolvedValueOnce(['GET']); manager.executeWebhook.mockResolvedValueOnce( mockResponse({ test: true }, { key: 'value ' }), @@ -75,13 +83,37 @@ describe('WebhookServer', () => { }); }); } + }); - const mockResponse = (data = {}, headers = {}, status = 200) => { - const response = mock(); - response.responseCode = status; - response.data = data; - response.headers = headers; - return response; - }; + describe('Routing for Waiting Webhooks', () => { + const pathPrefix = globalConfig.endpoints.webhookWaiting; + + waitingWebhooks.executeWebhook.mockImplementation(async (req) => { + return { + noWebhookResponse: false, + responseCode: 200, + data: { + params: req.params, + }, + }; + }); + + it('should handle URLs without suffix', async () => { + const response = await agent.get(`/${pathPrefix}/12345`); + + expect(response.statusCode).toEqual(200); + expect(response.body).toEqual({ + params: { path: '12345' }, + }); + }); + + it('should handle URLs with suffix', async () => { + const response = await agent.get(`/${pathPrefix}/12345/suffix`); + + expect(response.statusCode).toEqual(200); + expect(response.body).toEqual({ + params: { path: '12345', suffix: 'suffix' }, + }); + }); }); });