mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-20 01:02:50 +08:00
tests passing
This commit is contained in:
parent
32551624bb
commit
17b0555f55
@ -1,21 +1,21 @@
|
|||||||
namespace Ocelot.Provider.Eureka
|
namespace Ocelot.Provider.Eureka
|
||||||
{
|
{
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Ocelot.ServiceDiscovery;
|
using Ocelot.ServiceDiscovery;
|
||||||
using Steeltoe.Discovery;
|
using Steeltoe.Discovery;
|
||||||
|
|
||||||
public static class EurekaProviderFactory
|
public static class EurekaProviderFactory
|
||||||
{
|
{
|
||||||
public static ServiceDiscoveryFinderDelegate Get = (provider, config, route) =>
|
public static ServiceDiscoveryFinderDelegate Get = (provider, config, route) =>
|
||||||
{
|
{
|
||||||
var client = provider.GetService<IDiscoveryClient>();
|
var client = provider.GetService<IDiscoveryClient>();
|
||||||
|
|
||||||
if (config.Type?.ToLower() == "eureka" && client != null)
|
if (config.Type?.ToLower() == "eureka" && client != null)
|
||||||
{
|
{
|
||||||
return new Eureka(route.ServiceName, client);
|
return new Eureka(route.ServiceName, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Steeltoe.Discovery.ClientCore" Version="3.0.1" />
|
<PackageReference Include="Steeltoe.Discovery.ClientCore" Version="3.0.1" />
|
||||||
|
<PackageReference Include="Steeltoe.Discovery.Eureka" Version="3.0.1" />
|
||||||
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164">
|
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
namespace Ocelot.AcceptanceTests
|
namespace Ocelot.AcceptanceTests
|
||||||
{
|
{
|
||||||
using Configuration.File;
|
using Ocelot.Configuration.File;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Steeltoe.Common.Discovery;
|
using Steeltoe.Common.Discovery;
|
||||||
@ -38,24 +38,24 @@
|
|||||||
var configuration = new FileConfiguration
|
var configuration = new FileConfiguration
|
||||||
{
|
{
|
||||||
Routes = new List<FileRoute>
|
Routes = new List<FileRoute>
|
||||||
|
{
|
||||||
|
new FileRoute
|
||||||
{
|
{
|
||||||
new FileRoute
|
DownstreamPathTemplate = "/",
|
||||||
{
|
DownstreamScheme = "http",
|
||||||
DownstreamPathTemplate = "/",
|
UpstreamPathTemplate = "/",
|
||||||
DownstreamScheme = "http",
|
UpstreamHttpMethod = new List<string> { "Get" },
|
||||||
UpstreamPathTemplate = "/",
|
ServiceName = serviceName,
|
||||||
UpstreamHttpMethod = new List<string> { "Get" },
|
LoadBalancerOptions = new FileLoadBalancerOptions { Type = "LeastConnection" },
|
||||||
ServiceName = serviceName,
|
|
||||||
LoadBalancerOptions = new FileLoadBalancerOptions { Type = "LeastConnection" },
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
},
|
||||||
GlobalConfiguration = new FileGlobalConfiguration()
|
GlobalConfiguration = new FileGlobalConfiguration()
|
||||||
{
|
{
|
||||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||||
{
|
{
|
||||||
Type = "Eureka"
|
Type = "Eureka",
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
this.Given(x => x.GivenEurekaProductServiceOneIsRunning(downstreamServiceOneUrl))
|
this.Given(x => x.GivenEurekaProductServiceOneIsRunning(downstreamServiceOneUrl))
|
||||||
@ -91,42 +91,42 @@
|
|||||||
{
|
{
|
||||||
name = serviceName,
|
name = serviceName,
|
||||||
instance = new List<Instance>
|
instance = new List<Instance>
|
||||||
|
{
|
||||||
|
new Instance
|
||||||
|
{
|
||||||
|
instanceId = $"{serviceInstance.Host}:{serviceInstance}",
|
||||||
|
hostName = serviceInstance.Host,
|
||||||
|
app = serviceName,
|
||||||
|
ipAddr = "127.0.0.1",
|
||||||
|
status = "UP",
|
||||||
|
overriddenstatus = "UNKNOWN",
|
||||||
|
port = new Port {value = serviceInstance.Port, enabled = "true"},
|
||||||
|
securePort = new SecurePort {value = serviceInstance.Port, enabled = "true"},
|
||||||
|
countryId = 1,
|
||||||
|
dataCenterInfo = new DataCenterInfo {value = "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo", name = "MyOwn"},
|
||||||
|
leaseInfo = new LeaseInfo
|
||||||
{
|
{
|
||||||
new Instance
|
renewalIntervalInSecs = 30,
|
||||||
{
|
durationInSecs = 90,
|
||||||
instanceId = $"{serviceInstance.Host}:{serviceInstance}",
|
registrationTimestamp = 1457714988223,
|
||||||
hostName = serviceInstance.Host,
|
lastRenewalTimestamp= 1457716158319,
|
||||||
app = serviceName,
|
evictionTimestamp = 0,
|
||||||
ipAddr = "127.0.0.1",
|
serviceUpTimestamp = 1457714988223,
|
||||||
status = "UP",
|
},
|
||||||
overriddenstatus = "UNKNOWN",
|
metadata = new Metadata
|
||||||
port = new Port {value = serviceInstance.Port, enabled = "true"},
|
{
|
||||||
securePort = new SecurePort {value = serviceInstance.Port, enabled = "true"},
|
value = "java.util.Collections$EmptyMap",
|
||||||
countryId = 1,
|
},
|
||||||
dataCenterInfo = new DataCenterInfo {value = "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo", name = "MyOwn"},
|
homePageUrl = $"{serviceInstance.Host}:{serviceInstance.Port}",
|
||||||
leaseInfo = new LeaseInfo
|
statusPageUrl = $"{serviceInstance.Host}:{serviceInstance.Port}",
|
||||||
{
|
healthCheckUrl = $"{serviceInstance.Host}:{serviceInstance.Port}",
|
||||||
renewalIntervalInSecs = 30,
|
vipAddress = serviceName,
|
||||||
durationInSecs = 90,
|
isCoordinatingDiscoveryServer = "false",
|
||||||
registrationTimestamp = 1457714988223,
|
lastUpdatedTimestamp = "1457714988223",
|
||||||
lastRenewalTimestamp= 1457716158319,
|
lastDirtyTimestamp = "1457714988172",
|
||||||
evictionTimestamp = 0,
|
actionType = "ADDED",
|
||||||
serviceUpTimestamp = 1457714988223
|
},
|
||||||
},
|
},
|
||||||
metadata = new Metadata
|
|
||||||
{
|
|
||||||
value = "java.util.Collections$EmptyMap"
|
|
||||||
},
|
|
||||||
homePageUrl = $"{serviceInstance.Host}:{serviceInstance.Port}",
|
|
||||||
statusPageUrl = $"{serviceInstance.Host}:{serviceInstance.Port}",
|
|
||||||
healthCheckUrl = $"{serviceInstance.Host}:{serviceInstance.Port}",
|
|
||||||
vipAddress = serviceName,
|
|
||||||
isCoordinatingDiscoveryServer = "false",
|
|
||||||
lastUpdatedTimestamp = "1457714988223",
|
|
||||||
lastDirtyTimestamp = "1457714988172",
|
|
||||||
actionType = "ADDED"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
apps.Add(a);
|
apps.Add(a);
|
||||||
@ -138,8 +138,8 @@
|
|||||||
{
|
{
|
||||||
application = apps,
|
application = apps,
|
||||||
apps__hashcode = "UP_1_",
|
apps__hashcode = "UP_1_",
|
||||||
versions__delta = "1"
|
versions__delta = "1",
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
var json = JsonConvert.SerializeObject(applications);
|
var json = JsonConvert.SerializeObject(applications);
|
||||||
|
@ -1,304 +1,304 @@
|
|||||||
namespace Ocelot.UnitTests.Consul
|
namespace Ocelot.UnitTests.Consul
|
||||||
{
|
{
|
||||||
using global::Consul;
|
using global::Consul;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Ocelot.Logging;
|
using Ocelot.Logging;
|
||||||
using Provider.Consul;
|
using Provider.Consul;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Values;
|
using Values;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
public class ConsulServiceDiscoveryProviderTests : IDisposable
|
public class ConsulServiceDiscoveryProviderTests : IDisposable
|
||||||
{
|
{
|
||||||
private IWebHost _fakeConsulBuilder;
|
private IWebHost _fakeConsulBuilder;
|
||||||
private readonly List<ServiceEntry> _serviceEntries;
|
private readonly List<ServiceEntry> _serviceEntries;
|
||||||
private Consul _provider;
|
private Consul _provider;
|
||||||
private readonly string _serviceName;
|
private readonly string _serviceName;
|
||||||
private readonly int _port;
|
private readonly int _port;
|
||||||
private readonly string _consulHost;
|
private readonly string _consulHost;
|
||||||
private readonly string _consulScheme;
|
private readonly string _consulScheme;
|
||||||
private readonly string _fakeConsulServiceDiscoveryUrl;
|
private readonly string _fakeConsulServiceDiscoveryUrl;
|
||||||
private List<Service> _services;
|
private List<Service> _services;
|
||||||
private readonly Mock<IOcelotLoggerFactory> _factory;
|
private readonly Mock<IOcelotLoggerFactory> _factory;
|
||||||
private readonly Mock<IOcelotLogger> _logger;
|
private readonly Mock<IOcelotLogger> _logger;
|
||||||
private string _receivedToken;
|
private string _receivedToken;
|
||||||
private readonly IConsulClientFactory _clientFactory;
|
private readonly IConsulClientFactory _clientFactory;
|
||||||
|
|
||||||
public ConsulServiceDiscoveryProviderTests()
|
public ConsulServiceDiscoveryProviderTests()
|
||||||
{
|
{
|
||||||
_serviceName = "test";
|
_serviceName = "test";
|
||||||
_port = 8500;
|
_port = 8500;
|
||||||
_consulHost = "localhost";
|
_consulHost = "localhost";
|
||||||
_consulScheme = "http";
|
_consulScheme = "http";
|
||||||
_fakeConsulServiceDiscoveryUrl = $"{_consulScheme}://{_consulHost}:{_port}";
|
_fakeConsulServiceDiscoveryUrl = $"{_consulScheme}://{_consulHost}:{_port}";
|
||||||
_serviceEntries = new List<ServiceEntry>();
|
_serviceEntries = new List<ServiceEntry>();
|
||||||
_factory = new Mock<IOcelotLoggerFactory>();
|
_factory = new Mock<IOcelotLoggerFactory>();
|
||||||
_clientFactory = new ConsulClientFactory();
|
_clientFactory = new ConsulClientFactory();
|
||||||
_logger = new Mock<IOcelotLogger>();
|
_logger = new Mock<IOcelotLogger>();
|
||||||
_factory.Setup(x => x.CreateLogger<Consul>()).Returns(_logger.Object);
|
_factory.Setup(x => x.CreateLogger<Consul>()).Returns(_logger.Object);
|
||||||
_factory.Setup(x => x.CreateLogger<PollConsul>()).Returns(_logger.Object);
|
_factory.Setup(x => x.CreateLogger<PollConsul>()).Returns(_logger.Object);
|
||||||
var config = new ConsulRegistryConfiguration(_consulScheme, _consulHost, _port, _serviceName, null);
|
var config = new ConsulRegistryConfiguration(_consulScheme, _consulHost, _port, _serviceName, null);
|
||||||
_provider = new Consul(config, _factory.Object, _clientFactory);
|
_provider = new Consul(config, _factory.Object, _clientFactory);
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_return_service_from_consul()
|
|
||||||
{
|
|
||||||
var serviceEntryOne = new ServiceEntry()
|
|
||||||
{
|
|
||||||
Service = new AgentService()
|
|
||||||
{
|
|
||||||
Service = _serviceName,
|
|
||||||
Address = "localhost",
|
|
||||||
Port = 50881,
|
|
||||||
ID = Guid.NewGuid().ToString(),
|
|
||||||
Tags = new string[0]
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(_fakeConsulServiceDiscoveryUrl, _serviceName))
|
|
||||||
.And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne))
|
|
||||||
.When(x => WhenIGetTheServices())
|
|
||||||
.Then(x => ThenTheCountIs(1))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_use_token()
|
|
||||||
{
|
|
||||||
var token = "test token";
|
|
||||||
var config = new ConsulRegistryConfiguration(_consulScheme, _consulHost, _port, _serviceName, token);
|
|
||||||
_provider = new Consul(config, _factory.Object, _clientFactory);
|
|
||||||
|
|
||||||
var serviceEntryOne = new ServiceEntry()
|
|
||||||
{
|
|
||||||
Service = new AgentService()
|
|
||||||
{
|
|
||||||
Service = _serviceName,
|
|
||||||
Address = "localhost",
|
|
||||||
Port = 50881,
|
|
||||||
ID = Guid.NewGuid().ToString(),
|
|
||||||
Tags = new string[0],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
this.Given(_ => GivenThereIsAFakeConsulServiceDiscoveryProvider(_fakeConsulServiceDiscoveryUrl, _serviceName))
|
|
||||||
.And(_ => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne))
|
|
||||||
.When(_ => WhenIGetTheServices())
|
|
||||||
.Then(_ => ThenTheCountIs(1))
|
|
||||||
.And(_ => ThenTheTokenIs(token))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_not_return_services_with_invalid_address()
|
public void should_return_service_from_consul()
|
||||||
{
|
{
|
||||||
var serviceEntryOne = new ServiceEntry()
|
var serviceEntryOne = new ServiceEntry()
|
||||||
{
|
{
|
||||||
Service = new AgentService()
|
Service = new AgentService()
|
||||||
{
|
{
|
||||||
Service = _serviceName,
|
Service = _serviceName,
|
||||||
Address = "http://localhost",
|
Address = "localhost",
|
||||||
Port = 50881,
|
Port = 50881,
|
||||||
ID = Guid.NewGuid().ToString(),
|
ID = Guid.NewGuid().ToString(),
|
||||||
Tags = new string[0]
|
Tags = new string[0]
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
var serviceEntryTwo = new ServiceEntry()
|
this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(_fakeConsulServiceDiscoveryUrl, _serviceName))
|
||||||
{
|
.And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne))
|
||||||
Service = new AgentService()
|
.When(x => WhenIGetTheServices())
|
||||||
{
|
.Then(x => ThenTheCountIs(1))
|
||||||
Service = _serviceName,
|
.BDDfy();
|
||||||
Address = "http://localhost",
|
}
|
||||||
Port = 50888,
|
|
||||||
ID = Guid.NewGuid().ToString(),
|
[Fact]
|
||||||
Tags = new string[0]
|
public void should_use_token()
|
||||||
},
|
{
|
||||||
};
|
var token = "test token";
|
||||||
|
var config = new ConsulRegistryConfiguration(_consulScheme, _consulHost, _port, _serviceName, token);
|
||||||
this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(_fakeConsulServiceDiscoveryUrl, _serviceName))
|
_provider = new Consul(config, _factory.Object, _clientFactory);
|
||||||
.And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
|
|
||||||
.When(x => WhenIGetTheServices())
|
var serviceEntryOne = new ServiceEntry()
|
||||||
.Then(x => ThenTheCountIs(0))
|
{
|
||||||
.And(x => ThenTheLoggerHasBeenCalledCorrectlyForInvalidAddress())
|
Service = new AgentService()
|
||||||
.BDDfy();
|
{
|
||||||
}
|
Service = _serviceName,
|
||||||
|
Address = "localhost",
|
||||||
[Fact]
|
Port = 50881,
|
||||||
public void should_not_return_services_with_empty_address()
|
ID = Guid.NewGuid().ToString(),
|
||||||
{
|
Tags = new string[0],
|
||||||
var serviceEntryOne = new ServiceEntry()
|
},
|
||||||
{
|
};
|
||||||
Service = new AgentService()
|
|
||||||
{
|
this.Given(_ => GivenThereIsAFakeConsulServiceDiscoveryProvider(_fakeConsulServiceDiscoveryUrl, _serviceName))
|
||||||
Service = _serviceName,
|
.And(_ => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne))
|
||||||
Address = "",
|
.When(_ => WhenIGetTheServices())
|
||||||
Port = 50881,
|
.Then(_ => ThenTheCountIs(1))
|
||||||
ID = Guid.NewGuid().ToString(),
|
.And(_ => ThenTheTokenIs(token))
|
||||||
Tags = new string[0]
|
.BDDfy();
|
||||||
},
|
}
|
||||||
};
|
|
||||||
|
[Fact]
|
||||||
var serviceEntryTwo = new ServiceEntry()
|
public void should_not_return_services_with_invalid_address()
|
||||||
{
|
{
|
||||||
Service = new AgentService()
|
var serviceEntryOne = new ServiceEntry()
|
||||||
{
|
{
|
||||||
Service = _serviceName,
|
Service = new AgentService()
|
||||||
Address = null,
|
{
|
||||||
Port = 50888,
|
Service = _serviceName,
|
||||||
ID = Guid.NewGuid().ToString(),
|
Address = "http://localhost",
|
||||||
Tags = new string[0]
|
Port = 50881,
|
||||||
},
|
ID = Guid.NewGuid().ToString(),
|
||||||
};
|
Tags = new string[0]
|
||||||
|
},
|
||||||
this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(_fakeConsulServiceDiscoveryUrl, _serviceName))
|
};
|
||||||
.And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
|
|
||||||
.When(x => WhenIGetTheServices())
|
var serviceEntryTwo = new ServiceEntry()
|
||||||
.Then(x => ThenTheCountIs(0))
|
{
|
||||||
.And(x => ThenTheLoggerHasBeenCalledCorrectlyForEmptyAddress())
|
Service = new AgentService()
|
||||||
.BDDfy();
|
{
|
||||||
}
|
Service = _serviceName,
|
||||||
|
Address = "http://localhost",
|
||||||
[Fact]
|
Port = 50888,
|
||||||
public void should_not_return_services_with_invalid_port()
|
ID = Guid.NewGuid().ToString(),
|
||||||
{
|
Tags = new string[0]
|
||||||
var serviceEntryOne = new ServiceEntry()
|
},
|
||||||
{
|
};
|
||||||
Service = new AgentService()
|
|
||||||
{
|
this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(_fakeConsulServiceDiscoveryUrl, _serviceName))
|
||||||
Service = _serviceName,
|
.And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
|
||||||
Address = "localhost",
|
.When(x => WhenIGetTheServices())
|
||||||
Port = -1,
|
.Then(x => ThenTheCountIs(0))
|
||||||
ID = Guid.NewGuid().ToString(),
|
.And(x => ThenTheLoggerHasBeenCalledCorrectlyForInvalidAddress())
|
||||||
Tags = new string[0]
|
.BDDfy();
|
||||||
},
|
}
|
||||||
};
|
|
||||||
|
[Fact]
|
||||||
var serviceEntryTwo = new ServiceEntry()
|
public void should_not_return_services_with_empty_address()
|
||||||
{
|
{
|
||||||
Service = new AgentService()
|
var serviceEntryOne = new ServiceEntry()
|
||||||
{
|
{
|
||||||
Service = _serviceName,
|
Service = new AgentService()
|
||||||
Address = "localhost",
|
{
|
||||||
Port = 0,
|
Service = _serviceName,
|
||||||
ID = Guid.NewGuid().ToString(),
|
Address = "",
|
||||||
Tags = new string[0]
|
Port = 50881,
|
||||||
},
|
ID = Guid.NewGuid().ToString(),
|
||||||
};
|
Tags = new string[0]
|
||||||
|
},
|
||||||
this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(_fakeConsulServiceDiscoveryUrl, _serviceName))
|
};
|
||||||
.And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
|
|
||||||
.When(x => WhenIGetTheServices())
|
var serviceEntryTwo = new ServiceEntry()
|
||||||
.Then(x => ThenTheCountIs(0))
|
{
|
||||||
.And(x => ThenTheLoggerHasBeenCalledCorrectlyForInvalidPorts())
|
Service = new AgentService()
|
||||||
.BDDfy();
|
{
|
||||||
}
|
Service = _serviceName,
|
||||||
|
Address = null,
|
||||||
private void ThenTheLoggerHasBeenCalledCorrectlyForInvalidAddress()
|
Port = 50888,
|
||||||
{
|
ID = Guid.NewGuid().ToString(),
|
||||||
_logger.Verify(
|
Tags = new string[0]
|
||||||
x => x.LogWarning(
|
},
|
||||||
"Unable to use service Address: http://localhost and Port: 50881 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
|
};
|
||||||
Times.Once);
|
|
||||||
|
this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(_fakeConsulServiceDiscoveryUrl, _serviceName))
|
||||||
_logger.Verify(
|
.And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
|
||||||
x => x.LogWarning(
|
.When(x => WhenIGetTheServices())
|
||||||
"Unable to use service Address: http://localhost and Port: 50888 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
|
.Then(x => ThenTheCountIs(0))
|
||||||
Times.Once);
|
.And(x => ThenTheLoggerHasBeenCalledCorrectlyForEmptyAddress())
|
||||||
}
|
.BDDfy();
|
||||||
|
}
|
||||||
private void ThenTheLoggerHasBeenCalledCorrectlyForEmptyAddress()
|
|
||||||
{
|
[Fact]
|
||||||
_logger.Verify(
|
public void should_not_return_services_with_invalid_port()
|
||||||
x => x.LogWarning(
|
{
|
||||||
"Unable to use service Address: and Port: 50881 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
|
var serviceEntryOne = new ServiceEntry()
|
||||||
Times.Once);
|
{
|
||||||
|
Service = new AgentService()
|
||||||
_logger.Verify(
|
{
|
||||||
x => x.LogWarning(
|
Service = _serviceName,
|
||||||
"Unable to use service Address: and Port: 50888 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
|
Address = "localhost",
|
||||||
Times.Once);
|
Port = -1,
|
||||||
}
|
ID = Guid.NewGuid().ToString(),
|
||||||
|
Tags = new string[0]
|
||||||
private void ThenTheLoggerHasBeenCalledCorrectlyForInvalidPorts()
|
},
|
||||||
{
|
};
|
||||||
_logger.Verify(
|
|
||||||
x => x.LogWarning(
|
var serviceEntryTwo = new ServiceEntry()
|
||||||
"Unable to use service Address: localhost and Port: -1 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
|
{
|
||||||
Times.Once);
|
Service = new AgentService()
|
||||||
|
{
|
||||||
_logger.Verify(
|
Service = _serviceName,
|
||||||
x => x.LogWarning(
|
Address = "localhost",
|
||||||
"Unable to use service Address: localhost and Port: 0 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
|
Port = 0,
|
||||||
Times.Once);
|
ID = Guid.NewGuid().ToString(),
|
||||||
}
|
Tags = new string[0]
|
||||||
|
},
|
||||||
private void ThenTheCountIs(int count)
|
};
|
||||||
{
|
|
||||||
_services.Count.ShouldBe(count);
|
this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(_fakeConsulServiceDiscoveryUrl, _serviceName))
|
||||||
}
|
.And(x => GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
|
||||||
|
.When(x => WhenIGetTheServices())
|
||||||
private void WhenIGetTheServices()
|
.Then(x => ThenTheCountIs(0))
|
||||||
{
|
.And(x => ThenTheLoggerHasBeenCalledCorrectlyForInvalidPorts())
|
||||||
_services = _provider.Get().GetAwaiter().GetResult();
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheLoggerHasBeenCalledCorrectlyForInvalidAddress()
|
||||||
|
{
|
||||||
|
_logger.Verify(
|
||||||
|
x => x.LogWarning(
|
||||||
|
"Unable to use service Address: http://localhost and Port: 50881 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
|
||||||
|
Times.Once);
|
||||||
|
|
||||||
|
_logger.Verify(
|
||||||
|
x => x.LogWarning(
|
||||||
|
"Unable to use service Address: http://localhost and Port: 50888 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
|
||||||
|
Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheLoggerHasBeenCalledCorrectlyForEmptyAddress()
|
||||||
|
{
|
||||||
|
_logger.Verify(
|
||||||
|
x => x.LogWarning(
|
||||||
|
"Unable to use service Address: and Port: 50881 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
|
||||||
|
Times.Once);
|
||||||
|
|
||||||
|
_logger.Verify(
|
||||||
|
x => x.LogWarning(
|
||||||
|
"Unable to use service Address: and Port: 50888 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
|
||||||
|
Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheLoggerHasBeenCalledCorrectlyForInvalidPorts()
|
||||||
|
{
|
||||||
|
_logger.Verify(
|
||||||
|
x => x.LogWarning(
|
||||||
|
"Unable to use service Address: localhost and Port: -1 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
|
||||||
|
Times.Once);
|
||||||
|
|
||||||
|
_logger.Verify(
|
||||||
|
x => x.LogWarning(
|
||||||
|
"Unable to use service Address: localhost and Port: 0 as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"),
|
||||||
|
Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheCountIs(int count)
|
||||||
|
{
|
||||||
|
_services.Count.ShouldBe(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenIGetTheServices()
|
||||||
|
{
|
||||||
|
_services = _provider.Get().GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheTokenIs(string token)
|
private void ThenTheTokenIs(string token)
|
||||||
{
|
{
|
||||||
_receivedToken.ShouldBe(token);
|
_receivedToken.ShouldBe(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheServicesAreRegisteredWithConsul(params ServiceEntry[] serviceEntries)
|
private void GivenTheServicesAreRegisteredWithConsul(params ServiceEntry[] serviceEntries)
|
||||||
{
|
{
|
||||||
foreach (var serviceEntry in serviceEntries)
|
foreach (var serviceEntry in serviceEntries)
|
||||||
{
|
{
|
||||||
_serviceEntries.Add(serviceEntry);
|
_serviceEntries.Add(serviceEntry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url, string serviceName)
|
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url, string serviceName)
|
||||||
{
|
{
|
||||||
_fakeConsulBuilder = new WebHostBuilder()
|
_fakeConsulBuilder = new WebHostBuilder()
|
||||||
.UseUrls(url)
|
.UseUrls(url)
|
||||||
.UseKestrel()
|
.UseKestrel()
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
.UseIISIntegration()
|
.UseIISIntegration()
|
||||||
.UseUrls(url)
|
.UseUrls(url)
|
||||||
.Configure(app =>
|
.Configure(app =>
|
||||||
{
|
{
|
||||||
app.Run(async context =>
|
app.Run(async context =>
|
||||||
{
|
{
|
||||||
if (context.Request.Path.Value == $"/v1/health/service/{serviceName}")
|
if (context.Request.Path.Value == $"/v1/health/service/{serviceName}")
|
||||||
{
|
{
|
||||||
if (context.Request.Headers.TryGetValue("X-Consul-Token", out var values))
|
if (context.Request.Headers.TryGetValue("X-Consul-Token", out var values))
|
||||||
{
|
{
|
||||||
_receivedToken = values.First();
|
_receivedToken = values.First();
|
||||||
}
|
}
|
||||||
|
|
||||||
var json = JsonConvert.SerializeObject(_serviceEntries);
|
var json = JsonConvert.SerializeObject(_serviceEntries);
|
||||||
context.Response.Headers.Add("Content-Type", "application/json");
|
context.Response.Headers.Add("Content-Type", "application/json");
|
||||||
await context.Response.WriteAsync(json);
|
await context.Response.WriteAsync(json);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
_fakeConsulBuilder.Start();
|
_fakeConsulBuilder.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_fakeConsulBuilder?.Dispose();
|
_fakeConsulBuilder?.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user