Fixed some unit tests

This commit is contained in:
Philip Wood
2017-04-18 14:05:15 +01:00
parent 8b93f44077
commit 7c1a277147
12 changed files with 145 additions and 425 deletions

View File

@ -16,6 +16,7 @@ using Ocelot.Responses;
using Ocelot.Values;
using TestStack.BDDfy;
using Xunit;
using Shouldly;
namespace Ocelot.UnitTests.LoadBalancer
{
@ -33,6 +34,7 @@ namespace Ocelot.UnitTests.LoadBalancer
private OkResponse<DownstreamRoute> _downstreamRoute;
private ErrorResponse<ILoadBalancer> _getLoadBalancerHouseError;
private ErrorResponse<HostAndPort> _getHostAndPortError;
private HttpRequestMessage _downstreamRequest;
public LoadBalancerMiddlewareTests()
{
@ -59,6 +61,10 @@ namespace Ocelot.UnitTests.LoadBalancer
app.UseLoadBalancingMiddleware();
});
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "");
_scopedRepository
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
_server = new TestServer(builder);
_client = _server.CreateClient();
}
@ -71,12 +77,12 @@ namespace Ocelot.UnitTests.LoadBalancer
.WithUpstreamHttpMethod("Get")
.Build());
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
this.Given(x => x.GivenTheDownStreamUrlIs("http://my.url/abc?q=123"))
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
.And(x => x.GivenTheLoadBalancerHouseReturns())
.And(x => x.GivenTheLoadBalancerReturns())
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
.Then(x => x.ThenTheDownstreamUrlIsReplacedWith("http://127.0.0.1:80/abc?q=123"))
.BDDfy();
}
@ -88,7 +94,7 @@ namespace Ocelot.UnitTests.LoadBalancer
.WithUpstreamHttpMethod("Get")
.Build());
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
this.Given(x => x.GivenTheDownStreamUrlIs("http://my.url/abc?q=123"))
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
.And(x => x.GivenTheLoadBalancerHouseReturnsAnError())
.When(x => x.WhenICallTheMiddleware())
@ -104,7 +110,7 @@ namespace Ocelot.UnitTests.LoadBalancer
.WithUpstreamHttpMethod("Get")
.Build());
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
this.Given(x => x.GivenTheDownStreamUrlIs("http://my.url/abc?q=123"))
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
.And(x => x.GivenTheLoadBalancerHouseReturns())
.And(x => x.GivenTheLoadBalancerReturnsAnError())
@ -113,6 +119,11 @@ namespace Ocelot.UnitTests.LoadBalancer
.BDDfy();
}
private void GivenTheDownStreamUrlIs(string downstreamUrl)
{
_downstreamRequest.RequestUri = new System.Uri(downstreamUrl);
}
private void GivenTheLoadBalancerReturnsAnError()
{
_getHostAndPortError = new ErrorResponse<HostAndPort>(new List<Error>() { new ServicesAreNullError($"services were null for bah") });
@ -157,10 +168,9 @@ namespace Ocelot.UnitTests.LoadBalancer
.Returns(_getLoadBalancerHouseError);
}
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
private void WhenICallTheMiddleware()
{
_scopedRepository
.Verify(x => x.Add("HostAndPort", _hostAndPort), Times.Once());
_result = _client.GetAsync(_url).Result;
}
private void ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline()
@ -190,17 +200,11 @@ namespace Ocelot.UnitTests.LoadBalancer
.Verify(x => x.Add("OcelotMiddlewareErrors", _getHostAndPortError.Errors), Times.Once);
}
private void WhenICallTheMiddleware()
{
_result = _client.GetAsync(_url).Result;
}
private void GivenTheDownStreamUrlIs(string downstreamUrl)
private void ThenTheDownstreamUrlIsReplacedWith(string expectedUri)
{
_downstreamUrl = new OkResponse<string>(downstreamUrl);
_scopedRepository
.Setup(x => x.Get<string>(It.IsAny<string>()))
.Returns(_downstreamUrl);
_downstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
}
public void Dispose()