mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 14:02:49 +08:00
Merge branch 'samirsyed-master'
This commit is contained in:
commit
c004c43ff2
@ -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
|
||||
^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
@ -7,6 +7,7 @@ namespace Ocelot.Infrastructure
|
||||
using Ocelot.Responses;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class Placeholders : IPlaceholders
|
||||
{
|
||||
@ -25,12 +26,13 @@ namespace Ocelot.Infrastructure
|
||||
{
|
||||
{ "{BaseUrl}", GetBaseUrl() },
|
||||
{ "{TraceId}", GetTraceId() },
|
||||
{ "{RemoteIpAddress}", GetRemoteIpAddress() }
|
||||
{ "{RemoteIpAddress}", GetRemoteIpAddress() },
|
||||
{ "{UpstreamHost}", GetUpstreamHost() },
|
||||
};
|
||||
|
||||
_requestPlaceholders = new Dictionary<string, Func<DownstreamRequest, string>>
|
||||
{
|
||||
{ "{DownstreamBaseUrl}", GetDownstreamBaseUrl() }
|
||||
{ "{DownstreamBaseUrl}", GetDownstreamBaseUrl() },
|
||||
};
|
||||
}
|
||||
|
||||
@ -130,5 +132,25 @@ namespace Ocelot.Infrastructure
|
||||
{
|
||||
return () => new OkResponse<string>(_finder.Find());
|
||||
}
|
||||
|
||||
private Func<Response<string>> GetUpstreamHost()
|
||||
{
|
||||
return () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_httpContextAccessor.HttpContext.Request.Headers.TryGetValue("Host", out var upstreamHost))
|
||||
{
|
||||
return new OkResponse<string>(upstreamHost.First());
|
||||
}
|
||||
|
||||
return new ErrorResponse<string>(new CouldNotFindPlaceholderError("{UpstreamHost}"));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new ErrorResponse<string>(new CouldNotFindPlaceholderError("{UpstreamHost}"));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -123,5 +123,33 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
result.IsError.ShouldBeTrue();
|
||||
result.Errors[0].Message.ShouldBe("Unable to remove placeholder: {Test}, placeholder does not exists");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_upstreamHost()
|
||||
{
|
||||
var upstreamHost = "UpstreamHostA";
|
||||
var httpContext = new DefaultHttpContext();
|
||||
httpContext.Request.Headers.Add("Host", upstreamHost);
|
||||
_accessor.Setup(x => x.HttpContext).Returns(httpContext);
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user