mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
Added some configuration validation stuff but then realised probablt wont need this for a while
This commit is contained in:
parent
484270edbc
commit
87702141e2
@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Ocelot.Library.Infrastructure.Configuration
|
||||||
|
{
|
||||||
|
public class ConfigurationValidationResult
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
|
|
||||||
|
namespace Ocelot.Library.Infrastructure.Configuration
|
||||||
|
{
|
||||||
|
public class ConfigurationValidator : IConfigurationValidator
|
||||||
|
{
|
||||||
|
public Response<ConfigurationValidationResult> IsValid(Configuration configuration)
|
||||||
|
{
|
||||||
|
var duplicateUpstreamTemplates = configuration.ReRoutes
|
||||||
|
.Select(r => r.DownstreamTemplate)
|
||||||
|
.GroupBy(r => r)
|
||||||
|
.Where(r => r.Count() > 1)
|
||||||
|
.Select(r => r.Key)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
if (duplicateUpstreamTemplates.Count <= 0)
|
||||||
|
{
|
||||||
|
return new OkResponse<ConfigurationValidationResult>(new ConfigurationValidationResult());
|
||||||
|
}
|
||||||
|
|
||||||
|
var errors = new List<Error>();
|
||||||
|
|
||||||
|
foreach (var duplicateUpstreamTemplate in duplicateUpstreamTemplates)
|
||||||
|
{
|
||||||
|
var error = new DownstreamTemplateAlreadyUsedError(string.Format("Duplicate DownstreamTemplate: {0}",
|
||||||
|
duplicateUpstreamTemplate));
|
||||||
|
errors.Add(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ErrorResponse<ConfigurationValidationResult>(errors);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
|
|
||||||
|
namespace Ocelot.Library.Infrastructure.Configuration
|
||||||
|
{
|
||||||
|
public class DownstreamTemplateAlreadyUsedError : Error
|
||||||
|
{
|
||||||
|
public DownstreamTemplateAlreadyUsedError(string message) : base(message)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
|
|
||||||
|
namespace Ocelot.Library.Infrastructure.Configuration
|
||||||
|
{
|
||||||
|
public interface IConfigurationValidator
|
||||||
|
{
|
||||||
|
Response<ConfigurationValidationResult> IsValid(Configuration configuration);
|
||||||
|
}
|
||||||
|
}
|
84
test/Ocelot.UnitTests/ConfigurationValidationTests.cs
Normal file
84
test/Ocelot.UnitTests/ConfigurationValidationTests.cs
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using Ocelot.Library.Infrastructure.Configuration;
|
||||||
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
|
using Shouldly;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Ocelot.UnitTests
|
||||||
|
{
|
||||||
|
public class ConfigurationValidationTests
|
||||||
|
{
|
||||||
|
private Configuration _configuration;
|
||||||
|
private readonly IConfigurationValidator _configurationValidator;
|
||||||
|
private Response<ConfigurationValidationResult> _result;
|
||||||
|
|
||||||
|
public ConfigurationValidationTests()
|
||||||
|
{
|
||||||
|
_configurationValidator = new ConfigurationValidator();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void configuration_is_valid_with_one_reroute()
|
||||||
|
{
|
||||||
|
this.Given(x => x.GivenAConfiguration(new Configuration()
|
||||||
|
{
|
||||||
|
ReRoutes = new List<ReRoute>
|
||||||
|
{
|
||||||
|
new ReRoute
|
||||||
|
{
|
||||||
|
DownstreamTemplate = "http://www.bbc.co.uk",
|
||||||
|
UpstreamTemplate = "http://asdf.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
.When(x => x.WhenIValidateTheConfiguration())
|
||||||
|
.Then(x => x.ThenTheResultIsValid())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void configuration_is_not_valid_with_duplicate_reroutes()
|
||||||
|
{
|
||||||
|
this.Given(x => x.GivenAConfiguration(new Configuration()
|
||||||
|
{
|
||||||
|
ReRoutes = new List<ReRoute>
|
||||||
|
{
|
||||||
|
new ReRoute
|
||||||
|
{
|
||||||
|
DownstreamTemplate = "http://www.bbc.co.uk",
|
||||||
|
UpstreamTemplate = "http://asdf.com"
|
||||||
|
},
|
||||||
|
new ReRoute
|
||||||
|
{
|
||||||
|
DownstreamTemplate = "http://www.bbc.co.uk",
|
||||||
|
UpstreamTemplate = "http://lol.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
.When(x => x.WhenIValidateTheConfiguration())
|
||||||
|
.Then(x => x.ThenTheResultIsNotValid())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenAConfiguration(Configuration configuration)
|
||||||
|
{
|
||||||
|
_configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenIValidateTheConfiguration()
|
||||||
|
{
|
||||||
|
_result = _configurationValidator.IsValid(_configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheResultIsValid()
|
||||||
|
{
|
||||||
|
_result.IsError.ShouldBeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheResultIsNotValid()
|
||||||
|
{
|
||||||
|
_result.IsError.ShouldBeTrue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user