mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-05-01 21:42:50 +08:00

* #212 - hacked websockets proxy together * faffing around * #212 hacking away :( * #212 websockets proxy middleware working * #212 map when for webockets working * #212 some test refactor * #212 temp commit * #212 websockets proxy working, tests passing...need to do some tidying and write docs * #212 more code coverage * #212 docs for websockets * #212 updated readme * #212 tidying up after websockets refactoring * #212 tidying up after websockets refactoring * #212 tidying up after websockets refactoring * stuck a warning in about logging levels into docs!
127 lines
4.8 KiB
C#
127 lines
4.8 KiB
C#
using Moq;
|
|
using Ocelot.Configuration;
|
|
using Ocelot.Configuration.Builder;
|
|
using Ocelot.LoadBalancer.LoadBalancers;
|
|
using Ocelot.ServiceDiscovery;
|
|
using Shouldly;
|
|
using System.Collections.Generic;
|
|
using Ocelot.ServiceDiscovery.Providers;
|
|
using TestStack.BDDfy;
|
|
using Xunit;
|
|
|
|
namespace Ocelot.UnitTests.LoadBalancer
|
|
{
|
|
public class LoadBalancerFactoryTests
|
|
{
|
|
private DownstreamReRoute _reRoute;
|
|
private LoadBalancerFactory _factory;
|
|
private ILoadBalancer _result;
|
|
private Mock<IServiceDiscoveryProviderFactory> _serviceProviderFactory;
|
|
private Mock<IServiceDiscoveryProvider> _serviceProvider;
|
|
private ServiceProviderConfiguration _serviceProviderConfig;
|
|
|
|
public LoadBalancerFactoryTests()
|
|
{
|
|
_serviceProviderFactory = new Mock<IServiceDiscoveryProviderFactory>();
|
|
_serviceProvider = new Mock<IServiceDiscoveryProvider>();
|
|
_factory = new LoadBalancerFactory(_serviceProviderFactory.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_no_load_balancer()
|
|
{
|
|
var reRoute = new DownstreamReRouteBuilder()
|
|
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
.Build();
|
|
|
|
this.Given(x => x.GivenAReRoute(reRoute))
|
|
.And(x => GivenAServiceProviderConfig(new ServiceProviderConfigurationBuilder().Build()))
|
|
.And(x => x.GivenTheServiceProviderFactoryReturns())
|
|
.When(x => x.WhenIGetTheLoadBalancer())
|
|
.Then(x => x.ThenTheLoadBalancerIsReturned<NoLoadBalancer>())
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_round_robin_load_balancer()
|
|
{
|
|
var reRoute = new DownstreamReRouteBuilder()
|
|
.WithLoadBalancer("RoundRobin")
|
|
.WithUpstreamHttpMethod(new List<string> {"Get"})
|
|
.Build();
|
|
|
|
this.Given(x => x.GivenAReRoute(reRoute))
|
|
.And(x => GivenAServiceProviderConfig(new ServiceProviderConfigurationBuilder().Build()))
|
|
.And(x => x.GivenTheServiceProviderFactoryReturns())
|
|
.When(x => x.WhenIGetTheLoadBalancer())
|
|
.Then(x => x.ThenTheLoadBalancerIsReturned<RoundRobin>())
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_round_least_connection_balancer()
|
|
{
|
|
var reRoute = new DownstreamReRouteBuilder()
|
|
.WithLoadBalancer("LeastConnection")
|
|
.WithUpstreamHttpMethod(new List<string> {"Get"})
|
|
.Build();
|
|
|
|
this.Given(x => x.GivenAReRoute(reRoute))
|
|
.And(x => GivenAServiceProviderConfig(new ServiceProviderConfigurationBuilder().Build()))
|
|
.And(x => x.GivenTheServiceProviderFactoryReturns())
|
|
.When(x => x.WhenIGetTheLoadBalancer())
|
|
.Then(x => x.ThenTheLoadBalancerIsReturned<LeastConnection>())
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_call_service_provider()
|
|
{
|
|
var reRoute = new DownstreamReRouteBuilder()
|
|
.WithLoadBalancer("RoundRobin")
|
|
.WithUpstreamHttpMethod(new List<string> {"Get"})
|
|
.Build();
|
|
|
|
this.Given(x => x.GivenAReRoute(reRoute))
|
|
.And(x => GivenAServiceProviderConfig(new ServiceProviderConfigurationBuilder().Build()))
|
|
.And(x => x.GivenTheServiceProviderFactoryReturns())
|
|
.When(x => x.WhenIGetTheLoadBalancer())
|
|
.Then(x => x.ThenTheServiceProviderIsCalledCorrectly())
|
|
.BDDfy();
|
|
}
|
|
|
|
private void GivenAServiceProviderConfig(ServiceProviderConfiguration serviceProviderConfig)
|
|
{
|
|
_serviceProviderConfig = serviceProviderConfig;
|
|
}
|
|
|
|
private void GivenTheServiceProviderFactoryReturns()
|
|
{
|
|
_serviceProviderFactory
|
|
.Setup(x => x.Get(It.IsAny<ServiceProviderConfiguration>(), It.IsAny<DownstreamReRoute>()))
|
|
.Returns(_serviceProvider.Object);
|
|
}
|
|
|
|
private void ThenTheServiceProviderIsCalledCorrectly()
|
|
{
|
|
_serviceProviderFactory
|
|
.Verify(x => x.Get(It.IsAny<ServiceProviderConfiguration>(), It.IsAny<DownstreamReRoute>()), Times.Once);
|
|
}
|
|
|
|
private void GivenAReRoute(DownstreamReRoute reRoute)
|
|
{
|
|
_reRoute = reRoute;
|
|
}
|
|
|
|
private void WhenIGetTheLoadBalancer()
|
|
{
|
|
_result = _factory.Get(_reRoute, _serviceProviderConfig).Result;
|
|
}
|
|
|
|
private void ThenTheLoadBalancerIsReturned<T>()
|
|
{
|
|
_result.ShouldBeOfType<T>();
|
|
}
|
|
}
|
|
}
|