mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-08-04 17:52:27 +08:00
Remove Ocelot specific Middleware to make Ocelot more compatible with kestrel middleware and get ready for YARP
This commit is contained in:
@ -21,19 +21,19 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private readonly CookieStickySessions _stickySessions;
|
||||
private readonly Mock<ILoadBalancer> _loadBalancer;
|
||||
private readonly int _defaultExpiryInMs;
|
||||
private DownstreamContext _downstreamContext;
|
||||
private Response<ServiceHostAndPort> _result;
|
||||
private Response<ServiceHostAndPort> _firstHostAndPort;
|
||||
private Response<ServiceHostAndPort> _secondHostAndPort;
|
||||
private readonly FakeBus<StickySession> _bus;
|
||||
private HttpContext _httpContext;
|
||||
|
||||
public CookieStickySessionsTests()
|
||||
{
|
||||
_httpContext = new DefaultHttpContext();
|
||||
_bus = new FakeBus<StickySession>();
|
||||
_loadBalancer = new Mock<ILoadBalancer>();
|
||||
_defaultExpiryInMs = 0;
|
||||
_stickySessions = new CookieStickySessions(_loadBalancer.Object, "sessionid", _defaultExpiryInMs, _bus);
|
||||
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -116,7 +116,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private void GivenTheLoadBalancerReturnsError()
|
||||
{
|
||||
_loadBalancer
|
||||
.Setup(x => x.Lease(It.IsAny<DownstreamContext>()))
|
||||
.Setup(x => x.Lease(It.IsAny<HttpContext>()))
|
||||
.ReturnsAsync(new ErrorResponse<ServiceHostAndPort>(new AnyError()));
|
||||
}
|
||||
|
||||
@ -138,14 +138,14 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
var cookiesTwo = new FakeCookies();
|
||||
cookiesTwo.AddCookie("sessionid", "123");
|
||||
contextTwo.Request.Cookies = cookiesTwo;
|
||||
_firstHostAndPort = await _stickySessions.Lease(new DownstreamContext(contextOne));
|
||||
_secondHostAndPort = await _stickySessions.Lease(new DownstreamContext(contextTwo));
|
||||
_firstHostAndPort = await _stickySessions.Lease(contextOne);
|
||||
_secondHostAndPort = await _stickySessions.Lease(contextTwo);
|
||||
}
|
||||
|
||||
private void GivenTheLoadBalancerReturnsSequence()
|
||||
{
|
||||
_loadBalancer
|
||||
.SetupSequence(x => x.Lease(It.IsAny<DownstreamContext>()))
|
||||
.SetupSequence(x => x.Lease(It.IsAny<HttpContext>()))
|
||||
.ReturnsAsync(new OkResponse<ServiceHostAndPort>(new ServiceHostAndPort("one", 80)))
|
||||
.ReturnsAsync(new OkResponse<ServiceHostAndPort>(new ServiceHostAndPort("two", 80)));
|
||||
}
|
||||
@ -158,8 +158,8 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
private async Task WhenILeaseTwiceInARow()
|
||||
{
|
||||
_firstHostAndPort = await _stickySessions.Lease(_downstreamContext);
|
||||
_secondHostAndPort = await _stickySessions.Lease(_downstreamContext);
|
||||
_firstHostAndPort = await _stickySessions.Lease(_httpContext);
|
||||
_secondHostAndPort = await _stickySessions.Lease(_httpContext);
|
||||
}
|
||||
|
||||
private void GivenTheDownstreamRequestHasSessionId(string value)
|
||||
@ -168,19 +168,19 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
var cookies = new FakeCookies();
|
||||
cookies.AddCookie("sessionid", value);
|
||||
context.Request.Cookies = cookies;
|
||||
_downstreamContext = new DownstreamContext(context);
|
||||
_httpContext = context;
|
||||
}
|
||||
|
||||
private void GivenTheLoadBalancerReturns()
|
||||
{
|
||||
_loadBalancer
|
||||
.Setup(x => x.Lease(It.IsAny<DownstreamContext>()))
|
||||
.Setup(x => x.Lease(It.IsAny<HttpContext>()))
|
||||
.ReturnsAsync(new OkResponse<ServiceHostAndPort>(new ServiceHostAndPort("", 80)));
|
||||
}
|
||||
|
||||
private async Task WhenILease()
|
||||
{
|
||||
_result = await _stickySessions.Lease(_downstreamContext);
|
||||
_result = await _stickySessions.Lease(_httpContext);
|
||||
}
|
||||
|
||||
private void ThenTheHostAndPortIsNotNull()
|
||||
|
@ -14,6 +14,8 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
public class DelegateInvokingLoadBalancerCreatorTests
|
||||
{
|
||||
private DelegateInvokingLoadBalancerCreator<FakeLoadBalancer> _creator;
|
||||
@ -113,7 +115,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
public DownstreamReRoute ReRoute { get; }
|
||||
public IServiceDiscoveryProvider ServiceDiscoveryProvider { get; }
|
||||
|
||||
public Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
|
||||
public Task<Response<ServiceHostAndPort>> Lease(HttpContext httpContext)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
@ -19,11 +19,11 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private LeastConnection _leastConnection;
|
||||
private List<Service> _services;
|
||||
private Random _random;
|
||||
private DownstreamContext _context;
|
||||
private HttpContext _httpContext;
|
||||
|
||||
public LeastConnectionTests()
|
||||
{
|
||||
_context = new DownstreamContext(new DefaultHttpContext());
|
||||
_httpContext = new DefaultHttpContext();
|
||||
_random = new Random();
|
||||
}
|
||||
|
||||
@ -64,9 +64,9 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
_leastConnection = new LeastConnection(() => Task.FromResult(availableServices), serviceName);
|
||||
|
||||
var hostAndPortOne = _leastConnection.Lease(_context).Result;
|
||||
var hostAndPortOne = _leastConnection.Lease(_httpContext).Result;
|
||||
hostAndPortOne.Data.DownstreamHost.ShouldBe("127.0.0.1");
|
||||
var hostAndPortTwo = _leastConnection.Lease(_context).Result;
|
||||
var hostAndPortTwo = _leastConnection.Lease(_httpContext).Result;
|
||||
hostAndPortTwo.Data.DownstreamHost.ShouldBe("127.0.0.2");
|
||||
_leastConnection.Release(hostAndPortOne.Data);
|
||||
_leastConnection.Release(hostAndPortTwo.Data);
|
||||
@ -76,9 +76,9 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
new Service(serviceName, new ServiceHostAndPort("127.0.0.1", 80), string.Empty, string.Empty, new string[0]),
|
||||
};
|
||||
|
||||
hostAndPortOne = _leastConnection.Lease(_context).Result;
|
||||
hostAndPortOne = _leastConnection.Lease(_httpContext).Result;
|
||||
hostAndPortOne.Data.DownstreamHost.ShouldBe("127.0.0.1");
|
||||
hostAndPortTwo = _leastConnection.Lease(_context).Result;
|
||||
hostAndPortTwo = _leastConnection.Lease(_httpContext).Result;
|
||||
hostAndPortTwo.Data.DownstreamHost.ShouldBe("127.0.0.1");
|
||||
_leastConnection.Release(hostAndPortOne.Data);
|
||||
_leastConnection.Release(hostAndPortTwo.Data);
|
||||
@ -89,9 +89,9 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
new Service(serviceName, new ServiceHostAndPort("127.0.0.2", 80), string.Empty, string.Empty, new string[0]),
|
||||
};
|
||||
|
||||
hostAndPortOne = _leastConnection.Lease(_context).Result;
|
||||
hostAndPortOne = _leastConnection.Lease(_httpContext).Result;
|
||||
hostAndPortOne.Data.DownstreamHost.ShouldBe("127.0.0.1");
|
||||
hostAndPortTwo = _leastConnection.Lease(_context).Result;
|
||||
hostAndPortTwo = _leastConnection.Lease( _httpContext).Result;
|
||||
hostAndPortTwo.Data.DownstreamHost.ShouldBe("127.0.0.2");
|
||||
_leastConnection.Release(hostAndPortOne.Data);
|
||||
_leastConnection.Release(hostAndPortTwo.Data);
|
||||
@ -99,7 +99,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
private async Task LeaseDelayAndRelease()
|
||||
{
|
||||
var hostAndPort = await _leastConnection.Lease(_context);
|
||||
var hostAndPort = await _leastConnection.Lease(_httpContext);
|
||||
await Task.Delay(_random.Next(1, 100));
|
||||
_leastConnection.Release(hostAndPort.Data);
|
||||
}
|
||||
@ -138,15 +138,15 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
_services = availableServices;
|
||||
_leastConnection = new LeastConnection(() => Task.FromResult(_services), serviceName);
|
||||
|
||||
var response = _leastConnection.Lease(_context).Result;
|
||||
var response = _leastConnection.Lease(_httpContext).Result;
|
||||
|
||||
response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);
|
||||
|
||||
response = _leastConnection.Lease(_context).Result;
|
||||
response = _leastConnection.Lease(_httpContext).Result;
|
||||
|
||||
response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
|
||||
|
||||
response = _leastConnection.Lease(_context).Result;
|
||||
response = _leastConnection.Lease(_httpContext).Result;
|
||||
|
||||
response.Data.DownstreamHost.ShouldBe(availableServices[2].HostAndPort.DownstreamHost);
|
||||
}
|
||||
@ -165,19 +165,19 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
_services = availableServices;
|
||||
_leastConnection = new LeastConnection(() => Task.FromResult(_services), serviceName);
|
||||
|
||||
var response = _leastConnection.Lease(_context).Result;
|
||||
var response = _leastConnection.Lease(_httpContext).Result;
|
||||
|
||||
response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);
|
||||
|
||||
response = _leastConnection.Lease(_context).Result;
|
||||
response = _leastConnection.Lease(_httpContext).Result;
|
||||
|
||||
response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
|
||||
|
||||
response = _leastConnection.Lease(_context).Result;
|
||||
response = _leastConnection.Lease(_httpContext).Result;
|
||||
|
||||
response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);
|
||||
|
||||
response = _leastConnection.Lease(_context).Result;
|
||||
response = _leastConnection.Lease(_httpContext).Result;
|
||||
|
||||
response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
|
||||
}
|
||||
@ -196,26 +196,26 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
_services = availableServices;
|
||||
_leastConnection = new LeastConnection(() => Task.FromResult(_services), serviceName);
|
||||
|
||||
var response = _leastConnection.Lease(_context).Result;
|
||||
var response = _leastConnection.Lease(_httpContext).Result;
|
||||
|
||||
response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);
|
||||
|
||||
response = _leastConnection.Lease(_context).Result;
|
||||
response = _leastConnection.Lease(_httpContext).Result;
|
||||
|
||||
response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
|
||||
|
||||
response = _leastConnection.Lease(_context).Result;
|
||||
response = _leastConnection.Lease(_httpContext).Result;
|
||||
|
||||
response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);
|
||||
|
||||
response = _leastConnection.Lease(_context).Result;
|
||||
response = _leastConnection.Lease(_httpContext).Result;
|
||||
|
||||
response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
|
||||
|
||||
//release this so 2 should have 1 connection and we should get 2 back as our next host and port
|
||||
_leastConnection.Release(availableServices[1].HostAndPort);
|
||||
|
||||
response = _leastConnection.Lease(_context).Result;
|
||||
response = _leastConnection.Lease(_httpContext).Result;
|
||||
|
||||
response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
|
||||
}
|
||||
@ -276,7 +276,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
private void WhenIGetTheNextHostAndPort()
|
||||
{
|
||||
_result = _leastConnection.Lease(_context).Result;
|
||||
_result = _leastConnection.Lease(_httpContext).Result;
|
||||
}
|
||||
|
||||
private void ThenTheNextHostAndPortIsReturned()
|
||||
|
@ -17,6 +17,7 @@ using Xunit;
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
public class LoadBalancerFactoryTests
|
||||
{
|
||||
@ -228,7 +229,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
private class FakeLoadBalancerOne : ILoadBalancer
|
||||
{
|
||||
public Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
|
||||
public Task<Response<ServiceHostAndPort>> Lease(HttpContext httpContext)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
@ -241,7 +242,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
private class FakeLoadBalancerTwo : ILoadBalancer
|
||||
{
|
||||
public Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
|
||||
public Task<Response<ServiceHostAndPort>> Lease(HttpContext httpContext)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
@ -254,7 +255,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
private class FakeNoLoadBalancer : ILoadBalancer
|
||||
{
|
||||
public Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
|
||||
public Task<Response<ServiceHostAndPort>> Lease(HttpContext httpContext)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
@ -267,7 +268,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
private class BrokenLoadBalancer : ILoadBalancer
|
||||
{
|
||||
public Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
|
||||
public Task<Response<ServiceHostAndPort>> Lease(HttpContext httpContext)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
@ -13,6 +13,8 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
public class LoadBalancerHouseTests
|
||||
{
|
||||
private DownstreamReRoute _reRoute;
|
||||
@ -155,7 +157,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
private class FakeLoadBalancer : ILoadBalancer
|
||||
{
|
||||
public Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
|
||||
public Task<Response<ServiceHostAndPort>> Lease(HttpContext httpContext)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
@ -168,7 +170,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
private class FakeRoundRobinLoadBalancer : ILoadBalancer
|
||||
{
|
||||
public Task<Response<ServiceHostAndPort>> Lease(DownstreamContext context)
|
||||
public Task<Response<ServiceHostAndPort>> Lease(HttpContext httpContext)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
@ -1,7 +1,3 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using Ocelot.Middleware;
|
||||
|
||||
namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@ -19,8 +15,13 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
|
||||
public class LoadBalancerMiddlewareTests
|
||||
{
|
||||
@ -34,21 +35,22 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
private LoadBalancingMiddleware _middleware;
|
||||
private DownstreamContext _downstreamContext;
|
||||
private OcelotRequestDelegate _next;
|
||||
private RequestDelegate _next;
|
||||
private HttpContext _httpContext;
|
||||
private Mock<IRequestScopedDataRepository> _repo;
|
||||
|
||||
public LoadBalancerMiddlewareTests()
|
||||
{
|
||||
_repo = new Mock<IRequestScopedDataRepository>();
|
||||
_httpContext = new DefaultHttpContext();
|
||||
_loadBalancerHouse = new Mock<ILoadBalancerHouse>();
|
||||
_loadBalancer = new Mock<ILoadBalancer>();
|
||||
_loadBalancerHouse = new Mock<ILoadBalancerHouse>();
|
||||
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "http://test.com/");
|
||||
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_loggerFactory.Setup(x => x.CreateLogger<LoadBalancingMiddleware>()).Returns(_logger.Object);
|
||||
_next = context => Task.CompletedTask;
|
||||
_downstreamContext.DownstreamRequest = new DownstreamRequest(_downstreamRequest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -133,34 +135,34 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private void WhenICallTheMiddleware()
|
||||
{
|
||||
_middleware = new LoadBalancingMiddleware(_next, _loggerFactory.Object, _loadBalancerHouse.Object);
|
||||
_middleware.Invoke(_downstreamContext).GetAwaiter().GetResult();
|
||||
_middleware.Invoke(_httpContext).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(ServiceProviderConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
var configuration = new InternalConfiguration(null, null, config, null, null, null, null, null, null);
|
||||
_downstreamContext.Configuration = configuration;
|
||||
_httpContext.Items.SetIInternalConfiguration(configuration);
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamUrlIs(string downstreamUrl)
|
||||
{
|
||||
_downstreamRequest.RequestUri = new System.Uri(downstreamUrl);
|
||||
_downstreamContext.DownstreamRequest = new DownstreamRequest(_downstreamRequest);
|
||||
_downstreamRequest.RequestUri = new Uri(downstreamUrl);
|
||||
_httpContext.Items.UpsertDownstreamRequest(new DownstreamRequest(_downstreamRequest));
|
||||
}
|
||||
|
||||
private void GivenTheLoadBalancerReturnsAnError()
|
||||
{
|
||||
_getHostAndPortError = new ErrorResponse<ServiceHostAndPort>(new List<Error>() { new ServicesAreNullError($"services were null for bah") });
|
||||
_loadBalancer
|
||||
.Setup(x => x.Lease(It.IsAny<DownstreamContext>()))
|
||||
.Setup(x => x.Lease(It.IsAny<HttpContext>()))
|
||||
.ReturnsAsync(_getHostAndPortError);
|
||||
}
|
||||
|
||||
private void GivenTheLoadBalancerReturnsOk()
|
||||
{
|
||||
_loadBalancer
|
||||
.Setup(x => x.Lease(It.IsAny<DownstreamContext>()))
|
||||
.Setup(x => x.Lease(It.IsAny<HttpContext>()))
|
||||
.ReturnsAsync(new OkResponse<ServiceHostAndPort>(new ServiceHostAndPort("abc", 123, "https")));
|
||||
}
|
||||
|
||||
@ -168,14 +170,14 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
_hostAndPort = new ServiceHostAndPort("127.0.0.1", 80);
|
||||
_loadBalancer
|
||||
.Setup(x => x.Lease(It.IsAny<DownstreamContext>()))
|
||||
.Setup(x => x.Lease(It.IsAny<HttpContext>()))
|
||||
.ReturnsAsync(new OkResponse<ServiceHostAndPort>(_hostAndPort));
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamReRoute downstreamRoute, List<Ocelot.DownstreamRouteFinder.UrlMatcher.PlaceholderNameAndValue> placeholder)
|
||||
{
|
||||
_downstreamContext.TemplatePlaceholderNameAndValues = placeholder;
|
||||
_downstreamContext.DownstreamReRoute = downstreamRoute;
|
||||
_httpContext.Items.UpsertTemplatePlaceholderNameAndValues(placeholder);
|
||||
_httpContext.Items.UpsertDownstreamReRoute(downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheLoadBalancerHouseReturns()
|
||||
@ -199,32 +201,32 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
private void ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline()
|
||||
{
|
||||
_downstreamContext.IsError.ShouldBeTrue();
|
||||
_downstreamContext.Errors.ShouldBe(_getLoadBalancerHouseError.Errors);
|
||||
_httpContext.Items.Errors().Count.ShouldBeGreaterThan(0);
|
||||
_httpContext.Items.Errors().ShouldBe(_getLoadBalancerHouseError.Errors);
|
||||
}
|
||||
|
||||
private void ThenAnErrorSayingReleaseFailedIsSetOnThePipeline()
|
||||
{
|
||||
_downstreamContext.IsError.ShouldBeTrue();
|
||||
_downstreamContext.Errors.ShouldBe(It.IsAny<List<Error>>());
|
||||
_httpContext.Items.Errors().Count.ShouldBeGreaterThan(0);
|
||||
_httpContext.Items.Errors().ShouldBe(It.IsAny<List<Error>>());
|
||||
}
|
||||
|
||||
private void ThenAnErrorStatingHostAndPortCouldNotBeFoundIsSetOnPipeline()
|
||||
{
|
||||
_downstreamContext.IsError.ShouldBeTrue();
|
||||
_downstreamContext.Errors.ShouldBe(_getHostAndPortError.Errors);
|
||||
_httpContext.Items.Errors().Count.ShouldBeGreaterThan(0);
|
||||
_httpContext.Items.Errors().ShouldBe(_getHostAndPortError.Errors);
|
||||
}
|
||||
|
||||
private void ThenAnHostAndPortIsSetOnPipeline()
|
||||
{
|
||||
_downstreamContext.DownstreamRequest.Host.ShouldBeEquivalentTo("abc");
|
||||
_downstreamContext.DownstreamRequest.Port.ShouldBeEquivalentTo(123);
|
||||
_downstreamContext.DownstreamRequest.Scheme.ShouldBeEquivalentTo("https");
|
||||
_httpContext.Items.DownstreamRequest().Host.ShouldBeEquivalentTo("abc");
|
||||
_httpContext.Items.DownstreamRequest().Port.ShouldBeEquivalentTo(123);
|
||||
_httpContext.Items.DownstreamRequest().Scheme.ShouldBeEquivalentTo("https");
|
||||
}
|
||||
|
||||
private void ThenTheDownstreamUrlIsReplacedWith(string expectedUri)
|
||||
{
|
||||
_downstreamContext.DownstreamRequest.ToHttpRequestMessage().RequestUri.OriginalString.ShouldBe(expectedUri);
|
||||
_httpContext.Items.DownstreamRequest().ToHttpRequestMessage().RequestUri.OriginalString.ShouldBe(expectedUri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -91,7 +91,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
private void WhenIGetTheNextHostAndPort()
|
||||
{
|
||||
_result = _loadBalancer.Lease(new DownstreamContext(new DefaultHttpContext())).Result;
|
||||
_result = _loadBalancer.Lease(new DefaultHttpContext()).Result;
|
||||
}
|
||||
|
||||
private void ThenTheHostAndPortIs(ServiceHostAndPort expected)
|
||||
|
@ -17,12 +17,11 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
private readonly RoundRobin _roundRobin;
|
||||
private readonly List<Service> _services;
|
||||
private Response<ServiceHostAndPort> _hostAndPort;
|
||||
private DownstreamContext _context;
|
||||
private HttpContext _httpContext;
|
||||
|
||||
public RoundRobinTests()
|
||||
{
|
||||
_context = new DownstreamContext(new DefaultHttpContext());
|
||||
|
||||
_httpContext = new DefaultHttpContext();
|
||||
_services = new List<Service>
|
||||
{
|
||||
new Service("product", new ServiceHostAndPort("127.0.0.1", 5000), string.Empty, string.Empty, new string[0]),
|
||||
@ -52,18 +51,18 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
|
||||
while (stopWatch.ElapsedMilliseconds < 1000)
|
||||
{
|
||||
var address = _roundRobin.Lease(_context).Result;
|
||||
var address = _roundRobin.Lease(_httpContext).Result;
|
||||
address.Data.ShouldBe(_services[0].HostAndPort);
|
||||
address = _roundRobin.Lease(_context).Result;
|
||||
address = _roundRobin.Lease(_httpContext).Result;
|
||||
address.Data.ShouldBe(_services[1].HostAndPort);
|
||||
address = _roundRobin.Lease(_context).Result;
|
||||
address = _roundRobin.Lease(_httpContext).Result;
|
||||
address.Data.ShouldBe(_services[2].HostAndPort);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenIGetTheNextAddress()
|
||||
{
|
||||
_hostAndPort = _roundRobin.Lease(_context).Result;
|
||||
_hostAndPort = _roundRobin.Lease(_httpContext).Result;
|
||||
}
|
||||
|
||||
private void ThenTheNextAddressIndexIs(int index)
|
||||
|
Reference in New Issue
Block a user