Ocelot/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs
Tom Pallister 1e2e953b2c
Feature/automatic routes with sd (#351)
* #340 started looking at supporting automatic routing when using service discovery

* #340 getting old routing tests to pass

* #340 renamed stuff to provider rather than finder, as its not longer finding anything

* #340 working towards supporting dynamic routing

* #340 loads of refactoring to make configuration work with dynamic routing

* #340 refactor consul config code so the registry class owns it

* #340 default to consul to maintain backwards compat

* #340 added docs, finished this branches todos
2018-05-14 21:26:10 +01:00

115 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.Configuration.Repository;
using Ocelot.Responses;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Configuration
{
public class InMemoryConfigurationRepositoryTests
{
private readonly InMemoryInternalConfigurationRepository _repo;
private IInternalConfiguration _config;
private Response _result;
private Response<IInternalConfiguration> _getResult;
public InMemoryConfigurationRepositoryTests()
{
_repo = new InMemoryInternalConfigurationRepository();
}
[Fact]
public void can_add_config()
{
this.Given(x => x.GivenTheConfigurationIs(new FakeConfig("initial", "adminath")))
.When(x => x.WhenIAddOrReplaceTheConfig())
.Then(x => x.ThenNoErrorsAreReturned())
.BDDfy();
}
[Fact]
public void can_get_config()
{
this.Given(x => x.GivenThereIsASavedConfiguration())
.When(x => x.WhenIGetTheConfiguration())
.Then(x => x.ThenTheConfigurationIsReturned())
.BDDfy();
}
private void ThenTheConfigurationIsReturned()
{
_getResult.Data.ReRoutes[0].DownstreamReRoute[0].DownstreamPathTemplate.Value.ShouldBe("initial");
}
private void WhenIGetTheConfiguration()
{
_getResult = _repo.Get();
}
private void GivenThereIsASavedConfiguration()
{
GivenTheConfigurationIs(new FakeConfig("initial", "adminath"));
WhenIAddOrReplaceTheConfig();
}
private void GivenTheConfigurationIs(IInternalConfiguration config)
{
_config = config;
}
private void WhenIAddOrReplaceTheConfig()
{
_result = _repo.AddOrReplace(_config);
}
private void ThenNoErrorsAreReturned()
{
_result.IsError.ShouldBeFalse();
}
class FakeConfig : IInternalConfiguration
{
private readonly string _downstreamTemplatePath;
public FakeConfig(string downstreamTemplatePath, string administrationPath)
{
_downstreamTemplatePath = downstreamTemplatePath;
AdministrationPath = administrationPath;
}
public List<ReRoute> ReRoutes
{
get
{
var downstreamReRoute = new DownstreamReRouteBuilder()
.WithDownstreamPathTemplate(_downstreamTemplatePath)
.WithUpstreamHttpMethod(new List<string> { "Get" })
.Build();
return new List<ReRoute>
{
new ReRouteBuilder()
.WithDownstreamReRoute(downstreamReRoute)
.WithUpstreamHttpMethod(new List<string> {"Get"})
.Build()
};
}
}
public string AdministrationPath {get;}
public ServiceProviderConfiguration ServiceProviderConfiguration => throw new NotImplementedException();
public string RequestId {get;}
public LoadBalancerOptions LoadBalancerOptions { get; }
public string DownstreamScheme { get; }
public QoSOptions QoSOptions { get; }
public HttpHandlerOptions HttpHandlerOptions { get; }
}
}
}