mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 14:08:15 +08:00
unit tests for cache clearing passing
This commit is contained in:
@ -5,6 +5,7 @@ using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Moq;
|
||||
using Ocelot.Cache;
|
||||
using Ocelot.Cache.Middleware;
|
||||
@ -43,6 +44,7 @@ namespace Ocelot.UnitTests.Cache
|
||||
x.AddLogging();
|
||||
x.AddSingleton(_cacheManager.Object);
|
||||
x.AddSingleton(_scopedRepo.Object);
|
||||
x.AddSingleton<IRegionCreator, RegionCreator>();
|
||||
})
|
||||
.UseUrls(_url)
|
||||
.UseKestrel()
|
||||
|
46
test/Ocelot.UnitTests/Cache/RegionCreatorTests.cs
Normal file
46
test/Ocelot.UnitTests/Cache/RegionCreatorTests.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Cache;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Cache
|
||||
{
|
||||
public class RegionCreatorTests
|
||||
{
|
||||
private string _result;
|
||||
private ReRoute _reRoute;
|
||||
|
||||
[Fact]
|
||||
public void should_create_region()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithUpstreamHttpMethod(new List<string>{"Get"})
|
||||
.WithUpstreamPathTemplate("/test/dummy")
|
||||
.Build();
|
||||
|
||||
this.Given(_ => GivenTheReRoute(reRoute))
|
||||
.When(_ => WhenICreateTheRegion())
|
||||
.Then(_ => ThenTheRegionIs("Get /test/dummy"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheReRoute(ReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenICreateTheRegion()
|
||||
{
|
||||
RegionCreator regionCreator = new RegionCreator();
|
||||
_result = regionCreator.Region(_reRoute);
|
||||
}
|
||||
|
||||
private void ThenTheRegionIs(string expected)
|
||||
{
|
||||
_result.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
}
|
109
test/Ocelot.UnitTests/Cache/RegionsGetterTests.cs
Normal file
109
test/Ocelot.UnitTests/Cache/RegionsGetterTests.cs
Normal file
@ -0,0 +1,109 @@
|
||||
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 reRoute = new ReRouteBuilder()
|
||||
.WithUpstreamHttpMethod(new List<string>{"Get"})
|
||||
.WithUpstreamPathTemplate("/")
|
||||
.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);
|
||||
}
|
||||
}
|
||||
}
|
@ -15,12 +15,14 @@ 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>>();
|
||||
_controller = new OutputCacheController(_cache.Object);
|
||||
_getter = new Mock<IRegionsGetter>();
|
||||
_controller = new OutputCacheController(_cache.Object, _getter.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
Reference in New Issue
Block a user