started implementing service discovery integration

This commit is contained in:
Tom Gardham-Pallister
2017-01-20 19:03:18 +00:00
parent 622c49d057
commit 044b609ea9
16 changed files with 346 additions and 32 deletions

View File

@ -19,6 +19,44 @@ namespace Ocelot.UnitTests.Configuration
_configurationValidator = new FileConfigurationValidator();
}
[Fact]
public void configuration_is_invalid_if_scheme_in_downstream_template()
{
this.Given(x => x.GivenAConfiguration(new FileConfiguration()
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamTemplate = "http://www.bbc.co.uk/api/products/{productId}",
UpstreamTemplate = "http://asdf.com"
}
}
}))
.When(x => x.WhenIValidateTheConfiguration())
.Then(x => x.ThenTheResultIsNotValid())
.BDDfy();
}
[Fact]
public void configuration_is_invalid_if_host_in_downstream_template()
{
this.Given(x => x.GivenAConfiguration(new FileConfiguration()
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamTemplate = "www.bbc.co.uk/api/products/{productId}",
UpstreamTemplate = "http://asdf.com"
}
}
}))
.When(x => x.WhenIValidateTheConfiguration())
.Then(x => x.ThenTheResultIsNotValid())
.BDDfy();
}
[Fact]
public void configuration_is_valid_with_one_reroute()
{
@ -28,7 +66,7 @@ namespace Ocelot.UnitTests.Configuration
{
new FileReRoute
{
DownstreamTemplate = "http://www.bbc.co.uk",
DownstreamTemplate = "/api/products/",
UpstreamTemplate = "http://asdf.com"
}
}
@ -47,7 +85,7 @@ namespace Ocelot.UnitTests.Configuration
{
new FileReRoute
{
DownstreamTemplate = "http://www.bbc.co.uk",
DownstreamTemplate = "/api/products/",
UpstreamTemplate = "http://asdf.com",
AuthenticationOptions = new FileAuthenticationOptions
{
@ -70,7 +108,7 @@ namespace Ocelot.UnitTests.Configuration
{
new FileReRoute
{
DownstreamTemplate = "http://www.bbc.co.uk",
DownstreamTemplate = "/api/products/",
UpstreamTemplate = "http://asdf.com",
AuthenticationOptions = new FileAuthenticationOptions
{
@ -94,7 +132,7 @@ namespace Ocelot.UnitTests.Configuration
{
new FileReRoute
{
DownstreamTemplate = "http://www.bbc.co.uk",
DownstreamTemplate = "/api/products/",
UpstreamTemplate = "http://asdf.com"
},
new FileReRoute

View File

@ -35,6 +35,143 @@ namespace Ocelot.UnitTests.Configuration
_fileConfig.Object, _validator.Object, _configParser.Object, _logger.Object);
}
[Fact]
public void should_use_downstream_host()
{
this.Given(x => x.GivenTheConfigIs(new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamHost = "127.0.0.1",
UpstreamTemplate = "/api/products/{productId}",
DownstreamTemplate = "/products/{productId}",
UpstreamHttpMethod = "Get",
}
},
}))
.And(x => x.GivenTheConfigIsValid())
.When(x => x.WhenICreateTheConfig())
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
{
new ReRouteBuilder()
.WithDownstreamHost("127.0.0.1")
.WithDownstreamTemplate("/products/{productId}")
.WithUpstreamTemplate("/api/products/{productId}")
.WithUpstreamHttpMethod("Get")
.WithUpstreamTemplatePattern("(?i)/api/products/.*/$")
.Build()
}))
.BDDfy();
}
public void should_use_downstream_scheme()
{
this.Given(x => x.GivenTheConfigIs(new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamScheme = "https",
UpstreamTemplate = "/api/products/{productId}",
DownstreamTemplate = "/products/{productId}",
UpstreamHttpMethod = "Get",
}
},
}))
.And(x => x.GivenTheConfigIsValid())
.When(x => x.WhenICreateTheConfig())
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
{
new ReRouteBuilder()
.WithDownstreamScheme("https")
.WithDownstreamTemplate("/products/{productId}")
.WithUpstreamTemplate("/api/products/{productId}")
.WithUpstreamHttpMethod("Get")
.WithUpstreamTemplatePattern("(?i)/api/products/.*/$")
.Build()
}))
.BDDfy();
}
[Fact]
public void should_use_service_discovery_for_downstream_service_host()
{
this.Given(x => x.GivenTheConfigIs(new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
UpstreamTemplate = "/api/products/{productId}",
DownstreamTemplate = "/products/{productId}",
UpstreamHttpMethod = "Get",
ReRouteIsCaseSensitive = false,
ServiceName = "ProductService"
}
},
GlobalConfiguration = new FileGlobalConfiguration
{
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
{
Provider = "consul",
Address = "127.0.0.1"
}
}
}))
.And(x => x.GivenTheConfigIsValid())
.When(x => x.WhenICreateTheConfig())
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
{
new ReRouteBuilder()
.WithDownstreamTemplate("/products/{productId}")
.WithUpstreamTemplate("/api/products/{productId}")
.WithUpstreamHttpMethod("Get")
.WithUpstreamTemplatePattern("(?i)/api/products/.*/$")
.WithServiceName("ProductService")
.WithUseServiceDiscovery(true)
.WithServiceDiscoveryProvider("consul")
.WithServiceDiscoveryAddress("127.0.01")
.Build()
}))
.BDDfy();
}
[Fact]
public void should_not_use_service_discovery_for_downstream_host_url_when_no_service_name()
{
this.Given(x => x.GivenTheConfigIs(new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
UpstreamTemplate = "/api/products/{productId}",
DownstreamTemplate = "/products/{productId}",
UpstreamHttpMethod = "Get",
ReRouteIsCaseSensitive = false,
}
}
}))
.And(x => x.GivenTheConfigIsValid())
.When(x => x.WhenICreateTheConfig())
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
{
new ReRouteBuilder()
.WithDownstreamTemplate("/products/{productId}")
.WithUpstreamTemplate("/api/products/{productId}")
.WithUpstreamHttpMethod("Get")
.WithUpstreamTemplatePattern("(?i)/api/products/.*/$")
.WithUseServiceDiscovery(false)
.WithServiceDiscoveryProvider(null)
.WithServiceDiscoveryAddress(null)
.Build()
}))
.BDDfy();
}
[Fact]
public void should_use_reroute_case_sensitivity_value()
{

View File

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Moq;
using Ocelot.Configuration;
using Ocelot.Configuration.Creator;