From 8c40475cc6ccf0fac31ff038ea764509d48860ed Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Wed, 4 Dec 2024 11:31:03 +0100 Subject: [PATCH] Fix route data handling (#6176) * Fix route data handling * Cleanup --- .../Middleware/HttpWorkflowsMiddleware.cs | 3 +- .../Elsa.Http/Services/RouteMatcher.cs | 3 +- .../Scenarios/HttpWorkflows/RouteDataTests.cs | 15 +++++++++ .../Workflows/RouteDataWorkflow.cs | 32 +++++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 test/component/Elsa.Workflows.ComponentTests/Scenarios/HttpWorkflows/RouteDataTests.cs create mode 100644 test/component/Elsa.Workflows.ComponentTests/Scenarios/HttpWorkflows/Workflows/RouteDataWorkflow.cs diff --git a/src/modules/Elsa.Http/Middleware/HttpWorkflowsMiddleware.cs b/src/modules/Elsa.Http/Middleware/HttpWorkflowsMiddleware.cs index 2894519cf..bc0d81194 100644 --- a/src/modules/Elsa.Http/Middleware/HttpWorkflowsMiddleware.cs +++ b/src/modules/Elsa.Http/Middleware/HttpWorkflowsMiddleware.cs @@ -53,7 +53,6 @@ public class HttpWorkflowsMiddleware(RequestDelegate next, ITenantAccessor tenan } // Strip the base path. - path = path[basePath.Length..]; matchingPath = matchingPath[basePath.Length..]; } @@ -62,7 +61,7 @@ public class HttpWorkflowsMiddleware(RequestDelegate next, ITenantAccessor tenan var input = new Dictionary { [HttpEndpoint.HttpContextInputKey] = true, - [HttpEndpoint.RequestPathInputKey] = path + [HttpEndpoint.RequestPathInputKey] = path.NormalizeRoute() }; var cancellationToken = httpContext.RequestAborted; diff --git a/src/modules/Elsa.Http/Services/RouteMatcher.cs b/src/modules/Elsa.Http/Services/RouteMatcher.cs index f89326283..b72e59d1b 100644 --- a/src/modules/Elsa.Http/Services/RouteMatcher.cs +++ b/src/modules/Elsa.Http/Services/RouteMatcher.cs @@ -13,7 +13,8 @@ public class RouteMatcher : IRouteMatcher public RouteValueDictionary? Match(string routeTemplate, string route) { var normalizedRoute = route.NormalizeRoute(); - var template = TemplateParser.Parse(routeTemplate); + var normalizedRouteTemplate = routeTemplate.NormalizeRoute(); + var template = TemplateParser.Parse(normalizedRouteTemplate); var matcher = new TemplateMatcher(template, GetDefaults(template)); var values = new RouteValueDictionary(); diff --git a/test/component/Elsa.Workflows.ComponentTests/Scenarios/HttpWorkflows/RouteDataTests.cs b/test/component/Elsa.Workflows.ComponentTests/Scenarios/HttpWorkflows/RouteDataTests.cs new file mode 100644 index 000000000..db9ad4cb5 --- /dev/null +++ b/test/component/Elsa.Workflows.ComponentTests/Scenarios/HttpWorkflows/RouteDataTests.cs @@ -0,0 +1,15 @@ +using Elsa.Workflows.ComponentTests.Abstractions; +using Elsa.Workflows.ComponentTests.Fixtures; + +namespace Elsa.Workflows.ComponentTests.Scenarios.HttpWorkflows; + +public class RouteDataTests(App app) : AppComponentTest(app) +{ + [Fact] + public async Task RouteDataWorkflow_ShouldRespondWithRouteData() + { + var client = WorkflowServer.CreateHttpWorkflowClient(); + var response = await client.GetStringAsync("orders/42"); + Assert.Equal("42", response); + } +} \ No newline at end of file diff --git a/test/component/Elsa.Workflows.ComponentTests/Scenarios/HttpWorkflows/Workflows/RouteDataWorkflow.cs b/test/component/Elsa.Workflows.ComponentTests/Scenarios/HttpWorkflows/Workflows/RouteDataWorkflow.cs new file mode 100644 index 000000000..fad91b435 --- /dev/null +++ b/test/component/Elsa.Workflows.ComponentTests/Scenarios/HttpWorkflows/Workflows/RouteDataWorkflow.cs @@ -0,0 +1,32 @@ +using Elsa.Http; +using Elsa.Workflows.Activities; +using Microsoft.AspNetCore.Http; + +namespace Elsa.Workflows.ComponentTests.Scenarios.HttpWorkflows.Workflows; + +public class RouteDataWorkflow : WorkflowBase +{ + protected override void Build(IWorkflowBuilder builder) + { + var routeDataVariable = builder.WithVariable>(); + + builder.Root = new Sequence + { + Activities = + [ + new HttpEndpoint + { + Path = new("orders/{id}"), + SupportedMethods = new([HttpMethods.Get]), + CanStartWorkflow = true, + RouteData = new(routeDataVariable) + }, + new WriteHttpResponse + { + Content = new(context => routeDataVariable.Get(context)!["id"].ToString()), + ContentType = new("text/plain") + } + ] + }; + } +} \ No newline at end of file