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:
@ -1,86 +0,0 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Middleware.Multiplexer;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using static Ocelot.UnitTests.Middleware.UserDefinedResponseAggregatorTests;
|
||||
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
public class DefinedAggregatorProviderTests
|
||||
{
|
||||
private ServiceLocatorDefinedAggregatorProvider _provider;
|
||||
private Response<IDefinedAggregator> _aggregator;
|
||||
private ReRoute _reRoute;
|
||||
|
||||
[Fact]
|
||||
public void should_find_aggregator()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithAggregator("TestDefinedAggregator")
|
||||
.Build();
|
||||
|
||||
this.Given(_ => GivenDefinedAggregator())
|
||||
.And(_ => GivenReRoute(reRoute))
|
||||
.When(_ => WhenIGet())
|
||||
.Then(_ => ThenTheAggregatorIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_find_aggregator()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithAggregator("TestDefinedAggregator")
|
||||
.Build();
|
||||
|
||||
this.Given(_ => GivenNoDefinedAggregator())
|
||||
.And(_ => GivenReRoute(reRoute))
|
||||
.When(_ => WhenIGet())
|
||||
.Then(_ => ThenAnErrorIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenDefinedAggregator()
|
||||
{
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.AddSingleton<IDefinedAggregator, TestDefinedAggregator>();
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
_provider = new ServiceLocatorDefinedAggregatorProvider(services);
|
||||
}
|
||||
|
||||
private void ThenTheAggregatorIsReturned()
|
||||
{
|
||||
_aggregator.Data.ShouldNotBeNull();
|
||||
_aggregator.Data.ShouldBeOfType<TestDefinedAggregator>();
|
||||
_aggregator.IsError.ShouldBeFalse();
|
||||
}
|
||||
|
||||
private void GivenNoDefinedAggregator()
|
||||
{
|
||||
var serviceCollection = new ServiceCollection();
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
_provider = new ServiceLocatorDefinedAggregatorProvider(services);
|
||||
}
|
||||
|
||||
private void GivenReRoute(ReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenIGet()
|
||||
{
|
||||
_aggregator = _provider.Get(_reRoute);
|
||||
}
|
||||
|
||||
private void ThenAnErrorIsReturned()
|
||||
{
|
||||
_aggregator.IsError.ShouldBeTrue();
|
||||
_aggregator.Errors[0].Message.ShouldBe("Could not find Aggregator: TestDefinedAggregator");
|
||||
_aggregator.Errors[0].ShouldBeOfType<CouldNotFindAggregatorError>();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Middleware.Multiplexer;
|
||||
using Shouldly;
|
||||
using System.Threading.Tasks;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
public class MultiplexerTests
|
||||
{
|
||||
private readonly Multiplexer _multiplexer;
|
||||
private readonly DownstreamContext _context;
|
||||
private ReRoute _reRoute;
|
||||
private readonly OcelotRequestDelegate _pipeline;
|
||||
private int _count;
|
||||
private Mock<IResponseAggregator> _aggregator;
|
||||
private Mock<IResponseAggregatorFactory> _factory;
|
||||
|
||||
public MultiplexerTests()
|
||||
{
|
||||
_factory = new Mock<IResponseAggregatorFactory>();
|
||||
_aggregator = new Mock<IResponseAggregator>();
|
||||
_context = new DownstreamContext(new DefaultHttpContext());
|
||||
_pipeline = context => Task.FromResult(_count++);
|
||||
_factory.Setup(x => x.Get(It.IsAny<ReRoute>())).Returns(_aggregator.Object);
|
||||
_multiplexer = new Multiplexer(_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)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenIMultiplex()
|
||||
{
|
||||
_multiplexer.Multiplex(_context, _reRoute, _pipeline).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
private void ThePipelineIsCalled(int expected)
|
||||
{
|
||||
_count.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.UnitTests.Responder;
|
||||
using System.Collections.Generic;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
public class OcelotMiddlewareTests
|
||||
{
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
private FakeMiddleware _middleware;
|
||||
private List<Error> _errors;
|
||||
|
||||
public OcelotMiddlewareTests()
|
||||
{
|
||||
_errors = new List<Error>();
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_middleware = new FakeMiddleware(_logger.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_log_error()
|
||||
{
|
||||
this.Given(x => GivenAnError(new AnyError()))
|
||||
.When(x => WhenISetTheError())
|
||||
.Then(x => ThenTheErrorIsLogged(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_log_errors()
|
||||
{
|
||||
this.Given(x => GivenAnError(new AnyError()))
|
||||
.And(x => GivenAnError(new AnyError()))
|
||||
.When(x => WhenISetTheErrors())
|
||||
.Then(x => ThenTheErrorIsLogged(2))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void WhenISetTheErrors()
|
||||
{
|
||||
_middleware.SetPipelineError(new DownstreamContext(new DefaultHttpContext()), _errors);
|
||||
}
|
||||
|
||||
private void ThenTheErrorIsLogged(int times)
|
||||
{
|
||||
_logger.Verify(x => x.LogWarning("blahh"), Times.Exactly(times));
|
||||
}
|
||||
|
||||
private void WhenISetTheError()
|
||||
{
|
||||
_middleware.SetPipelineError(new DownstreamContext(new DefaultHttpContext()), _errors[0]);
|
||||
}
|
||||
|
||||
private void GivenAnError(Error error)
|
||||
{
|
||||
_errors.Add(error);
|
||||
}
|
||||
}
|
||||
|
||||
public class FakeMiddleware : OcelotMiddleware
|
||||
{
|
||||
public FakeMiddleware(IOcelotLogger logger)
|
||||
: base(logger)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.DependencyInjection;
|
||||
@ -7,7 +9,6 @@ namespace Ocelot.UnitTests.Middleware
|
||||
using Ocelot.DownstreamUrlCreator.Middleware;
|
||||
using Ocelot.LoadBalancer.Middleware;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Middleware.Pipeline;
|
||||
using Ocelot.Request.Middleware;
|
||||
using Ocelot.WebSockets.Middleware;
|
||||
using Shouldly;
|
||||
@ -16,8 +17,8 @@ namespace Ocelot.UnitTests.Middleware
|
||||
|
||||
public class OcelotPipelineExtensionsTests
|
||||
{
|
||||
private OcelotPipelineBuilder _builder;
|
||||
private OcelotRequestDelegate _handlers;
|
||||
private ApplicationBuilder _builder;
|
||||
private RequestDelegate _handlers;
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_pipeline()
|
||||
@ -50,15 +51,14 @@ namespace Ocelot.UnitTests.Middleware
|
||||
private void WhenIExpandBuild()
|
||||
{
|
||||
OcelotPipelineConfiguration configuration = new OcelotPipelineConfiguration();
|
||||
configuration.MapWhenOcelotPipeline.Add((app) =>
|
||||
//Func<HttpContext, bool>, Action<IApplicationBuilder>
|
||||
configuration.MapWhenOcelotPipeline.Add((httpContext) => httpContext.WebSockets.IsWebSocketRequest, app =>
|
||||
{
|
||||
app.UseDownstreamRouteFinderMiddleware();
|
||||
app.UseDownstreamRequestInitialiser();
|
||||
app.UseLoadBalancingMiddleware();
|
||||
app.UseDownstreamUrlCreatorMiddleware();
|
||||
app.UseWebSocketsProxyMiddleware();
|
||||
|
||||
return context => context.HttpContext.WebSockets.IsWebSocketRequest;
|
||||
});
|
||||
_handlers = _builder.BuildOcelotPipeline(new OcelotPipelineConfiguration());
|
||||
}
|
||||
@ -71,7 +71,7 @@ namespace Ocelot.UnitTests.Middleware
|
||||
services.AddSingleton<IConfiguration>(root);
|
||||
services.AddOcelot();
|
||||
var provider = services.BuildServiceProvider();
|
||||
_builder = new OcelotPipelineBuilder(provider);
|
||||
_builder = new ApplicationBuilder(provider);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@ -11,19 +8,22 @@ namespace Ocelot.UnitTests.Middleware
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Middleware.Pipeline;
|
||||
using Shouldly;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Ocelot.Errors.Middleware;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class OcelotPiplineBuilderTests
|
||||
{
|
||||
private readonly IServiceCollection _services;
|
||||
private readonly IConfiguration _configRoot;
|
||||
private DownstreamContext _downstreamContext;
|
||||
private int _counter;
|
||||
private HttpContext _httpContext;
|
||||
|
||||
public OcelotPiplineBuilderTests()
|
||||
{
|
||||
@ -32,6 +32,7 @@ namespace Ocelot.UnitTests.Middleware
|
||||
_services.AddSingleton<IWebHostEnvironment>(GetHostingEnvironment());
|
||||
_services.AddSingleton<IConfiguration>(_configRoot);
|
||||
_services.AddOcelot();
|
||||
_httpContext = new DefaultHttpContext();
|
||||
}
|
||||
|
||||
|
||||
@ -64,61 +65,58 @@ namespace Ocelot.UnitTests.Middleware
|
||||
private void WhenIUseAGeneric()
|
||||
{
|
||||
var provider = _services.BuildServiceProvider();
|
||||
IOcelotPipelineBuilder builder = new OcelotPipelineBuilder(provider);
|
||||
builder = builder.UseMiddleware<Ocelot.Errors.Middleware.ExceptionHandlerMiddleware>();
|
||||
IApplicationBuilder builder = new ApplicationBuilder(provider);
|
||||
builder = builder.UseMiddleware<ExceptionHandlerMiddleware>();
|
||||
var del = builder.Build();
|
||||
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||
del.Invoke(_downstreamContext);
|
||||
del.Invoke(_httpContext);
|
||||
}
|
||||
|
||||
private void ThenTheGenericIsInThePipeline()
|
||||
{
|
||||
_downstreamContext.HttpContext.Response.StatusCode.ShouldBe(500);
|
||||
_httpContext.Response.StatusCode.ShouldBe(500);
|
||||
}
|
||||
|
||||
private void WhenIUseAFunc()
|
||||
{
|
||||
_counter = 0;
|
||||
var provider = _services.BuildServiceProvider();
|
||||
IOcelotPipelineBuilder builder = new OcelotPipelineBuilder(provider);
|
||||
IApplicationBuilder builder = new ApplicationBuilder(provider);
|
||||
builder = builder.Use(async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
});
|
||||
var del = builder.Build();
|
||||
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||
del.Invoke(_downstreamContext);
|
||||
del.Invoke(_httpContext);
|
||||
}
|
||||
|
||||
private void ThenTheFuncIsInThePipeline()
|
||||
{
|
||||
_counter.ShouldBe(1);
|
||||
_downstreamContext.HttpContext.Response.StatusCode.ShouldBe(404);
|
||||
_httpContext.Response.StatusCode.ShouldBe(404);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Middleware_Multi_Parameters_Invoke()
|
||||
{
|
||||
var provider = _services.BuildServiceProvider();
|
||||
IOcelotPipelineBuilder builder = new OcelotPipelineBuilder(provider);
|
||||
IApplicationBuilder builder = new ApplicationBuilder(provider);
|
||||
builder = builder.UseMiddleware<MultiParametersInvokeMiddleware>();
|
||||
var del = builder.Build();
|
||||
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||
del.Invoke(_downstreamContext);
|
||||
del.Invoke(_httpContext);
|
||||
}
|
||||
|
||||
private class MultiParametersInvokeMiddleware : OcelotMiddleware
|
||||
{
|
||||
private readonly OcelotRequestDelegate _next;
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public MultiParametersInvokeMiddleware(OcelotRequestDelegate next)
|
||||
public MultiParametersInvokeMiddleware(RequestDelegate next)
|
||||
: base(new FakeLogger())
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public Task Invoke(DownstreamContext context, IServiceProvider serviceProvider)
|
||||
public Task Invoke(HttpContext context, IServiceProvider serviceProvider)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
@ -1,65 +0,0 @@
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Middleware.Multiplexer;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class ResponseAggregatorFactoryTests
|
||||
{
|
||||
private readonly InMemoryResponseAggregatorFactory _factory;
|
||||
private Mock<IDefinedAggregatorProvider> _provider;
|
||||
private ReRoute _reRoute;
|
||||
private IResponseAggregator _aggregator;
|
||||
|
||||
public ResponseAggregatorFactoryTests()
|
||||
{
|
||||
_provider = new Mock<IDefinedAggregatorProvider>();
|
||||
_aggregator = new SimpleJsonResponseAggregator();
|
||||
_factory = new InMemoryResponseAggregatorFactory(_provider.Object, _aggregator);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_simple_json_aggregator()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.Build();
|
||||
|
||||
this.Given(_ => GivenReRoute(reRoute))
|
||||
.When(_ => WhenIGet())
|
||||
.Then(_ => ThenTheAggregatorIs<SimpleJsonResponseAggregator>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_user_defined_aggregator()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithAggregator("doesntmatter")
|
||||
.Build();
|
||||
|
||||
this.Given(_ => GivenReRoute(reRoute))
|
||||
.When(_ => WhenIGet())
|
||||
.Then(_ => ThenTheAggregatorIs<UserDefinedResponseAggregator>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenReRoute(ReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenIGet()
|
||||
{
|
||||
_aggregator = _factory.Get(_reRoute);
|
||||
}
|
||||
|
||||
private void ThenTheAggregatorIs<T>()
|
||||
{
|
||||
_aggregator.ShouldBeOfType<T>();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,222 +0,0 @@
|
||||
using Castle.Components.DictionaryAdapter;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Middleware.Multiplexer;
|
||||
using Ocelot.UnitTests.Responder;
|
||||
using Ocelot.Values;
|
||||
using Shouldly;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
public class SimpleJsonResponseAggregatorTests
|
||||
{
|
||||
private readonly SimpleJsonResponseAggregator _aggregator;
|
||||
private List<DownstreamContext> _downstreamContexts;
|
||||
private DownstreamContext _upstreamContext;
|
||||
private ReRoute _reRoute;
|
||||
|
||||
public SimpleJsonResponseAggregatorTests()
|
||||
{
|
||||
_aggregator = new SimpleJsonResponseAggregator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_aggregate_n_responses_and_set_response_content_on_upstream_context_withConfig()
|
||||
{
|
||||
var commentsDownstreamReRoute = new DownstreamReRouteBuilder().WithKey("Comments").Build();
|
||||
|
||||
var userDetailsDownstreamReRoute = new DownstreamReRouteBuilder().WithKey("UserDetails")
|
||||
.WithUpstreamPathTemplate(new UpstreamPathTemplate("", 0, false, "/v1/users/{userId}"))
|
||||
.Build();
|
||||
|
||||
var downstreamReRoutes = new List<DownstreamReRoute>
|
||||
{
|
||||
commentsDownstreamReRoute,
|
||||
userDetailsDownstreamReRoute
|
||||
};
|
||||
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithDownstreamReRoutes(downstreamReRoutes)
|
||||
.WithAggregateReRouteConfig(new List<AggregateReRouteConfig>()
|
||||
{
|
||||
new AggregateReRouteConfig(){ReRouteKey = "UserDetails",JsonPath = "$[*].writerId",Parameter = "userId"}
|
||||
})
|
||||
.Build();
|
||||
|
||||
var commentsResponseContent = @"[{""id"":1,""writerId"":1,""postId"":1,""text"":""text1""},{""id"":2,""writerId"":2,""postId"":2,""text"":""text2""},{""id"":3,""writerId"":2,""postId"":1,""text"":""text21""}]";
|
||||
var commentsDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent(commentsResponseContent, Encoding.UTF8, "application/json"), HttpStatusCode.OK, new EditableList<KeyValuePair<string, IEnumerable<string>>>(), "some reason"),
|
||||
DownstreamReRoute = commentsDownstreamReRoute
|
||||
};
|
||||
|
||||
var userDetailsResponseContent = @"[{""id"":1,""firstName"":""abolfazl"",""lastName"":""rajabpour""},{""id"":2,""firstName"":""reza"",""lastName"":""rezaei""}]";
|
||||
var userDetailsDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent(userDetailsResponseContent, Encoding.UTF8, "application/json"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason"),
|
||||
DownstreamReRoute = userDetailsDownstreamReRoute
|
||||
};
|
||||
|
||||
var downstreamContexts = new List<DownstreamContext> { commentsDownstreamContext, userDetailsDownstreamContext };
|
||||
|
||||
var expected = "{\"Comments\":" + commentsResponseContent + ",\"UserDetails\":" + userDetailsResponseContent + "}";
|
||||
|
||||
this.Given(x => GivenTheUpstreamContext(new DownstreamContext(new DefaultHttpContext())))
|
||||
.And(x => GivenTheReRoute(reRoute))
|
||||
.And(x => GivenTheDownstreamContext(downstreamContexts))
|
||||
.When(x => WhenIAggregate())
|
||||
.Then(x => ThenTheContentIs(expected))
|
||||
.And(x => ThenTheContentTypeIs("application/json"))
|
||||
.And(x => ThenTheReasonPhraseIs("cannot return from aggregate..which reason phrase would you use?"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_aggregate_n_responses_and_set_response_content_on_upstream_context()
|
||||
{
|
||||
var billDownstreamReRoute = new DownstreamReRouteBuilder().WithKey("Bill").Build();
|
||||
|
||||
var georgeDownstreamReRoute = new DownstreamReRouteBuilder().WithKey("George").Build();
|
||||
|
||||
var downstreamReRoutes = new List<DownstreamReRoute>
|
||||
{
|
||||
billDownstreamReRoute,
|
||||
georgeDownstreamReRoute
|
||||
};
|
||||
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithDownstreamReRoutes(downstreamReRoutes)
|
||||
.Build();
|
||||
|
||||
var billDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Bill says hi"), HttpStatusCode.OK, new EditableList<KeyValuePair<string, IEnumerable<string>>>(), "some reason"),
|
||||
DownstreamReRoute = billDownstreamReRoute
|
||||
};
|
||||
|
||||
var georgeDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("George says hi"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason"),
|
||||
DownstreamReRoute = georgeDownstreamReRoute
|
||||
};
|
||||
|
||||
var downstreamContexts = new List<DownstreamContext> { billDownstreamContext, georgeDownstreamContext };
|
||||
|
||||
var expected = "{\"Bill\":Bill says hi,\"George\":George says hi}";
|
||||
|
||||
this.Given(x => GivenTheUpstreamContext(new DownstreamContext(new DefaultHttpContext())))
|
||||
.And(x => GivenTheReRoute(reRoute))
|
||||
.And(x => GivenTheDownstreamContext(downstreamContexts))
|
||||
.When(x => WhenIAggregate())
|
||||
.Then(x => ThenTheContentIs(expected))
|
||||
.And(x => ThenTheContentTypeIs("application/json"))
|
||||
.And(x => ThenTheReasonPhraseIs("cannot return from aggregate..which reason phrase would you use?"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_any_downstreams_have_errored()
|
||||
{
|
||||
var billDownstreamReRoute = new DownstreamReRouteBuilder().WithKey("Bill").Build();
|
||||
|
||||
var georgeDownstreamReRoute = new DownstreamReRouteBuilder().WithKey("George").Build();
|
||||
|
||||
var downstreamReRoutes = new List<DownstreamReRoute>
|
||||
{
|
||||
billDownstreamReRoute,
|
||||
georgeDownstreamReRoute
|
||||
};
|
||||
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithDownstreamReRoutes(downstreamReRoutes)
|
||||
.Build();
|
||||
|
||||
var billDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Bill says hi"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason"),
|
||||
DownstreamReRoute = billDownstreamReRoute
|
||||
};
|
||||
|
||||
var georgeDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Error"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason"),
|
||||
DownstreamReRoute = georgeDownstreamReRoute,
|
||||
};
|
||||
|
||||
georgeDownstreamContext.Errors.Add(new AnyError());
|
||||
|
||||
var downstreamContexts = new List<DownstreamContext> { billDownstreamContext, georgeDownstreamContext };
|
||||
|
||||
var expected = "Error";
|
||||
|
||||
this.Given(x => GivenTheUpstreamContext(new DownstreamContext(new DefaultHttpContext())))
|
||||
.And(x => GivenTheReRoute(reRoute))
|
||||
.And(x => GivenTheDownstreamContext(downstreamContexts))
|
||||
.When(x => WhenIAggregate())
|
||||
.Then(x => ThenTheContentIs(expected))
|
||||
.And(x => ThenTheErrorIsMapped())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheReasonPhraseIs(string expected)
|
||||
{
|
||||
_upstreamContext.DownstreamResponse.ReasonPhrase.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void ThenTheErrorIsMapped()
|
||||
{
|
||||
_upstreamContext.Errors.ShouldBe(_downstreamContexts[1].Errors);
|
||||
_upstreamContext.DownstreamResponse.ShouldBe(_downstreamContexts[1].DownstreamResponse);
|
||||
}
|
||||
|
||||
private void GivenTheReRoute(ReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void GivenTheUpstreamContext(DownstreamContext upstreamContext)
|
||||
{
|
||||
_upstreamContext = upstreamContext;
|
||||
}
|
||||
|
||||
private void GivenTheDownstreamContext(List<DownstreamContext> downstreamContexts)
|
||||
{
|
||||
_downstreamContexts = downstreamContexts;
|
||||
}
|
||||
|
||||
private void WhenIAggregate()
|
||||
{
|
||||
_aggregator.Aggregate(_reRoute, _upstreamContext, _downstreamContexts).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
private void ThenTheContentIs(string expected)
|
||||
{
|
||||
var content = _upstreamContext.DownstreamResponse.Content.ReadAsStringAsync()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
content.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void ThenTheContentTypeIs(string expected)
|
||||
{
|
||||
_upstreamContext.DownstreamResponse.Content.Headers.ContentType.MediaType.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void ThenTheUpstreamContextIsMappedForNonAggregate()
|
||||
{
|
||||
_upstreamContext.DownstreamRequest.ShouldBe(_downstreamContexts[0].DownstreamRequest);
|
||||
_upstreamContext.DownstreamResponse.ShouldBe(_downstreamContexts[0].DownstreamResponse);
|
||||
_upstreamContext.Errors.ShouldBe(_downstreamContexts[0].Errors);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,152 +0,0 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Middleware.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;
|
||||
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
public class UserDefinedResponseAggregatorTests
|
||||
{
|
||||
private readonly UserDefinedResponseAggregator _aggregator;
|
||||
private readonly Mock<IDefinedAggregatorProvider> _provider;
|
||||
private ReRoute _reRoute;
|
||||
private List<DownstreamContext> _contexts;
|
||||
private DownstreamContext _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 DownstreamContext(new DefaultHttpContext());
|
||||
|
||||
var contexts = new List<DownstreamContext>
|
||||
{
|
||||
new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Tom"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason")
|
||||
},
|
||||
new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Laura"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason")
|
||||
}
|
||||
};
|
||||
|
||||
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 DownstreamContext(new DefaultHttpContext());
|
||||
|
||||
var contexts = new List<DownstreamContext>
|
||||
{
|
||||
new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Tom"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason")
|
||||
},
|
||||
new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Laura"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason")
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(_ => GivenTheProviderReturnsError())
|
||||
.And(_ => GivenReRoute(reRoute))
|
||||
.And(_ => GivenContexts(contexts))
|
||||
.And(_ => GivenContext(context))
|
||||
.When(_ => WhenIAggregate())
|
||||
.Then(_ => ThenTheProviderIsCalled())
|
||||
.And(_ => ThenTheErrorIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheErrorIsReturned()
|
||||
{
|
||||
_context.IsError.ShouldBeTrue();
|
||||
_context.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.DownstreamResponse.Content.ReadAsStringAsync();
|
||||
content.ShouldBe("Tom, Laura");
|
||||
}
|
||||
|
||||
private void ThenTheProviderIsCalled()
|
||||
{
|
||||
_provider.Verify(x => x.Get(_reRoute), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenContext(DownstreamContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
private void GivenContexts(List<DownstreamContext> 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<DownstreamContext> responses)
|
||||
{
|
||||
var tom = await responses[0].DownstreamResponse.Content.ReadAsStringAsync();
|
||||
var laura = await responses[1].DownstreamResponse.Content.ReadAsStringAsync();
|
||||
var content = $"{tom}, {laura}";
|
||||
var headers = responses.SelectMany(x => x.DownstreamResponse.Headers).ToList();
|
||||
return new DownstreamResponse(new StringContent(content), HttpStatusCode.OK, headers, "some reason");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user