first passing proxy acceptnace test yeyyy!

This commit is contained in:
TomPallister
2016-09-07 21:47:39 +01:00
parent 71b7e7743e
commit 03ef47038a
12 changed files with 264 additions and 43 deletions

View File

@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Moq;
using Ocelot.Library.Infrastructure.Configuration;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.UrlMatcher;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests
{
public class DownstreamRouteFinderTests
{
private readonly IDownstreamRouteFinder _downstreamRouteFinder;
private readonly Mock<IOptions<Configuration>> _mockConfig;
private readonly Mock<IUrlPathToUrlTemplateMatcher> _mockMatcher;
private string _upstreamUrlPath;
private Response<DownstreamRoute> _result;
private Response<DownstreamRoute> _response;
private Configuration _configuration;
private UrlMatch _match;
public DownstreamRouteFinderTests()
{
_mockConfig = new Mock<IOptions<Configuration>>();
_mockMatcher = new Mock<IUrlPathToUrlTemplateMatcher>();
_downstreamRouteFinder = new DownstreamRouteFinder(_mockConfig.Object, _mockMatcher.Object);
}
[Fact]
public void should_return_route()
{
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("somePath"))
.And(x => x.GivenTheConfigurationIs(new Configuration {
ReRoutes = new List<ReRoute>
{
new ReRoute()
{
UpstreamTemplate = "somePath",
DownstreamTemplate = "somPath"
}
}
}))
.And(x => x.GivenTheUrlMatcherReturns(new UrlMatch(true, new List<TemplateVariableNameAndValue>(), "somePath")))
.When(x => x.WhenICallTheFinder())
.Then(
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "somePath")))
.And(x => x.ThenTheUrlMatcherIsCalledCorrectly())
.BDDfy();
}
[Fact]
public void should_not_return_route()
{
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("somePath"))
.And(x => x.GivenTheConfigurationIs(new Configuration
{
ReRoutes = new List<ReRoute>
{
new ReRoute()
{
UpstreamTemplate = "somePath",
DownstreamTemplate = "somPath"
}
}
}))
.And(x => x.GivenTheUrlMatcherReturns(new UrlMatch(false, new List<TemplateVariableNameAndValue>(), null)))
.When(x => x.WhenICallTheFinder())
.Then(
x => x.ThenAnErrorResponseIsReturned())
.And(x => x.ThenTheUrlMatcherIsCalledCorrectly())
.BDDfy();
}
private void ThenAnErrorResponseIsReturned()
{
_result.IsError.ShouldBeTrue();
}
private void ThenTheUrlMatcherIsCalledCorrectly()
{
_mockMatcher
.Verify(x => x.Match(_upstreamUrlPath, _configuration.ReRoutes[0].UpstreamTemplate), Times.Once);
}
private void GivenTheUrlMatcherReturns(UrlMatch match)
{
_match = match;
_mockMatcher
.Setup(x => x.Match(It.IsAny<string>(), It.IsAny<string>()))
.Returns(_match);
}
private void GivenTheConfigurationIs(Configuration configuration)
{
_configuration = configuration;
_mockConfig
.Setup(x => x.Value)
.Returns(_configuration);
}
private void GivenThereIsAnUpstreamUrlPath(string upstreamUrlPath)
{
_upstreamUrlPath = upstreamUrlPath;
}
private void WhenICallTheFinder()
{
_result = _downstreamRouteFinder.FindDownstreamRoute(_upstreamUrlPath);
}
private void ThenTheFollowingIsReturned(DownstreamRoute expected)
{
_result.Data.DownstreamUrlTemplate.ShouldBe(expected.DownstreamUrlTemplate);
for (int i = 0; i < _result.Data.TemplateVariableNameAndValues.Count; i++)
{
_result.Data.TemplateVariableNameAndValues[i].TemplateVariableName.ShouldBe(
expected.TemplateVariableNameAndValues[i].TemplateVariableName);
_result.Data.TemplateVariableNameAndValues[i].TemplateVariableValue.ShouldBe(
expected.TemplateVariableNameAndValues[i].TemplateVariableValue);
}
_result.IsError.ShouldBeFalse();
}
}
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.UrlMatcher;
using Ocelot.Library.Infrastructure.UrlTemplateReplacer;
using Shouldly;
@ -10,7 +11,7 @@ namespace Ocelot.UnitTests
public class UpstreamUrlPathTemplateVariableReplacerTests
{
private UrlMatch _urlMatch;
private DownstreamRoute _downstreamRoute;
private string _result;
private readonly IDownstreamUrlTemplateVariableReplacer _downstreamUrlPathReplacer;
@ -22,7 +23,7 @@ namespace Ocelot.UnitTests
[Fact]
public void can_replace_no_template_variables()
{
this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List<TemplateVariableNameAndValue>(), "")))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "")))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned(""))
.BDDfy();
@ -31,7 +32,7 @@ namespace Ocelot.UnitTests
[Fact]
public void can_replace_no_template_variables_with_slash()
{
this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List<TemplateVariableNameAndValue>(), "/")))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "/")))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("/"))
.BDDfy();
@ -40,7 +41,7 @@ namespace Ocelot.UnitTests
[Fact]
public void can_replace_url_no_slash()
{
this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List<TemplateVariableNameAndValue>(), "api")))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "api")))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api"))
.BDDfy();
@ -49,7 +50,7 @@ namespace Ocelot.UnitTests
[Fact]
public void can_replace_url_one_slash()
{
this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List<TemplateVariableNameAndValue>(), "api/")))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "api/")))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/"))
.BDDfy();
@ -58,7 +59,7 @@ namespace Ocelot.UnitTests
[Fact]
public void can_replace_url_multiple_slash()
{
this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List<TemplateVariableNameAndValue>(), "api/product/products/")))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "api/product/products/")))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/product/products/"))
.BDDfy();
@ -72,7 +73,7 @@ namespace Ocelot.UnitTests
new TemplateVariableNameAndValue("{productId}", "1")
};
this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, templateVariables, "productservice/products/{productId}/")))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, "productservice/products/{productId}/")))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/"))
.BDDfy();
@ -86,7 +87,7 @@ namespace Ocelot.UnitTests
new TemplateVariableNameAndValue("{productId}", "1")
};
this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, templateVariables, "productservice/products/{productId}/variants")))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, "productservice/products/{productId}/variants")))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants"))
.BDDfy();
@ -101,7 +102,7 @@ namespace Ocelot.UnitTests
new TemplateVariableNameAndValue("{variantId}", "12")
};
this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, templateVariables, "productservice/products/{productId}/variants/{variantId}")))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, "productservice/products/{productId}/variants/{variantId}")))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants/12"))
.BDDfy();
@ -117,20 +118,20 @@ namespace Ocelot.UnitTests
new TemplateVariableNameAndValue("{categoryId}", "34")
};
this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, templateVariables, "productservice/category/{categoryId}/products/{productId}/variants/{variantId}")))
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, "productservice/category/{categoryId}/products/{productId}/variants/{variantId}")))
.When(x => x.WhenIReplaceTheTemplateVariables())
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/category/34/products/1/variants/12"))
.BDDfy();
}
private void GivenThereIsAUrlMatch(UrlMatch urlMatch)
private void GivenThereIsAUrlMatch(DownstreamRoute downstreamRoute)
{
_urlMatch = urlMatch;
_downstreamRoute = downstreamRoute;
}
private void WhenIReplaceTheTemplateVariables()
{
_result = _downstreamUrlPathReplacer.ReplaceTemplateVariable(_urlMatch);
_result = _downstreamUrlPathReplacer.ReplaceTemplateVariable(_downstreamRoute);
}
private void ThenTheDownstreamUrlPathIsReturned(string expected)

View File

@ -24,7 +24,8 @@
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Shouldly": "2.8.0",
"TestStack.BDDfy": "4.3.1",
"YamlDotNet": "3.9.0"
"YamlDotNet": "3.9.0",
"Moq": "4.6.38-alpha"
},
"frameworks": {