mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-29 08:22:51 +08:00

* test for issue * added service fabric sample * working!! * changed sample naming to Ocelot * removed files we dont need * removed files we dont need * updated sample gitignore * updated sample gitignore * getting ocelot to work with service fabric using the reverse proxy * #238 - added support for service fabric discovery provider, proxies requests through naming service, wont work on partioned service fabric services yet * #238 - Manually tested service fabric using sample..all seems OK. Made some changes after testing, added docs * #238 - added docs for servic fabric
130 lines
4.6 KiB
C#
130 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Moq;
|
|
using Ocelot.Configuration;
|
|
using Ocelot.Configuration.Builder;
|
|
using Ocelot.Logging;
|
|
using Ocelot.ServiceDiscovery;
|
|
using Shouldly;
|
|
using TestStack.BDDfy;
|
|
using Xunit;
|
|
|
|
namespace Ocelot.UnitTests.ServiceDiscovery
|
|
{
|
|
public class ServiceProviderFactoryTests
|
|
{
|
|
private ServiceProviderConfiguration _serviceConfig;
|
|
private IServiceDiscoveryProvider _result;
|
|
private readonly ServiceDiscoveryProviderFactory _factory;
|
|
private DownstreamReRoute _reRoute;
|
|
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
|
|
|
public ServiceProviderFactoryTests()
|
|
{
|
|
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
|
_factory = new ServiceDiscoveryProviderFactory(_loggerFactory.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_no_service_provider()
|
|
{
|
|
var serviceConfig = new ServiceProviderConfigurationBuilder()
|
|
.Build();
|
|
|
|
var reRoute = new DownstreamReRouteBuilder().Build();
|
|
|
|
this.Given(x => x.GivenTheReRoute(serviceConfig, reRoute))
|
|
.When(x => x.WhenIGetTheServiceProvider())
|
|
.Then(x => x.ThenTheServiceProviderIs<ConfigurationServiceProvider>())
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_list_of_configuration_services()
|
|
{
|
|
var serviceConfig = new ServiceProviderConfigurationBuilder()
|
|
.Build();
|
|
|
|
var downstreamAddresses = new List<DownstreamHostAndPort>()
|
|
{
|
|
new DownstreamHostAndPort("asdf.com", 80),
|
|
new DownstreamHostAndPort("abc.com", 80)
|
|
};
|
|
|
|
var reRoute = new DownstreamReRouteBuilder().WithDownstreamAddresses(downstreamAddresses).Build();
|
|
|
|
this.Given(x => x.GivenTheReRoute(serviceConfig, reRoute))
|
|
.When(x => x.WhenIGetTheServiceProvider())
|
|
.Then(x => x.ThenTheServiceProviderIs<ConfigurationServiceProvider>())
|
|
.Then(x => ThenTheFollowingServicesAreReturned(downstreamAddresses))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_consul_service_provider()
|
|
{
|
|
var reRoute = new DownstreamReRouteBuilder()
|
|
.WithServiceName("product")
|
|
.WithUseServiceDiscovery(true)
|
|
.Build();
|
|
|
|
var serviceConfig = new ServiceProviderConfigurationBuilder()
|
|
.Build();
|
|
|
|
this.Given(x => x.GivenTheReRoute(serviceConfig, reRoute))
|
|
.When(x => x.WhenIGetTheServiceProvider())
|
|
.Then(x => x.ThenTheServiceProviderIs<ConsulServiceDiscoveryProvider>())
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_service_fabric_provider()
|
|
{
|
|
var reRoute = new DownstreamReRouteBuilder()
|
|
.WithServiceName("product")
|
|
.WithUseServiceDiscovery(true)
|
|
.Build();
|
|
|
|
var serviceConfig = new ServiceProviderConfigurationBuilder()
|
|
.WithServiceDiscoveryProviderType("ServiceFabric")
|
|
.Build();
|
|
|
|
this.Given(x => x.GivenTheReRoute(serviceConfig, reRoute))
|
|
.When(x => x.WhenIGetTheServiceProvider())
|
|
.Then(x => x.ThenTheServiceProviderIs<ServiceFabricServiceDiscoveryProvider>())
|
|
.BDDfy();
|
|
}
|
|
|
|
private void ThenTheFollowingServicesAreReturned(List<DownstreamHostAndPort> downstreamAddresses)
|
|
{
|
|
var result = (ConfigurationServiceProvider)_result;
|
|
var services = result.Get().Result;
|
|
|
|
for (int i = 0; i < services.Count; i++)
|
|
{
|
|
var service = services[i];
|
|
var downstreamAddress = downstreamAddresses[i];
|
|
|
|
service.HostAndPort.DownstreamHost.ShouldBe(downstreamAddress.Host);
|
|
service.HostAndPort.DownstreamPort.ShouldBe(downstreamAddress.Port);
|
|
}
|
|
}
|
|
|
|
private void GivenTheReRoute(ServiceProviderConfiguration serviceConfig, DownstreamReRoute reRoute)
|
|
{
|
|
_serviceConfig = serviceConfig;
|
|
_reRoute = reRoute;
|
|
}
|
|
|
|
private void WhenIGetTheServiceProvider()
|
|
{
|
|
_result = _factory.Get(_serviceConfig, _reRoute);
|
|
}
|
|
|
|
private void ThenTheServiceProviderIs<T>()
|
|
{
|
|
_result.ShouldBeOfType<T>();
|
|
}
|
|
}
|
|
}
|