Skip to content

Commit 5a81cce

Browse files
fku-raman-mRaynaldM
authored
Second parameter of Substring is wrong in HeaderFindAndReplaceCreator (#1424)
* Update HeaderFindAndReplaceCreator.cs Second parameter of Substring was wrong. Existing implementation works only for replacements at the beginning of a string (startOfPlaceholder = 0) * Update HeaderFindAndReplaceCreator.cs Remove and Sort Usings * Update HeaderFindAndReplaceCreator.cs: Replace by char Co-authored-by: Raynald Messié <[email protected]> * Update HeaderFindAndReplaceCreator.cs: Split by char * Review * Fix 'length' value for 'Substring' 2nd param * Add unit test to check replacements in the middle --------- Co-authored-by: Raman Maksimchuk <[email protected]> Co-authored-by: Raynald Messié <[email protected]>
1 parent 5dbbbef commit 5a81cce

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/Ocelot/Configuration/Creator/HeaderFindAndReplaceCreator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Ocelot.Configuration.File;
22
using Ocelot.Infrastructure;
33
using Ocelot.Logging;
4-
using Ocelot.Responses;
4+
using Ocelot.Responses;
55

66
namespace Ocelot.Configuration.Creator
77
{
@@ -69,16 +69,16 @@ public HeaderTransformations Create(FileRoute fileRoute)
6969

7070
private Response<HeaderFindAndReplace> Map(KeyValuePair<string, string> input)
7171
{
72-
var findAndReplace = input.Value.Split(",");
72+
var findAndReplace = input.Value.Split(',');
7373

7474
var replace = findAndReplace[1].TrimStart();
7575

7676
var startOfPlaceholder = replace.IndexOf('{', StringComparison.Ordinal);
7777
if (startOfPlaceholder > -1)
7878
{
79-
var endOfPlaceholder = replace.IndexOf("}", startOfPlaceholder, StringComparison.Ordinal);
79+
var endOfPlaceholder = replace.IndexOf('}', startOfPlaceholder);
8080

81-
var placeholder = replace.Substring(startOfPlaceholder, startOfPlaceholder + (endOfPlaceholder + 1));
81+
var placeholder = replace.Substring(startOfPlaceholder, endOfPlaceholder - startOfPlaceholder + 1);
8282

8383
var value = _placeholders.Get(placeholder);
8484

test/Ocelot.UnitTests/Configuration/HeaderFindAndReplaceCreatorTests.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void should_use_base_url_placeholder()
101101
};
102102

103103
this.Given(x => GivenTheRoute(route))
104-
.And(x => GivenTheBaseUrlIs("http://ocelot.com/"))
104+
.And(x => GivenThePlaceholderIs("http://ocelot.com/"))
105105
.When(x => WhenICreate())
106106
.Then(x => ThenTheFollowingDownstreamIsReturned(downstream))
107107
.BDDfy();
@@ -139,7 +139,7 @@ private void ThenTheLoggerIsCalledCorrectly(string message)
139139
_logger.Verify(x => x.LogWarning(message), Times.Once);
140140
}
141141

142-
[Fact]
142+
[Fact]
143143
public void should_use_base_url_partial_placeholder()
144144
{
145145
var route = new FileRoute
@@ -156,12 +156,35 @@ public void should_use_base_url_partial_placeholder()
156156
};
157157

158158
this.Given(x => GivenTheRoute(route))
159-
.And(x => GivenTheBaseUrlIs("http://ocelot.com/"))
159+
.And(x => GivenThePlaceholderIs("http://ocelot.com/"))
160160
.When(x => WhenICreate())
161161
.Then(x => ThenTheFollowingDownstreamIsReturned(downstream))
162162
.BDDfy();
163163
}
164164

165+
[Fact]
166+
public void should_map_with_partial_placeholder_in_the_middle()
167+
{
168+
var route = new FileRoute
169+
{
170+
DownstreamHeaderTransform = new Dictionary<string, string>
171+
{
172+
{"Host-Next", "www.bbc.co.uk, subdomain.{Host}/path"},
173+
},
174+
};
175+
176+
var expected = new List<HeaderFindAndReplace>
177+
{
178+
new("Host-Next", "www.bbc.co.uk", "subdomain.ocelot.next/path", 0),
179+
};
180+
181+
this.Given(x => GivenTheRoute(route))
182+
.And(x => GivenThePlaceholderIs("ocelot.next"))
183+
.When(x => WhenICreate())
184+
.Then(x => ThenTheFollowingDownstreamIsReturned(expected))
185+
.BDDfy();
186+
}
187+
165188
[Fact]
166189
public void should_add_trace_id_header()
167190
{
@@ -176,7 +199,7 @@ public void should_add_trace_id_header()
176199
var expected = new AddHeader("Trace-Id", "{TraceId}");
177200

178201
this.Given(x => GivenTheRoute(route))
179-
.And(x => GivenTheBaseUrlIs("http://ocelot.com/"))
202+
.And(x => GivenThePlaceholderIs("http://ocelot.com/"))
180203
.When(x => WhenICreate())
181204
.Then(x => ThenTheFollowingAddHeaderToDownstreamIsReturned(expected))
182205
.BDDfy();
@@ -220,9 +243,9 @@ public void should_add_upstream_header_as_is_when_no_replacement_is_given()
220243
.BDDfy();
221244
}
222245

223-
private void GivenTheBaseUrlIs(string baseUrl)
246+
private void GivenThePlaceholderIs(string placeholderValue)
224247
{
225-
_placeholders.Setup(x => x.Get(It.IsAny<string>())).Returns(new OkResponse<string>(baseUrl));
248+
_placeholders.Setup(x => x.Get(It.IsAny<string>())).Returns(new OkResponse<string>(placeholderValue));
226249
}
227250

228251
private void GivenTheBaseUrlErrors()

0 commit comments

Comments
 (0)