diff --git a/src/Ocelot.Library/Infrastructure/Authentication/CouldNotFindConfigurationError.cs b/src/Ocelot.Library/Infrastructure/Authentication/CouldNotFindConfigurationError.cs deleted file mode 100644 index 5b30c463..00000000 --- a/src/Ocelot.Library/Infrastructure/Authentication/CouldNotFindConfigurationError.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Ocelot.Library.Infrastructure.Authentication -{ - using Responses; - public class CouldNotFindConfigurationError : Error - { - public CouldNotFindConfigurationError(string message) - : base(message) - { - } - } -} diff --git a/src/Ocelot.Library/Infrastructure/Authentication/IRouteRequiresAuthentication.cs b/src/Ocelot.Library/Infrastructure/Authentication/IRouteRequiresAuthentication.cs deleted file mode 100644 index 0a3f9858..00000000 --- a/src/Ocelot.Library/Infrastructure/Authentication/IRouteRequiresAuthentication.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Ocelot.Library.Infrastructure.Authentication -{ - using DownstreamRouteFinder; - using Responses; - - public interface IRouteRequiresAuthentication - { - Response IsAuthenticated(DownstreamRoute downstreamRoute, string httpMethod); - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/Authentication/RouteRequiresAuthentication.cs b/src/Ocelot.Library/Infrastructure/Authentication/RouteRequiresAuthentication.cs deleted file mode 100644 index 988959e4..00000000 --- a/src/Ocelot.Library/Infrastructure/Authentication/RouteRequiresAuthentication.cs +++ /dev/null @@ -1,35 +0,0 @@ -namespace Ocelot.Library.Infrastructure.Authentication -{ - using System; - using System.Collections.Generic; - using System.Linq; - using Configuration; - using DownstreamRouteFinder; - using Responses; - - public class RouteRequiresAuthentication : IRouteRequiresAuthentication - { - private readonly IOcelotConfiguration _configuration; - - public RouteRequiresAuthentication(IOcelotConfiguration configuration) - { - _configuration = configuration; - } - - public Response IsAuthenticated(DownstreamRoute downstreamRoute, string httpMethod) - { - var reRoute = - _configuration.ReRoutes.FirstOrDefault( - x => - x.DownstreamTemplate == downstreamRoute.DownstreamUrlTemplate && - string.Equals(x.UpstreamHttpMethod, httpMethod, StringComparison.CurrentCultureIgnoreCase)); - - if (reRoute == null) - { - return new ErrorResponse(new List {new CouldNotFindConfigurationError($"Could not find configuration for {downstreamRoute.DownstreamUrlTemplate} using method {httpMethod}")}); - } - - return new OkResponse(reRoute.IsAuthenticated); - } - } -} diff --git a/src/Ocelot.Library/Infrastructure/Builder/ReRouteBuilder.cs b/src/Ocelot.Library/Infrastructure/Builder/ReRouteBuilder.cs new file mode 100644 index 00000000..3966ae7c --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/Builder/ReRouteBuilder.cs @@ -0,0 +1,47 @@ +namespace Ocelot.Library.Infrastructure.Builder +{ + using Configuration; + + public class ReRouteBuilder + { + private string _downstreamTemplate; + private string _upstreamTemplate; + private string _upstreamTemplatePattern; + private string _upstreamHttpMethod; + private bool _isAuthenticated; + private string _authenticationProvider; + + public void WithDownstreamTemplate(string input) + { + _downstreamTemplate = input; + } + + public void WithUpstreamTemplate(string input) + { + _upstreamTemplate = input; + } + + public void WithUpstreamTemplatePattern(string input) + { + _upstreamTemplatePattern = input; + } + public void WithUpstreamHttpMethod(string input) + { + _upstreamHttpMethod = input; + } + public void WithIsAuthenticated(bool input) + { + _isAuthenticated = input; + + } + public void WithAuthenticationProvider(string input) + { + _authenticationProvider = input; + } + + public ReRoute Build() + { + return new ReRoute(_downstreamTemplate, _upstreamTemplate, _upstreamHttpMethod, _upstreamTemplatePattern, _isAuthenticated, _authenticationProvider); + } + } +} diff --git a/src/Ocelot.Library/Infrastructure/Configuration/OcelotConfiguration.cs b/src/Ocelot.Library/Infrastructure/Configuration/OcelotConfiguration.cs index 7ac93b3c..1b93447a 100644 --- a/src/Ocelot.Library/Infrastructure/Configuration/OcelotConfiguration.cs +++ b/src/Ocelot.Library/Infrastructure/Configuration/OcelotConfiguration.cs @@ -45,7 +45,7 @@ namespace Ocelot.Library.Infrastructure.Configuration var isAuthenticated = !string.IsNullOrEmpty(reRoute.Authentication); - _reRoutes.Add(new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated)); + _reRoutes.Add(new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated, reRoute.Authentication)); } } diff --git a/src/Ocelot.Library/Infrastructure/Configuration/ReRoute.cs b/src/Ocelot.Library/Infrastructure/Configuration/ReRoute.cs index 9c5edf0f..5d000511 100644 --- a/src/Ocelot.Library/Infrastructure/Configuration/ReRoute.cs +++ b/src/Ocelot.Library/Infrastructure/Configuration/ReRoute.cs @@ -2,13 +2,14 @@ { public class ReRoute { - public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated) + public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated, string authenticationProvider) { DownstreamTemplate = downstreamTemplate; UpstreamTemplate = upstreamTemplate; UpstreamHttpMethod = upstreamHttpMethod; UpstreamTemplatePattern = upstreamTemplatePattern; IsAuthenticated = isAuthenticated; + AuthenticationProvider = authenticationProvider; } public string DownstreamTemplate { get; private set; } @@ -16,5 +17,6 @@ public string UpstreamTemplatePattern { get; private set; } public string UpstreamHttpMethod { get; private set; } public bool IsAuthenticated { get; private set; } + public string AuthenticationProvider { get; private set; } } } \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/DownstreamRouteFinder/DownstreamRoute.cs b/src/Ocelot.Library/Infrastructure/DownstreamRouteFinder/DownstreamRoute.cs index b88f0063..8d8cd074 100644 --- a/src/Ocelot.Library/Infrastructure/DownstreamRouteFinder/DownstreamRoute.cs +++ b/src/Ocelot.Library/Infrastructure/DownstreamRouteFinder/DownstreamRoute.cs @@ -3,14 +3,16 @@ using Ocelot.Library.Infrastructure.UrlMatcher; namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder { + using Configuration; + public class DownstreamRoute { - public DownstreamRoute(List templateVariableNameAndValues, string downstreamUrlTemplate) + public DownstreamRoute(List templateVariableNameAndValues, ReRoute reRoute) { TemplateVariableNameAndValues = templateVariableNameAndValues; - DownstreamUrlTemplate = downstreamUrlTemplate; + ReRoute = reRoute; } public List TemplateVariableNameAndValues { get; private set; } - public string DownstreamUrlTemplate { get; private set; } + public ReRoute ReRoute { get; private set; } } } \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/DownstreamRouteFinder/DownstreamRouteFinder.cs b/src/Ocelot.Library/Infrastructure/DownstreamRouteFinder/DownstreamRouteFinder.cs index d87b4ccd..3063cf76 100644 --- a/src/Ocelot.Library/Infrastructure/DownstreamRouteFinder/DownstreamRouteFinder.cs +++ b/src/Ocelot.Library/Infrastructure/DownstreamRouteFinder/DownstreamRouteFinder.cs @@ -32,7 +32,7 @@ namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder var templateVariableNameAndValues = _templateVariableNameAndValueFinder.Find(upstreamUrlPath, template.UpstreamTemplate); - return new OkResponse(new DownstreamRoute(templateVariableNameAndValues.Data, template.DownstreamTemplate)); + return new OkResponse(new DownstreamRoute(templateVariableNameAndValues.Data, template)); } } diff --git a/src/Ocelot.Library/Infrastructure/UrlTemplateReplacer/DownstreamUrlTemplateVariableReplacer.cs b/src/Ocelot.Library/Infrastructure/UrlTemplateReplacer/DownstreamUrlTemplateVariableReplacer.cs index d9502341..65f8a759 100644 --- a/src/Ocelot.Library/Infrastructure/UrlTemplateReplacer/DownstreamUrlTemplateVariableReplacer.cs +++ b/src/Ocelot.Library/Infrastructure/UrlTemplateReplacer/DownstreamUrlTemplateVariableReplacer.cs @@ -10,7 +10,7 @@ namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer { var upstreamUrl = new StringBuilder(); - upstreamUrl.Append(downstreamRoute.DownstreamUrlTemplate); + upstreamUrl.Append(downstreamRoute.ReRoute.DownstreamTemplate); foreach (var templateVarAndValue in downstreamRoute.TemplateVariableNameAndValues) { diff --git a/src/Ocelot.Library/Middleware/AuthenticationMiddleware.cs b/src/Ocelot.Library/Middleware/AuthenticationMiddleware.cs index 1d745c1c..6fc73817 100644 --- a/src/Ocelot.Library/Middleware/AuthenticationMiddleware.cs +++ b/src/Ocelot.Library/Middleware/AuthenticationMiddleware.cs @@ -1,41 +1,35 @@ namespace Ocelot.Library.Middleware { using System.Threading.Tasks; - using Infrastructure.Authentication; + using Infrastructure.Configuration; using Infrastructure.DownstreamRouteFinder; using Infrastructure.Repository; - using Infrastructure.Responses; using Microsoft.AspNetCore.Http; public class AuthenticationMiddleware : OcelotMiddleware { private readonly RequestDelegate _next; private readonly IScopedRequestDataRepository _scopedRequestDataRepository; - private readonly IRouteRequiresAuthentication _requiresAuthentication; public AuthenticationMiddleware(RequestDelegate next, - IScopedRequestDataRepository scopedRequestDataRepository, - IRouteRequiresAuthentication requiresAuthentication) + IScopedRequestDataRepository scopedRequestDataRepository) : base(scopedRequestDataRepository) { _next = next; _scopedRequestDataRepository = scopedRequestDataRepository; - _requiresAuthentication = requiresAuthentication; } public async Task Invoke(HttpContext context) { var downstreamRoute = _scopedRequestDataRepository.Get("DownstreamRoute"); - var isAuthenticated = _requiresAuthentication.IsAuthenticated(downstreamRoute.Data, context.Request.Method); - - if (isAuthenticated.IsError) + if (downstreamRoute.IsError) { SetPipelineError(downstreamRoute.Errors); return; } - if (IsAuthenticatedRoute(isAuthenticated)) + if (IsAuthenticatedRoute(downstreamRoute.Data.ReRoute)) { //todo - build auth pipeline and then call normal pipeline if all good? await _next.Invoke(context); @@ -46,9 +40,9 @@ } } - private static bool IsAuthenticatedRoute(Response isAuthenticated) + private static bool IsAuthenticatedRoute(ReRoute reRoute) { - return isAuthenticated.Data; + return reRoute.IsAuthenticated; } } } diff --git a/src/Ocelot/Startup.cs b/src/Ocelot/Startup.cs index 9056daf9..b9298d0a 100644 --- a/src/Ocelot/Startup.cs +++ b/src/Ocelot/Startup.cs @@ -20,8 +20,6 @@ using Ocelot.Library.Middleware; namespace Ocelot { - using Library.Infrastructure.Authentication; - public class Startup { public Startup(IHostingEnvironment env) @@ -54,7 +52,6 @@ namespace Ocelot services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); // see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc services.AddSingleton(); diff --git a/test/Ocelot.UnitTests/Authentication/RequiresAuthenticationTests.cs b/test/Ocelot.UnitTests/Authentication/RequiresAuthenticationTests.cs deleted file mode 100644 index 5747c963..00000000 --- a/test/Ocelot.UnitTests/Authentication/RequiresAuthenticationTests.cs +++ /dev/null @@ -1,96 +0,0 @@ -namespace Ocelot.UnitTests.Authentication -{ - using System.Collections.Generic; - using Library.Infrastructure.Authentication; - using Library.Infrastructure.Configuration; - using Library.Infrastructure.DownstreamRouteFinder; - using Library.Infrastructure.Responses; - using Library.Infrastructure.UrlMatcher; - using Moq; - using Shouldly; - using TestStack.BDDfy; - using Xunit; - - public class RequiresAuthenticationTests - { - private readonly RouteRequiresAuthentication _routeRequiresAuthentication; - private string _url; - private readonly Mock _config; - private Response _result; - private string _httpMethod; - - public RequiresAuthenticationTests() - { - _config = new Mock(); - _routeRequiresAuthentication = new RouteRequiresAuthentication(_config.Object); - } - - [Fact] - public void should_return_true_if_route_requires_authentication() - { - this.Given(x => x.GivenIHaveADownstreamUrl("http://www.bbc.co.uk")) - .And( - x => - x.GivenTheConfigurationForTheRouteIs(new ReRoute("http://www.bbc.co.uk", "/api/poo", "get", - "/api/poo$", true))) - .When(x => x.WhenICheckToSeeIfTheRouteShouldBeAuthenticated()) - .Then(x => x.ThenTheResultIs(true)) - .BDDfy(); - } - - [Fact] - public void should_return_false_if_route_requires_authentication() - { - this.Given(x => x.GivenIHaveADownstreamUrl("http://www.bbc.co.uk")) - .And( - x => - x.GivenTheConfigurationForTheRouteIs(new ReRoute("http://www.bbc.co.uk", "/api/poo", "get", - "/api/poo$", false))) - .When(x => x.WhenICheckToSeeIfTheRouteShouldBeAuthenticated()) - .Then(x => x.ThenTheResultIs(false)) - .BDDfy(); - } - - [Fact] - public void should_return_error_if_no_matching_config() - { - this.Given(x => x.GivenIHaveADownstreamUrl("http://www.bbc.co.uk")) - .And(x => x.GivenTheConfigurationForTheRouteIs(new ReRoute(string.Empty, string.Empty, string.Empty, string.Empty,false))) - .When(x => x.WhenICheckToSeeIfTheRouteShouldBeAuthenticated()) - .Then(x => x.ThenAnErrorIsReturned()) - .BDDfy(); - } - - private void ThenAnErrorIsReturned() - { - _result.IsError.ShouldBeTrue(); - } - - public void GivenIHaveADownstreamUrl(string url) - { - _url = url; - } - - private void GivenTheConfigurationForTheRouteIs(ReRoute reRoute) - { - _httpMethod = reRoute.UpstreamHttpMethod; - - _config - .Setup(x => x.ReRoutes) - .Returns(new List - { - reRoute - }); - } - - private void WhenICheckToSeeIfTheRouteShouldBeAuthenticated() - { - _result = _routeRequiresAuthentication.IsAuthenticated(new DownstreamRoute(new List(), _url), _httpMethod); - } - - private void ThenTheResultIs(bool expected) - { - _result.Data.ShouldBe(expected); - } - } -} diff --git a/test/Ocelot.UnitTests/Configuration/OcelotConfigurationTests.cs b/test/Ocelot.UnitTests/Configuration/OcelotConfigurationTests.cs index 56ee89b8..2977f54f 100644 --- a/test/Ocelot.UnitTests/Configuration/OcelotConfigurationTests.cs +++ b/test/Ocelot.UnitTests/Configuration/OcelotConfigurationTests.cs @@ -38,7 +38,7 @@ namespace Ocelot.UnitTests.Configuration .When(x => x.WhenIInstanciateTheOcelotConfig()) .Then(x => x.ThenTheReRoutesAre(new List { - new ReRoute("/products/{productId}","/api/products/{productId}", "Get", "/api/products/.*$", false) + new ReRoute("/products/{productId}","/api/products/{productId}", "Get", "/api/products/.*$", false, "") })) .BDDfy(); } @@ -61,7 +61,7 @@ namespace Ocelot.UnitTests.Configuration .When(x => x.WhenIInstanciateTheOcelotConfig()) .Then(x => x.ThenTheReRoutesAre(new List { - new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}", "Get", "/api/products/.*/variants/.*$", false) + new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}", "Get", "/api/products/.*/variants/.*$", false, "") })) .BDDfy(); } @@ -84,7 +84,7 @@ namespace Ocelot.UnitTests.Configuration .When(x => x.WhenIInstanciateTheOcelotConfig()) .Then(x => x.ThenTheReRoutesAre(new List { - new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}/", "Get", "/api/products/.*/variants/.*/$", false) + new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}/", "Get", "/api/products/.*/variants/.*/$", false, "") })) .BDDfy(); } @@ -107,7 +107,7 @@ namespace Ocelot.UnitTests.Configuration .When(x => x.WhenIInstanciateTheOcelotConfig()) .Then(x => x.ThenTheReRoutesAre(new List { - new ReRoute("/api/products/","/", "Get", "/$", false) + new ReRoute("/api/products/","/", "Get", "/$", false, "") })) .BDDfy(); } diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs index 791a6aa2..127ec7d8 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs @@ -37,14 +37,14 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder .And(x => x.GivenTheTemplateVariableAndNameFinderReturns(new OkResponse>(new List()))) .And(x => x.GivenTheConfigurationIs(new List { - new ReRoute("someDownstreamPath","someUpstreamPath", "Get", "someUpstreamPath", false) + new ReRoute("someDownstreamPath","someUpstreamPath", "Get", "someUpstreamPath", false, "") } )) .And(x => x.GivenTheUrlMatcherReturns(new OkResponse(new UrlMatch(true)))) .And(x => x.GivenTheUpstreamHttpMethodIs("Get")) .When(x => x.WhenICallTheFinder()) .Then( - x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List(), "someDownstreamPath"))) + x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List(), new ReRoute("someDownstreamPath","","","",false, "")))) .And(x => x.ThenTheUrlMatcherIsCalledCorrectly()) .BDDfy(); } @@ -56,15 +56,15 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder .And(x => x.GivenTheTemplateVariableAndNameFinderReturns(new OkResponse>(new List()))) .And(x => x.GivenTheConfigurationIs(new List { - new ReRoute("someDownstreamPath", "someUpstreamPath", "Get", string.Empty, false), - new ReRoute("someDownstreamPathForAPost", "someUpstreamPath", "Post", string.Empty, false) + new ReRoute("someDownstreamPath", "someUpstreamPath", "Get", string.Empty, false, ""), + new ReRoute("someDownstreamPathForAPost", "someUpstreamPath", "Post", string.Empty, false, "") } )) .And(x => x.GivenTheUrlMatcherReturns(new OkResponse(new UrlMatch(true)))) .And(x => x.GivenTheUpstreamHttpMethodIs("Post")) .When(x => x.WhenICallTheFinder()) .Then( - x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List(), "someDownstreamPathForAPost"))) + x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List(), new ReRoute("someDownstreamPathForAPost", "","","",false, "")))) .BDDfy(); } @@ -74,7 +74,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder this.Given(x => x.GivenThereIsAnUpstreamUrlPath("somePath")) .And(x => x.GivenTheConfigurationIs(new List { - new ReRoute("somPath", "somePath", "Get", "somePath", false) + new ReRoute("somPath", "somePath", "Get", "somePath", false, "") } )) .And(x => x.GivenTheUrlMatcherReturns(new OkResponse(new UrlMatch(false)))) @@ -137,7 +137,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder private void ThenTheFollowingIsReturned(DownstreamRoute expected) { - _result.Data.DownstreamUrlTemplate.ShouldBe(expected.DownstreamUrlTemplate); + _result.Data.ReRoute.DownstreamTemplate.ShouldBe(expected.ReRoute.DownstreamTemplate); for (int i = 0; i < _result.Data.TemplateVariableNameAndValues.Count; i++) { diff --git a/test/Ocelot.UnitTests/Middleware/AuthenticationMiddlewareTests.cs b/test/Ocelot.UnitTests/Middleware/AuthenticationMiddlewareTests.cs index 4b1d2e07..f5cc37f5 100644 --- a/test/Ocelot.UnitTests/Middleware/AuthenticationMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Middleware/AuthenticationMiddlewareTests.cs @@ -6,7 +6,6 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using Moq; -using Ocelot.Library.Infrastructure.Authentication; using Ocelot.Library.Infrastructure.DownstreamRouteFinder; using Ocelot.Library.Infrastructure.Repository; using Ocelot.Library.Infrastructure.Responses; @@ -17,9 +16,10 @@ using Xunit; namespace Ocelot.UnitTests.Middleware { + using Library.Infrastructure.Configuration; + public class AuthenticationMiddlewareTests : IDisposable { - private readonly Mock _requiresAuth; private readonly Mock _scopedRepository; private readonly string _url; private readonly TestServer _server; @@ -30,13 +30,11 @@ namespace Ocelot.UnitTests.Middleware public AuthenticationMiddlewareTests() { _url = "http://localhost:51879"; - _requiresAuth = new Mock(); _scopedRepository = new Mock(); var builder = new WebHostBuilder() .ConfigureServices(x => { - x.AddSingleton(_requiresAuth.Object); x.AddSingleton(_scopedRepository.Object); }) .UseUrls(_url) @@ -56,8 +54,7 @@ namespace Ocelot.UnitTests.Middleware [Fact] public void happy_path() { - this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List(), "any old string"))) - .And(x => x.GivenTheRouteIsNotAuthenticated()) + this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List(), new ReRoute("","","","",false, "")))) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenNoExceptionsAreThrown()) .BDDfy(); @@ -76,13 +73,6 @@ namespace Ocelot.UnitTests.Middleware .Returns(_downstreamRoute); } - private void GivenTheRouteIsNotAuthenticated() - { - _requiresAuth - .Setup(x => x.IsAuthenticated(It.IsAny(), It.IsAny())) - .Returns(new OkResponse(false)); - } - private void WhenICallTheMiddleware() { _result = _client.GetAsync(_url).Result; diff --git a/test/Ocelot.UnitTests/Middleware/DownstreamRouteFinderMiddlewareTests.cs b/test/Ocelot.UnitTests/Middleware/DownstreamRouteFinderMiddlewareTests.cs index e3264653..b960e399 100644 --- a/test/Ocelot.UnitTests/Middleware/DownstreamRouteFinderMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Middleware/DownstreamRouteFinderMiddlewareTests.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Net.Http; + using Library.Infrastructure.Configuration; using Library.Infrastructure.DownstreamRouteFinder; using Library.Infrastructure.Repository; using Library.Infrastructure.Responses; @@ -55,7 +56,7 @@ [Fact] public void happy_path() { - this.Given(x => x.GivenTheDownStreamRouteFinderReturns(new DownstreamRoute(new List(), "any old string"))) + this.Given(x => x.GivenTheDownStreamRouteFinderReturns(new DownstreamRoute(new List(), new ReRoute("any old string", "", "", "",false, "")))) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly()) .BDDfy(); diff --git a/test/Ocelot.UnitTests/Middleware/DownstreamUrlCreatorMiddlewareTests.cs b/test/Ocelot.UnitTests/Middleware/DownstreamUrlCreatorMiddlewareTests.cs index b607cea7..df9260c8 100644 --- a/test/Ocelot.UnitTests/Middleware/DownstreamUrlCreatorMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Middleware/DownstreamUrlCreatorMiddlewareTests.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Net.Http; + using Library.Infrastructure.Configuration; using Library.Infrastructure.DownstreamRouteFinder; using Library.Infrastructure.Repository; using Library.Infrastructure.Responses; @@ -57,7 +58,7 @@ [Fact] public void happy_path() { - this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List(), "any old string"))) + this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List(), new ReRoute("any old string", "", "", "", false, "")))) .And(x => x.TheUrlReplacerReturns("any old string")) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly()) diff --git a/test/Ocelot.UnitTests/UrlTemplateReplacer/UpstreamUrlPathTemplateVariableReplacerTests.cs b/test/Ocelot.UnitTests/UrlTemplateReplacer/UpstreamUrlPathTemplateVariableReplacerTests.cs index 597c7d74..18bf22d8 100644 --- a/test/Ocelot.UnitTests/UrlTemplateReplacer/UpstreamUrlPathTemplateVariableReplacerTests.cs +++ b/test/Ocelot.UnitTests/UrlTemplateReplacer/UpstreamUrlPathTemplateVariableReplacerTests.cs @@ -9,6 +9,8 @@ using Xunit; namespace Ocelot.UnitTests.UrlTemplateReplacer { + using Library.Infrastructure.Configuration; + public class UpstreamUrlPathTemplateVariableReplacerTests { private DownstreamRoute _downstreamRoute; @@ -23,7 +25,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer [Fact] public void can_replace_no_template_variables() { - this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List(), ""))) + this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List(), new ReRoute("", "", "", "", false, "")))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("")) .BDDfy(); @@ -32,7 +34,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer [Fact] public void can_replace_no_template_variables_with_slash() { - this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List(), "/"))) + this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List(), new ReRoute("/", "", "", "", false, "")))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("/")) .BDDfy(); @@ -41,7 +43,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer [Fact] public void can_replace_url_no_slash() { - this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List(), "api"))) + this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List(), new ReRoute("api", "", "", "", false, "")))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("api")) .BDDfy(); @@ -50,7 +52,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer [Fact] public void can_replace_url_one_slash() { - this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List(), "api/"))) + this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List(), new ReRoute("api/", "", "", "", false, "")))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/")) .BDDfy(); @@ -59,7 +61,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer [Fact] public void can_replace_url_multiple_slash() { - this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List(), "api/product/products/"))) + this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List(), new ReRoute("api/product/products/", "", "", "", false, "")))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/product/products/")) .BDDfy(); @@ -73,7 +75,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer new TemplateVariableNameAndValue("{productId}", "1") }; - this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, "productservice/products/{productId}/"))) + this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, new ReRoute("productservice/products/{productId}/", "", "", "", false, "")))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/")) .BDDfy(); @@ -87,7 +89,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer new TemplateVariableNameAndValue("{productId}", "1") }; - this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, "productservice/products/{productId}/variants"))) + this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, new ReRoute("productservice/products/{productId}/variants", "", "", "", false, "")))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants")) .BDDfy(); @@ -102,7 +104,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer new TemplateVariableNameAndValue("{variantId}", "12") }; - this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, "productservice/products/{productId}/variants/{variantId}"))) + this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, new ReRoute("productservice/products/{productId}/variants/{variantId}", "", "", "", false, "")))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants/12")) .BDDfy(); @@ -118,7 +120,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer new TemplateVariableNameAndValue("{categoryId}", "34") }; - this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, "productservice/category/{categoryId}/products/{productId}/variants/{variantId}"))) + this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, new ReRoute("productservice/category/{categoryId}/products/{productId}/variants/{variantId}", "", "", "", false, "")))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/category/34/products/1/variants/12")) .BDDfy(); @@ -138,6 +140,5 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer { _result.Data.ShouldBe(expected); } - } }