mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 20:12:51 +08:00
Feature/rename middleware (#637)
* #630 only set status code if response hasnt started, otherwise exception * #623 made {RemoteIpAddress} available as placeholder so you can do x-forwarded-for * #623 local address different on mac, windows and linux for integration test * renamed some middlewares so they make more sense * add downstreamroutefindermiddleware benchmark
This commit is contained in:
parent
4a8f4c2e03
commit
dc28d49bda
@ -3,11 +3,11 @@ using Ocelot.Middleware.Pipeline;
|
|||||||
|
|
||||||
namespace Ocelot.Claims.Middleware
|
namespace Ocelot.Claims.Middleware
|
||||||
{
|
{
|
||||||
public static class ClaimsBuilderMiddlewareExtensions
|
public static class ClaimsToClaimsMiddlewareExtensions
|
||||||
{
|
{
|
||||||
public static IOcelotPipelineBuilder UseClaimsBuilderMiddleware(this IOcelotPipelineBuilder builder)
|
public static IOcelotPipelineBuilder UseClaimsToClaimsMiddleware(this IOcelotPipelineBuilder builder)
|
||||||
{
|
{
|
||||||
return builder.UseMiddleware<ClaimsBuilderMiddleware>();
|
return builder.UseMiddleware<ClaimsToClaimsMiddleware>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,15 +8,15 @@ using Ocelot.Middleware;
|
|||||||
|
|
||||||
namespace Ocelot.Claims.Middleware
|
namespace Ocelot.Claims.Middleware
|
||||||
{
|
{
|
||||||
public class ClaimsBuilderMiddleware : OcelotMiddleware
|
public class ClaimsToClaimsMiddleware : OcelotMiddleware
|
||||||
{
|
{
|
||||||
private readonly OcelotRequestDelegate _next;
|
private readonly OcelotRequestDelegate _next;
|
||||||
private readonly IAddClaimsToRequest _addClaimsToRequest;
|
private readonly IAddClaimsToRequest _addClaimsToRequest;
|
||||||
|
|
||||||
public ClaimsBuilderMiddleware(OcelotRequestDelegate next,
|
public ClaimsToClaimsMiddleware(OcelotRequestDelegate next,
|
||||||
IOcelotLoggerFactory loggerFactory,
|
IOcelotLoggerFactory loggerFactory,
|
||||||
IAddClaimsToRequest addClaimsToRequest)
|
IAddClaimsToRequest addClaimsToRequest)
|
||||||
:base(loggerFactory.CreateLogger<ClaimsBuilderMiddleware>())
|
:base(loggerFactory.CreateLogger<ClaimsToClaimsMiddleware>())
|
||||||
{
|
{
|
||||||
_next = next;
|
_next = next;
|
||||||
_addClaimsToRequest = addClaimsToRequest;
|
_addClaimsToRequest = addClaimsToRequest;
|
@ -8,15 +8,15 @@ using Ocelot.Middleware;
|
|||||||
|
|
||||||
namespace Ocelot.Headers.Middleware
|
namespace Ocelot.Headers.Middleware
|
||||||
{
|
{
|
||||||
public class HttpRequestHeadersBuilderMiddleware : OcelotMiddleware
|
public class ClaimsToHeadersMiddleware : OcelotMiddleware
|
||||||
{
|
{
|
||||||
private readonly OcelotRequestDelegate _next;
|
private readonly OcelotRequestDelegate _next;
|
||||||
private readonly IAddHeadersToRequest _addHeadersToRequest;
|
private readonly IAddHeadersToRequest _addHeadersToRequest;
|
||||||
|
|
||||||
public HttpRequestHeadersBuilderMiddleware(OcelotRequestDelegate next,
|
public ClaimsToHeadersMiddleware(OcelotRequestDelegate next,
|
||||||
IOcelotLoggerFactory loggerFactory,
|
IOcelotLoggerFactory loggerFactory,
|
||||||
IAddHeadersToRequest addHeadersToRequest)
|
IAddHeadersToRequest addHeadersToRequest)
|
||||||
:base(loggerFactory.CreateLogger<HttpRequestHeadersBuilderMiddleware>())
|
:base(loggerFactory.CreateLogger<ClaimsToHeadersMiddleware>())
|
||||||
{
|
{
|
||||||
_next = next;
|
_next = next;
|
||||||
_addHeadersToRequest = addHeadersToRequest;
|
_addHeadersToRequest = addHeadersToRequest;
|
@ -0,0 +1,13 @@
|
|||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Ocelot.Middleware.Pipeline;
|
||||||
|
|
||||||
|
namespace Ocelot.Headers.Middleware
|
||||||
|
{
|
||||||
|
public static class ClaimsToHeadersMiddlewareExtensions
|
||||||
|
{
|
||||||
|
public static IOcelotPipelineBuilder UseClaimsToHeadersMiddleware(this IOcelotPipelineBuilder builder)
|
||||||
|
{
|
||||||
|
return builder.UseMiddleware<ClaimsToHeadersMiddleware>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Ocelot.Middleware.Pipeline;
|
|
||||||
|
|
||||||
namespace Ocelot.Headers.Middleware
|
|
||||||
{
|
|
||||||
public static class HttpRequestHeadersBuilderMiddlewareExtensions
|
|
||||||
{
|
|
||||||
public static IOcelotPipelineBuilder UseHttpRequestHeadersBuilderMiddleware(this IOcelotPipelineBuilder builder)
|
|
||||||
{
|
|
||||||
return builder.UseMiddleware<HttpRequestHeadersBuilderMiddleware>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -91,7 +91,7 @@ namespace Ocelot.Middleware.Pipeline
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The next thing we do is look at any claims transforms in case this is important for authorisation
|
// The next thing we do is look at any claims transforms in case this is important for authorisation
|
||||||
builder.UseClaimsBuilderMiddleware();
|
builder.UseClaimsToClaimsMiddleware();
|
||||||
|
|
||||||
// Allow pre authorisation logic. The idea being people might want to run something custom before what is built in.
|
// Allow pre authorisation logic. The idea being people might want to run something custom before what is built in.
|
||||||
builder.UseIfNotNull(pipelineConfiguration.PreAuthorisationMiddleware);
|
builder.UseIfNotNull(pipelineConfiguration.PreAuthorisationMiddleware);
|
||||||
@ -109,14 +109,14 @@ namespace Ocelot.Middleware.Pipeline
|
|||||||
builder.Use(pipelineConfiguration.AuthorisationMiddleware);
|
builder.Use(pipelineConfiguration.AuthorisationMiddleware);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we can run any header transformation logic
|
// Now we can run the claims to headers transformation middleware
|
||||||
builder.UseHttpRequestHeadersBuilderMiddleware();
|
builder.UseClaimsToHeadersMiddleware();
|
||||||
|
|
||||||
// Allow the user to implement their own query string manipulation logic
|
// Allow the user to implement their own query string manipulation logic
|
||||||
builder.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware);
|
builder.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware);
|
||||||
|
|
||||||
// Now we can run any query string transformation logic
|
// Now we can run any claims to query string transformation middleware
|
||||||
builder.UseQueryStringBuilderMiddleware();
|
builder.UseClaimsToQueryStringMiddleware();
|
||||||
|
|
||||||
// Get the load balancer for this request
|
// Get the load balancer for this request
|
||||||
builder.UseLoadBalancingMiddleware();
|
builder.UseLoadBalancingMiddleware();
|
||||||
|
@ -1,22 +1,22 @@
|
|||||||
using System.Linq;
|
namespace Ocelot.QueryStrings.Middleware
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Middleware;
|
|
||||||
|
|
||||||
namespace Ocelot.QueryStrings.Middleware
|
|
||||||
{
|
{
|
||||||
public class QueryStringBuilderMiddleware : OcelotMiddleware
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||||
|
using Ocelot.Infrastructure.RequestData;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Middleware;
|
||||||
|
|
||||||
|
public class ClaimsToQueryStringMiddleware : OcelotMiddleware
|
||||||
{
|
{
|
||||||
private readonly OcelotRequestDelegate _next;
|
private readonly OcelotRequestDelegate _next;
|
||||||
private readonly IAddQueriesToRequest _addQueriesToRequest;
|
private readonly IAddQueriesToRequest _addQueriesToRequest;
|
||||||
|
|
||||||
public QueryStringBuilderMiddleware(OcelotRequestDelegate next,
|
public ClaimsToQueryStringMiddleware(OcelotRequestDelegate next,
|
||||||
IOcelotLoggerFactory loggerFactory,
|
IOcelotLoggerFactory loggerFactory,
|
||||||
IAddQueriesToRequest addQueriesToRequest)
|
IAddQueriesToRequest addQueriesToRequest)
|
||||||
: base(loggerFactory.CreateLogger<QueryStringBuilderMiddleware>())
|
: base(loggerFactory.CreateLogger<ClaimsToQueryStringMiddleware>())
|
||||||
{
|
{
|
||||||
_next = next;
|
_next = next;
|
||||||
_addQueriesToRequest = addQueriesToRequest;
|
_addQueriesToRequest = addQueriesToRequest;
|
@ -0,0 +1,13 @@
|
|||||||
|
namespace Ocelot.QueryStrings.Middleware
|
||||||
|
{
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Ocelot.Middleware.Pipeline;
|
||||||
|
|
||||||
|
public static class ClaimsToQueryStringMiddlewareExtensions
|
||||||
|
{
|
||||||
|
public static IOcelotPipelineBuilder UseClaimsToQueryStringMiddleware(this IOcelotPipelineBuilder builder)
|
||||||
|
{
|
||||||
|
return builder.UseMiddleware<ClaimsToQueryStringMiddleware>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Ocelot.Middleware.Pipeline;
|
|
||||||
|
|
||||||
namespace Ocelot.QueryStrings.Middleware
|
|
||||||
{
|
|
||||||
public static class QueryStringBuilderMiddlewareExtensions
|
|
||||||
{
|
|
||||||
public static IOcelotPipelineBuilder UseQueryStringBuilderMiddleware(this IOcelotPipelineBuilder builder)
|
|
||||||
{
|
|
||||||
return builder.UseMiddleware<QueryStringBuilderMiddleware>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,75 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
using BenchmarkDotNet.Columns;
|
||||||
|
using BenchmarkDotNet.Configs;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Ocelot.Configuration.File;
|
||||||
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
|
using Ocelot.Middleware;
|
||||||
|
using Ocelot.DependencyInjection;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using BenchmarkDotNet.Attributes.Jobs;
|
||||||
|
using Ocelot.Configuration.Repository;
|
||||||
|
using Ocelot.Infrastructure.RequestData;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Errors.Middleware;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using BenchmarkDotNet.Diagnosers;
|
||||||
|
using BenchmarkDotNet.Validators;
|
||||||
|
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||||
|
using Ocelot.DownstreamRouteFinder.Finder;
|
||||||
|
using Ocelot.Middleware.Multiplexer;
|
||||||
|
|
||||||
|
namespace Ocelot.Benchmarks
|
||||||
|
{
|
||||||
|
[SimpleJob(launchCount: 1, warmupCount: 2, targetCount: 5)]
|
||||||
|
[Config(typeof(DownstreamRouteFinderMiddlewareBenchmarks))]
|
||||||
|
public class DownstreamRouteFinderMiddlewareBenchmarks : ManualConfig
|
||||||
|
{
|
||||||
|
private DownstreamRouteFinderMiddleware _middleware;
|
||||||
|
private DownstreamContext _downstreamContext;
|
||||||
|
private OcelotRequestDelegate _next;
|
||||||
|
|
||||||
|
public DownstreamRouteFinderMiddlewareBenchmarks()
|
||||||
|
{
|
||||||
|
Add(StatisticColumn.AllStatistics);
|
||||||
|
Add(MemoryDiagnoser.Default);
|
||||||
|
Add(BaselineValidator.FailOnError);
|
||||||
|
}
|
||||||
|
|
||||||
|
[GlobalSetup]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
var serviceCollection = new ServiceCollection();
|
||||||
|
var config = new ConfigurationRoot(new List<IConfigurationProvider>());
|
||||||
|
var builder = new OcelotBuilder(serviceCollection, config);
|
||||||
|
var services = serviceCollection.BuildServiceProvider();
|
||||||
|
var loggerFactory = services.GetService<IOcelotLoggerFactory>();
|
||||||
|
var drpf = services.GetService<IDownstreamRouteProviderFactory>();
|
||||||
|
var icr = services.GetService<IInternalConfigurationRepository>();
|
||||||
|
var multiplexer = services.GetService<IMultiplexer>();
|
||||||
|
|
||||||
|
_next = async context => {
|
||||||
|
await Task.CompletedTask;
|
||||||
|
throw new Exception("BOOM");
|
||||||
|
};
|
||||||
|
|
||||||
|
_middleware = new DownstreamRouteFinderMiddleware(_next, loggerFactory, drpf, icr, multiplexer);
|
||||||
|
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark(Baseline = true)]
|
||||||
|
public async Task Baseline()
|
||||||
|
{
|
||||||
|
await _middleware.Invoke(_downstreamContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -17,24 +17,24 @@ namespace Ocelot.UnitTests.Claims
|
|||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
public class ClaimsBuilderMiddlewareTests
|
public class ClaimsToClaimsMiddlewareTests
|
||||||
{
|
{
|
||||||
private readonly Mock<IAddClaimsToRequest> _addHeaders;
|
private readonly Mock<IAddClaimsToRequest> _addHeaders;
|
||||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||||
private Mock<IOcelotLogger> _logger;
|
private Mock<IOcelotLogger> _logger;
|
||||||
private readonly ClaimsBuilderMiddleware _middleware;
|
private readonly ClaimsToClaimsMiddleware _middleware;
|
||||||
private readonly DownstreamContext _downstreamContext;
|
private readonly DownstreamContext _downstreamContext;
|
||||||
private OcelotRequestDelegate _next;
|
private OcelotRequestDelegate _next;
|
||||||
|
|
||||||
public ClaimsBuilderMiddlewareTests()
|
public ClaimsToClaimsMiddlewareTests()
|
||||||
{
|
{
|
||||||
_addHeaders = new Mock<IAddClaimsToRequest>();
|
_addHeaders = new Mock<IAddClaimsToRequest>();
|
||||||
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||||
_logger = new Mock<IOcelotLogger>();
|
_logger = new Mock<IOcelotLogger>();
|
||||||
_loggerFactory.Setup(x => x.CreateLogger<ClaimsBuilderMiddleware>()).Returns(_logger.Object);
|
_loggerFactory.Setup(x => x.CreateLogger<ClaimsToClaimsMiddleware>()).Returns(_logger.Object);
|
||||||
_next = context => Task.CompletedTask;
|
_next = context => Task.CompletedTask;
|
||||||
_middleware = new ClaimsBuilderMiddleware(_next, _loggerFactory.Object, _addHeaders.Object);
|
_middleware = new ClaimsToClaimsMiddleware(_next, _loggerFactory.Object, _addHeaders.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
@ -19,25 +19,25 @@ namespace Ocelot.UnitTests.Headers
|
|||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
public class HttpRequestHeadersBuilderMiddlewareTests
|
public class ClaimsToHeadersMiddlewareTests
|
||||||
{
|
{
|
||||||
private readonly Mock<IAddHeadersToRequest> _addHeaders;
|
private readonly Mock<IAddHeadersToRequest> _addHeaders;
|
||||||
private Response<DownstreamRoute> _downstreamRoute;
|
private Response<DownstreamRoute> _downstreamRoute;
|
||||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||||
private Mock<IOcelotLogger> _logger;
|
private Mock<IOcelotLogger> _logger;
|
||||||
private HttpRequestHeadersBuilderMiddleware _middleware;
|
private ClaimsToHeadersMiddleware _middleware;
|
||||||
private DownstreamContext _downstreamContext;
|
private DownstreamContext _downstreamContext;
|
||||||
private OcelotRequestDelegate _next;
|
private OcelotRequestDelegate _next;
|
||||||
|
|
||||||
public HttpRequestHeadersBuilderMiddlewareTests()
|
public ClaimsToHeadersMiddlewareTests()
|
||||||
{
|
{
|
||||||
_addHeaders = new Mock<IAddHeadersToRequest>();
|
_addHeaders = new Mock<IAddHeadersToRequest>();
|
||||||
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||||
_logger = new Mock<IOcelotLogger>();
|
_logger = new Mock<IOcelotLogger>();
|
||||||
_loggerFactory.Setup(x => x.CreateLogger<HttpRequestHeadersBuilderMiddleware>()).Returns(_logger.Object);
|
_loggerFactory.Setup(x => x.CreateLogger<ClaimsToHeadersMiddleware>()).Returns(_logger.Object);
|
||||||
_next = context => Task.CompletedTask;
|
_next = context => Task.CompletedTask;
|
||||||
_middleware = new HttpRequestHeadersBuilderMiddleware(_next, _loggerFactory.Object, _addHeaders.Object);
|
_middleware = new ClaimsToHeadersMiddleware(_next, _loggerFactory.Object, _addHeaders.Object);
|
||||||
_downstreamContext.DownstreamRequest = new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com"));
|
_downstreamContext.DownstreamRequest = new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com"));
|
||||||
}
|
}
|
||||||
|
|
@ -20,25 +20,25 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Ocelot.Request.Middleware;
|
using Ocelot.Request.Middleware;
|
||||||
|
|
||||||
public class QueryStringBuilderMiddlewareTests
|
public class ClaimsToQueryStringMiddlewareTests
|
||||||
{
|
{
|
||||||
private readonly Mock<IAddQueriesToRequest> _addQueries;
|
private readonly Mock<IAddQueriesToRequest> _addQueries;
|
||||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||||
private Mock<IOcelotLogger> _logger;
|
private Mock<IOcelotLogger> _logger;
|
||||||
private QueryStringBuilderMiddleware _middleware;
|
private ClaimsToQueryStringMiddleware _middleware;
|
||||||
private DownstreamContext _downstreamContext;
|
private DownstreamContext _downstreamContext;
|
||||||
private OcelotRequestDelegate _next;
|
private OcelotRequestDelegate _next;
|
||||||
|
|
||||||
public QueryStringBuilderMiddlewareTests()
|
public ClaimsToQueryStringMiddlewareTests()
|
||||||
{
|
{
|
||||||
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||||
_logger = new Mock<IOcelotLogger>();
|
_logger = new Mock<IOcelotLogger>();
|
||||||
_loggerFactory.Setup(x => x.CreateLogger<QueryStringBuilderMiddleware>()).Returns(_logger.Object);
|
_loggerFactory.Setup(x => x.CreateLogger<ClaimsToQueryStringMiddleware>()).Returns(_logger.Object);
|
||||||
_next = context => Task.CompletedTask;
|
_next = context => Task.CompletedTask;
|
||||||
_addQueries = new Mock<IAddQueriesToRequest>();
|
_addQueries = new Mock<IAddQueriesToRequest>();
|
||||||
_downstreamContext.DownstreamRequest = new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com"));
|
_downstreamContext.DownstreamRequest = new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com"));
|
||||||
_middleware = new QueryStringBuilderMiddleware(_next, _loggerFactory.Object, _addQueries.Object);
|
_middleware = new ClaimsToQueryStringMiddleware(_next, _loggerFactory.Object, _addQueries.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
Loading…
x
Reference in New Issue
Block a user