mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-05-02 23:22:52 +08:00
82 lines
3.0 KiB
C#
82 lines
3.0 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|