diff --git a/src/Ocelot.Library/Infrastructure/Configuration/Configuration.cs b/src/Ocelot.Library/Infrastructure/Configuration/Configuration.cs index 1a118086..f1cc9b90 100644 --- a/src/Ocelot.Library/Infrastructure/Configuration/Configuration.cs +++ b/src/Ocelot.Library/Infrastructure/Configuration/Configuration.cs @@ -6,9 +6,9 @@ namespace Ocelot.Library.Infrastructure.Configuration { public Configuration() { - Routes = new List(); + ReRoutes = new List(); } - public List Routes { get; set; } + public List ReRoutes { get; set; } } } diff --git a/src/Ocelot.Library/Infrastructure/Configuration/ReRoute.cs b/src/Ocelot.Library/Infrastructure/Configuration/ReRoute.cs new file mode 100644 index 00000000..4c27ec37 --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/Configuration/ReRoute.cs @@ -0,0 +1,8 @@ +namespace Ocelot.Library.Infrastructure.Configuration +{ + public class ReRoute + { + public string DownstreamTemplate { get; set; } + public string UpstreamTemplate { get; set; } + } +} diff --git a/src/Ocelot.Library/Infrastructure/Configuration/Route.cs b/src/Ocelot.Library/Infrastructure/Configuration/Route.cs deleted file mode 100644 index a30d3bbf..00000000 --- a/src/Ocelot.Library/Infrastructure/Configuration/Route.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Ocelot.Library.Infrastructure.Configuration -{ - public class Route - { - public string Downstream { get; set; } - public string Upstream { get; set; } - } -} diff --git a/src/Ocelot.Library/Infrastructure/UrlMatcher/IUrlPathToUrlTemplateMatcher.cs b/src/Ocelot.Library/Infrastructure/UrlMatcher/IUrlPathToUrlTemplateMatcher.cs index 6f96a28c..954e3fee 100644 --- a/src/Ocelot.Library/Infrastructure/UrlMatcher/IUrlPathToUrlTemplateMatcher.cs +++ b/src/Ocelot.Library/Infrastructure/UrlMatcher/IUrlPathToUrlTemplateMatcher.cs @@ -2,6 +2,6 @@ namespace Ocelot.Library.Infrastructure.UrlMatcher { public interface IUrlPathToUrlTemplateMatcher { - UrlMatch Match(string downstreamUrlPath, string downstreamUrlTemplate); + UrlMatch Match(string upstreamUrlPath, string upstreamUrlTemplate); } } \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlMatcher/UrlPathToUrlTemplateMatcher.cs b/src/Ocelot.Library/Infrastructure/UrlMatcher/UrlPathToUrlTemplateMatcher.cs index fc34153e..0c396e32 100644 --- a/src/Ocelot.Library/Infrastructure/UrlMatcher/UrlPathToUrlTemplateMatcher.cs +++ b/src/Ocelot.Library/Infrastructure/UrlMatcher/UrlPathToUrlTemplateMatcher.cs @@ -5,31 +5,31 @@ namespace Ocelot.Library.Infrastructure.UrlMatcher { public class UrlPathToUrlTemplateMatcher : IUrlPathToUrlTemplateMatcher { - public UrlMatch Match(string downstreamUrlPath, string downstreamUrlTemplate) + public UrlMatch Match(string upstreamUrlPath, string upstreamUrlTemplate) { - var urlPathTemplateCopy = downstreamUrlTemplate; + var urlPathTemplateCopy = upstreamUrlTemplate; var templateKeysAndValues = new List(); int counterForUrl = 0; - for (int counterForTemplate = 0; counterForTemplate < downstreamUrlTemplate.Length; counterForTemplate++) + for (int counterForTemplate = 0; counterForTemplate < upstreamUrlTemplate.Length; counterForTemplate++) { - if (CharactersDontMatch(downstreamUrlTemplate[counterForTemplate], downstreamUrlPath[counterForUrl]) && ContinueScanningUrl(counterForUrl,downstreamUrlPath.Length)) + if (CharactersDontMatch(upstreamUrlTemplate[counterForTemplate], upstreamUrlPath[counterForUrl]) && ContinueScanningUrl(counterForUrl,upstreamUrlPath.Length)) { - if (IsPlaceholder(downstreamUrlTemplate[counterForTemplate])) + if (IsPlaceholder(upstreamUrlTemplate[counterForTemplate])) { - var variableName = GetPlaceholderVariableName(downstreamUrlTemplate, counterForTemplate); + var variableName = GetPlaceholderVariableName(upstreamUrlTemplate, counterForTemplate); - var variableValue = GetPlaceholderVariableValue(downstreamUrlPath, counterForUrl); + var variableValue = GetPlaceholderVariableValue(upstreamUrlPath, counterForUrl); var templateVariableNameAndValue = new TemplateVariableNameAndValue(variableName, variableValue); templateKeysAndValues.Add(templateVariableNameAndValue); - counterForTemplate = GetNextCounterPosition(downstreamUrlTemplate, counterForTemplate, '}'); + counterForTemplate = GetNextCounterPosition(upstreamUrlTemplate, counterForTemplate, '}'); - counterForUrl = GetNextCounterPosition(downstreamUrlPath, counterForUrl, '/'); + counterForUrl = GetNextCounterPosition(upstreamUrlPath, counterForUrl, '/'); continue; } diff --git a/src/Ocelot.Library/Infrastructure/UrlTemplateReplacer/DownstreamUrlTemplateVariableReplacer.cs b/src/Ocelot.Library/Infrastructure/UrlTemplateReplacer/DownstreamUrlTemplateVariableReplacer.cs index 14e99be9..35dc563c 100644 --- a/src/Ocelot.Library/Infrastructure/UrlTemplateReplacer/DownstreamUrlTemplateVariableReplacer.cs +++ b/src/Ocelot.Library/Infrastructure/UrlTemplateReplacer/DownstreamUrlTemplateVariableReplacer.cs @@ -5,11 +5,11 @@ namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer { public class DownstreamUrlTemplateVariableReplacer : IDownstreamUrlTemplateVariableReplacer { - public string ReplaceTemplateVariable(string downstreamPathTemplate, UrlMatch urlMatch) + public string ReplaceTemplateVariable(UrlMatch urlMatch) { var upstreamUrl = new StringBuilder(); - upstreamUrl.Append(downstreamPathTemplate); + upstreamUrl.Append(urlMatch.DownstreamUrlTemplate); foreach (var templateVarAndValue in urlMatch.TemplateVariableNameAndValues) { diff --git a/src/Ocelot.Library/Infrastructure/UrlTemplateReplacer/IDownstreamUrlPathTemplateVariableReplacer.cs b/src/Ocelot.Library/Infrastructure/UrlTemplateReplacer/IDownstreamUrlPathTemplateVariableReplacer.cs index 53b532f4..62eac8bc 100644 --- a/src/Ocelot.Library/Infrastructure/UrlTemplateReplacer/IDownstreamUrlPathTemplateVariableReplacer.cs +++ b/src/Ocelot.Library/Infrastructure/UrlTemplateReplacer/IDownstreamUrlPathTemplateVariableReplacer.cs @@ -4,6 +4,6 @@ namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer { public interface IDownstreamUrlTemplateVariableReplacer { - string ReplaceTemplateVariable(string downstreamPathTemplate, UrlMatch urlMatch); + string ReplaceTemplateVariable(UrlMatch urlMatch); } } \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/DownstreamUrlAlreadyExists.cs b/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/DownstreamUrlAlreadyExists.cs deleted file mode 100644 index cf471ba3..00000000 --- a/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/DownstreamUrlAlreadyExists.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.UrlTemplateRepository -{ - public class DownstreamUrlTemplateAlreadyExists : Error - { - public DownstreamUrlTemplateAlreadyExists() - : base("This key has already been used") - { - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/DownstreamUrlDoesNotExist.cs b/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/DownstreamUrlDoesNotExist.cs deleted file mode 100644 index ba960046..00000000 --- a/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/DownstreamUrlDoesNotExist.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.UrlTemplateRepository -{ - public class DownstreamUrlTemplateDoesNotExist : Error - { - public DownstreamUrlTemplateDoesNotExist() - : base("This key does not exist") - { - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/IUrlTemplateMapRepository.cs b/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/IUrlTemplateMapRepository.cs deleted file mode 100644 index d4be69ae..00000000 --- a/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/IUrlTemplateMapRepository.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Collections.Generic; -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.UrlTemplateRepository -{ - public interface IUrlTemplateMapRepository - { - Response AddUrlTemplateMap(UrlTemplateMap urlPathMap); - Response> All { get; } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/InMemoryUrlTemplateMapRepository.cs b/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/InMemoryUrlTemplateMapRepository.cs deleted file mode 100644 index 0129b4d6..00000000 --- a/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/InMemoryUrlTemplateMapRepository.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.UrlTemplateRepository -{ - public class InMemoryUrlTemplateMapRepository : IUrlTemplateMapRepository - { - private readonly Dictionary _urlTemplates; - public InMemoryUrlTemplateMapRepository() - { - _urlTemplates = new Dictionary(); - } - - public Response> All - { - get - { - var routes = _urlTemplates - .Select(r => new UrlTemplateMap(r.Key, r.Value)) - .ToList(); - return new OkResponse>(routes); - } - } - - public Response AddUrlTemplateMap(UrlTemplateMap urlMap) - { - if(_urlTemplates.ContainsKey(urlMap.DownstreamUrlTemplate)) - { - return new ErrorResponse(new List(){new DownstreamUrlTemplateAlreadyExists()}); - } - - _urlTemplates.Add(urlMap.DownstreamUrlTemplate, urlMap.UpstreamUrlPathTemplate); - - return new OkResponse(); - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/UrlTemplateMap.cs b/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/UrlTemplateMap.cs deleted file mode 100644 index 198917a9..00000000 --- a/src/Ocelot.Library/Infrastructure/UrlTemplateRepository/UrlTemplateMap.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Ocelot.Library.Infrastructure.UrlTemplateRepository -{ - public class UrlTemplateMap - { - public UrlTemplateMap(string downstreamUrlTemplate, string upstreamUrlPathTemplate) - { - DownstreamUrlTemplate = downstreamUrlTemplate; - UpstreamUrlPathTemplate = upstreamUrlPathTemplate; - } - - public string DownstreamUrlTemplate {get;private set;} - public string UpstreamUrlPathTemplate {get;private set;} - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Middleware/ProxyMiddleware.cs b/src/Ocelot.Library/Middleware/ProxyMiddleware.cs index 0acf6c25..e723392e 100644 --- a/src/Ocelot.Library/Middleware/ProxyMiddleware.cs +++ b/src/Ocelot.Library/Middleware/ProxyMiddleware.cs @@ -1,7 +1,6 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Ocelot.Library.Infrastructure.UrlMatcher; -using Ocelot.Library.Infrastructure.UrlTemplateRepository; using Ocelot.Library.Infrastructure.UrlTemplateReplacer; namespace Ocelot.Library.Middleware @@ -14,40 +13,31 @@ namespace Ocelot.Library.Middleware { private readonly RequestDelegate _next; private readonly IUrlPathToUrlTemplateMatcher _urlMatcher; - private readonly IUrlTemplateMapRepository _urlTemplateMapRepository; private readonly IDownstreamUrlTemplateVariableReplacer _urlReplacer; - private readonly IOptions _optionsAccessor; + private readonly IOptions _configuration; public ProxyMiddleware(RequestDelegate next, IUrlPathToUrlTemplateMatcher urlMatcher, - IUrlTemplateMapRepository urlPathRepository, - IDownstreamUrlTemplateVariableReplacer urlReplacer, IOptions optionsAccessor) + IDownstreamUrlTemplateVariableReplacer urlReplacer, IOptions configuration) { _next = next; _urlMatcher = urlMatcher; - _urlTemplateMapRepository = urlPathRepository; _urlReplacer = urlReplacer; - _optionsAccessor = optionsAccessor; + _configuration = configuration; } public async Task Invoke(HttpContext context) - { - - var downstreamUrlPath = context.Request.Path.ToString(); - - var upstreamUrlTemplates = _urlTemplateMapRepository.All; + { + var upstreamUrlPath = context.Request.Path.ToString(); UrlMatch urlMatch = null; - string downstreamUrlTemplate = string.Empty; - - foreach (var template in upstreamUrlTemplates.Data) + foreach (var template in _configuration.Value.ReRoutes) { - urlMatch = _urlMatcher.Match(downstreamUrlPath, template.DownstreamUrlTemplate); + urlMatch = _urlMatcher.Match(upstreamUrlPath, template.UpstreamTemplate); if (urlMatch.Match) { - downstreamUrlTemplate = template.DownstreamUrlTemplate; break; } } @@ -58,7 +48,7 @@ namespace Ocelot.Library.Middleware return; } - var downstreamUrl = _urlReplacer.ReplaceTemplateVariable(downstreamUrlTemplate, urlMatch); + var downstreamUrl = _urlReplacer.ReplaceTemplateVariable(urlMatch); //make a http request to this endpoint...maybe bring in a library diff --git a/src/Ocelot/Startup.cs b/src/Ocelot/Startup.cs index 93346a40..d9400980 100644 --- a/src/Ocelot/Startup.cs +++ b/src/Ocelot/Startup.cs @@ -10,7 +10,6 @@ namespace Ocelot using Library.Infrastructure.Configuration; using Library.Infrastructure.UrlMatcher; using Library.Infrastructure.UrlTemplateReplacer; - using Library.Infrastructure.UrlTemplateRepository; public class Startup { @@ -37,7 +36,6 @@ namespace Ocelot // Add framework services. services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/test/Ocelot.AcceptanceTests/OcelotTests.cs b/test/Ocelot.AcceptanceTests/OcelotTests.cs index 6989ce99..6f270e2c 100644 --- a/test/Ocelot.AcceptanceTests/OcelotTests.cs +++ b/test/Ocelot.AcceptanceTests/OcelotTests.cs @@ -41,12 +41,12 @@ namespace Ocelot.AcceptanceTests this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879")) .And(x => x.GivenThereIsAConfiguration(new Configuration { - Routes = new List + ReRoutes = new List { - new Route + new ReRoute { - Downstream = "http://localhost:51879/", - Upstream = "/heee" + DownstreamTemplate = "http://localhost:51879/", + UpstreamTemplate = "/" } } })) diff --git a/test/Ocelot.AcceptanceTests/configuration.yaml b/test/Ocelot.AcceptanceTests/configuration.yaml index 2e47e77d..97b35e41 100644 --- a/test/Ocelot.AcceptanceTests/configuration.yaml +++ b/test/Ocelot.AcceptanceTests/configuration.yaml @@ -1,3 +1,3 @@ -Routes: -- Downstream: http://localhost:51879/ - Upstream: /heee +ReRoutes: +- DownstreamTemplate: http://localhost:51879/ + UpstreamTemplate: / diff --git a/test/Ocelot.UnitTests/UpstreamUrlPathTemplateVariableReplacerTests.cs b/test/Ocelot.UnitTests/UpstreamUrlPathTemplateVariableReplacerTests.cs index aef6e6c6..6b55c013 100644 --- a/test/Ocelot.UnitTests/UpstreamUrlPathTemplateVariableReplacerTests.cs +++ b/test/Ocelot.UnitTests/UpstreamUrlPathTemplateVariableReplacerTests.cs @@ -10,7 +10,6 @@ namespace Ocelot.UnitTests public class UpstreamUrlPathTemplateVariableReplacerTests { - private string _downstreamUrlTemplate; private UrlMatch _urlMatch; private string _result; private readonly IDownstreamUrlTemplateVariableReplacer _downstreamUrlPathReplacer; @@ -23,18 +22,25 @@ namespace Ocelot.UnitTests [Fact] public void can_replace_no_template_variables() { - this.Given(x => x.GivenThereIsADownstreamUrl("")) - .And(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List(), ""))) + this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List(), ""))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("")) .BDDfy(); } + [Fact] + public void can_replace_no_template_variables_with_slash() + { + this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List(), "/"))) + .When(x => x.WhenIReplaceTheTemplateVariables()) + .Then(x => x.ThenTheDownstreamUrlPathIsReturned("/")) + .BDDfy(); + } + [Fact] public void can_replace_url_no_slash() { - this.Given(x => x.GivenThereIsADownstreamUrl("api")) - .And(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List(), "api"))) + this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List(), "api"))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("api")) .BDDfy(); @@ -43,8 +49,7 @@ namespace Ocelot.UnitTests [Fact] public void can_replace_url_one_slash() { - this.Given(x => x.GivenThereIsADownstreamUrl("api/")) - .And(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List(), "api/"))) + this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List(), "api/"))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/")) .BDDfy(); @@ -53,8 +58,7 @@ namespace Ocelot.UnitTests [Fact] public void can_replace_url_multiple_slash() { - this.Given(x => x.GivenThereIsADownstreamUrl("api/product/products/")) - .And(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List(), "api/product/products/"))) + this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, new List(), "api/product/products/"))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/product/products/")) .BDDfy(); @@ -68,8 +72,7 @@ namespace Ocelot.UnitTests new TemplateVariableNameAndValue("{productId}", "1") }; - this.Given(x => x.GivenThereIsADownstreamUrl("productservice/products/{productId}/")) - .And(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, templateVariables, "api/products/{productId}/"))) + this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, templateVariables, "productservice/products/{productId}/"))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/")) .BDDfy(); @@ -83,8 +86,7 @@ namespace Ocelot.UnitTests new TemplateVariableNameAndValue("{productId}", "1") }; - this.Given(x => x.GivenThereIsADownstreamUrl("productservice/products/{productId}/variants")) - .And(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, templateVariables, "api/products/{productId}/"))) + this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, templateVariables, "productservice/products/{productId}/variants"))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants")) .BDDfy(); @@ -99,8 +101,7 @@ namespace Ocelot.UnitTests new TemplateVariableNameAndValue("{variantId}", "12") }; - this.Given(x => x.GivenThereIsADownstreamUrl("productservice/products/{productId}/variants/{variantId}")) - .And(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, templateVariables, "api/products/{productId}/{variantId}"))) + this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, templateVariables, "productservice/products/{productId}/variants/{variantId}"))) .When(x => x.WhenIReplaceTheTemplateVariables()) .Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants/12")) .BDDfy(); @@ -116,18 +117,12 @@ namespace Ocelot.UnitTests new TemplateVariableNameAndValue("{categoryId}", "34") }; - this.Given(x => x.GivenThereIsADownstreamUrl("productservice/category/{categoryId}/products/{productId}/variants/{variantId}")) - .And(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, templateVariables, "api/products/{categoryId}/{productId}/{variantId}"))) + this.Given(x => x.GivenThereIsAUrlMatch(new UrlMatch(true, 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 GivenThereIsADownstreamUrl(string downstreamUrlTemplate) - { - _downstreamUrlTemplate = downstreamUrlTemplate; - } - private void GivenThereIsAUrlMatch(UrlMatch urlMatch) { _urlMatch = urlMatch; @@ -135,7 +130,7 @@ namespace Ocelot.UnitTests private void WhenIReplaceTheTemplateVariables() { - _result = _downstreamUrlPathReplacer.ReplaceTemplateVariable(_downstreamUrlTemplate, _urlMatch); + _result = _downstreamUrlPathReplacer.ReplaceTemplateVariable(_urlMatch); } private void ThenTheDownstreamUrlPathIsReturned(string expected) diff --git a/test/Ocelot.UnitTests/UrlPathTemplateMapRepositoryTests.cs b/test/Ocelot.UnitTests/UrlPathTemplateMapRepositoryTests.cs deleted file mode 100644 index 19811718..00000000 --- a/test/Ocelot.UnitTests/UrlPathTemplateMapRepositoryTests.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System.Collections.Generic; -using Ocelot.Library.Infrastructure.Responses; -using Ocelot.Library.Infrastructure.UrlTemplateRepository; -using Shouldly; -using Xunit; - -namespace Ocelot.UnitTests -{ - using TestStack.BDDfy; - - public class UrlPathTemplateMapRepositoryTests - { - private string _upstreamUrlPathTemplate; - private string _downstreamUrlTemplate; - private IUrlTemplateMapRepository _repository; - private Response _response; - private Response> _listResponse; - - public UrlPathTemplateMapRepositoryTests() - { - _repository = new InMemoryUrlTemplateMapRepository(); - } - - [Fact] - public void can_add_url_path() - { - this.Given(x => x.GivenIHaveAnUpstreamUrlPathTemplate("/api/products/products/{productId}")) - .And(x => x.GivenADownstreamUrlTemplate("/api/products/{productId}")) - .When(x => x.WhenIAddTheConfiguration()) - .Then(x => x.ThenTheResponseIsSuccesful()) - .BDDfy(); - } - - [Fact] - public void can_get_all_urls() - { - this.Given(x => x.GivenIHaveSetUpADownstreamUrlTemplateAndAnUpstreamUrlPathTemplate("/api2", "http://www.someapi.com/api2")) - .When(x => x.WhenIRetrieveTheUrls()) - .Then(x => x.ThenTheUrlsAreReturned()) - .BDDfy(); - } - - [Fact] - public void should_return_error_response_when_url_path_already_used() - { - this.Given(x => x.GivenIHaveSetUpADownstreamUrlTemplateAndAnUpstreamUrlPathTemplate("/api2", "http://www.someapi.com/api2")) - .When(x => x.WhenITryToUseTheSameDownstreamUrl()) - .Then(x => x.ThenTheDownstreamUrlAlreadyBeenUsed()) - .BDDfy(); - } - - private void WhenITryToUseTheSameDownstreamUrl() - { - WhenIAddTheConfiguration(); - } - - private void ThenTheDownstreamUrlAlreadyBeenUsed() - { - _response.ShouldNotBeNull(); - _response.ShouldBeOfType(); - _response.Errors[0].Message.ShouldBe("This key has already been used"); - } - - private void WhenIRetrieveTheUrls() - { - _listResponse = _repository.All; - } - - private void ThenTheUrlsAreReturned() - { - _listResponse.Data.Count.ShouldBeGreaterThan(0); - } - - private void GivenIHaveSetUpADownstreamUrlTemplateAndAnUpstreamUrlPathTemplate(string downstreamUrlTemplate, string upstreamUrlPathTemplate) - { - GivenIHaveAnUpstreamUrlPathTemplate(upstreamUrlPathTemplate); - GivenADownstreamUrlTemplate(downstreamUrlTemplate); - WhenIAddTheConfiguration(); - } - - private void GivenIHaveAnUpstreamUrlPathTemplate(string upstreamUrlPathTemplate) - { - _upstreamUrlPathTemplate = upstreamUrlPathTemplate; - } - - private void GivenADownstreamUrlTemplate(string downstreamUrlTemplate) - { - _downstreamUrlTemplate = downstreamUrlTemplate; - } - - private void WhenIAddTheConfiguration() - { - _response = _repository.AddUrlTemplateMap(new UrlTemplateMap(_downstreamUrlTemplate, _upstreamUrlPathTemplate)); - } - - private void ThenTheResponseIsSuccesful() - { - _response.ShouldBeOfType(); - } - } -} \ No newline at end of file