mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-05-03 03:32:49 +08:00

* started messing around with this on the train last night * mega hacking away to change middleware into Ocelot iddleware * scoped data repo back in * broken commit getting tests working * another broken commit farting around with tests * all unit tests passing again * mw pipeline for ocelot...still loads of hacks but getting there now to get acceptance tests working, then fix config so you can have aggregate and then imlement multiplexer, then mapping to response...loads to do * all tests passing before aggregation feature implemented * removed all the request middleware stuff we dont need it * updated how errors work...tho i think there could be edge case here when aggregating because one downstream could error and this would effect another * removed multiplexer so you dont have to send route down, this isnt very thread safe...sigh * hacking around getting the config for aggregates in, this might change * refactored builder and unit tests passing now * Updated a bunch of ports for tests * plugged in code to create reroutes that are aggregates * made multiplexer a class * hacked test to death * simple aggregator done, initial validation done * removed request id from context, it is still specific for http request * now aggregates to json always * docs for aggregate reroutes * Updated docs
203 lines
7.6 KiB
C#
203 lines
7.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Ocelot.Configuration;
|
|
using Ocelot.Configuration.Builder;
|
|
using Ocelot.Errors;
|
|
using Ocelot.Middleware;
|
|
using Ocelot.Middleware.Multiplexer;
|
|
using Ocelot.UnitTests.Responder;
|
|
using Shouldly;
|
|
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_map_all_downstream_to_upstream_when_not_aggregate()
|
|
{
|
|
var billDownstreamReRoute = new DownstreamReRouteBuilder().WithKey("Bill").Build();
|
|
|
|
var downstreamReRoutes = new List<DownstreamReRoute>
|
|
{
|
|
billDownstreamReRoute,
|
|
};
|
|
|
|
var reRoute = new ReRouteBuilder()
|
|
.WithDownstreamReRoutes(downstreamReRoutes)
|
|
.Build();
|
|
|
|
|
|
var billDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
|
{
|
|
DownstreamResponse =
|
|
new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("Bill says hi") },
|
|
DownstreamReRoute = billDownstreamReRoute,
|
|
Errors = new List<Error> { new AnyError() },
|
|
DownstreamRequest = new HttpRequestMessage(HttpMethod.Get, new Uri("http://www.bbc.co.uk")),
|
|
};
|
|
|
|
var downstreamContexts = new List<DownstreamContext> { billDownstreamContext };
|
|
|
|
this.Given(x => GivenTheUpstreamContext(new DownstreamContext(new DefaultHttpContext())))
|
|
.And(x => GivenTheReRoute(reRoute))
|
|
.And(x => GivenTheDownstreamContext(downstreamContexts))
|
|
.When(x => WhenIAggregate())
|
|
.Then(x => ThenTheContentIs("Bill says hi"))
|
|
.And(x => ThenTheUpstreamContextIsMappedForNonAggregate())
|
|
.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 HttpResponseMessage(HttpStatusCode.OK) {Content = new StringContent("Bill says hi")},
|
|
DownstreamReRoute = billDownstreamReRoute
|
|
};
|
|
|
|
var georgeDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
|
{
|
|
DownstreamResponse =
|
|
new HttpResponseMessage(HttpStatusCode.OK) {Content = new StringContent("George says hi")},
|
|
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"))
|
|
.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 HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("Bill says hi") },
|
|
DownstreamReRoute = billDownstreamReRoute
|
|
};
|
|
|
|
var georgeDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
|
{
|
|
DownstreamResponse =
|
|
new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("Error") },
|
|
DownstreamReRoute = georgeDownstreamReRoute,
|
|
Errors = new List<Error>() { 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 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);
|
|
}
|
|
}
|
|
}
|