mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-05-03 02:32: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
112 lines
3.9 KiB
C#
112 lines
3.9 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();
|
|
}
|
|
|
|
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>();
|
|
}
|
|
}
|
|
}
|