mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 10:18:17 +08:00
still hacking around
This commit is contained in:
@ -0,0 +1,53 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Controllers;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Services;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Controllers
|
||||
{
|
||||
public class FileConfigurationControllerTests
|
||||
{
|
||||
private FileConfigurationController _controller;
|
||||
private Mock<IGetFileConfiguration> _getFileConfig;
|
||||
private IActionResult _result;
|
||||
|
||||
public FileConfigurationControllerTests()
|
||||
{
|
||||
_getFileConfig = new Mock<IGetFileConfiguration>();
|
||||
_controller = new FileConfigurationController(_getFileConfig.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_file_configuration()
|
||||
{
|
||||
var expected = new OkResponse<FileConfiguration>(new FileConfiguration());
|
||||
|
||||
this.Given(x => x.GivenTheGetConfigurationReturns(expected))
|
||||
.When(x => x.WhenIGetTheFileConfiguration())
|
||||
.Then(x => x.ThenTheFileConfigurationIsReturned(expected.Data))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheGetConfigurationReturns(Response<FileConfiguration> fileConfiguration)
|
||||
{
|
||||
_getFileConfig
|
||||
.Setup(x => x.Invoke())
|
||||
.Returns(fileConfiguration);
|
||||
}
|
||||
|
||||
private void WhenIGetTheFileConfiguration()
|
||||
{
|
||||
_result = _controller.Get();
|
||||
}
|
||||
|
||||
private void ThenTheFileConfigurationIsReturned(FileConfiguration expected)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
96
test/Ocelot.UnitTests/Services/GetFileConfigurationTests.cs
Normal file
96
test/Ocelot.UnitTests/Services/GetFileConfigurationTests.cs
Normal file
@ -0,0 +1,96 @@
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Ocelot.Services;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
|
||||
namespace Ocelot.UnitTests.Services
|
||||
{
|
||||
public class GetFileConfigurationTests
|
||||
{
|
||||
private IGetFileConfiguration _getReRoutes;
|
||||
private FileConfiguration _result;
|
||||
|
||||
public GetFileConfigurationTests()
|
||||
{
|
||||
_getReRoutes = new GetFileConfiguration();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_file_configuration()
|
||||
{
|
||||
var reRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 80,
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/test/test/{test}"
|
||||
}
|
||||
};
|
||||
|
||||
var globalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
AdministrationPath = "testy",
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
||||
{
|
||||
Provider = "consul",
|
||||
Port = 198,
|
||||
Host = "blah"
|
||||
}
|
||||
};
|
||||
|
||||
var config = new FileConfiguration();
|
||||
config.GlobalConfiguration = globalConfiguration;
|
||||
config.ReRoutes.AddRange(reRoutes);
|
||||
|
||||
this.Given(x => x.GivenTheConfigurationIs(config))
|
||||
.When(x => x.WhenIGetTheReRoutes())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(config))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var configurationPath = "configuration.json";
|
||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
}
|
||||
|
||||
private void WhenIGetTheReRoutes()
|
||||
{
|
||||
_result = _getReRoutes.Invoke().Data;
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(FileConfiguration expected)
|
||||
{
|
||||
_result.GlobalConfiguration.AdministrationPath.ShouldBe(expected.GlobalConfiguration.AdministrationPath);
|
||||
_result.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey);
|
||||
_result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
||||
_result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
||||
_result.GlobalConfiguration.ServiceDiscoveryProvider.Provider.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Provider);
|
||||
|
||||
for(var i = 0; i < _result.ReRoutes.Count; i++)
|
||||
{
|
||||
_result.ReRoutes[i].DownstreamHost.ShouldBe(expected.ReRoutes[i].DownstreamHost);
|
||||
_result.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expected.ReRoutes[i].DownstreamPathTemplate);
|
||||
_result.ReRoutes[i].DownstreamPort.ShouldBe(expected.ReRoutes[i].DownstreamPort);
|
||||
_result.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme);
|
||||
//todo -- add more!
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1
test/Ocelot.UnitTests/configuration.json
Executable file
1
test/Ocelot.UnitTests/configuration.json
Executable file
@ -0,0 +1 @@
|
||||
{"ReRoutes":[{"DownstreamPathTemplate":"/test/test/{test}","UpstreamPathTemplate":null,"UpstreamHttpMethod":null,"AuthenticationOptions":{"Provider":null,"ProviderRootUrl":null,"ScopeName":null,"RequireHttps":false,"AdditionalScopes":[],"ScopeSecret":null},"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0},"ReRouteIsCaseSensitive":false,"ServiceName":null,"DownstreamScheme":"https","DownstreamHost":"localhost","DownstreamPort":80,"QoSOptions":{"ExceptionsAllowedBeforeBreaking":0,"DurationOfBreak":0,"TimeoutValue":0},"LoadBalancer":null}],"GlobalConfiguration":{"RequestIdKey":null,"ServiceDiscoveryProvider":{"Provider":"consul","Host":"blah","Port":198},"AdministrationPath":"testy"}}
|
Reference in New Issue
Block a user