mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 15:58:16 +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,152 @@
|
||||
namespace Ocelot.UnitTests.Multiplexing
|
||||
{
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Multiplexer;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.UnitTests.Responder;
|
||||
using Shouldly;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class UserDefinedResponseAggregatorTests
|
||||
{
|
||||
private readonly UserDefinedResponseAggregator _aggregator;
|
||||
private readonly Mock<IDefinedAggregatorProvider> _provider;
|
||||
private ReRoute _reRoute;
|
||||
private List<HttpContext> _contexts;
|
||||
private HttpContext _context;
|
||||
|
||||
public UserDefinedResponseAggregatorTests()
|
||||
{
|
||||
_provider = new Mock<IDefinedAggregatorProvider>();
|
||||
_aggregator = new UserDefinedResponseAggregator(_provider.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_aggregator()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder().Build();
|
||||
|
||||
var context = new DefaultHttpContext();
|
||||
|
||||
var contextA = new DefaultHttpContext();
|
||||
contextA.Items.UpsertDownstreamResponse(new DownstreamResponse(new StringContent("Tom"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason"));
|
||||
|
||||
var contextB = new DefaultHttpContext();
|
||||
contextB.Items.UpsertDownstreamResponse(new DownstreamResponse(new StringContent("Laura"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason"));
|
||||
|
||||
var contexts = new List<HttpContext>()
|
||||
{
|
||||
contextA,
|
||||
contextB,
|
||||
};
|
||||
|
||||
this.Given(_ => GivenTheProviderReturnsAggregator())
|
||||
.And(_ => GivenReRoute(reRoute))
|
||||
.And(_ => GivenContexts(contexts))
|
||||
.And(_ => GivenContext(context))
|
||||
.When(_ => WhenIAggregate())
|
||||
.Then(_ => ThenTheProviderIsCalled())
|
||||
.And(_ => ThenTheContentIsCorrect())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_find_aggregator()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder().Build();
|
||||
|
||||
var context = new DefaultHttpContext();
|
||||
|
||||
var contextA = new DefaultHttpContext();
|
||||
contextA.Items.UpsertDownstreamResponse(new DownstreamResponse(new StringContent("Tom"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason"));
|
||||
|
||||
var contextB = new DefaultHttpContext();
|
||||
contextB.Items.UpsertDownstreamResponse(new DownstreamResponse(new StringContent("Laura"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason"));
|
||||
|
||||
var contexts = new List<HttpContext>()
|
||||
{
|
||||
contextA,
|
||||
contextB,
|
||||
};
|
||||
|
||||
this.Given(_ => GivenTheProviderReturnsError())
|
||||
.And(_ => GivenReRoute(reRoute))
|
||||
.And(_ => GivenContexts(contexts))
|
||||
.And(_ => GivenContext(context))
|
||||
.When(_ => WhenIAggregate())
|
||||
.Then(_ => ThenTheProviderIsCalled())
|
||||
.And(_ => ThenTheErrorIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheErrorIsReturned()
|
||||
{
|
||||
_context.Items.Errors().Count.ShouldBeGreaterThan(0);
|
||||
_context.Items.Errors().Count.ShouldBe(1);
|
||||
}
|
||||
|
||||
private void GivenTheProviderReturnsError()
|
||||
{
|
||||
_provider.Setup(x => x.Get(It.IsAny<ReRoute>())).Returns(new ErrorResponse<IDefinedAggregator>(new AnyError()));
|
||||
}
|
||||
|
||||
private async Task ThenTheContentIsCorrect()
|
||||
{
|
||||
var content = await _context.Items.DownstreamResponse().Content.ReadAsStringAsync();
|
||||
content.ShouldBe("Tom, Laura");
|
||||
}
|
||||
|
||||
private void ThenTheProviderIsCalled()
|
||||
{
|
||||
_provider.Verify(x => x.Get(_reRoute), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenContext(HttpContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
private void GivenContexts(List<HttpContext> contexts)
|
||||
{
|
||||
_contexts = contexts;
|
||||
}
|
||||
|
||||
private async Task WhenIAggregate()
|
||||
{
|
||||
await _aggregator.Aggregate(_reRoute, _context, _contexts);
|
||||
}
|
||||
|
||||
private void GivenTheProviderReturnsAggregator()
|
||||
{
|
||||
var aggregator = new TestDefinedAggregator();
|
||||
_provider.Setup(x => x.Get(It.IsAny<ReRoute>())).Returns(new OkResponse<IDefinedAggregator>(aggregator));
|
||||
}
|
||||
|
||||
private void GivenReRoute(ReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
public class TestDefinedAggregator : IDefinedAggregator
|
||||
{
|
||||
public async Task<DownstreamResponse> Aggregate(List<HttpContext> responses)
|
||||
{
|
||||
var tom = await responses[0].Items.DownstreamResponse().Content.ReadAsStringAsync();
|
||||
var laura = await responses[1].Items.DownstreamResponse().Content.ReadAsStringAsync();
|
||||
var content = $"{tom}, {laura}";
|
||||
var headers = responses.SelectMany(x => x.Items.DownstreamResponse().Headers).ToList();
|
||||
return new DownstreamResponse(new StringContent(content), HttpStatusCode.OK, headers, "some reason");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user