mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
#296 started writing merge code
This commit is contained in:
parent
fe9bca7b77
commit
7ca828836a
@ -18,7 +18,6 @@ namespace Ocelot.Configuration.Creator
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class FileInternalConfigurationCreator : IInternalConfigurationCreator
|
public class FileInternalConfigurationCreator : IInternalConfigurationCreator
|
||||||
{
|
{
|
||||||
private readonly IOptions<FileConfiguration> _options;
|
|
||||||
private readonly IConfigurationValidator _configurationValidator;
|
private readonly IConfigurationValidator _configurationValidator;
|
||||||
private readonly IOcelotLogger _logger;
|
private readonly IOcelotLogger _logger;
|
||||||
private readonly IClaimsToThingCreator _claimsToThingCreator;
|
private readonly IClaimsToThingCreator _claimsToThingCreator;
|
||||||
@ -36,7 +35,6 @@ namespace Ocelot.Configuration.Creator
|
|||||||
private readonly IDownstreamAddressesCreator _downstreamAddressesCreator;
|
private readonly IDownstreamAddressesCreator _downstreamAddressesCreator;
|
||||||
|
|
||||||
public FileInternalConfigurationCreator(
|
public FileInternalConfigurationCreator(
|
||||||
IOptions<FileConfiguration> options,
|
|
||||||
IConfigurationValidator configurationValidator,
|
IConfigurationValidator configurationValidator,
|
||||||
IOcelotLoggerFactory loggerFactory,
|
IOcelotLoggerFactory loggerFactory,
|
||||||
IClaimsToThingCreator claimsToThingCreator,
|
IClaimsToThingCreator claimsToThingCreator,
|
||||||
@ -62,7 +60,6 @@ namespace Ocelot.Configuration.Creator
|
|||||||
_requestIdKeyCreator = requestIdKeyCreator;
|
_requestIdKeyCreator = requestIdKeyCreator;
|
||||||
_upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
|
_upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
|
||||||
_authOptionsCreator = authOptionsCreator;
|
_authOptionsCreator = authOptionsCreator;
|
||||||
_options = options;
|
|
||||||
_configurationValidator = configurationValidator;
|
_configurationValidator = configurationValidator;
|
||||||
_logger = loggerFactory.CreateLogger<FileInternalConfigurationCreator>();
|
_logger = loggerFactory.CreateLogger<FileInternalConfigurationCreator>();
|
||||||
_claimsToThingCreator = claimsToThingCreator;
|
_claimsToThingCreator = claimsToThingCreator;
|
||||||
|
@ -18,5 +18,14 @@ namespace Ocelot.DependencyInjection
|
|||||||
builder.Add(memorySource);
|
builder.Add(memorySource);
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder)
|
||||||
|
{
|
||||||
|
//var load all files with ocelot*.json
|
||||||
|
//merge these files into one
|
||||||
|
//save it as ocelot.json
|
||||||
|
builder.AddJsonFile("ocelot.json");
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,18 @@
|
|||||||
using System.Collections.Generic;
|
namespace Ocelot.UnitTests.Configuration
|
||||||
using Castle.Components.DictionaryAdapter;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Cache;
|
|
||||||
using Ocelot.Configuration;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.Configuration.Creator;
|
|
||||||
using Ocelot.Configuration.File;
|
|
||||||
using Ocelot.Configuration.Validator;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Configuration
|
|
||||||
{
|
{
|
||||||
using System;
|
using System.Collections.Generic;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Cache;
|
||||||
|
using Ocelot.Configuration;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.Configuration.Creator;
|
||||||
|
using Ocelot.Configuration.File;
|
||||||
|
using Ocelot.Configuration.Validator;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using Shouldly;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
using Ocelot.DependencyInjection;
|
using Ocelot.DependencyInjection;
|
||||||
using Ocelot.Errors;
|
using Ocelot.Errors;
|
||||||
using Ocelot.UnitTests.TestData;
|
using Ocelot.UnitTests.TestData;
|
||||||
@ -24,23 +20,22 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
|
|
||||||
public class FileConfigurationCreatorTests
|
public class FileConfigurationCreatorTests
|
||||||
{
|
{
|
||||||
private readonly Mock<IOptions<FileConfiguration>> _fileConfig;
|
|
||||||
private readonly Mock<IConfigurationValidator> _validator;
|
private readonly Mock<IConfigurationValidator> _validator;
|
||||||
private Response<IInternalConfiguration> _config;
|
private Response<IInternalConfiguration> _config;
|
||||||
private FileConfiguration _fileConfiguration;
|
private FileConfiguration _fileConfiguration;
|
||||||
private readonly Mock<IOcelotLoggerFactory> _logger;
|
private readonly Mock<IOcelotLoggerFactory> _logger;
|
||||||
private readonly FileInternalConfigurationCreator _internalConfigurationCreator;
|
private readonly FileInternalConfigurationCreator _internalConfigurationCreator;
|
||||||
private Mock<IClaimsToThingCreator> _claimsToThingCreator;
|
private readonly Mock<IClaimsToThingCreator> _claimsToThingCreator;
|
||||||
private Mock<IAuthenticationOptionsCreator> _authOptionsCreator;
|
private readonly Mock<IAuthenticationOptionsCreator> _authOptionsCreator;
|
||||||
private Mock<IUpstreamTemplatePatternCreator> _upstreamTemplatePatternCreator;
|
private readonly Mock<IUpstreamTemplatePatternCreator> _upstreamTemplatePatternCreator;
|
||||||
private Mock<IRequestIdKeyCreator> _requestIdKeyCreator;
|
private readonly Mock<IRequestIdKeyCreator> _requestIdKeyCreator;
|
||||||
private Mock<IServiceProviderConfigurationCreator> _serviceProviderConfigCreator;
|
private readonly Mock<IServiceProviderConfigurationCreator> _serviceProviderConfigCreator;
|
||||||
private Mock<IQoSOptionsCreator> _qosOptionsCreator;
|
private readonly Mock<IQoSOptionsCreator> _qosOptionsCreator;
|
||||||
private Mock<IReRouteOptionsCreator> _fileReRouteOptionsCreator;
|
private readonly Mock<IReRouteOptionsCreator> _fileReRouteOptionsCreator;
|
||||||
private Mock<IRateLimitOptionsCreator> _rateLimitOptions;
|
private readonly Mock<IRateLimitOptionsCreator> _rateLimitOptions;
|
||||||
private Mock<IRegionCreator> _regionCreator;
|
private readonly Mock<IRegionCreator> _regionCreator;
|
||||||
private Mock<IHttpHandlerOptionsCreator> _httpHandlerOptionsCreator;
|
private readonly Mock<IHttpHandlerOptionsCreator> _httpHandlerOptionsCreator;
|
||||||
private Mock<IAdministrationPath> _adminPath;
|
private readonly Mock<IAdministrationPath> _adminPath;
|
||||||
private readonly Mock<IHeaderFindAndReplaceCreator> _headerFindAndReplaceCreator;
|
private readonly Mock<IHeaderFindAndReplaceCreator> _headerFindAndReplaceCreator;
|
||||||
private readonly Mock<IDownstreamAddressesCreator> _downstreamAddressesCreator;
|
private readonly Mock<IDownstreamAddressesCreator> _downstreamAddressesCreator;
|
||||||
|
|
||||||
@ -48,7 +43,6 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
{
|
{
|
||||||
_logger = new Mock<IOcelotLoggerFactory>();
|
_logger = new Mock<IOcelotLoggerFactory>();
|
||||||
_validator = new Mock<IConfigurationValidator>();
|
_validator = new Mock<IConfigurationValidator>();
|
||||||
_fileConfig = new Mock<IOptions<FileConfiguration>>();
|
|
||||||
_claimsToThingCreator = new Mock<IClaimsToThingCreator>();
|
_claimsToThingCreator = new Mock<IClaimsToThingCreator>();
|
||||||
_authOptionsCreator = new Mock<IAuthenticationOptionsCreator>();
|
_authOptionsCreator = new Mock<IAuthenticationOptionsCreator>();
|
||||||
_upstreamTemplatePatternCreator = new Mock<IUpstreamTemplatePatternCreator>();
|
_upstreamTemplatePatternCreator = new Mock<IUpstreamTemplatePatternCreator>();
|
||||||
@ -64,7 +58,6 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
_downstreamAddressesCreator = new Mock<IDownstreamAddressesCreator>();
|
_downstreamAddressesCreator = new Mock<IDownstreamAddressesCreator>();
|
||||||
|
|
||||||
_internalConfigurationCreator = new FileInternalConfigurationCreator(
|
_internalConfigurationCreator = new FileInternalConfigurationCreator(
|
||||||
_fileConfig.Object,
|
|
||||||
_validator.Object,
|
_validator.Object,
|
||||||
_logger.Object,
|
_logger.Object,
|
||||||
_claimsToThingCreator.Object,
|
_claimsToThingCreator.Object,
|
||||||
@ -262,7 +255,7 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
.And(x => x.GivenTheConfigIsValid())
|
.And(x => x.GivenTheConfigIsValid())
|
||||||
.And(x => x.GivenTheFollowingRegionIsReturned("region"))
|
.And(x => x.GivenTheFollowingRegionIsReturned("region"))
|
||||||
.When(x => x.WhenICreateTheConfig())
|
.When(x => x.WhenICreateTheConfig())
|
||||||
.Then(x => x.ThenTheRegionCreatorIsCalledCorrectly("region"))
|
.Then(x => x.ThenTheRegionCreatorIsCalledCorrectly())
|
||||||
.And(x => x.ThenTheHeaderFindAndReplaceCreatorIsCalledCorrectly())
|
.And(x => x.ThenTheHeaderFindAndReplaceCreatorIsCalledCorrectly())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
@ -800,9 +793,6 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
private void GivenTheConfigIs(FileConfiguration fileConfiguration)
|
private void GivenTheConfigIs(FileConfiguration fileConfiguration)
|
||||||
{
|
{
|
||||||
_fileConfiguration = fileConfiguration;
|
_fileConfiguration = fileConfiguration;
|
||||||
_fileConfig
|
|
||||||
.Setup(x => x.Value)
|
|
||||||
.Returns(_fileConfiguration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICreateTheConfig()
|
private void WhenICreateTheConfig()
|
||||||
@ -927,7 +917,7 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
.Returns(region);
|
.Returns(region);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheRegionCreatorIsCalledCorrectly(string expected)
|
private void ThenTheRegionCreatorIsCalledCorrectly()
|
||||||
{
|
{
|
||||||
_regionCreator
|
_regionCreator
|
||||||
.Verify(x => x.Create(_fileConfiguration.ReRoutes[0]), Times.Once);
|
.Verify(x => x.Create(_fileConfiguration.ReRoutes[0]), Times.Once);
|
||||||
|
@ -6,6 +6,11 @@ using Xunit;
|
|||||||
|
|
||||||
namespace Ocelot.UnitTests.DependencyInjection
|
namespace Ocelot.UnitTests.DependencyInjection
|
||||||
{
|
{
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Ocelot.Configuration.File;
|
||||||
|
|
||||||
public class ConfigurationBuilderExtensionsTests
|
public class ConfigurationBuilderExtensionsTests
|
||||||
{
|
{
|
||||||
private IConfigurationRoot _configuration;
|
private IConfigurationRoot _configuration;
|
||||||
@ -20,6 +25,61 @@ namespace Ocelot.UnitTests.DependencyInjection
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_merge_files()
|
||||||
|
{
|
||||||
|
var globalConfig = new FileConfiguration
|
||||||
|
{
|
||||||
|
GlobalConfiguration = new FileGlobalConfiguration
|
||||||
|
{
|
||||||
|
BaseUrl = "BaseUrl",
|
||||||
|
RateLimitOptions = new FileRateLimitOptions
|
||||||
|
{
|
||||||
|
HttpStatusCode = 500,
|
||||||
|
ClientIdHeader = "ClientIdHeader",
|
||||||
|
DisableRateLimitHeaders = true,
|
||||||
|
QuotaExceededMessage = "QuotaExceededMessage",
|
||||||
|
RateLimitCounterPrefix = "RateLimitCounterPrefix"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var reRoute = new FileConfiguration
|
||||||
|
{
|
||||||
|
ReRoutes = new List<FileReRoute>
|
||||||
|
{
|
||||||
|
new FileReRoute
|
||||||
|
{
|
||||||
|
DownstreamScheme = "DownstreamScheme",
|
||||||
|
Key = "Key",
|
||||||
|
UpstreamHost = "UpstreamHost",
|
||||||
|
UpstreamHttpMethod = new List<string>
|
||||||
|
{
|
||||||
|
"UpstreamHttpMethod"
|
||||||
|
},
|
||||||
|
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||||
|
{
|
||||||
|
new FileHostAndPort
|
||||||
|
{
|
||||||
|
Host = "Host",
|
||||||
|
Port = 80
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var globalJson = JsonConvert.SerializeObject(globalConfig);
|
||||||
|
//File.WriteAllText("ocelot.global.json", globalJson);
|
||||||
|
|
||||||
|
var reRouteJson = JsonConvert.SerializeObject(reRoute);
|
||||||
|
//File.WriteAllText("ocelot.reRoute.json", reRouteJson);
|
||||||
|
|
||||||
|
IConfigurationBuilder builder = new ConfigurationBuilder();
|
||||||
|
//builder.AddOcelot();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheBaseUrl(string baseUrl)
|
private void GivenTheBaseUrl(string baseUrl)
|
||||||
{
|
{
|
||||||
#pragma warning disable CS0618
|
#pragma warning disable CS0618
|
||||||
|
Loading…
x
Reference in New Issue
Block a user