Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ public UpstreamPathTemplate Create(IRoute route)
if (upstreamTemplate.Contains('?'))
{
containsQueryString = true;
upstreamTemplate = upstreamTemplate.Replace("?", "(|\\?)");

if (upstreamTemplate.Contains("/?"))
{
upstreamTemplate = upstreamTemplate.Replace("/?", "(\\/|\\?|$)");
}
else if (upstreamTemplate.Contains('?'))
{
upstreamTemplate = upstreamTemplate.Replace("?", "(\\/|\\?|$)");
}
}

for (var i = 0; i < placeholders.Count; i++)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File;
using Ocelot.Values;
using System.Text.RegularExpressions;

namespace Ocelot.UnitTests.Configuration;

Expand Down Expand Up @@ -215,7 +216,7 @@ public void should_create_template_pattern_that_matches_query_string()

this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
.When(x => x.WhenICreateTheTemplatePattern())
.Then(x => x.ThenTheFollowingIsReturned($"^(?i)/api/subscriptions/[^/]+/updates(|\\?)unitId={MatchEverything}$"))
.Then(x => x.ThenTheFollowingIsReturned($"^(?i)/api/subscriptions/[^/]+/updates(\\/|\\?|$)unitId={MatchEverything}$"))
.And(x => ThenThePriorityIs(1))
.BDDfy();
}
Expand All @@ -230,11 +231,31 @@ public void should_create_template_pattern_that_matches_query_string_with_multip

this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
.When(x => x.WhenICreateTheTemplatePattern())
.Then(x => x.ThenTheFollowingIsReturned($"^(?i)/api/subscriptions/[^/]+/updates(|\\?)unitId={MatchEverything}&productId={MatchEverything}$"))
.Then(x => x.ThenTheFollowingIsReturned($"^(?i)/api/subscriptions/[^/]+/updates(\\/|\\?|$)unitId={MatchEverything}&productId={MatchEverything}$"))
.And(x => ThenThePriorityIs(1))
.BDDfy();
}

[Theory]
[InlineData(@"/api/v1/abc?{everything}", false)]
[InlineData(@"/api/v1/abc2/{everything}", true)]
public void ShouldCreateCorrectTemplatePattern(string urlPath, bool shouldMatch)
{
// Arrange
var creator = new UpstreamTemplatePatternCreator();
var fileRoute = new FileRoute
{
UpstreamPathTemplate = urlPath,
};

// Act
var result = creator.Create(fileRoute);
var match = Regex.Match("/api/v1/abc2/apple", result.Template);

// Assert
Assert.Equal(shouldMatch, match.Success);
}

private void GivenTheFollowingFileRoute(FileRoute fileRoute)
{
_fileRoute = fileRoute;
Expand Down