mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 07:38:14 +08:00
regex for url match, means annoying constructor ocelot configuration object but cant work out a better way to do this at the moment
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Library.Infrastructure.Configuration;
|
||||
using Ocelot.Library.Infrastructure.Configuration.Yaml;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
@ -9,7 +9,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ConfigurationValidationTests
|
||||
{
|
||||
private Library.Infrastructure.Configuration.Configuration _configuration;
|
||||
private YamlConfiguration _yamlConfiguration;
|
||||
private readonly IConfigurationValidator _configurationValidator;
|
||||
private Response<ConfigurationValidationResult> _result;
|
||||
|
||||
@ -21,11 +21,11 @@ namespace Ocelot.UnitTests.Configuration
|
||||
[Fact]
|
||||
public void configuration_is_valid_with_one_reroute()
|
||||
{
|
||||
this.Given(x => x.GivenAConfiguration(new Library.Infrastructure.Configuration.Configuration()
|
||||
this.Given(x => x.GivenAConfiguration(new YamlConfiguration()
|
||||
{
|
||||
ReRoutes = new List<ReRoute>
|
||||
ReRoutes = new List<YamlReRoute>
|
||||
{
|
||||
new ReRoute
|
||||
new YamlReRoute
|
||||
{
|
||||
DownstreamTemplate = "http://www.bbc.co.uk",
|
||||
UpstreamTemplate = "http://asdf.com"
|
||||
@ -40,16 +40,16 @@ namespace Ocelot.UnitTests.Configuration
|
||||
[Fact]
|
||||
public void configuration_is_not_valid_with_duplicate_reroutes()
|
||||
{
|
||||
this.Given(x => x.GivenAConfiguration(new Library.Infrastructure.Configuration.Configuration()
|
||||
this.Given(x => x.GivenAConfiguration(new YamlConfiguration()
|
||||
{
|
||||
ReRoutes = new List<ReRoute>
|
||||
ReRoutes = new List<YamlReRoute>
|
||||
{
|
||||
new ReRoute
|
||||
new YamlReRoute
|
||||
{
|
||||
DownstreamTemplate = "http://www.bbc.co.uk",
|
||||
UpstreamTemplate = "http://asdf.com"
|
||||
},
|
||||
new ReRoute
|
||||
new YamlReRoute
|
||||
{
|
||||
DownstreamTemplate = "http://www.bbc.co.uk",
|
||||
UpstreamTemplate = "http://lol.com"
|
||||
@ -62,14 +62,14 @@ namespace Ocelot.UnitTests.Configuration
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAConfiguration(Library.Infrastructure.Configuration.Configuration configuration)
|
||||
private void GivenAConfiguration(YamlConfiguration yamlConfiguration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_yamlConfiguration = yamlConfiguration;
|
||||
}
|
||||
|
||||
private void WhenIValidateTheConfiguration()
|
||||
{
|
||||
_result = _configurationValidator.IsValid(_configuration);
|
||||
_result = _configurationValidator.IsValid(_yamlConfiguration);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsValid()
|
||||
|
142
test/Ocelot.UnitTests/Configuration/OcelotConfigurationTests.cs
Normal file
142
test/Ocelot.UnitTests/Configuration/OcelotConfigurationTests.cs
Normal file
@ -0,0 +1,142 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using Ocelot.Library.Infrastructure.Configuration;
|
||||
using Ocelot.Library.Infrastructure.Configuration.Yaml;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class OcelotConfigurationTests
|
||||
{
|
||||
private readonly Mock<IOptions<YamlConfiguration>> _yamlConfig;
|
||||
private OcelotConfiguration _config;
|
||||
private YamlConfiguration _yamlConfiguration;
|
||||
|
||||
public OcelotConfigurationTests()
|
||||
{
|
||||
_yamlConfig = new Mock<IOptions<YamlConfiguration>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_anything_to_end_of_string()
|
||||
{
|
||||
this.Given(x => x.GivenTheYamlConfigIs(new YamlConfiguration
|
||||
{
|
||||
ReRoutes = new List<YamlReRoute>
|
||||
{
|
||||
new YamlReRoute
|
||||
{
|
||||
UpstreamTemplate = "/api/products/{productId}",
|
||||
DownstreamTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = "Get"
|
||||
}
|
||||
}
|
||||
}))
|
||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||
{
|
||||
new ReRoute("/products/{productId}","/api/products/{productId}", "Get", "/api/products/.*$")
|
||||
}))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_more_than_one_placeholder()
|
||||
{
|
||||
this.Given(x => x.GivenTheYamlConfigIs(new YamlConfiguration
|
||||
{
|
||||
ReRoutes = new List<YamlReRoute>
|
||||
{
|
||||
new YamlReRoute
|
||||
{
|
||||
UpstreamTemplate = "/api/products/{productId}/variants/{variantId}",
|
||||
DownstreamTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = "Get"
|
||||
}
|
||||
}
|
||||
}))
|
||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||
{
|
||||
new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}", "Get", "/api/products/.*/variants/.*$")
|
||||
}))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_more_than_one_placeholder_with_trailing_slash()
|
||||
{
|
||||
this.Given(x => x.GivenTheYamlConfigIs(new YamlConfiguration
|
||||
{
|
||||
ReRoutes = new List<YamlReRoute>
|
||||
{
|
||||
new YamlReRoute
|
||||
{
|
||||
UpstreamTemplate = "/api/products/{productId}/variants/{variantId}/",
|
||||
DownstreamTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = "Get"
|
||||
}
|
||||
}
|
||||
}))
|
||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||
{
|
||||
new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}/", "Get", "/api/products/.*/variants/.*/$")
|
||||
}))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_to_end_of_string()
|
||||
{
|
||||
this.Given(x => x.GivenTheYamlConfigIs(new YamlConfiguration
|
||||
{
|
||||
ReRoutes = new List<YamlReRoute>
|
||||
{
|
||||
new YamlReRoute
|
||||
{
|
||||
UpstreamTemplate = "/",
|
||||
DownstreamTemplate = "/api/products/",
|
||||
UpstreamHttpMethod = "Get"
|
||||
}
|
||||
}
|
||||
}))
|
||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||
{
|
||||
new ReRoute("/api/products/","/", "Get", "/$")
|
||||
}))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheYamlConfigIs(YamlConfiguration yamlConfiguration)
|
||||
{
|
||||
_yamlConfiguration = yamlConfiguration;
|
||||
_yamlConfig
|
||||
.Setup(x => x.Value)
|
||||
.Returns(_yamlConfiguration);
|
||||
}
|
||||
|
||||
private void WhenIInstanciateTheOcelotConfig()
|
||||
{
|
||||
_config = new OcelotConfiguration(_yamlConfig.Object);
|
||||
}
|
||||
|
||||
private void ThenTheReRoutesAre(List<ReRoute> expectedReRoutes)
|
||||
{
|
||||
for (int i = 0; i < _config.ReRoutes.Count; i++)
|
||||
{
|
||||
var result = _config.ReRoutes[i];
|
||||
var expected = expectedReRoutes[i];
|
||||
|
||||
result.DownstreamTemplate.ShouldBe(expected.DownstreamTemplate);
|
||||
result.UpstreamHttpMethod.ShouldBe(expected.UpstreamHttpMethod);
|
||||
result.UpstreamTemplate.ShouldBe(expected.UpstreamTemplate);
|
||||
result.UpstreamTemplatePattern.ShouldBe(expected.UpstreamTemplatePattern);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user