Skip to content

Commit b7cfac9

Browse files
committed
more tests and docs
1 parent 6c20b11 commit b7cfac9

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

docs/features/headerstransformation.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Ocelot allows placeholders that can be used in header transformation.
8585
{BaseUrl} - This will use Ocelot's base url e.g. http://localhost:5000 as its value.
8686
{DownstreamBaseUrl} - This will use the downstream services base url e.g. http://localhost:5000 as its value. This only works for DownstreamHeaderTransform at the moment.
8787
{TraceId} - This will use the Butterfly APM Trace Id. This only works for DownstreamHeaderTransform at the moment.
88+
{UpstreamHost} - This will look for the incoming Host header.
8889

8990
Handling 302 Redirects
9091
^^^^^^^^^^^^^^^^^^^^^^

src/Ocelot/Infrastructure/Placeholders.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public Placeholders(IBaseUrlFinder finder, IRequestScopedDataRepository repo, IH
3232

3333
_requestPlaceholders = new Dictionary<string, Func<DownstreamRequest, string>>
3434
{
35-
{ "{DownstreamBaseUrl}", GetDownstreamBaseUrl() }
35+
{ "{DownstreamBaseUrl}", GetDownstreamBaseUrl() },
3636
};
3737
}
3838

@@ -143,10 +143,8 @@ private Func<Response<string>> GetUpstreamHost()
143143
{
144144
return new OkResponse<string>(upstreamHost.First());
145145
}
146-
else
147-
{
148-
return new ErrorResponse<string>(new CouldNotFindPlaceholderError("{UpstreamHost}"));
149-
}
146+
147+
return new ErrorResponse<string>(new CouldNotFindPlaceholderError("{UpstreamHost}"));
150148
}
151149
catch
152150
{

test/Ocelot.UnitTests/Infrastructure/PlaceholdersTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,22 @@ public void should_return_upstreamHost()
134134
var result = _placeholders.Get("{UpstreamHost}");
135135
result.Data.ShouldBe(upstreamHost);
136136
}
137+
138+
[Fact]
139+
public void should_return_error_when_finding_upstbecause_Host_not_set()
140+
{
141+
var httpContext = new DefaultHttpContext();
142+
_accessor.Setup(x => x.HttpContext).Returns(httpContext);
143+
var result = _placeholders.Get("{UpstreamHost}");
144+
result.IsError.ShouldBeTrue();
145+
}
146+
147+
[Fact]
148+
public void should_return_error_when_finding_upstream_host_because_exception_thrown()
149+
{
150+
_accessor.Setup(x => x.HttpContext).Throws(new Exception());
151+
var result = _placeholders.Get("{UpstreamHost}");
152+
result.IsError.ShouldBeTrue();
153+
}
137154
}
138155
}

0 commit comments

Comments
 (0)