mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 03:38:16 +08:00
now set region in config...or it defaults to something
This commit is contained in:
@ -18,6 +18,7 @@ namespace Ocelot.UnitTests.Cache
|
||||
private string _resultGet;
|
||||
private TimeSpan _ttlSeconds;
|
||||
private List<string> _resultKeys;
|
||||
private string _region;
|
||||
|
||||
public CacheManagerCacheTests()
|
||||
{
|
||||
@ -28,7 +29,7 @@ namespace Ocelot.UnitTests.Cache
|
||||
[Fact]
|
||||
public void should_get_from_cache()
|
||||
{
|
||||
this.Given(x => x.GivenTheFollowingIsCached("someKey", "someValue"))
|
||||
this.Given(x => x.GivenTheFollowingIsCached("someKey", "someRegion", "someValue"))
|
||||
.When(x => x.WhenIGetFromTheCache())
|
||||
.Then(x => x.ThenTheResultIs("someValue"))
|
||||
.BDDfy();
|
||||
@ -88,15 +89,16 @@ namespace Ocelot.UnitTests.Cache
|
||||
|
||||
private void WhenIGetFromTheCache()
|
||||
{
|
||||
_resultGet = _ocelotOcelotCacheManager.Get(_key);
|
||||
_resultGet = _ocelotOcelotCacheManager.Get(_key, _region);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingIsCached(string key, string value)
|
||||
private void GivenTheFollowingIsCached(string key, string region, string value)
|
||||
{
|
||||
_key = key;
|
||||
_value = value;
|
||||
_region = region;
|
||||
_mockCacheManager
|
||||
.Setup(x => x.Get<string>(It.IsAny<string>()))
|
||||
.Setup(x => x.Get<string>(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(value);
|
||||
}
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ namespace Ocelot.UnitTests.Cache
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithIsCached(true)
|
||||
.WithCacheOptions(new CacheOptions(100))
|
||||
.WithCacheOptions(new CacheOptions(100, "kanken"))
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build();
|
||||
|
||||
@ -120,7 +120,7 @@ namespace Ocelot.UnitTests.Cache
|
||||
private void ThenTheCacheGetIsCalledCorrectly()
|
||||
{
|
||||
_cacheManager
|
||||
.Verify(x => x.Get(It.IsAny<string>()), Times.Once);
|
||||
.Verify(x => x.Get(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheCacheAddIsCalledCorrectly()
|
||||
@ -140,7 +140,7 @@ namespace Ocelot.UnitTests.Cache
|
||||
{
|
||||
_response = response;
|
||||
_cacheManager
|
||||
.Setup(x => x.Get(It.IsAny<string>()))
|
||||
.Setup(x => x.Get(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(_response);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ using System.Collections.Generic;
|
||||
using Ocelot.Cache;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
@ -11,23 +12,41 @@ namespace Ocelot.UnitTests.Cache
|
||||
public class RegionCreatorTests
|
||||
{
|
||||
private string _result;
|
||||
private ReRoute _reRoute;
|
||||
private FileReRoute _reRoute;
|
||||
|
||||
[Fact]
|
||||
public void should_create_region()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithUpstreamHttpMethod(new List<string>{"Get"})
|
||||
.WithUpstreamPathTemplate("/test/dummy")
|
||||
.Build();
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
UpstreamPathTemplate = "/testdummy"
|
||||
};
|
||||
|
||||
this.Given(_ => GivenTheReRoute(reRoute))
|
||||
.When(_ => WhenICreateTheRegion())
|
||||
.Then(_ => ThenTheRegionIs("Gettestdummy"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_region()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
Region = "region"
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(_ => GivenTheReRoute(reRoute))
|
||||
.When(_ => WhenICreateTheRegion())
|
||||
.Then(_ => ThenTheRegionIs("region"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheReRoute(ReRoute reRoute)
|
||||
private void GivenTheReRoute(FileReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
@ -35,7 +54,7 @@ namespace Ocelot.UnitTests.Cache
|
||||
private void WhenICreateTheRegion()
|
||||
{
|
||||
RegionCreator regionCreator = new RegionCreator();
|
||||
_result = regionCreator.Region(_reRoute);
|
||||
_result = regionCreator.Create(_reRoute);
|
||||
}
|
||||
|
||||
private void ThenTheRegionIs(string expected)
|
||||
|
@ -1,113 +0,0 @@
|
||||
using Xunit;
|
||||
using TestStack.BDDfy;
|
||||
using Shouldly;
|
||||
using Ocelot.Cache;
|
||||
using Moq;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Configuration;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using System;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Logging;
|
||||
|
||||
namespace Ocelot.UnitTests.Cache
|
||||
{
|
||||
public class RegionsGetterTests
|
||||
{
|
||||
private RegionsGetter _regionsGetter;
|
||||
private readonly Mock<IOcelotConfigurationProvider> _provider;
|
||||
private readonly Mock<IRegionCreator> _creator;
|
||||
private readonly Mock<IOcelotLoggerFactory> _factory;
|
||||
private List<string> _result;
|
||||
|
||||
public RegionsGetterTests()
|
||||
{
|
||||
_provider = new Mock<IOcelotConfigurationProvider>();
|
||||
_creator = new Mock<IRegionCreator>();
|
||||
_factory = new Mock<IOcelotLoggerFactory>();
|
||||
var logger = new Mock<IOcelotLogger>();
|
||||
_factory
|
||||
.Setup(x => x.CreateLogger<RegionsGetter>())
|
||||
.Returns(logger.Object);
|
||||
_regionsGetter = new RegionsGetter(_provider.Object, _creator.Object, _factory.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_regions()
|
||||
{
|
||||
var cacheOptions = new CacheOptions(12);
|
||||
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithUpstreamHttpMethod(new List<string>{"Get"})
|
||||
.WithUpstreamPathTemplate("/")
|
||||
.WithCacheOptions(cacheOptions)
|
||||
.WithIsCached(true)
|
||||
.Build();
|
||||
|
||||
var reRoutes = new List<ReRoute>
|
||||
{
|
||||
reRoute
|
||||
};
|
||||
|
||||
var config = new OcelotConfiguration(reRoutes, "whocares!");
|
||||
|
||||
var expected = new List<string>
|
||||
{
|
||||
"balls"
|
||||
};
|
||||
|
||||
this.Given(_ => GivenTheFollowingConfig(config))
|
||||
.And(_ => GivenTheProviderReturns("balls"))
|
||||
.When(_ => WhenIGetTheRegions())
|
||||
.Then(_ => ThenTheFollowingIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_empty_regions()
|
||||
{
|
||||
var expected = new List<string>();
|
||||
|
||||
this.Given(_ => GivenAnErrorGettingTheConfig())
|
||||
.When(_ => WhenIGetTheRegions())
|
||||
.Then(_ => ThenTheFollowingIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAnErrorGettingTheConfig()
|
||||
{
|
||||
var config = new OcelotConfiguration(new List<ReRoute>(), "whocares!");
|
||||
|
||||
_provider
|
||||
.Setup(x => x.Get())
|
||||
.ReturnsAsync(new ErrorResponse<IOcelotConfiguration>(It.IsAny<Error>()));
|
||||
}
|
||||
|
||||
private void GivenTheProviderReturns(string expected)
|
||||
{
|
||||
_creator
|
||||
.Setup(x => x.Region(It.IsAny<ReRoute>()))
|
||||
.Returns(expected);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingConfig(IOcelotConfiguration config)
|
||||
{
|
||||
_provider
|
||||
.Setup(x => x.Get())
|
||||
.ReturnsAsync(new OkResponse<IOcelotConfiguration>(config));
|
||||
}
|
||||
|
||||
private void WhenIGetTheRegions()
|
||||
{
|
||||
_result = _regionsGetter.Regions().Result;
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(List<string> expected)
|
||||
{
|
||||
_result.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using Ocelot.Cache;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
@ -39,6 +40,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
private Mock<IQoSOptionsCreator> _qosOptionsCreator;
|
||||
private Mock<IReRouteOptionsCreator> _fileReRouteOptionsCreator;
|
||||
private Mock<IRateLimitOptionsCreator> _rateLimitOptions;
|
||||
private Mock<IRegionCreator> _regionCreator;
|
||||
|
||||
public FileConfigurationCreatorTests()
|
||||
{
|
||||
@ -59,6 +61,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
_qosOptionsCreator = new Mock<IQoSOptionsCreator>();
|
||||
_fileReRouteOptionsCreator = new Mock<IReRouteOptionsCreator>();
|
||||
_rateLimitOptions = new Mock<IRateLimitOptionsCreator>();
|
||||
_regionCreator = new Mock<IRegionCreator>();
|
||||
|
||||
_ocelotConfigurationCreator = new FileOcelotConfigurationCreator(
|
||||
_fileConfig.Object, _validator.Object, _logger.Object,
|
||||
@ -66,7 +69,51 @@ namespace Ocelot.UnitTests.Configuration
|
||||
_qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object,
|
||||
_authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object,
|
||||
_serviceProviderConfigCreator.Object, _qosOptionsCreator.Object, _fileReRouteOptionsCreator.Object,
|
||||
_rateLimitOptions.Object);
|
||||
_rateLimitOptions.Object, _regionCreator.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_region_creator()
|
||||
{
|
||||
var reRouteOptions = new ReRouteOptionsBuilder()
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheConfigIs(new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHost = "127.0.0.1",
|
||||
UpstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
Region = "region"
|
||||
}
|
||||
}
|
||||
},
|
||||
}))
|
||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => x.GivenTheFollowingRegionIsReturned("region"))
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
.Then(x => x.ThenTheRegionCreatorIsCalledCorrectly("region"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingRegionIsReturned(string region)
|
||||
{
|
||||
_regionCreator
|
||||
.Setup(x => x.Create(It.IsAny<FileReRoute>()))
|
||||
.Returns(region);
|
||||
}
|
||||
|
||||
private void ThenTheRegionCreatorIsCalledCorrectly(string expected)
|
||||
{
|
||||
_regionCreator
|
||||
.Verify(x => x.Create(_fileConfiguration.ReRoutes[0]), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -15,23 +15,12 @@ namespace Ocelot.UnitTests.Controllers
|
||||
{
|
||||
private OutputCacheController _controller;
|
||||
private Mock<IOcelotCache<HttpResponseMessage>> _cache;
|
||||
private Mock<IRegionsGetter> _getter;
|
||||
private IActionResult _result;
|
||||
|
||||
public OutputCacheControllerTests()
|
||||
{
|
||||
_cache = new Mock<IOcelotCache<HttpResponseMessage>>();
|
||||
_getter = new Mock<IRegionsGetter>();
|
||||
_controller = new OutputCacheController(_cache.Object, _getter.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_all_keys_from_server()
|
||||
{
|
||||
this.Given(_ => GivenTheFollowingKeys(new List<string>{"b", "a"}))
|
||||
.When(_ => WhenIGetTheKeys())
|
||||
.Then(_ => ThenTheKeysAreReturned())
|
||||
.BDDfy();
|
||||
_controller = new OutputCacheController(_cache.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -53,20 +42,5 @@ namespace Ocelot.UnitTests.Controllers
|
||||
{
|
||||
_result = _controller.Delete(key);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingKeys(List<string> keys)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void WhenIGetTheKeys()
|
||||
{
|
||||
_result = _controller.Get().Result;
|
||||
}
|
||||
|
||||
private void ThenTheKeysAreReturned()
|
||||
{
|
||||
_result.ShouldBeOfType<OkObjectResult>();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user