mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 11:58:15 +08:00
Remove Ocelot specific Middleware to make Ocelot more compatible with kestrel middleware and get ready for YARP
This commit is contained in:
@ -0,0 +1,81 @@
|
||||
namespace Ocelot.UnitTests.Multiplexing
|
||||
{
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Multiplexer;
|
||||
using Shouldly;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class MultiplexingMiddlewareTests
|
||||
{
|
||||
private readonly MultiplexingMiddleware _middleware;
|
||||
private DownstreamRoute _downstreamRoute;
|
||||
private int _count;
|
||||
private Mock<IResponseAggregator> _aggregator;
|
||||
private Mock<IResponseAggregatorFactory> _factory;
|
||||
private HttpContext _httpContext;
|
||||
private RequestDelegate _next;
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
|
||||
public MultiplexingMiddlewareTests()
|
||||
{
|
||||
_httpContext = new DefaultHttpContext();
|
||||
_factory = new Mock<IResponseAggregatorFactory>();
|
||||
_aggregator = new Mock<IResponseAggregator>();
|
||||
_factory.Setup(x => x.Get(It.IsAny<ReRoute>())).Returns(_aggregator.Object);
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_loggerFactory.Setup(x => x.CreateLogger<MultiplexingMiddleware>()).Returns(_logger.Object);
|
||||
_next = context => Task.FromResult(_count++);
|
||||
_middleware = new MultiplexingMiddleware(_next, _loggerFactory.Object, _factory.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_multiplex()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder().WithDownstreamReRoute(new DownstreamReRouteBuilder().Build()).WithDownstreamReRoute(new DownstreamReRouteBuilder().Build()).Build();
|
||||
|
||||
this.Given(x => GivenTheFollowing(reRoute))
|
||||
.When(x => WhenIMultiplex())
|
||||
.Then(x => ThePipelineIsCalled(2))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_multiplex()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder().WithDownstreamReRoute(new DownstreamReRouteBuilder().Build()).Build();
|
||||
|
||||
this.Given(x => GivenTheFollowing(reRoute))
|
||||
.When(x => WhenIMultiplex())
|
||||
.Then(x => ThePipelineIsCalled(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowing(ReRoute reRoute)
|
||||
{
|
||||
_downstreamRoute = new DownstreamRoute(new List<PlaceholderNameAndValue>(), reRoute);
|
||||
_httpContext.Items.UpsertDownstreamRoute(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void WhenIMultiplex()
|
||||
{
|
||||
_middleware.Invoke(_httpContext).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
private void ThePipelineIsCalled(int expected)
|
||||
{
|
||||
_count.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user