mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 20:08:17 +08:00
Rename all ReRoute to Route to move closer to YARP +semver: breaking
This commit is contained in:
@ -3,7 +3,6 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Request.Middleware;
|
||||
using Ocelot.Responses;
|
||||
@ -16,7 +15,7 @@
|
||||
|
||||
public class IPSecurityPolicyTests
|
||||
{
|
||||
private readonly DownstreamReRouteBuilder _downstreamReRouteBuilder;
|
||||
private readonly DownstreamRouteBuilder _downstreamRouteBuilder;
|
||||
private readonly IPSecurityPolicy _ipSecurityPolicy;
|
||||
private Response response;
|
||||
private HttpContext _httpContext;
|
||||
@ -26,14 +25,14 @@
|
||||
_httpContext = new DefaultHttpContext();
|
||||
_httpContext.Items.UpsertDownstreamRequest(new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com")));
|
||||
_httpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.1")[0];
|
||||
_downstreamReRouteBuilder = new DownstreamReRouteBuilder();
|
||||
_downstreamRouteBuilder = new DownstreamRouteBuilder();
|
||||
_ipSecurityPolicy = new IPSecurityPolicy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_No_blocked_Ip_and_allowed_Ip()
|
||||
{
|
||||
this.Given(x => x.GivenSetDownstreamReRoute())
|
||||
this.Given(x => x.GivenSetDownstreamRoute())
|
||||
.When(x => x.WhenTheSecurityPolicy())
|
||||
.Then(x => x.ThenSecurityPassing())
|
||||
.BDDfy();
|
||||
@ -44,7 +43,7 @@
|
||||
{
|
||||
_httpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.1")[0];
|
||||
this.Given(x => x.GivenSetBlockedIP())
|
||||
.Given(x => x.GivenSetDownstreamReRoute())
|
||||
.Given(x => x.GivenSetDownstreamRoute())
|
||||
.When(x => x.WhenTheSecurityPolicy())
|
||||
.Then(x => x.ThenNotSecurityPassing())
|
||||
.BDDfy();
|
||||
@ -55,7 +54,7 @@
|
||||
{
|
||||
_httpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.2")[0];
|
||||
this.Given(x => x.GivenSetBlockedIP())
|
||||
.Given(x => x.GivenSetDownstreamReRoute())
|
||||
.Given(x => x.GivenSetDownstreamRoute())
|
||||
.When(x => x.WhenTheSecurityPolicy())
|
||||
.Then(x => x.ThenSecurityPassing())
|
||||
.BDDfy();
|
||||
@ -66,7 +65,7 @@
|
||||
{
|
||||
_httpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.1")[0];
|
||||
this.Given(x => x.GivenSetAllowedIP())
|
||||
.Given(x => x.GivenSetDownstreamReRoute())
|
||||
.Given(x => x.GivenSetDownstreamRoute())
|
||||
.When(x => x.WhenTheSecurityPolicy())
|
||||
.Then(x => x.ThenSecurityPassing())
|
||||
.BDDfy();
|
||||
@ -77,7 +76,7 @@
|
||||
{
|
||||
_httpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.2")[0];
|
||||
this.Given(x => x.GivenSetAllowedIP())
|
||||
.Given(x => x.GivenSetDownstreamReRoute())
|
||||
.Given(x => x.GivenSetDownstreamRoute())
|
||||
.When(x => x.WhenTheSecurityPolicy())
|
||||
.Then(x => x.ThenNotSecurityPassing())
|
||||
.BDDfy();
|
||||
@ -85,22 +84,22 @@
|
||||
|
||||
private void GivenSetAllowedIP()
|
||||
{
|
||||
_downstreamReRouteBuilder.WithSecurityOptions(new SecurityOptions(new List<string> { "192.168.1.1" }, new List<string>()));
|
||||
_downstreamRouteBuilder.WithSecurityOptions(new SecurityOptions(new List<string> { "192.168.1.1" }, new List<string>()));
|
||||
}
|
||||
|
||||
private void GivenSetBlockedIP()
|
||||
{
|
||||
_downstreamReRouteBuilder.WithSecurityOptions(new SecurityOptions(new List<string>(), new List<string> { "192.168.1.1" }));
|
||||
_downstreamRouteBuilder.WithSecurityOptions(new SecurityOptions(new List<string>(), new List<string> { "192.168.1.1" }));
|
||||
}
|
||||
|
||||
private void GivenSetDownstreamReRoute()
|
||||
private void GivenSetDownstreamRoute()
|
||||
{
|
||||
_httpContext.Items.UpsertDownstreamReRoute(_downstreamReRouteBuilder.Build());
|
||||
_httpContext.Items.UpsertDownstreamRoute(_downstreamRouteBuilder.Build());
|
||||
}
|
||||
|
||||
private void WhenTheSecurityPolicy()
|
||||
{
|
||||
response = _ipSecurityPolicy.Security(_httpContext.Items.DownstreamReRoute(), _httpContext).GetAwaiter().GetResult();
|
||||
response = _ipSecurityPolicy.Security(_httpContext.Items.DownstreamRoute(), _httpContext).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
private void ThenSecurityPassing()
|
||||
|
@ -15,11 +15,11 @@ using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Security
|
||||
{
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Shouldly;
|
||||
|
||||
{
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Shouldly;
|
||||
|
||||
public class SecurityMiddlewareTests
|
||||
{
|
||||
private List<Mock<ISecurityPolicy>> _securityPolicyList;
|
||||
@ -42,7 +42,7 @@ namespace Ocelot.UnitTests.Security
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
_middleware = new SecurityMiddleware(_next, _loggerFactory.Object, _securityPolicyList.Select(f => f.Object).ToList());
|
||||
_middleware = new SecurityMiddleware(_next, _loggerFactory.Object, _securityPolicyList.Select(f => f.Object).ToList());
|
||||
_httpContext.Items.UpsertDownstreamRequest(new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com")));
|
||||
}
|
||||
|
||||
@ -69,7 +69,7 @@ namespace Ocelot.UnitTests.Security
|
||||
foreach (var item in _securityPolicyList)
|
||||
{
|
||||
Response response = new OkResponse();
|
||||
item.Setup(x => x.Security(_httpContext.Items.DownstreamReRoute(), _httpContext)).Returns(Task.FromResult(response));
|
||||
item.Setup(x => x.Security(_httpContext.Items.DownstreamRoute(), _httpContext)).Returns(Task.FromResult(response));
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,12 +82,12 @@ namespace Ocelot.UnitTests.Security
|
||||
{
|
||||
Error error = new UnauthenticatedError($"Not passing security verification");
|
||||
Response response = new ErrorResponse(error);
|
||||
item.Setup(x => x.Security(_httpContext.Items.DownstreamReRoute(), _httpContext)).Returns(Task.FromResult(response));
|
||||
item.Setup(x => x.Security(_httpContext.Items.DownstreamRoute(), _httpContext)).Returns(Task.FromResult(response));
|
||||
}
|
||||
else
|
||||
{
|
||||
Response response = new OkResponse();
|
||||
item.Setup(x => x.Security(_httpContext.Items.DownstreamReRoute(), _httpContext)).Returns(Task.FromResult(response));
|
||||
item.Setup(x => x.Security(_httpContext.Items.DownstreamRoute(), _httpContext)).Returns(Task.FromResult(response));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -98,12 +98,12 @@ namespace Ocelot.UnitTests.Security
|
||||
}
|
||||
|
||||
private void ThenTheRequestIsPassingSecurity()
|
||||
{
|
||||
{
|
||||
_httpContext.Items.Errors().Count.ShouldBe(0);
|
||||
}
|
||||
|
||||
private void ThenTheRequestIsNotPassingSecurity()
|
||||
{
|
||||
{
|
||||
_httpContext.Items.Errors().Count.ShouldBeGreaterThan(0);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user