mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
renamed and removed some stuff that wasnt needed
This commit is contained in:
parent
84256e7bac
commit
707f1d6908
@ -25,8 +25,6 @@ namespace Ocelot.Library.Authentication
|
|||||||
ScopeSecret = authOptions.ScopeSecret
|
ScopeSecret = authOptions.ScopeSecret
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.UseMvc();
|
|
||||||
|
|
||||||
var authenticationNext = builder.Build();
|
var authenticationNext = builder.Build();
|
||||||
|
|
||||||
return new OkResponse<RequestDelegate>(authenticationNext);
|
return new OkResponse<RequestDelegate>(authenticationNext);
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
using Ocelot.Library.RequestBuilder;
|
using System.Collections.Generic;
|
||||||
|
using Ocelot.Library.Configuration;
|
||||||
|
|
||||||
namespace Ocelot.Library.Builder
|
namespace Ocelot.Library.Builder
|
||||||
{
|
{
|
||||||
using System.Collections.Generic;
|
|
||||||
using Configuration;
|
|
||||||
|
|
||||||
public class ReRouteBuilder
|
public class ReRouteBuilder
|
||||||
{
|
{
|
||||||
private string _downstreamTemplate;
|
private string _downstreamTemplate;
|
||||||
@ -18,7 +16,7 @@ namespace Ocelot.Library.Builder
|
|||||||
private List<string> _additionalScopes;
|
private List<string> _additionalScopes;
|
||||||
private bool _requireHttps;
|
private bool _requireHttps;
|
||||||
private string _scopeSecret;
|
private string _scopeSecret;
|
||||||
private List<ConfigurationHeaderExtractorProperties> _configHeaderExtractorProperties;
|
private List<ClaimToHeader> _configHeaderExtractorProperties;
|
||||||
|
|
||||||
public ReRouteBuilder()
|
public ReRouteBuilder()
|
||||||
{
|
{
|
||||||
@ -89,7 +87,7 @@ namespace Ocelot.Library.Builder
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReRouteBuilder WithConfigurationHeaderExtractorProperties(List<ConfigurationHeaderExtractorProperties> input)
|
public ReRouteBuilder WithConfigurationHeaderExtractorProperties(List<ClaimToHeader> input)
|
||||||
{
|
{
|
||||||
_configHeaderExtractorProperties = input;
|
_configHeaderExtractorProperties = input;
|
||||||
return this;
|
return this;
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
namespace Ocelot.Library.RequestBuilder
|
namespace Ocelot.Library.Configuration
|
||||||
{
|
{
|
||||||
public class ConfigurationHeaderExtractorProperties
|
public class ClaimToHeader
|
||||||
{
|
{
|
||||||
public ConfigurationHeaderExtractorProperties(string headerKey, string claimKey, string delimiter, int index)
|
public ClaimToHeader(string headerKey, string claimKey, string delimiter, int index)
|
||||||
{
|
{
|
||||||
ClaimKey = claimKey;
|
ClaimKey = claimKey;
|
||||||
Delimiter = delimiter;
|
Delimiter = delimiter;
|
@ -2,17 +2,18 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Ocelot.Library.Errors;
|
using Ocelot.Library.Errors;
|
||||||
|
using Ocelot.Library.RequestBuilder;
|
||||||
using Ocelot.Library.Responses;
|
using Ocelot.Library.Responses;
|
||||||
|
|
||||||
namespace Ocelot.Library.RequestBuilder
|
namespace Ocelot.Library.Configuration
|
||||||
{
|
{
|
||||||
public class ConfigurationHeaderExtrator : IConfigurationHeaderExtrator
|
public class ClaimToHeaderConfigurationParser : IClaimToHeaderConfigurationParser
|
||||||
{
|
{
|
||||||
private readonly Regex _claimRegex = new Regex("Claims\\[.*\\]");
|
private readonly Regex _claimRegex = new Regex("Claims\\[.*\\]");
|
||||||
private readonly Regex _indexRegex = new Regex("value\\[.*\\]");
|
private readonly Regex _indexRegex = new Regex("value\\[.*\\]");
|
||||||
private const string SplitToken = ">";
|
private const string SplitToken = ">";
|
||||||
|
|
||||||
public Response<ConfigurationHeaderExtractorProperties> Extract(string headerKey, string value)
|
public Response<ClaimToHeader> Extract(string headerKey, string value)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -20,7 +21,7 @@ namespace Ocelot.Library.RequestBuilder
|
|||||||
|
|
||||||
if (instructions.Length <= 1)
|
if (instructions.Length <= 1)
|
||||||
{
|
{
|
||||||
return new ErrorResponse<ConfigurationHeaderExtractorProperties>(
|
return new ErrorResponse<ClaimToHeader>(
|
||||||
new List<Error>
|
new List<Error>
|
||||||
{
|
{
|
||||||
new NoInstructionsError(SplitToken)
|
new NoInstructionsError(SplitToken)
|
||||||
@ -31,7 +32,7 @@ namespace Ocelot.Library.RequestBuilder
|
|||||||
|
|
||||||
if (!claimMatch)
|
if (!claimMatch)
|
||||||
{
|
{
|
||||||
return new ErrorResponse<ConfigurationHeaderExtractorProperties>(
|
return new ErrorResponse<ClaimToHeader>(
|
||||||
new List<Error>
|
new List<Error>
|
||||||
{
|
{
|
||||||
new InstructionNotForClaimsError()
|
new InstructionNotForClaimsError()
|
||||||
@ -48,12 +49,12 @@ namespace Ocelot.Library.RequestBuilder
|
|||||||
delimiter = instructions[2].Trim();
|
delimiter = instructions[2].Trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new OkResponse<ConfigurationHeaderExtractorProperties>(
|
return new OkResponse<ClaimToHeader>(
|
||||||
new ConfigurationHeaderExtractorProperties(headerKey, claimKey, delimiter, index));
|
new ClaimToHeader(headerKey, claimKey, delimiter, index));
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
return new ErrorResponse<ConfigurationHeaderExtractorProperties>(
|
return new ErrorResponse<ClaimToHeader>(
|
||||||
new List<Error>
|
new List<Error>
|
||||||
{
|
{
|
||||||
new ParsingConfigurationHeaderError(exception)
|
new ParsingConfigurationHeaderError(exception)
|
@ -0,0 +1,9 @@
|
|||||||
|
using Ocelot.Library.Responses;
|
||||||
|
|
||||||
|
namespace Ocelot.Library.Configuration
|
||||||
|
{
|
||||||
|
public interface IClaimToHeaderConfigurationParser
|
||||||
|
{
|
||||||
|
Response<ClaimToHeader> Extract(string headerKey, string value);
|
||||||
|
}
|
||||||
|
}
|
@ -16,17 +16,17 @@ namespace Ocelot.Library.Configuration
|
|||||||
private readonly List<ReRoute> _reRoutes;
|
private readonly List<ReRoute> _reRoutes;
|
||||||
private const string RegExMatchEverything = ".*";
|
private const string RegExMatchEverything = ".*";
|
||||||
private const string RegExMatchEndString = "$";
|
private const string RegExMatchEndString = "$";
|
||||||
private readonly IConfigurationHeaderExtrator _configurationHeaderExtrator;
|
private readonly IClaimToHeaderConfigurationParser _claimToHeaderConfigurationParser;
|
||||||
private readonly ILogger<OcelotConfiguration> _logger;
|
private readonly ILogger<OcelotConfiguration> _logger;
|
||||||
|
|
||||||
public OcelotConfiguration(IOptions<YamlConfiguration> options,
|
public OcelotConfiguration(IOptions<YamlConfiguration> options,
|
||||||
IConfigurationValidator configurationValidator,
|
IConfigurationValidator configurationValidator,
|
||||||
IConfigurationHeaderExtrator configurationHeaderExtrator,
|
IClaimToHeaderConfigurationParser claimToHeaderConfigurationParser,
|
||||||
ILogger<OcelotConfiguration> logger)
|
ILogger<OcelotConfiguration> logger)
|
||||||
{
|
{
|
||||||
_options = options;
|
_options = options;
|
||||||
_configurationValidator = configurationValidator;
|
_configurationValidator = configurationValidator;
|
||||||
_configurationHeaderExtrator = configurationHeaderExtrator;
|
_claimToHeaderConfigurationParser = claimToHeaderConfigurationParser;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_reRoutes = new List<ReRoute>();
|
_reRoutes = new List<ReRoute>();
|
||||||
SetUpConfiguration();
|
SetUpConfiguration();
|
||||||
@ -92,17 +92,17 @@ namespace Ocelot.Library.Configuration
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
_reRoutes.Add(new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod,
|
_reRoutes.Add(new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod,
|
||||||
upstreamTemplate, isAuthenticated, null, new List<ConfigurationHeaderExtractorProperties>()));
|
upstreamTemplate, isAuthenticated, null, new List<ClaimToHeader>()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<ConfigurationHeaderExtractorProperties> GetHeadersToAddToRequest(YamlReRoute reRoute)
|
private List<ClaimToHeader> GetHeadersToAddToRequest(YamlReRoute reRoute)
|
||||||
{
|
{
|
||||||
var configHeaders = new List<ConfigurationHeaderExtractorProperties>();
|
var configHeaders = new List<ClaimToHeader>();
|
||||||
|
|
||||||
foreach (var add in reRoute.AddHeadersToRequest)
|
foreach (var add in reRoute.AddHeadersToRequest)
|
||||||
{
|
{
|
||||||
var configurationHeader = _configurationHeaderExtrator.Extract(add.Key, add.Value);
|
var configurationHeader = _claimToHeaderConfigurationParser.Extract(add.Key, add.Value);
|
||||||
|
|
||||||
if (configurationHeader.IsError)
|
if (configurationHeader.IsError)
|
||||||
{
|
{
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Ocelot.Library.RequestBuilder;
|
|
||||||
|
|
||||||
namespace Ocelot.Library.Configuration
|
namespace Ocelot.Library.Configuration
|
||||||
{
|
{
|
||||||
public class ReRoute
|
public class ReRoute
|
||||||
{
|
{
|
||||||
public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated, AuthenticationOptions authenticationOptions, List<ConfigurationHeaderExtractorProperties> configurationHeaderExtractorProperties)
|
public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated, AuthenticationOptions authenticationOptions, List<ClaimToHeader> configurationHeaderExtractorProperties)
|
||||||
{
|
{
|
||||||
DownstreamTemplate = downstreamTemplate;
|
DownstreamTemplate = downstreamTemplate;
|
||||||
UpstreamTemplate = upstreamTemplate;
|
UpstreamTemplate = upstreamTemplate;
|
||||||
@ -13,8 +12,8 @@ namespace Ocelot.Library.Configuration
|
|||||||
UpstreamTemplatePattern = upstreamTemplatePattern;
|
UpstreamTemplatePattern = upstreamTemplatePattern;
|
||||||
IsAuthenticated = isAuthenticated;
|
IsAuthenticated = isAuthenticated;
|
||||||
AuthenticationOptions = authenticationOptions;
|
AuthenticationOptions = authenticationOptions;
|
||||||
ConfigurationHeaderExtractorProperties = configurationHeaderExtractorProperties
|
ClaimsToHeaders = configurationHeaderExtractorProperties
|
||||||
?? new List<ConfigurationHeaderExtractorProperties>();
|
?? new List<ClaimToHeader>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string DownstreamTemplate { get; private set; }
|
public string DownstreamTemplate { get; private set; }
|
||||||
@ -23,6 +22,6 @@ namespace Ocelot.Library.Configuration
|
|||||||
public string UpstreamHttpMethod { get; private set; }
|
public string UpstreamHttpMethod { get; private set; }
|
||||||
public bool IsAuthenticated { get; private set; }
|
public bool IsAuthenticated { get; private set; }
|
||||||
public AuthenticationOptions AuthenticationOptions { get; private set; }
|
public AuthenticationOptions AuthenticationOptions { get; private set; }
|
||||||
public List<ConfigurationHeaderExtractorProperties> ConfigurationHeaderExtractorProperties { get; private set; }
|
public List<ClaimToHeader> ClaimsToHeaders { get; private set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -18,12 +18,18 @@
|
|||||||
{
|
{
|
||||||
public static IServiceCollection AddOcelot(this IServiceCollection services, IConfigurationRoot configurationRoot)
|
public static IServiceCollection AddOcelot(this IServiceCollection services, IConfigurationRoot configurationRoot)
|
||||||
{
|
{
|
||||||
|
// framework services
|
||||||
|
services.AddOptions();
|
||||||
|
services.AddMvcCore().AddJsonFormatters();
|
||||||
|
services.AddLogging();
|
||||||
|
|
||||||
|
// initial configuration from yaml
|
||||||
services.Configure<YamlConfiguration>(configurationRoot);
|
services.Configure<YamlConfiguration>(configurationRoot);
|
||||||
|
|
||||||
// Add framework services.
|
// ocelot services.
|
||||||
services.AddSingleton<IAddHeadersToRequest, AddHeadersToRequest>();
|
services.AddSingleton<IAddHeadersToRequest, AddHeadersToRequest>();
|
||||||
services.AddSingleton<IClaimsParser, ClaimsParser>();
|
services.AddSingleton<IClaimsParser, ClaimsParser>();
|
||||||
services.AddSingleton<IConfigurationHeaderExtrator, ConfigurationHeaderExtrator>();
|
services.AddSingleton<IClaimToHeaderConfigurationParser, ClaimToHeaderConfigurationParser>();
|
||||||
services.AddSingleton<IConfigurationValidator, ConfigurationValidator>();
|
services.AddSingleton<IConfigurationValidator, ConfigurationValidator>();
|
||||||
services.AddSingleton<IOcelotConfiguration, OcelotConfiguration>();
|
services.AddSingleton<IOcelotConfiguration, OcelotConfiguration>();
|
||||||
services.AddSingleton<IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
|
services.AddSingleton<IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
|
||||||
@ -38,6 +44,7 @@
|
|||||||
services.AddSingleton<IAuthenticationHandlerCreator, AuthenticationHandlerCreator>();
|
services.AddSingleton<IAuthenticationHandlerCreator, AuthenticationHandlerCreator>();
|
||||||
|
|
||||||
// see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc
|
// see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc
|
||||||
|
// could maybe use a scoped data repository
|
||||||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||||
services.AddScoped<IScopedRequestDataRepository, ScopedRequestDataRepository>();
|
services.AddScoped<IScopedRequestDataRepository, ScopedRequestDataRepository>();
|
||||||
|
|
||||||
|
@ -30,9 +30,9 @@ namespace Ocelot.Library.Middleware
|
|||||||
{
|
{
|
||||||
var downstreamRoute = _scopedRequestDataRepository.Get<DownstreamRoute>("DownstreamRoute");
|
var downstreamRoute = _scopedRequestDataRepository.Get<DownstreamRoute>("DownstreamRoute");
|
||||||
|
|
||||||
if (downstreamRoute.Data.ReRoute.ConfigurationHeaderExtractorProperties.Any())
|
if (downstreamRoute.Data.ReRoute.ClaimsToHeaders.Any())
|
||||||
{
|
{
|
||||||
_addHeadersToRequest.SetHeadersOnContext(downstreamRoute.Data.ReRoute.ConfigurationHeaderExtractorProperties, context);
|
_addHeadersToRequest.SetHeadersOnContext(downstreamRoute.Data.ReRoute.ClaimsToHeaders, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
await _next.Invoke(context);
|
await _next.Invoke(context);
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Primitives;
|
using Microsoft.Extensions.Primitives;
|
||||||
|
using Ocelot.Library.Configuration;
|
||||||
using Ocelot.Library.Responses;
|
using Ocelot.Library.Responses;
|
||||||
|
|
||||||
namespace Ocelot.Library.RequestBuilder
|
namespace Ocelot.Library.RequestBuilder
|
||||||
@ -15,7 +16,7 @@ namespace Ocelot.Library.RequestBuilder
|
|||||||
_claimsParser = claimsParser;
|
_claimsParser = claimsParser;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Response SetHeadersOnContext(List<ConfigurationHeaderExtractorProperties> configurationHeaderExtractorProperties, HttpContext context)
|
public Response SetHeadersOnContext(List<ClaimToHeader> configurationHeaderExtractorProperties, HttpContext context)
|
||||||
{
|
{
|
||||||
foreach (var config in configurationHeaderExtractorProperties)
|
foreach (var config in configurationHeaderExtractorProperties)
|
||||||
{
|
{
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Ocelot.Library.Configuration;
|
||||||
using Ocelot.Library.Responses;
|
using Ocelot.Library.Responses;
|
||||||
|
|
||||||
namespace Ocelot.Library.RequestBuilder
|
namespace Ocelot.Library.RequestBuilder
|
||||||
{
|
{
|
||||||
public interface IAddHeadersToRequest
|
public interface IAddHeadersToRequest
|
||||||
{
|
{
|
||||||
Response SetHeadersOnContext(List<ConfigurationHeaderExtractorProperties> configurationHeaderExtractorProperties,
|
Response SetHeadersOnContext(List<ClaimToHeader> configurationHeaderExtractorProperties,
|
||||||
HttpContext context);
|
HttpContext context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using Ocelot.Library.Responses;
|
|
||||||
|
|
||||||
namespace Ocelot.Library.RequestBuilder
|
|
||||||
{
|
|
||||||
public interface IConfigurationHeaderExtrator
|
|
||||||
{
|
|
||||||
Response<ConfigurationHeaderExtractorProperties> Extract(string headerKey, string value);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
|
|
||||||
namespace Ocelot.Controllers
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// This controller is a catch all for requests so that if we build it into a pipeline
|
|
||||||
/// the requests get authorised.
|
|
||||||
/// </summary>
|
|
||||||
public class HomeController : Controller
|
|
||||||
{
|
|
||||||
//[Authorize]
|
|
||||||
[Route("{*url}")]
|
|
||||||
public void Index()
|
|
||||||
{
|
|
||||||
if (true == true)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -28,11 +28,6 @@ namespace Ocelot
|
|||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddOptions();
|
|
||||||
services.AddMvc();
|
|
||||||
services.AddMvcCore().AddAuthorization().AddJsonFormatters();
|
|
||||||
services.AddAuthentication();
|
|
||||||
services.AddLogging();
|
|
||||||
services.AddOcelot(Configuration);
|
services.AddOcelot(Configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ namespace Ocelot.AcceptanceTests
|
|||||||
{
|
{
|
||||||
using Library.Configuration.Yaml;
|
using Library.Configuration.Yaml;
|
||||||
|
|
||||||
public class OcelotTests : IDisposable
|
public class RoutingTests : IDisposable
|
||||||
{
|
{
|
||||||
private TestServer _server;
|
private TestServer _server;
|
||||||
private HttpClient _client;
|
private HttpClient _client;
|
||||||
@ -28,7 +28,7 @@ namespace Ocelot.AcceptanceTests
|
|||||||
// Sadly we need to change this when we update the netcoreapp version to make the test update the config correctly
|
// Sadly we need to change this when we update the netcoreapp version to make the test update the config correctly
|
||||||
private double _netCoreAppVersion = 1.4;
|
private double _netCoreAppVersion = 1.4;
|
||||||
|
|
||||||
public OcelotTests()
|
public RoutingTests()
|
||||||
{
|
{
|
||||||
_configurationPath = $"./bin/Debug/netcoreapp{_netCoreAppVersion}/configuration.yaml";
|
_configurationPath = $"./bin/Debug/netcoreapp{_netCoreAppVersion}/configuration.yaml";
|
||||||
}
|
}
|
@ -20,13 +20,13 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
private readonly Mock<IConfigurationValidator> _validator;
|
private readonly Mock<IConfigurationValidator> _validator;
|
||||||
private OcelotConfiguration _config;
|
private OcelotConfiguration _config;
|
||||||
private YamlConfiguration _yamlConfiguration;
|
private YamlConfiguration _yamlConfiguration;
|
||||||
private readonly Mock<IConfigurationHeaderExtrator> _configExtractor;
|
private readonly Mock<IClaimToHeaderConfigurationParser> _configExtractor;
|
||||||
private readonly Mock<ILogger<OcelotConfiguration>> _logger;
|
private readonly Mock<ILogger<OcelotConfiguration>> _logger;
|
||||||
|
|
||||||
public OcelotConfigurationTests()
|
public OcelotConfigurationTests()
|
||||||
{
|
{
|
||||||
_logger = new Mock<ILogger<OcelotConfiguration>>();
|
_logger = new Mock<ILogger<OcelotConfiguration>>();
|
||||||
_configExtractor = new Mock<IConfigurationHeaderExtrator>();
|
_configExtractor = new Mock<IClaimToHeaderConfigurationParser>();
|
||||||
_validator = new Mock<IConfigurationValidator>();
|
_validator = new Mock<IConfigurationValidator>();
|
||||||
_yamlConfig = new Mock<IOptions<YamlConfiguration>>();
|
_yamlConfig = new Mock<IOptions<YamlConfiguration>>();
|
||||||
}
|
}
|
||||||
@ -75,9 +75,9 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
.WithRequireHttps(false)
|
.WithRequireHttps(false)
|
||||||
.WithScopeSecret("secret")
|
.WithScopeSecret("secret")
|
||||||
.WithAuthenticationProviderScopeName("api")
|
.WithAuthenticationProviderScopeName("api")
|
||||||
.WithConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
|
.WithConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||||
{
|
{
|
||||||
new ConfigurationHeaderExtractorProperties("CustomerId", "CustomerId", "", 0),
|
new ClaimToHeader("CustomerId", "CustomerId", "", 0),
|
||||||
})
|
})
|
||||||
.Build()
|
.Build()
|
||||||
};
|
};
|
||||||
@ -108,18 +108,18 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
.And(x => x.GivenTheYamlConfigIsValid())
|
.And(x => x.GivenTheYamlConfigIsValid())
|
||||||
.And(x => x.GivenTheConfigHeaderExtractorReturns(new ConfigurationHeaderExtractorProperties("CustomerId", "CustomerId", "", 0)))
|
.And(x => x.GivenTheConfigHeaderExtractorReturns(new ClaimToHeader("CustomerId", "CustomerId", "", 0)))
|
||||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||||
.Then(x => x.ThenTheReRoutesAre(expected))
|
.Then(x => x.ThenTheReRoutesAre(expected))
|
||||||
.And(x => x.ThenTheAuthenticationOptionsAre(expected))
|
.And(x => x.ThenTheAuthenticationOptionsAre(expected))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheConfigHeaderExtractorReturns(ConfigurationHeaderExtractorProperties expected)
|
private void GivenTheConfigHeaderExtractorReturns(ClaimToHeader expected)
|
||||||
{
|
{
|
||||||
_configExtractor
|
_configExtractor
|
||||||
.Setup(x => x.Extract(It.IsAny<string>(), It.IsAny<string>()))
|
.Setup(x => x.Extract(It.IsAny<string>(), It.IsAny<string>()))
|
||||||
.Returns(new OkResponse<ConfigurationHeaderExtractorProperties>(expected));
|
.Returns(new OkResponse<ClaimToHeader>(expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
@ -8,6 +8,7 @@ using Microsoft.AspNetCore.TestHost;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Library.Builder;
|
using Ocelot.Library.Builder;
|
||||||
|
using Ocelot.Library.Configuration;
|
||||||
using Ocelot.Library.DownstreamRouteFinder;
|
using Ocelot.Library.DownstreamRouteFinder;
|
||||||
using Ocelot.Library.Middleware;
|
using Ocelot.Library.Middleware;
|
||||||
using Ocelot.Library.Repository;
|
using Ocelot.Library.Repository;
|
||||||
@ -60,9 +61,9 @@ namespace Ocelot.UnitTests.Middleware
|
|||||||
var downstreamRoute = new DownstreamRoute(new List<TemplateVariableNameAndValue>(),
|
var downstreamRoute = new DownstreamRoute(new List<TemplateVariableNameAndValue>(),
|
||||||
new ReRouteBuilder()
|
new ReRouteBuilder()
|
||||||
.WithDownstreamTemplate("any old string")
|
.WithDownstreamTemplate("any old string")
|
||||||
.WithConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
|
.WithConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||||
{
|
{
|
||||||
new ConfigurationHeaderExtractorProperties("UserId", "Subject", "", 0)
|
new ClaimToHeader("UserId", "Subject", "", 0)
|
||||||
})
|
})
|
||||||
.Build());
|
.Build());
|
||||||
|
|
||||||
@ -76,7 +77,7 @@ namespace Ocelot.UnitTests.Middleware
|
|||||||
private void GivenTheAddHeadersToRequestReturns(string claimValue)
|
private void GivenTheAddHeadersToRequestReturns(string claimValue)
|
||||||
{
|
{
|
||||||
_addHeaders
|
_addHeaders
|
||||||
.Setup(x => x.SetHeadersOnContext(It.IsAny<List<ConfigurationHeaderExtractorProperties>>(),
|
.Setup(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToHeader>>(),
|
||||||
It.IsAny<HttpContext>()))
|
It.IsAny<HttpContext>()))
|
||||||
.Returns(new OkResponse());
|
.Returns(new OkResponse());
|
||||||
}
|
}
|
||||||
@ -84,7 +85,7 @@ namespace Ocelot.UnitTests.Middleware
|
|||||||
private void ThenTheAddHeadersToRequestIsCalledCorrectly()
|
private void ThenTheAddHeadersToRequestIsCalledCorrectly()
|
||||||
{
|
{
|
||||||
_addHeaders
|
_addHeaders
|
||||||
.Verify(x => x.SetHeadersOnContext(It.IsAny<List<ConfigurationHeaderExtractorProperties>>(),
|
.Verify(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToHeader>>(),
|
||||||
It.IsAny<HttpContext>()), Times.Once);
|
It.IsAny<HttpContext>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ using System.Security.Claims;
|
|||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Primitives;
|
using Microsoft.Extensions.Primitives;
|
||||||
using Moq;
|
using Moq;
|
||||||
|
using Ocelot.Library.Configuration;
|
||||||
using Ocelot.Library.Errors;
|
using Ocelot.Library.Errors;
|
||||||
using Ocelot.Library.RequestBuilder;
|
using Ocelot.Library.RequestBuilder;
|
||||||
using Ocelot.Library.Responses;
|
using Ocelot.Library.Responses;
|
||||||
@ -17,7 +18,7 @@ namespace Ocelot.UnitTests.RequestBuilder
|
|||||||
{
|
{
|
||||||
private readonly AddHeadersToRequest _addHeadersToRequest;
|
private readonly AddHeadersToRequest _addHeadersToRequest;
|
||||||
private readonly Mock<IClaimsParser> _parser;
|
private readonly Mock<IClaimsParser> _parser;
|
||||||
private List<ConfigurationHeaderExtractorProperties> _configuration;
|
private List<ClaimToHeader> _configuration;
|
||||||
private HttpContext _context;
|
private HttpContext _context;
|
||||||
private Response _result;
|
private Response _result;
|
||||||
private Response<string> _claimValue;
|
private Response<string> _claimValue;
|
||||||
@ -40,9 +41,9 @@ namespace Ocelot.UnitTests.RequestBuilder
|
|||||||
};
|
};
|
||||||
|
|
||||||
this.Given(
|
this.Given(
|
||||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
|
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||||
{
|
{
|
||||||
new ConfigurationHeaderExtractorProperties("header-key", "", "", 0)
|
new ClaimToHeader("header-key", "", "", 0)
|
||||||
}))
|
}))
|
||||||
.Given(x => x.GivenHttpContext(context))
|
.Given(x => x.GivenHttpContext(context))
|
||||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||||
@ -66,9 +67,9 @@ namespace Ocelot.UnitTests.RequestBuilder
|
|||||||
context.Request.Headers.Add("header-key", new StringValues("initial"));
|
context.Request.Headers.Add("header-key", new StringValues("initial"));
|
||||||
|
|
||||||
this.Given(
|
this.Given(
|
||||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
|
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||||
{
|
{
|
||||||
new ConfigurationHeaderExtractorProperties("header-key", "", "", 0)
|
new ClaimToHeader("header-key", "", "", 0)
|
||||||
}))
|
}))
|
||||||
.Given(x => x.GivenHttpContext(context))
|
.Given(x => x.GivenHttpContext(context))
|
||||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||||
@ -82,9 +83,9 @@ namespace Ocelot.UnitTests.RequestBuilder
|
|||||||
public void should_return_error()
|
public void should_return_error()
|
||||||
{
|
{
|
||||||
this.Given(
|
this.Given(
|
||||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
|
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||||
{
|
{
|
||||||
new ConfigurationHeaderExtractorProperties("", "", "", 0)
|
new ClaimToHeader("", "", "", 0)
|
||||||
}))
|
}))
|
||||||
.Given(x => x.GivenHttpContext(new DefaultHttpContext()))
|
.Given(x => x.GivenHttpContext(new DefaultHttpContext()))
|
||||||
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
||||||
@ -102,7 +103,7 @@ namespace Ocelot.UnitTests.RequestBuilder
|
|||||||
header.Value.First().ShouldBe(_claimValue.Data);
|
header.Value.First().ShouldBe(_claimValue.Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenConfigurationHeaderExtractorProperties(List<ConfigurationHeaderExtractorProperties> configuration)
|
private void GivenConfigurationHeaderExtractorProperties(List<ClaimToHeader> configuration)
|
||||||
{
|
{
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Ocelot.Library.Configuration;
|
||||||
using Ocelot.Library.Errors;
|
using Ocelot.Library.Errors;
|
||||||
using Ocelot.Library.RequestBuilder;
|
using Ocelot.Library.RequestBuilder;
|
||||||
using Ocelot.Library.Responses;
|
using Ocelot.Library.Responses;
|
||||||
@ -12,12 +13,12 @@ namespace Ocelot.UnitTests.RequestBuilder
|
|||||||
public class ConfigurationHeadersExtractorTests
|
public class ConfigurationHeadersExtractorTests
|
||||||
{
|
{
|
||||||
private Dictionary<string, string> _dictionary;
|
private Dictionary<string, string> _dictionary;
|
||||||
private readonly IConfigurationHeaderExtrator _configurationHeaderExtrator;
|
private readonly IClaimToHeaderConfigurationParser _claimToHeaderConfigurationParser;
|
||||||
private Response<ConfigurationHeaderExtractorProperties> _result;
|
private Response<ClaimToHeader> _result;
|
||||||
|
|
||||||
public ConfigurationHeadersExtractorTests()
|
public ConfigurationHeadersExtractorTests()
|
||||||
{
|
{
|
||||||
_configurationHeaderExtrator = new ConfigurationHeaderExtrator();
|
_claimToHeaderConfigurationParser = new ClaimToHeaderConfigurationParser();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -30,7 +31,7 @@ namespace Ocelot.UnitTests.RequestBuilder
|
|||||||
.When(x => x.WhenICallTheExtractor())
|
.When(x => x.WhenICallTheExtractor())
|
||||||
.Then(
|
.Then(
|
||||||
x =>
|
x =>
|
||||||
x.ThenAnErrorIsReturned(new ErrorResponse<ConfigurationHeaderExtractorProperties>(
|
x.ThenAnErrorIsReturned(new ErrorResponse<ClaimToHeader>(
|
||||||
new List<Error>
|
new List<Error>
|
||||||
{
|
{
|
||||||
new NoInstructionsError(">")
|
new NoInstructionsError(">")
|
||||||
@ -48,7 +49,7 @@ namespace Ocelot.UnitTests.RequestBuilder
|
|||||||
.When(x => x.WhenICallTheExtractor())
|
.When(x => x.WhenICallTheExtractor())
|
||||||
.Then(
|
.Then(
|
||||||
x =>
|
x =>
|
||||||
x.ThenAnErrorIsReturned(new ErrorResponse<ConfigurationHeaderExtractorProperties>(
|
x.ThenAnErrorIsReturned(new ErrorResponse<ClaimToHeader>(
|
||||||
new List<Error>
|
new List<Error>
|
||||||
{
|
{
|
||||||
new InstructionNotForClaimsError()
|
new InstructionNotForClaimsError()
|
||||||
@ -67,8 +68,8 @@ namespace Ocelot.UnitTests.RequestBuilder
|
|||||||
.Then(
|
.Then(
|
||||||
x =>
|
x =>
|
||||||
x.ThenTheClaimParserPropertiesAreReturned(
|
x.ThenTheClaimParserPropertiesAreReturned(
|
||||||
new OkResponse<ConfigurationHeaderExtractorProperties>(
|
new OkResponse<ClaimToHeader>(
|
||||||
new ConfigurationHeaderExtractorProperties("CustomerId", "CustomerId", "", 0))))
|
new ClaimToHeader("CustomerId", "CustomerId", "", 0))))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,18 +84,18 @@ namespace Ocelot.UnitTests.RequestBuilder
|
|||||||
.Then(
|
.Then(
|
||||||
x =>
|
x =>
|
||||||
x.ThenTheClaimParserPropertiesAreReturned(
|
x.ThenTheClaimParserPropertiesAreReturned(
|
||||||
new OkResponse<ConfigurationHeaderExtractorProperties>(
|
new OkResponse<ClaimToHeader>(
|
||||||
new ConfigurationHeaderExtractorProperties("UserId", "Subject", "|", 0))))
|
new ClaimToHeader("UserId", "Subject", "|", 0))))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenAnErrorIsReturned(Response<ConfigurationHeaderExtractorProperties> expected)
|
private void ThenAnErrorIsReturned(Response<ClaimToHeader> expected)
|
||||||
{
|
{
|
||||||
_result.IsError.ShouldBe(expected.IsError);
|
_result.IsError.ShouldBe(expected.IsError);
|
||||||
_result.Errors[0].ShouldBeOfType(expected.Errors[0].GetType());
|
_result.Errors[0].ShouldBeOfType(expected.Errors[0].GetType());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheClaimParserPropertiesAreReturned(Response<ConfigurationHeaderExtractorProperties> expected)
|
private void ThenTheClaimParserPropertiesAreReturned(Response<ClaimToHeader> expected)
|
||||||
{
|
{
|
||||||
_result.Data.ClaimKey.ShouldBe(expected.Data.ClaimKey);
|
_result.Data.ClaimKey.ShouldBe(expected.Data.ClaimKey);
|
||||||
_result.Data.Delimiter.ShouldBe(expected.Data.Delimiter);
|
_result.Data.Delimiter.ShouldBe(expected.Data.Delimiter);
|
||||||
@ -105,7 +106,7 @@ namespace Ocelot.UnitTests.RequestBuilder
|
|||||||
private void WhenICallTheExtractor()
|
private void WhenICallTheExtractor()
|
||||||
{
|
{
|
||||||
var first = _dictionary.First();
|
var first = _dictionary.First();
|
||||||
_result = _configurationHeaderExtrator.Extract(first.Key, first.Value);
|
_result = _claimToHeaderConfigurationParser.Extract(first.Key, first.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheDictionaryIs(Dictionary<string, string> dictionary)
|
private void GivenTheDictionaryIs(Dictionary<string, string> dictionary)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user