mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-05-02 21:22:51 +08:00

* started messing around with this on the train last night * mega hacking away to change middleware into Ocelot iddleware * scoped data repo back in * broken commit getting tests working * another broken commit farting around with tests * all unit tests passing again * mw pipeline for ocelot...still loads of hacks but getting there now to get acceptance tests working, then fix config so you can have aggregate and then imlement multiplexer, then mapping to response...loads to do * all tests passing before aggregation feature implemented * removed all the request middleware stuff we dont need it * updated how errors work...tho i think there could be edge case here when aggregating because one downstream could error and this would effect another * removed multiplexer so you dont have to send route down, this isnt very thread safe...sigh * hacking around getting the config for aggregates in, this might change * refactored builder and unit tests passing now * Updated a bunch of ports for tests * plugged in code to create reroutes that are aggregates * made multiplexer a class * hacked test to death * simple aggregator done, initial validation done * removed request id from context, it is still specific for http request * now aggregates to json always * docs for aggregate reroutes * Updated docs
200 lines
8.6 KiB
C#
200 lines
8.6 KiB
C#
using Ocelot.Middleware;
|
|
|
|
namespace Ocelot.UnitTests.LoadBalancer
|
|
{
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Moq;
|
|
using Ocelot.Configuration;
|
|
using Ocelot.Configuration.Builder;
|
|
using Ocelot.Configuration.Provider;
|
|
using Ocelot.DownstreamRouteFinder;
|
|
using Ocelot.DownstreamRouteFinder.Middleware;
|
|
using Ocelot.Errors;
|
|
using Ocelot.LoadBalancer.LoadBalancers;
|
|
using Ocelot.LoadBalancer.Middleware;
|
|
using Ocelot.Logging;
|
|
using Ocelot.Responses;
|
|
using Ocelot.Values;
|
|
using Shouldly;
|
|
using TestStack.BDDfy;
|
|
using Xunit;
|
|
|
|
public class LoadBalancerMiddlewareTests
|
|
{
|
|
private readonly Mock<ILoadBalancerHouse> _loadBalancerHouse;
|
|
private readonly Mock<ILoadBalancer> _loadBalancer;
|
|
private ServiceHostAndPort _hostAndPort;
|
|
private OkResponse<DownstreamRoute> _downstreamRoute;
|
|
private ErrorResponse<ILoadBalancer> _getLoadBalancerHouseError;
|
|
private ErrorResponse<ServiceHostAndPort> _getHostAndPortError;
|
|
private HttpRequestMessage _downstreamRequest;
|
|
private ServiceProviderConfiguration _config;
|
|
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
|
private Mock<IOcelotLogger> _logger;
|
|
private LoadBalancingMiddleware _middleware;
|
|
private DownstreamContext _downstreamContext;
|
|
private OcelotRequestDelegate _next;
|
|
|
|
public LoadBalancerMiddlewareTests()
|
|
{
|
|
_loadBalancerHouse = new Mock<ILoadBalancerHouse>();
|
|
_loadBalancer = new Mock<ILoadBalancer>();
|
|
_loadBalancerHouse = new Mock<ILoadBalancerHouse>();
|
|
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "");
|
|
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
|
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
|
_logger = new Mock<IOcelotLogger>();
|
|
_loggerFactory.Setup(x => x.CreateLogger<LoadBalancingMiddleware>()).Returns(_logger.Object);
|
|
_next = async context => {
|
|
//do nothing
|
|
};
|
|
_downstreamContext.DownstreamRequest = _downstreamRequest;
|
|
}
|
|
|
|
[Fact]
|
|
public void should_call_scoped_data_repository_correctly()
|
|
{
|
|
var downstreamRoute = new DownstreamReRouteBuilder()
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build();
|
|
|
|
var serviceProviderConfig = new ServiceProviderConfigurationBuilder()
|
|
.Build();
|
|
|
|
this.Given(x => x.GivenTheDownStreamUrlIs("http://my.url/abc?q=123"))
|
|
.And(x => GivenTheConfigurationIs(serviceProviderConfig))
|
|
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute, new List<Ocelot.DownstreamRouteFinder.UrlMatcher.PlaceholderNameAndValue>()))
|
|
.And(x => x.GivenTheLoadBalancerHouseReturns())
|
|
.And(x => x.GivenTheLoadBalancerReturns())
|
|
.When(x => x.WhenICallTheMiddleware())
|
|
.Then(x => x.ThenTheDownstreamUrlIsReplacedWith("http://127.0.0.1:80/abc?q=123"))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_set_pipeline_error_if_cannot_get_load_balancer()
|
|
{
|
|
var downstreamRoute = new DownstreamReRouteBuilder()
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build();
|
|
|
|
var serviceProviderConfig = new ServiceProviderConfigurationBuilder()
|
|
.Build();
|
|
|
|
this.Given(x => x.GivenTheDownStreamUrlIs("http://my.url/abc?q=123"))
|
|
.And(x => GivenTheConfigurationIs(serviceProviderConfig))
|
|
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute, new List<Ocelot.DownstreamRouteFinder.UrlMatcher.PlaceholderNameAndValue>()))
|
|
.And(x => x.GivenTheLoadBalancerHouseReturnsAnError())
|
|
.When(x => x.WhenICallTheMiddleware())
|
|
.Then(x => x.ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline())
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_set_pipeline_error_if_cannot_get_least()
|
|
{
|
|
var downstreamRoute = new DownstreamReRouteBuilder()
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build();
|
|
|
|
var serviceProviderConfig = new ServiceProviderConfigurationBuilder()
|
|
.Build();
|
|
|
|
this.Given(x => x.GivenTheDownStreamUrlIs("http://my.url/abc?q=123"))
|
|
.And(x => GivenTheConfigurationIs(serviceProviderConfig))
|
|
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute, new List<Ocelot.DownstreamRouteFinder.UrlMatcher.PlaceholderNameAndValue>()))
|
|
.And(x => x.GivenTheLoadBalancerHouseReturns())
|
|
.And(x => x.GivenTheLoadBalancerReturnsAnError())
|
|
.When(x => x.WhenICallTheMiddleware())
|
|
.Then(x => x.ThenAnErrorStatingHostAndPortCouldNotBeFoundIsSetOnPipeline())
|
|
.BDDfy();
|
|
}
|
|
|
|
private void WhenICallTheMiddleware()
|
|
{
|
|
_middleware = new LoadBalancingMiddleware(_next, _loggerFactory.Object, _loadBalancerHouse.Object);
|
|
_middleware.Invoke(_downstreamContext).GetAwaiter().GetResult();
|
|
}
|
|
|
|
private void GivenTheConfigurationIs(ServiceProviderConfiguration config)
|
|
{
|
|
_config = config;
|
|
_downstreamContext.ServiceProviderConfiguration = config;
|
|
}
|
|
|
|
private void GivenTheDownStreamUrlIs(string downstreamUrl)
|
|
{
|
|
_downstreamRequest.RequestUri = new System.Uri(downstreamUrl);
|
|
}
|
|
|
|
private void GivenTheLoadBalancerReturnsAnError()
|
|
{
|
|
_getHostAndPortError = new ErrorResponse<ServiceHostAndPort>(new List<Error>() { new ServicesAreNullError($"services were null for bah") });
|
|
_loadBalancer
|
|
.Setup(x => x.Lease())
|
|
.ReturnsAsync(_getHostAndPortError);
|
|
}
|
|
|
|
private void GivenTheLoadBalancerReturns()
|
|
{
|
|
_hostAndPort = new ServiceHostAndPort("127.0.0.1", 80);
|
|
_loadBalancer
|
|
.Setup(x => x.Lease())
|
|
.ReturnsAsync(new OkResponse<ServiceHostAndPort>(_hostAndPort));
|
|
}
|
|
|
|
private void GivenTheDownStreamRouteIs(DownstreamReRoute downstreamRoute, List<Ocelot.DownstreamRouteFinder.UrlMatcher.PlaceholderNameAndValue> placeholder)
|
|
{
|
|
_downstreamContext.TemplatePlaceholderNameAndValues = placeholder;
|
|
_downstreamContext.DownstreamReRoute = downstreamRoute;
|
|
}
|
|
|
|
private void GivenTheLoadBalancerHouseReturns()
|
|
{
|
|
_loadBalancerHouse
|
|
.Setup(x => x.Get(It.IsAny<DownstreamReRoute>(), It.IsAny<ServiceProviderConfiguration>()))
|
|
.ReturnsAsync(new OkResponse<ILoadBalancer>(_loadBalancer.Object));
|
|
}
|
|
|
|
private void GivenTheLoadBalancerHouseReturnsAnError()
|
|
{
|
|
_getLoadBalancerHouseError = new ErrorResponse<ILoadBalancer>(new List<Ocelot.Errors.Error>()
|
|
{
|
|
new UnableToFindLoadBalancerError($"unabe to find load balancer for bah")
|
|
});
|
|
|
|
_loadBalancerHouse
|
|
.Setup(x => x.Get(It.IsAny<DownstreamReRoute>(), It.IsAny<ServiceProviderConfiguration>()))
|
|
.ReturnsAsync(_getLoadBalancerHouseError);
|
|
}
|
|
|
|
private void ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline()
|
|
{
|
|
_downstreamContext.IsError.ShouldBeTrue();
|
|
_downstreamContext.Errors.ShouldBe(_getLoadBalancerHouseError.Errors);
|
|
}
|
|
|
|
private void ThenAnErrorSayingReleaseFailedIsSetOnThePipeline()
|
|
{
|
|
_downstreamContext.IsError.ShouldBeTrue();
|
|
_downstreamContext.Errors.ShouldBe(It.IsAny<List<Error>>());
|
|
}
|
|
|
|
private void ThenAnErrorStatingHostAndPortCouldNotBeFoundIsSetOnPipeline()
|
|
{
|
|
_downstreamContext.IsError.ShouldBeTrue();
|
|
_downstreamContext.Errors.ShouldBe(_getHostAndPortError.Errors);
|
|
|
|
}
|
|
|
|
private void ThenTheDownstreamUrlIsReplacedWith(string expectedUri)
|
|
{
|
|
_downstreamContext.DownstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
|
|
}
|
|
}
|
|
}
|