mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 03:18:15 +08:00
after much hacking unit tests passing
This commit is contained in:
@ -38,6 +38,7 @@
|
||||
"runtimes": {
|
||||
"win10-x64": {},
|
||||
"osx.10.11-x64": {},
|
||||
"osx.10.12-x64": {},
|
||||
"win7-x64": {}
|
||||
},
|
||||
"frameworks": {
|
||||
|
@ -11,6 +11,7 @@
|
||||
"runtimes": {
|
||||
"win10-x64": {},
|
||||
"osx.10.11-x64":{},
|
||||
"osx.10.12-x64": {},
|
||||
"win7-x64": {}
|
||||
},
|
||||
"frameworks": {
|
||||
|
@ -39,6 +39,7 @@
|
||||
"runtimes": {
|
||||
"win10-x64": {},
|
||||
"osx.10.11-x64": {},
|
||||
"osx.10.12-x64": {},
|
||||
"win7-x64": {}
|
||||
},
|
||||
"frameworks": {
|
||||
|
@ -23,6 +23,7 @@
|
||||
"runtimes": {
|
||||
"win10-x64": {},
|
||||
"osx.10.11-x64":{},
|
||||
"osx.10.12-x64": {},
|
||||
"win7-x64": {}
|
||||
},
|
||||
"frameworks": {
|
||||
|
@ -16,14 +16,12 @@ namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
private readonly IOcelotConfigurationProvider _ocelotConfigurationProvider;
|
||||
private readonly Mock<IOcelotConfigurationRepository> _configurationRepository;
|
||||
private readonly Mock<IOcelotConfigurationCreator> _creator;
|
||||
private Response<IOcelotConfiguration> _result;
|
||||
|
||||
public FileConfigurationProviderTests()
|
||||
{
|
||||
_creator = new Mock<IOcelotConfigurationCreator>();
|
||||
_configurationRepository = new Mock<IOcelotConfigurationRepository>();
|
||||
_ocelotConfigurationProvider = new OcelotConfigurationProvider(_configurationRepository.Object, _creator.Object);
|
||||
_ocelotConfigurationProvider = new OcelotConfigurationProvider(_configurationRepository.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -35,16 +33,6 @@ namespace Ocelot.UnitTests.Configuration
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_config_if_it_doesnt_exist()
|
||||
{
|
||||
this.Given(x => x.GivenTheRepoReturns(new OkResponse<IOcelotConfiguration>(null)))
|
||||
.And(x => x.GivenTheCreatorReturns(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(new List<ReRoute>(), string.Empty))))
|
||||
.When(x => x.WhenIGetTheConfig())
|
||||
.Then(x => x.TheFollowingIsReturned(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(new List<ReRoute>(), string.Empty))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
@ -61,29 +49,6 @@ namespace Ocelot.UnitTests.Configuration
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_creator_errors()
|
||||
{
|
||||
this.Given(x => x.GivenTheRepoReturns(new OkResponse<IOcelotConfiguration>(null)))
|
||||
.And(x => x.GivenTheCreatorReturns(new ErrorResponse<IOcelotConfiguration>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.When(x => x.WhenIGetTheConfig())
|
||||
.Then(x => x.TheFollowingIsReturned(new ErrorResponse<IOcelotConfiguration>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheCreatorReturns(Response<IOcelotConfiguration> config)
|
||||
{
|
||||
_creator
|
||||
.Setup(x => x.Create())
|
||||
.ReturnsAsync(config);
|
||||
}
|
||||
|
||||
private void GivenTheRepoReturns(Response<IOcelotConfiguration> config)
|
||||
{
|
||||
_configurationRepository
|
||||
@ -93,7 +58,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
|
||||
private void WhenIGetTheConfig()
|
||||
{
|
||||
_result = _ocelotConfigurationProvider.Get().Result;
|
||||
_result = _ocelotConfigurationProvider.Get();
|
||||
}
|
||||
|
||||
private void TheFollowingIsReturned(Response<IOcelotConfiguration> expected)
|
||||
|
@ -0,0 +1,86 @@
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Configuration.Repository;
|
||||
using Ocelot.Configuration.Setter;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class FileConfigurationSetterTests
|
||||
{
|
||||
private FileConfiguration _fileConfiguration;
|
||||
private FileConfigurationSetter _configSetter;
|
||||
private Mock<IOcelotConfigurationRepository> _configRepo;
|
||||
private Mock<IOcelotConfigurationCreator> _configCreator;
|
||||
private Response<IOcelotConfiguration> _configuration;
|
||||
private object _result;
|
||||
|
||||
public FileConfigurationSetterTests()
|
||||
{
|
||||
_configRepo = new Mock<IOcelotConfigurationRepository>();
|
||||
_configCreator = new Mock<IOcelotConfigurationCreator>();
|
||||
_configSetter = new FileConfigurationSetter(_configRepo.Object, _configCreator.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_configuration()
|
||||
{
|
||||
var fileConfig = new FileConfiguration();
|
||||
var config = new OcelotConfiguration(new List<ReRoute>(), string.Empty);
|
||||
|
||||
this.Given(x => GivenTheFollowingConfiguration(fileConfig))
|
||||
.And(x => GivenTheCreatorReturns(new OkResponse<IOcelotConfiguration>(config)))
|
||||
.When(x => WhenISetTheConfiguration())
|
||||
.Then(x => ThenTheConfigurationRepositoryIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_unable_to_set_configuration()
|
||||
{
|
||||
var fileConfig = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFollowingConfiguration(fileConfig))
|
||||
.And(x => GivenTheCreatorReturns(new ErrorResponse<IOcelotConfiguration>(It.IsAny<Error>())))
|
||||
.When(x => WhenISetTheConfiguration())
|
||||
.And(x => ThenAnErrorResponseIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenAnErrorResponseIsReturned()
|
||||
{
|
||||
_result.ShouldBeOfType<ErrorResponse>();
|
||||
}
|
||||
|
||||
private void GivenTheCreatorReturns(Response<IOcelotConfiguration> configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_configCreator
|
||||
.Setup(x => x.Create(_fileConfiguration))
|
||||
.ReturnsAsync(_configuration);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
_fileConfiguration = fileConfiguration;
|
||||
}
|
||||
|
||||
private void WhenISetTheConfiguration()
|
||||
{
|
||||
_result = _configSetter.Set(_fileConfiguration).Result;
|
||||
}
|
||||
|
||||
private void ThenTheConfigurationRepositoryIsCalledCorrectly()
|
||||
{
|
||||
_configRepo
|
||||
.Verify(x => x.AddOrReplace(_configuration.Data), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,28 +1,36 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moq;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Configuration.Setter;
|
||||
using Ocelot.Controllers;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Services;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
|
||||
namespace Ocelot.UnitTests.Controllers
|
||||
{
|
||||
public class FileConfigurationControllerTests
|
||||
{
|
||||
private FileConfigurationController _controller;
|
||||
private Mock<IGetFileConfiguration> _getFileConfig;
|
||||
private Mock<IFileConfigurationProvider> _configGetter;
|
||||
private Mock<IFileConfigurationSetter> _configSetter;
|
||||
private IActionResult _result;
|
||||
private FileConfiguration _fileConfiguration;
|
||||
|
||||
public FileConfigurationControllerTests()
|
||||
{
|
||||
_getFileConfig = new Mock<IGetFileConfiguration>();
|
||||
_controller = new FileConfigurationController(_getFileConfig.Object);
|
||||
_configGetter = new Mock<IFileConfigurationProvider>();
|
||||
_configSetter = new Mock<IFileConfigurationSetter>();
|
||||
_controller = new FileConfigurationController(_configGetter.Object, _configSetter.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_file_configuration()
|
||||
public void should_get_file_configuration()
|
||||
{
|
||||
var expected = new OkResponse<FileConfiguration>(new FileConfiguration());
|
||||
|
||||
@ -32,10 +40,75 @@ namespace Ocelot.UnitTests.Controllers
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_cannot_get_config()
|
||||
{
|
||||
var expected = new ErrorResponse<FileConfiguration>(It.IsAny<Error>());
|
||||
|
||||
this.Given(x => x.GivenTheGetConfigurationReturns(expected))
|
||||
.When(x => x.WhenIGetTheFileConfiguration())
|
||||
.Then(x => x.TheTheGetFileConfigurationIsCalledCorrectly())
|
||||
.And(x => x.ThenTheResponseIs<BadRequestObjectResult>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_post_file_configuration()
|
||||
{
|
||||
var expected = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFileConfiguration(expected))
|
||||
.And(x => GivenTheConfigSetterReturnsAnError(new OkResponse()))
|
||||
.When(x => WhenIPostTheFileConfiguration())
|
||||
.Then(x => x.ThenTheConfigrationSetterIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_cannot_set_config()
|
||||
{
|
||||
var expected = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFileConfiguration(expected))
|
||||
.And(x => GivenTheConfigSetterReturnsAnError(new ErrorResponse(new FakeError())))
|
||||
.When(x => WhenIPostTheFileConfiguration())
|
||||
.Then(x => x.ThenTheConfigrationSetterIsCalledCorrectly())
|
||||
.And(x => ThenTheResponseIs<BadRequestObjectResult>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheConfigSetterReturnsAnError(Response response)
|
||||
{
|
||||
_configSetter
|
||||
.Setup(x => x.Set(It.IsAny<FileConfiguration>()))
|
||||
.ReturnsAsync(response);
|
||||
}
|
||||
|
||||
private void ThenTheConfigrationSetterIsCalledCorrectly()
|
||||
{
|
||||
_configSetter
|
||||
.Verify(x => x.Set(_fileConfiguration), Times.Once);
|
||||
}
|
||||
|
||||
private void WhenIPostTheFileConfiguration()
|
||||
{
|
||||
_result = _controller.Post(_fileConfiguration).Result;
|
||||
}
|
||||
|
||||
private void GivenTheFileConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
_fileConfiguration = fileConfiguration;
|
||||
}
|
||||
|
||||
private void ThenTheResponseIs<T>()
|
||||
{
|
||||
_result.ShouldBeOfType<T>();
|
||||
}
|
||||
|
||||
private void GivenTheGetConfigurationReturns(Response<FileConfiguration> fileConfiguration)
|
||||
{
|
||||
_getFileConfig
|
||||
.Setup(x => x.Invoke())
|
||||
_configGetter
|
||||
.Setup(x => x.Get())
|
||||
.Returns(fileConfiguration);
|
||||
}
|
||||
|
||||
@ -46,8 +119,15 @@ namespace Ocelot.UnitTests.Controllers
|
||||
|
||||
private void TheTheGetFileConfigurationIsCalledCorrectly()
|
||||
{
|
||||
_getFileConfig
|
||||
.Verify(x => x.Invoke(), Times.Once);
|
||||
_configGetter
|
||||
.Verify(x => x.Get(), Times.Once);
|
||||
}
|
||||
|
||||
class FakeError : Error
|
||||
{
|
||||
public FakeError() : base(string.Empty, OcelotErrorCode.CannotAddDataError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -90,7 +90,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
_downstreamRouteFinder
|
||||
.Setup(x => x.FindDownstreamRoute(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.ReturnsAsync(_downstreamRoute);
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
@ -199,7 +199,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
|
||||
_reRoutesConfig = reRoutesConfig;
|
||||
_mockConfig
|
||||
.Setup(x => x.Get())
|
||||
.ReturnsAsync(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(_reRoutesConfig, adminPath)));
|
||||
.Returns(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(_reRoutesConfig, adminPath)));
|
||||
}
|
||||
|
||||
private void GivenThereIsAnUpstreamUrlPath(string upstreamUrlPath)
|
||||
@ -209,7 +209,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
|
||||
|
||||
private void WhenICallTheFinder()
|
||||
{
|
||||
_result = _downstreamRouteFinder.FindDownstreamRoute(_upstreamUrlPath, _upstreamHttpMethod).Result;
|
||||
_result = _downstreamRouteFinder.FindDownstreamRoute(_upstreamUrlPath, _upstreamHttpMethod);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(DownstreamRoute expected)
|
||||
|
@ -15,12 +15,12 @@ namespace Ocelot.UnitTests.Services
|
||||
{
|
||||
public class GetFileConfigurationTests
|
||||
{
|
||||
private readonly IGetFileConfiguration _getReRoutes;
|
||||
private readonly IFileConfigurationProvider _getReRoutes;
|
||||
private FileConfiguration _result;
|
||||
|
||||
public GetFileConfigurationTests()
|
||||
{
|
||||
_getReRoutes = new GetFileConfiguration();
|
||||
_getReRoutes = new FileConfigurationProvider();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -74,7 +74,7 @@ namespace Ocelot.UnitTests.Services
|
||||
|
||||
private void WhenIGetTheReRoutes()
|
||||
{
|
||||
_result = _getReRoutes.Invoke().Data;
|
||||
_result = _getReRoutes.Get().Data;
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(FileConfiguration expected)
|
@ -29,6 +29,7 @@
|
||||
"runtimes": {
|
||||
"win10-x64": {},
|
||||
"osx.10.11-x64":{},
|
||||
"osx.10.12-x64": {},
|
||||
"win7-x64": {}
|
||||
},
|
||||
"frameworks": {
|
||||
|
Reference in New Issue
Block a user