mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 11:18:16 +08:00
Feature/downstream aggregation (#248)
* 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
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Requester;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
@ -14,7 +17,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
private readonly DelegatingHandlerHandlerHouse _house;
|
||||
private Mock<IDelegatingHandlerHandlerProviderFactory> _factory;
|
||||
private readonly Mock<IDelegatingHandlerHandlerProvider> _provider;
|
||||
private Ocelot.Request.Request _request;
|
||||
private DownstreamReRoute _request;
|
||||
private Response<IDelegatingHandlerHandlerProvider> _result;
|
||||
|
||||
public DelegatingHandlerHandlerHouseTests()
|
||||
@ -27,9 +30,10 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_create_and_store_provider()
|
||||
{
|
||||
var request = new Ocelot.Request.Request(new HttpRequestMessage(), true, null, true, true, "key", false);
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(true)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("key").Build();
|
||||
|
||||
this.Given(x => GivenTheRequest(request))
|
||||
this.Given(x => GivenTheRequest(reRoute))
|
||||
.And(x => GivenTheProviderReturns())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenTheFactoryIsCalled(1))
|
||||
@ -40,9 +44,10 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_get_provider()
|
||||
{
|
||||
var request = new Ocelot.Request.Request(new HttpRequestMessage(), true, null, true, true, "key", false);
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(true)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("key").Build();
|
||||
|
||||
this.Given(x => GivenTheRequest(request))
|
||||
this.Given(x => GivenTheRequest(reRoute))
|
||||
.And(x => GivenTheProviderReturns())
|
||||
.And(x => WhenIGet())
|
||||
.And(x => GivenTheFactoryIsCleared())
|
||||
@ -55,15 +60,34 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
var request = new Ocelot.Request.Request(new HttpRequestMessage(), true, null, true, true, "key", false);
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(true)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("key").Build();
|
||||
|
||||
this.Given(x => GivenTheRequest(request))
|
||||
this.Given(x => GivenTheRequest(reRoute))
|
||||
.And(x => GivenTheProviderThrows())
|
||||
.When(x => WhenIGet())
|
||||
.And(x => ThenAnErrorIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_factory_errors()
|
||||
{
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(true)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("key").Build();
|
||||
|
||||
this.Given(x => GivenTheRequest(reRoute))
|
||||
.And(x => GivenTheProviderReturnsError())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenAnUnknownErrorIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenAnUnknownErrorIsReturned()
|
||||
{
|
||||
_result.IsError.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void ThenAnErrorIsReturned()
|
||||
{
|
||||
_result.IsError.ShouldBeTrue();
|
||||
@ -72,7 +96,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
|
||||
private void GivenTheProviderThrows()
|
||||
{
|
||||
_factory.Setup(x => x.Get(It.IsAny<Ocelot.Request.Request>())).Throws<Exception>();
|
||||
_factory.Setup(x => x.Get(It.IsAny<DownstreamReRoute>())).Throws<Exception>();
|
||||
}
|
||||
|
||||
private void GivenTheFactoryIsCleared()
|
||||
@ -90,14 +114,19 @@ namespace Ocelot.UnitTests.Requester
|
||||
_result = _house.Get(_request);
|
||||
}
|
||||
|
||||
private void GivenTheRequest(Ocelot.Request.Request request)
|
||||
private void GivenTheRequest(DownstreamReRoute request)
|
||||
{
|
||||
_request = request;
|
||||
}
|
||||
|
||||
private void GivenTheProviderReturns()
|
||||
{
|
||||
_factory.Setup(x => x.Get(It.IsAny<Ocelot.Request.Request>())).Returns(_provider.Object);
|
||||
_factory.Setup(x => x.Get(It.IsAny<DownstreamReRoute>())).Returns(new OkResponse<IDelegatingHandlerHandlerProvider>(_provider.Object));
|
||||
}
|
||||
|
||||
private void GivenTheProviderReturnsError()
|
||||
{
|
||||
_factory.Setup(x => x.Get(It.IsAny<DownstreamReRoute>())).Returns(new ErrorResponse<IDelegatingHandlerHandlerProvider>(It.IsAny<Error>()));
|
||||
}
|
||||
|
||||
private void ThenTheFactoryIsCalled(int times)
|
||||
|
@ -2,8 +2,13 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Requester;
|
||||
using Ocelot.Requester.QoS;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
@ -14,17 +19,27 @@ namespace Ocelot.UnitTests.Requester
|
||||
{
|
||||
private readonly DelegatingHandlerHandlerProviderFactory _factory;
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private Ocelot.Request.Request _request;
|
||||
private IDelegatingHandlerHandlerProvider _provider;
|
||||
private DownstreamReRoute _request;
|
||||
private Response<IDelegatingHandlerHandlerProvider> _provider;
|
||||
private readonly Mock<IDelegatingHandlerHandlerProvider> _allRoutesProvider;
|
||||
private readonly Mock<IQosProviderHouse> _qosProviderHouse;
|
||||
|
||||
public DelegatingHandlerHandlerProviderFactoryTests()
|
||||
{
|
||||
_qosProviderHouse = new Mock<IQosProviderHouse>();
|
||||
_allRoutesProvider = new Mock<IDelegatingHandlerHandlerProvider>();
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_factory = new DelegatingHandlerHandlerProviderFactory(_loggerFactory.Object, _allRoutesProvider.Object, null);
|
||||
_factory = new DelegatingHandlerHandlerProviderFactory(_loggerFactory.Object, _allRoutesProvider.Object, null, _qosProviderHouse.Object);
|
||||
}
|
||||
|
||||
private void GivenTheQosProviderHouseReturns(Response<IQoSProvider> qosProvider)
|
||||
{
|
||||
_qosProviderHouse
|
||||
.Setup(x => x.Get(It.IsAny<DownstreamReRoute>()))
|
||||
.Returns(qosProvider);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_all_from_all_routes_provider_and_qos()
|
||||
{
|
||||
@ -34,9 +49,11 @@ namespace Ocelot.UnitTests.Requester
|
||||
() => new FakeDelegatingHandler(1)
|
||||
};
|
||||
|
||||
var request = new Ocelot.Request.Request(new HttpRequestMessage(), true, null, true, true, "", false);
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(true)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("").Build();
|
||||
|
||||
this.Given(x => GivenTheFollowingRequest(request))
|
||||
this.Given(x => GivenTheFollowingRequest(reRoute))
|
||||
.And(x => GivenTheQosProviderHouseReturns(new OkResponse<IQoSProvider>(It.IsAny<PollyQoSProvider>())))
|
||||
.And(x => GivenTheAllRoutesProviderReturns(handlers))
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenThereIsDelegatesInProvider(3))
|
||||
@ -48,9 +65,10 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_return_provider_with_no_delegates()
|
||||
{
|
||||
var request = new Ocelot.Request.Request(new HttpRequestMessage(), false, null, true, true, "", false);
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(false)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("").Build();
|
||||
|
||||
this.Given(x => GivenTheFollowingRequest(request))
|
||||
this.Given(x => GivenTheFollowingRequest(reRoute))
|
||||
.And(x => GivenTheAllRoutesProviderReturns())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenNoDelegatesAreInTheProvider())
|
||||
@ -60,9 +78,11 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_return_provider_with_qos_delegate()
|
||||
{
|
||||
var request = new Ocelot.Request.Request(new HttpRequestMessage(), true, null, true, true, "", false);
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(true)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("").Build();
|
||||
|
||||
this.Given(x => GivenTheFollowingRequest(request))
|
||||
this.Given(x => GivenTheFollowingRequest(reRoute))
|
||||
.And(x => GivenTheQosProviderHouseReturns(new OkResponse<IQoSProvider>(It.IsAny<PollyQoSProvider>())))
|
||||
.And(x => GivenTheAllRoutesProviderReturns())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenThereIsDelegatesInProvider(1))
|
||||
@ -70,9 +90,28 @@ namespace Ocelot.UnitTests.Requester
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(true)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("").Build();
|
||||
|
||||
this.Given(x => GivenTheFollowingRequest(reRoute))
|
||||
.And(x => GivenTheQosProviderHouseReturns(new ErrorResponse<IQoSProvider>(It.IsAny<Error>())))
|
||||
.And(x => GivenTheAllRoutesProviderReturns())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenAnErrorIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenAnErrorIsReturned()
|
||||
{
|
||||
_provider.IsError.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void ThenTheDelegatesAreAddedCorrectly()
|
||||
{
|
||||
var delegates = _provider.Get();
|
||||
var delegates = _provider.Data.Get();
|
||||
var del = delegates[0].Invoke();
|
||||
var handler = (FakeDelegatingHandler) del;
|
||||
handler.Order.ShouldBe(0);
|
||||
@ -94,7 +133,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
|
||||
private void ThenItIsPolly(int i)
|
||||
{
|
||||
var delegates = _provider.Get();
|
||||
var delegates = _provider.Data.Get();
|
||||
var del = delegates[i].Invoke();
|
||||
del.ShouldBeOfType<PollyCircuitBreakingDelegatingHandler>();
|
||||
}
|
||||
@ -102,10 +141,10 @@ namespace Ocelot.UnitTests.Requester
|
||||
private void ThenThereIsDelegatesInProvider(int count)
|
||||
{
|
||||
_provider.ShouldNotBeNull();
|
||||
_provider.Get().Count.ShouldBe(count);
|
||||
_provider.Data.Get().Count.ShouldBe(count);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingRequest(Ocelot.Request.Request request)
|
||||
private void GivenTheFollowingRequest(DownstreamReRoute request)
|
||||
{
|
||||
_request = request;
|
||||
}
|
||||
@ -118,7 +157,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
private void ThenNoDelegatesAreInTheProvider()
|
||||
{
|
||||
_provider.ShouldNotBeNull();
|
||||
_provider.Get().Count.ShouldBe(0);
|
||||
_provider.Data.Get().Count.ShouldBe(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Requester;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
@ -18,7 +20,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
private IHttpClientBuilder _builderResult;
|
||||
private IHttpClient _httpClient;
|
||||
private HttpResponseMessage _response;
|
||||
private Ocelot.Request.Request _request;
|
||||
private DownstreamReRoute _request;
|
||||
|
||||
public HttpClientBuilderTests()
|
||||
{
|
||||
@ -62,13 +64,16 @@ namespace Ocelot.UnitTests.Requester
|
||||
|
||||
private void GivenARequest()
|
||||
{
|
||||
_request = new Ocelot.Request.Request(null, false, null, false, false, "", false);
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(false)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(false, false, false)).WithReRouteKey("").Build();
|
||||
|
||||
_request = reRoute;
|
||||
}
|
||||
|
||||
private void GivenTheHouseReturns()
|
||||
{
|
||||
_house
|
||||
.Setup(x => x.Get(It.IsAny<Ocelot.Request.Request>()))
|
||||
.Setup(x => x.Get(It.IsAny<DownstreamReRoute>()))
|
||||
.Returns(new OkResponse<IDelegatingHandlerHandlerProvider>(_provider.Object));
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Moq;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Requester;
|
||||
using Ocelot.Requester.QoS;
|
||||
using Ocelot.Responses;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Middleware;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
@ -21,7 +22,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
private Mock<IDelegatingHandlerHandlerProvider> _provider;
|
||||
private Response<HttpResponseMessage> _response;
|
||||
private readonly HttpClientHttpRequester _httpClientRequester;
|
||||
private Ocelot.Request.Request _request;
|
||||
private DownstreamContext _request;
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
|
||||
@ -30,7 +31,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
_provider = new Mock<IDelegatingHandlerHandlerProvider>();
|
||||
_provider.Setup(x => x.Get()).Returns(new List<Func<DelegatingHandler>>());
|
||||
_house = new Mock<IDelegatingHandlerHandlerHouse>();
|
||||
_house.Setup(x => x.Get(It.IsAny<Ocelot.Request.Request>())).Returns(new OkResponse<IDelegatingHandlerHandlerProvider>(_provider.Object));
|
||||
_house.Setup(x => x.Get(It.IsAny<DownstreamReRoute>())).Returns(new OkResponse<IDelegatingHandlerHandlerProvider>(_provider.Object));
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_loggerFactory
|
||||
@ -43,7 +44,16 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_call_request_correctly()
|
||||
{
|
||||
this.Given(x=>x.GivenTheRequestIs(new Ocelot.Request.Request(new HttpRequestMessage() { RequestUri = new Uri("http://www.bbc.co.uk") }, false, new NoQoSProvider(), false, false, "", false)))
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(false)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(false, false, false)).WithReRouteKey("").Build();
|
||||
|
||||
var context = new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamReRoute = reRoute,
|
||||
DownstreamRequest = new HttpRequestMessage() { RequestUri = new Uri("http://www.bbc.co.uk") },
|
||||
};
|
||||
|
||||
this.Given(x=>x.GivenTheRequestIs(context))
|
||||
.When(x=>x.WhenIGetResponse())
|
||||
.Then(x => x.ThenTheResponseIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
@ -52,13 +62,22 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_call_request_unable_to_complete_request()
|
||||
{
|
||||
this.Given(x => x.GivenTheRequestIs(new Ocelot.Request.Request(new HttpRequestMessage() { RequestUri = new Uri("http://localhost:60080") }, false, new NoQoSProvider(), false, false, "", false)))
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(false)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(false, false, false)).WithReRouteKey("").Build();
|
||||
|
||||
var context = new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamReRoute = reRoute,
|
||||
DownstreamRequest = new HttpRequestMessage() { RequestUri = new Uri("http://localhost:60080") },
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheRequestIs(context))
|
||||
.When(x => x.WhenIGetResponse())
|
||||
.Then(x => x.ThenTheResponseIsCalledError())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheRequestIs(Ocelot.Request.Request request)
|
||||
private void GivenTheRequestIs(DownstreamContext request)
|
||||
{
|
||||
_request = request;
|
||||
}
|
||||
|
@ -1,81 +1,73 @@
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Middleware;
|
||||
|
||||
namespace Ocelot.UnitTests.Requester
|
||||
{
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Requester;
|
||||
using Ocelot.Requester.Middleware;
|
||||
using Ocelot.Requester.QoS;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
|
||||
public class HttpRequesterMiddlewareTests : ServerHostedMiddlewareTest
|
||||
public class HttpRequesterMiddlewareTests
|
||||
{
|
||||
private readonly Mock<IHttpRequester> _requester;
|
||||
private OkResponse<HttpResponseMessage> _response;
|
||||
private OkResponse<Ocelot.Request.Request> _request;
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
private readonly HttpRequesterMiddleware _middleware;
|
||||
private DownstreamContext _downstreamContext;
|
||||
private OcelotRequestDelegate _next;
|
||||
|
||||
public HttpRequesterMiddlewareTests()
|
||||
{
|
||||
_requester = new Mock<IHttpRequester>();
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_loggerFactory.Setup(x => x.CreateLogger<HttpRequesterMiddleware>()).Returns(_logger.Object);
|
||||
_next = async context => {
|
||||
//do nothing
|
||||
};
|
||||
_middleware = new HttpRequesterMiddleware(_next, _loggerFactory.Object, _requester.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_scoped_data_repository_correctly()
|
||||
public void should_call_services_correctly()
|
||||
{
|
||||
this.Given(x => x.GivenTheRequestIs(new Ocelot.Request.Request(new HttpRequestMessage(),true, new NoQoSProvider(), false, false, "", false)))
|
||||
this.Given(x => x.GivenTheRequestIs())
|
||||
.And(x => x.GivenTheRequesterReturns(new HttpResponseMessage()))
|
||||
.And(x => x.GivenTheScopedRepoReturns())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheScopedRepoIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
private void WhenICallTheMiddleware()
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_requester.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
_middleware.Invoke(_downstreamContext).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
private void GivenTheRequestIs()
|
||||
{
|
||||
app.UseHttpRequesterMiddleware();
|
||||
}
|
||||
|
||||
private void GivenTheRequestIs(Ocelot.Request.Request request)
|
||||
{
|
||||
_request = new OkResponse<Ocelot.Request.Request>(request);
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<Ocelot.Request.Request>(It.IsAny<string>()))
|
||||
.Returns(_request);
|
||||
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||
_downstreamContext.DownstreamReRoute = new DownstreamReRouteBuilder().Build();
|
||||
}
|
||||
|
||||
private void GivenTheRequesterReturns(HttpResponseMessage response)
|
||||
{
|
||||
_response = new OkResponse<HttpResponseMessage>(response);
|
||||
_requester
|
||||
.Setup(x => x.GetResponse(It.IsAny<Ocelot.Request.Request>()))
|
||||
.Setup(x => x.GetResponse(It.IsAny<DownstreamContext>()))
|
||||
.ReturnsAsync(_response);
|
||||
}
|
||||
|
||||
private void GivenTheScopedRepoReturns()
|
||||
{
|
||||
ScopedRepository
|
||||
.Setup(x => x.Add(It.IsAny<string>(), _response.Data))
|
||||
.Returns(new OkResponse());
|
||||
}
|
||||
|
||||
private void ThenTheScopedRepoIsCalledCorrectly()
|
||||
{
|
||||
ScopedRepository
|
||||
.Verify(x => x.Add("HttpResponseMessage", _response.Data), Times.Once());
|
||||
_downstreamContext.DownstreamResponse.ShouldBe(_response.Data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
public class QoSProviderFactoryTests
|
||||
{
|
||||
private readonly IQoSProviderFactory _factory;
|
||||
private ReRoute _reRoute;
|
||||
private DownstreamReRoute _reRoute;
|
||||
private IQoSProvider _result;
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
@ -31,7 +31,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_return_no_qos_provider()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
var reRoute = new DownstreamReRouteBuilder()
|
||||
.WithUpstreamHttpMethod(new List<string> { "get" })
|
||||
.WithIsQos(false)
|
||||
.Build();
|
||||
@ -51,7 +51,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
.WithExceptionsAllowedBeforeBreaking(100)
|
||||
.Build();
|
||||
|
||||
var reRoute = new ReRouteBuilder()
|
||||
var reRoute = new DownstreamReRouteBuilder()
|
||||
.WithUpstreamHttpMethod(new List<string> { "get" })
|
||||
.WithIsQos(true)
|
||||
.WithQosOptions(qosOptions)
|
||||
@ -63,7 +63,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAReRoute(ReRoute reRoute)
|
||||
private void GivenAReRoute(DownstreamReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
private readonly QosProviderHouse _qosProviderHouse;
|
||||
private Response _addResult;
|
||||
private Response<IQoSProvider> _getResult;
|
||||
private ReRoute _reRoute;
|
||||
private DownstreamReRoute _reRoute;
|
||||
private readonly Mock<IQoSProviderFactory> _factory;
|
||||
|
||||
public QosProviderHouseTests()
|
||||
@ -27,7 +27,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_store_qos_provider_on_first_request()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder().WithReRouteKey("test").Build();
|
||||
var reRoute = new DownstreamReRouteBuilder().WithReRouteKey("test").Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAQoSProvider(reRoute, new FakeQoSProvider()))
|
||||
.Then(x => x.ThenItIsAdded())
|
||||
@ -37,7 +37,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_not_store_qos_provider_on_first_request()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder().WithReRouteKey("test").Build();
|
||||
var reRoute = new DownstreamReRouteBuilder().WithReRouteKey("test").Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAQoSProvider(reRoute, new FakeQoSProvider()))
|
||||
.When(x => x.WhenWeGetTheQoSProvider(reRoute))
|
||||
@ -48,8 +48,8 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_store_qos_providers_by_key()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder().WithReRouteKey("test").Build();
|
||||
var reRouteTwo = new ReRouteBuilder().WithReRouteKey("testTwo").Build();
|
||||
var reRoute = new DownstreamReRouteBuilder().WithReRouteKey("test").Build();
|
||||
var reRouteTwo = new DownstreamReRouteBuilder().WithReRouteKey("testTwo").Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAQoSProvider(reRoute, new FakeQoSProvider()))
|
||||
.And(x => x.GivenThereIsAQoSProvider(reRouteTwo, new FakePollyQoSProvider()))
|
||||
@ -63,7 +63,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_return_error_if_no_qos_provider_with_key()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder().Build();
|
||||
var reRoute = new DownstreamReRouteBuilder().Build();
|
||||
|
||||
this.When(x => x.WhenWeGetTheQoSProvider(reRoute))
|
||||
.Then(x => x.ThenAnErrorIsReturned())
|
||||
@ -73,9 +73,9 @@ namespace Ocelot.UnitTests.Requester
|
||||
[Fact]
|
||||
public void should_get_new_qos_provider_if_reroute_qos_provider_has_changed()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder().WithReRouteKey("test").Build();
|
||||
var reRoute = new DownstreamReRouteBuilder().WithReRouteKey("test").Build();
|
||||
|
||||
var reRouteTwo = new ReRouteBuilder().WithReRouteKey("test").WithIsQos(true).Build();
|
||||
var reRouteTwo = new DownstreamReRouteBuilder().WithReRouteKey("test").WithIsQos(true).Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAQoSProvider(reRoute, new FakeQoSProvider()))
|
||||
.When(x => x.WhenWeGetTheQoSProvider(reRoute))
|
||||
@ -85,7 +85,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void WhenIGetTheReRouteWithTheSameKeyButDifferentQosProvider(ReRoute reRoute)
|
||||
private void WhenIGetTheReRouteWithTheSameKeyButDifferentQosProvider(DownstreamReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
_factory.Setup(x => x.Get(_reRoute)).Returns(new FakePollyQoSProvider());
|
||||
@ -112,7 +112,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
}
|
||||
|
||||
|
||||
private void GivenThereIsAQoSProvider(ReRoute reRoute, IQoSProvider qoSProvider)
|
||||
private void GivenThereIsAQoSProvider(DownstreamReRoute reRoute, IQoSProvider qoSProvider)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
_qoSProvider = qoSProvider;
|
||||
@ -120,7 +120,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
_getResult = _qosProviderHouse.Get(reRoute);
|
||||
}
|
||||
|
||||
private void WhenWeGetTheQoSProvider(ReRoute reRoute)
|
||||
private void WhenWeGetTheQoSProvider(DownstreamReRoute reRoute)
|
||||
{
|
||||
_getResult = _qosProviderHouse.Get(reRoute);
|
||||
}
|
||||
|
Reference in New Issue
Block a user