Skip to content

Commit

Permalink
more tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
TomPallister committed Apr 13, 2020
1 parent 6c20b11 commit b7cfac9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/features/headerstransformation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Ocelot allows placeholders that can be used in header transformation.
{BaseUrl} - This will use Ocelot's base url e.g. http://localhost:5000 as its value.
{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.
{TraceId} - This will use the Butterfly APM Trace Id. This only works for DownstreamHeaderTransform at the moment.
{UpstreamHost} - This will look for the incoming Host header.

Handling 302 Redirects
^^^^^^^^^^^^^^^^^^^^^^
Expand Down
8 changes: 3 additions & 5 deletions src/Ocelot/Infrastructure/Placeholders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Placeholders(IBaseUrlFinder finder, IRequestScopedDataRepository repo, IH

_requestPlaceholders = new Dictionary<string, Func<DownstreamRequest, string>>
{
{ "{DownstreamBaseUrl}", GetDownstreamBaseUrl() }
{ "{DownstreamBaseUrl}", GetDownstreamBaseUrl() },
};
}

Expand Down Expand Up @@ -143,10 +143,8 @@ private Func<Response<string>> GetUpstreamHost()
{
return new OkResponse<string>(upstreamHost.First());
}
else
{
return new ErrorResponse<string>(new CouldNotFindPlaceholderError("{UpstreamHost}"));
}
return new ErrorResponse<string>(new CouldNotFindPlaceholderError("{UpstreamHost}"));
}
catch
{
Expand Down
17 changes: 17 additions & 0 deletions test/Ocelot.UnitTests/Infrastructure/PlaceholdersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,22 @@ public void should_return_upstreamHost()
var result = _placeholders.Get("{UpstreamHost}");
result.Data.ShouldBe(upstreamHost);
}

[Fact]
public void should_return_error_when_finding_upstbecause_Host_not_set()
{
var httpContext = new DefaultHttpContext();
_accessor.Setup(x => x.HttpContext).Returns(httpContext);
var result = _placeholders.Get("{UpstreamHost}");
result.IsError.ShouldBeTrue();
}

[Fact]
public void should_return_error_when_finding_upstream_host_because_exception_thrown()
{
_accessor.Setup(x => x.HttpContext).Throws(new Exception());
var result = _placeholders.Get("{UpstreamHost}");
result.IsError.ShouldBeTrue();
}
}
}

0 comments on commit b7cfac9

Please sign in to comment.