using System.Collections.Generic; using Microsoft.Extensions.Options; using Moq; using Shouldly; using TestStack.BDDfy; using Xunit; namespace Ocelot.UnitTests.Configuration { using Library.Builder; using Library.Configuration; using Library.Configuration.Yaml; using Library.Responses; public class OcelotConfigurationTests { private readonly Mock> _yamlConfig; private readonly Mock _validator; private OcelotConfiguration _config; private YamlConfiguration _yamlConfiguration; public OcelotConfigurationTests() { _validator = new Mock(); _yamlConfig = new Mock>(); } [Fact] public void should_create_template_pattern_that_matches_anything_to_end_of_string() { this.Given(x => x.GivenTheYamlConfigIs(new YamlConfiguration { ReRoutes = new List { new YamlReRoute { UpstreamTemplate = "/api/products/{productId}", DownstreamTemplate = "/products/{productId}", UpstreamHttpMethod = "Get" } } })) .And(x => x.GivenTheYamlConfigIsValid()) .When(x => x.WhenIInstanciateTheOcelotConfig()) .Then(x => x.ThenTheReRoutesAre(new List { new ReRouteBuilder() .WithDownstreamTemplate("/products/{productId}") .WithUpstreamTemplate("/api/products/{productId}") .WithUpstreamHttpMethod("Get") .WithUpstreamTemplatePattern("/api/products/.*$") .Build() })) .BDDfy(); } [Fact] public void should_create_template_pattern_that_matches_more_than_one_placeholder() { this.Given(x => x.GivenTheYamlConfigIs(new YamlConfiguration { ReRoutes = new List { new YamlReRoute { UpstreamTemplate = "/api/products/{productId}/variants/{variantId}", DownstreamTemplate = "/products/{productId}", UpstreamHttpMethod = "Get" } } })) .And(x => x.GivenTheYamlConfigIsValid()) .When(x => x.WhenIInstanciateTheOcelotConfig()) .Then(x => x.ThenTheReRoutesAre(new List { new ReRouteBuilder() .WithDownstreamTemplate("/products/{productId}") .WithUpstreamTemplate("/api/products/{productId}/variants/{variantId}") .WithUpstreamHttpMethod("Get") .WithUpstreamTemplatePattern("/api/products/.*/variants/.*$") .Build() })) .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 { new YamlReRoute { UpstreamTemplate = "/api/products/{productId}/variants/{variantId}/", DownstreamTemplate = "/products/{productId}", UpstreamHttpMethod = "Get" } } })) .And(x => x.GivenTheYamlConfigIsValid()) .When(x => x.WhenIInstanciateTheOcelotConfig()) .Then(x => x.ThenTheReRoutesAre(new List { new ReRouteBuilder() .WithDownstreamTemplate("/products/{productId}") .WithUpstreamTemplate("/api/products/{productId}/variants/{variantId}/") .WithUpstreamHttpMethod("Get") .WithUpstreamTemplatePattern("/api/products/.*/variants/.*/$") .Build() })) .BDDfy(); } [Fact] public void should_create_template_pattern_that_matches_to_end_of_string() { this.Given(x => x.GivenTheYamlConfigIs(new YamlConfiguration { ReRoutes = new List { new YamlReRoute { UpstreamTemplate = "/", DownstreamTemplate = "/api/products/", UpstreamHttpMethod = "Get" } } })) .And(x => x.GivenTheYamlConfigIsValid()) .When(x => x.WhenIInstanciateTheOcelotConfig()) .Then(x => x.ThenTheReRoutesAre(new List { new ReRouteBuilder() .WithDownstreamTemplate("/api/products/") .WithUpstreamTemplate("/") .WithUpstreamHttpMethod("Get") .WithUpstreamTemplatePattern("/$") .Build() })) .BDDfy(); } private void GivenTheYamlConfigIsValid() { _validator .Setup(x => x.IsValid(It.IsAny())) .Returns(new OkResponse(new ConfigurationValidationResult(false))); } private void GivenTheYamlConfigIs(YamlConfiguration yamlConfiguration) { _yamlConfiguration = yamlConfiguration; _yamlConfig .Setup(x => x.Value) .Returns(_yamlConfiguration); } private void WhenIInstanciateTheOcelotConfig() { _config = new OcelotConfiguration(_yamlConfig.Object, _validator.Object); } private void ThenTheReRoutesAre(List 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); } } } }