mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 03:58:15 +08:00
Remove Ocelot specific Middleware to make Ocelot more compatible with kestrel middleware and get ready for YARP
This commit is contained in:
@ -1,7 +1,3 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using Ocelot.Middleware;
|
||||
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@ -19,8 +15,13 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
|
||||
public class LoadBalancerMiddlewareTests
|
||||
{
|
||||
@ -34,21 +35,22 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
private LoadBalancingMiddleware _middleware;
|
||||
private DownstreamContext _downstreamContext;
|
||||
private OcelotRequestDelegate _next;
|
||||
private RequestDelegate _next;
|
||||
private HttpContext _httpContext;
|
||||
private Mock<IRequestScopedDataRepository> _repo;
|
||||
|
||||
public LoadBalancerMiddlewareTests()
|
||||
{
|
||||
_repo = new Mock<IRequestScopedDataRepository>();
|
||||
_httpContext = new DefaultHttpContext();
|
||||
_loadBalancerHouse = new Mock<ILoadBalancerHouse>();
|
||||
_loadBalancer = new Mock<ILoadBalancer>();
|
||||
_loadBalancerHouse = new Mock<ILoadBalancerHouse>();
|
||||
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "http://test.com/");
|
||||
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_loggerFactory.Setup(x => x.CreateLogger<LoadBalancingMiddleware>()).Returns(_logger.Object);
|
||||
_next = context => Task.CompletedTask;
|
||||
_downstreamContext.DownstreamRequest = new DownstreamRequest(_downstreamRequest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -133,34 +135,34 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private void WhenICallTheMiddleware()
|
||||
{
|
||||
_middleware = new LoadBalancingMiddleware(_next, _loggerFactory.Object, _loadBalancerHouse.Object);
|
||||
_middleware.Invoke(_downstreamContext).GetAwaiter().GetResult();
|
||||
_middleware.Invoke(_httpContext).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(ServiceProviderConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
var configuration = new InternalConfiguration(null, null, config, null, null, null, null, null, null);
|
||||
_downstreamContext.Configuration = configuration;
|
||||
_httpContext.Items.SetIInternalConfiguration(configuration);
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamUrlIs(string downstreamUrl)
|
||||
{
|
||||
_downstreamRequest.RequestUri = new System.Uri(downstreamUrl);
|
||||
_downstreamContext.DownstreamRequest = new DownstreamRequest(_downstreamRequest);
|
||||
_downstreamRequest.RequestUri = new Uri(downstreamUrl);
|
||||
_httpContext.Items.UpsertDownstreamRequest(new DownstreamRequest(_downstreamRequest));
|
||||
}
|
||||
|
||||
private void GivenTheLoadBalancerReturnsAnError()
|
||||
{
|
||||
_getHostAndPortError = new ErrorResponse<ServiceHostAndPort>(new List<Error>() { new ServicesAreNullError($"services were null for bah") });
|
||||
_loadBalancer
|
||||
.Setup(x => x.Lease(It.IsAny<DownstreamContext>()))
|
||||
.Setup(x => x.Lease(It.IsAny<HttpContext>()))
|
||||
.ReturnsAsync(_getHostAndPortError);
|
||||
}
|
||||
|
||||
private void GivenTheLoadBalancerReturnsOk()
|
||||
{
|
||||
_loadBalancer
|
||||
.Setup(x => x.Lease(It.IsAny<DownstreamContext>()))
|
||||
.Setup(x => x.Lease(It.IsAny<HttpContext>()))
|
||||
.ReturnsAsync(new OkResponse<ServiceHostAndPort>(new ServiceHostAndPort("abc", 123, "https")));
|
||||
}
|
||||
|
||||
@ -168,14 +170,14 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
_hostAndPort = new ServiceHostAndPort("127.0.0.1", 80);
|
||||
_loadBalancer
|
||||
.Setup(x => x.Lease(It.IsAny<DownstreamContext>()))
|
||||
.Setup(x => x.Lease(It.IsAny<HttpContext>()))
|
||||
.ReturnsAsync(new OkResponse<ServiceHostAndPort>(_hostAndPort));
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamReRoute downstreamRoute, List<Ocelot.DownstreamRouteFinder.UrlMatcher.PlaceholderNameAndValue> placeholder)
|
||||
{
|
||||
_downstreamContext.TemplatePlaceholderNameAndValues = placeholder;
|
||||
_downstreamContext.DownstreamReRoute = downstreamRoute;
|
||||
_httpContext.Items.UpsertTemplatePlaceholderNameAndValues(placeholder);
|
||||
_httpContext.Items.UpsertDownstreamReRoute(downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheLoadBalancerHouseReturns()
|
||||
@ -199,32 +201,32 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
private void ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline()
|
||||
{
|
||||
_downstreamContext.IsError.ShouldBeTrue();
|
||||
_downstreamContext.Errors.ShouldBe(_getLoadBalancerHouseError.Errors);
|
||||
_httpContext.Items.Errors().Count.ShouldBeGreaterThan(0);
|
||||
_httpContext.Items.Errors().ShouldBe(_getLoadBalancerHouseError.Errors);
|
||||
}
|
||||
|
||||
private void ThenAnErrorSayingReleaseFailedIsSetOnThePipeline()
|
||||
{
|
||||
_downstreamContext.IsError.ShouldBeTrue();
|
||||
_downstreamContext.Errors.ShouldBe(It.IsAny<List<Error>>());
|
||||
_httpContext.Items.Errors().Count.ShouldBeGreaterThan(0);
|
||||
_httpContext.Items.Errors().ShouldBe(It.IsAny<List<Error>>());
|
||||
}
|
||||
|
||||
private void ThenAnErrorStatingHostAndPortCouldNotBeFoundIsSetOnPipeline()
|
||||
{
|
||||
_downstreamContext.IsError.ShouldBeTrue();
|
||||
_downstreamContext.Errors.ShouldBe(_getHostAndPortError.Errors);
|
||||
_httpContext.Items.Errors().Count.ShouldBeGreaterThan(0);
|
||||
_httpContext.Items.Errors().ShouldBe(_getHostAndPortError.Errors);
|
||||
}
|
||||
|
||||
private void ThenAnHostAndPortIsSetOnPipeline()
|
||||
{
|
||||
_downstreamContext.DownstreamRequest.Host.ShouldBeEquivalentTo("abc");
|
||||
_downstreamContext.DownstreamRequest.Port.ShouldBeEquivalentTo(123);
|
||||
_downstreamContext.DownstreamRequest.Scheme.ShouldBeEquivalentTo("https");
|
||||
_httpContext.Items.DownstreamRequest().Host.ShouldBeEquivalentTo("abc");
|
||||
_httpContext.Items.DownstreamRequest().Port.ShouldBeEquivalentTo(123);
|
||||
_httpContext.Items.DownstreamRequest().Scheme.ShouldBeEquivalentTo("https");
|
||||
}
|
||||
|
||||
private void ThenTheDownstreamUrlIsReplacedWith(string expectedUri)
|
||||
{
|
||||
_downstreamContext.DownstreamRequest.ToHttpRequestMessage().RequestUri.OriginalString.ShouldBe(expectedUri);
|
||||
_httpContext.Items.DownstreamRequest().ToHttpRequestMessage().RequestUri.OriginalString.ShouldBe(expectedUri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user