diff --git a/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMap.cs b/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMap.cs deleted file mode 100644 index 564bb26e..00000000 --- a/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMap.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Ocelot.Library.Infrastructure.BaseUrlRepository -{ - public class BaseUrlMap - { - public BaseUrlMap(string downstreamBaseUrl, string upstreamBaseUrl) - { - DownstreamBaseUrl = downstreamBaseUrl; - UpstreamBaseUrl = upstreamBaseUrl; - } - - public string DownstreamBaseUrl {get;private set;} - public string UpstreamBaseUrl {get;private set;} - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMapKeyAlreadyExists.cs b/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMapKeyAlreadyExists.cs deleted file mode 100644 index 91b4ae4a..00000000 --- a/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMapKeyAlreadyExists.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.BaseUrlRepository -{ - public class BaseUrlMapKeyAlreadyExists : Error - { - public BaseUrlMapKeyAlreadyExists() - : base("This key has already been used") - { - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMapKeyDoesNotExist.cs b/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMapKeyDoesNotExist.cs deleted file mode 100644 index d8367abf..00000000 --- a/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMapKeyDoesNotExist.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.BaseUrlRepository -{ - public class BaseUrlMapKeyDoesNotExist : Error - { - public BaseUrlMapKeyDoesNotExist() - : base("This key does not exist") - { - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/BaseUrlRepository/IBaseUrlMapRepository.cs b/src/Ocelot.Library/Infrastructure/BaseUrlRepository/IBaseUrlMapRepository.cs deleted file mode 100644 index 2e76444d..00000000 --- a/src/Ocelot.Library/Infrastructure/BaseUrlRepository/IBaseUrlMapRepository.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.BaseUrlRepository -{ - public interface IBaseUrlMapRepository - { - Response AddBaseUrlMap(BaseUrlMap baseUrlMap); - Response GetBaseUrlMap(string downstreamUrl); - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/BaseUrlRepository/InMemoryBaseUrlMapRepository.cs b/src/Ocelot.Library/Infrastructure/BaseUrlRepository/InMemoryBaseUrlMapRepository.cs deleted file mode 100644 index d43fd23c..00000000 --- a/src/Ocelot.Library/Infrastructure/BaseUrlRepository/InMemoryBaseUrlMapRepository.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System.Collections.Generic; -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.BaseUrlRepository -{ - public class InMemoryBaseUrlMapRepository : IBaseUrlMapRepository - { - private readonly Dictionary _routes; - public InMemoryBaseUrlMapRepository() - { - _routes = new Dictionary(); - } - public Response AddBaseUrlMap(BaseUrlMap baseUrlMap) - { - if(_routes.ContainsKey(baseUrlMap.DownstreamBaseUrl)) - { - return new ErrorResponse(new List(){new BaseUrlMapKeyAlreadyExists()}); - } - - _routes.Add(baseUrlMap.DownstreamBaseUrl, baseUrlMap.UpstreamBaseUrl); - - return new OkResponse(); - } - - public Response GetBaseUrlMap(string downstreamUrl) - { - string upstreamUrl = null; - - if(_routes.TryGetValue(downstreamUrl, out upstreamUrl)) - { - return new OkResponse(new BaseUrlMap(downstreamUrl, upstreamUrl)); - } - - return new ErrorResponse(new List(){new BaseUrlMapKeyDoesNotExist()}); - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/HostUrlRepository/HostUrlMap.cs b/src/Ocelot.Library/Infrastructure/HostUrlRepository/HostUrlMap.cs new file mode 100644 index 00000000..1a13ee65 --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/HostUrlRepository/HostUrlMap.cs @@ -0,0 +1,14 @@ +namespace Ocelot.Library.Infrastructure.HostUrlRepository +{ + public class HostUrlMap + { + public HostUrlMap(string downstreamHostUrl, string upstreamHostUrl) + { + DownstreamHostUrl = downstreamHostUrl; + UpstreamHostUrl = upstreamHostUrl; + } + + public string DownstreamHostUrl {get;private set;} + public string UpstreamHostUrl {get;private set;} + } +} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/HostUrlRepository/HostUrlMapKeyAlreadyExists.cs b/src/Ocelot.Library/Infrastructure/HostUrlRepository/HostUrlMapKeyAlreadyExists.cs new file mode 100644 index 00000000..236cac79 --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/HostUrlRepository/HostUrlMapKeyAlreadyExists.cs @@ -0,0 +1,12 @@ +using Ocelot.Library.Infrastructure.Responses; + +namespace Ocelot.Library.Infrastructure.HostUrlRepository +{ + public class HostUrlMapKeyAlreadyExists : Error + { + public HostUrlMapKeyAlreadyExists() + : base("This key has already been used") + { + } + } +} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/HostUrlRepository/HostUrlMapKeyDoesNotExist.cs b/src/Ocelot.Library/Infrastructure/HostUrlRepository/HostUrlMapKeyDoesNotExist.cs new file mode 100644 index 00000000..913f2b67 --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/HostUrlRepository/HostUrlMapKeyDoesNotExist.cs @@ -0,0 +1,12 @@ +using Ocelot.Library.Infrastructure.Responses; + +namespace Ocelot.Library.Infrastructure.HostUrlRepository +{ + public class HostUrlMapKeyDoesNotExist : Error + { + public HostUrlMapKeyDoesNotExist() + : base("This key does not exist") + { + } + } +} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/HostUrlRepository/IBaseUrlMapRepository.cs b/src/Ocelot.Library/Infrastructure/HostUrlRepository/IBaseUrlMapRepository.cs new file mode 100644 index 00000000..50a7e248 --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/HostUrlRepository/IBaseUrlMapRepository.cs @@ -0,0 +1,10 @@ +using Ocelot.Library.Infrastructure.Responses; + +namespace Ocelot.Library.Infrastructure.HostUrlRepository +{ + public interface IHostUrlMapRepository + { + Response AddBaseUrlMap(HostUrlMap baseUrlMap); + Response GetBaseUrlMap(string downstreamUrl); + } +} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/HostUrlRepository/InMemoryBaseUrlMapRepository.cs b/src/Ocelot.Library/Infrastructure/HostUrlRepository/InMemoryBaseUrlMapRepository.cs new file mode 100644 index 00000000..acb07542 --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/HostUrlRepository/InMemoryBaseUrlMapRepository.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; +using Ocelot.Library.Infrastructure.Responses; + +namespace Ocelot.Library.Infrastructure.HostUrlRepository +{ + public class InMemoryHostUrlMapRepository : IHostUrlMapRepository + { + private readonly Dictionary _routes; + public InMemoryHostUrlMapRepository() + { + _routes = new Dictionary(); + } + public Response AddBaseUrlMap(HostUrlMap baseUrlMap) + { + if(_routes.ContainsKey(baseUrlMap.DownstreamHostUrl)) + { + return new ErrorResponse(new List(){new HostUrlMapKeyAlreadyExists()}); + } + + _routes.Add(baseUrlMap.DownstreamHostUrl, baseUrlMap.UpstreamHostUrl); + + return new OkResponse(); + } + + public Response GetBaseUrlMap(string downstreamUrl) + { + string upstreamUrl = null; + + if(_routes.TryGetValue(downstreamUrl, out upstreamUrl)) + { + return new OkResponse(new HostUrlMap(downstreamUrl, upstreamUrl)); + } + + return new ErrorResponse(new List(){new HostUrlMapKeyDoesNotExist()}); + } + } +} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/Responses/Response.cs b/src/Ocelot.Library/Infrastructure/Responses/Response.cs index 6651c0f7..ead3141c 100644 --- a/src/Ocelot.Library/Infrastructure/Responses/Response.cs +++ b/src/Ocelot.Library/Infrastructure/Responses/Response.cs @@ -15,5 +15,13 @@ namespace Ocelot.Library.Infrastructure.Responses } public List Errors { get; private set; } + + public bool IsError + { + get + { + return Errors.Count > 0; + } + } } } \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/Responses/ResponseGeneric.cs b/src/Ocelot.Library/Infrastructure/Responses/ResponseGeneric.cs index 97e72e10..ed020292 100644 --- a/src/Ocelot.Library/Infrastructure/Responses/ResponseGeneric.cs +++ b/src/Ocelot.Library/Infrastructure/Responses/ResponseGeneric.cs @@ -14,5 +14,6 @@ namespace Ocelot.Library.Infrastructure.Responses } public T Data { get; private set; } + } } \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlFinder/IUpstreamBaseUrlFinder.cs b/src/Ocelot.Library/Infrastructure/UrlFinder/IUpstreamBaseUrlFinder.cs deleted file mode 100644 index c4623939..00000000 --- a/src/Ocelot.Library/Infrastructure/UrlFinder/IUpstreamBaseUrlFinder.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.UrlFinder -{ - public interface IUpstreamBaseUrlFinder - { - Response FindUpstreamBaseUrl(string downstreamBaseUrl); - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlFinder/IUpstreamHostUrlFinder.cs b/src/Ocelot.Library/Infrastructure/UrlFinder/IUpstreamHostUrlFinder.cs new file mode 100644 index 00000000..84a78493 --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/UrlFinder/IUpstreamHostUrlFinder.cs @@ -0,0 +1,9 @@ +using Ocelot.Library.Infrastructure.Responses; + +namespace Ocelot.Library.Infrastructure.UrlFinder +{ + public interface IUpstreamHostUrlFinder + { + Response FindUpstreamHostUrl(string downstreamHostUrl); + } +} diff --git a/src/Ocelot.Library/Infrastructure/UrlFinder/UnableToFindUpstreamHostUrl.cs b/src/Ocelot.Library/Infrastructure/UrlFinder/UnableToFindUpstreamHostUrl.cs new file mode 100644 index 00000000..10defcee --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/UrlFinder/UnableToFindUpstreamHostUrl.cs @@ -0,0 +1,12 @@ +using Ocelot.Library.Infrastructure.Responses; + +namespace Ocelot.Library.Infrastructure.UrlFinder +{ + public class UnableToFindUpstreamHostUrl : Error + { + public UnableToFindUpstreamHostUrl() + : base("Unable to find upstream base url") + { + } + } +} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlFinder/UpstreamBaseUrlFinder.cs b/src/Ocelot.Library/Infrastructure/UrlFinder/UpstreamBaseUrlFinder.cs deleted file mode 100644 index 135c8fcc..00000000 --- a/src/Ocelot.Library/Infrastructure/UrlFinder/UpstreamBaseUrlFinder.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using Ocelot.Library.Infrastructure.BaseUrlRepository; -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.UrlFinder -{ - public class UpstreamBaseUrlFinder : IUpstreamBaseUrlFinder - { - private readonly IBaseUrlMapRepository _baseUrlMapRepository; - - public UpstreamBaseUrlFinder(IBaseUrlMapRepository baseUrlMapRepository) - { - _baseUrlMapRepository = baseUrlMapRepository; - } - public Response FindUpstreamBaseUrl(string downstreamBaseUrl) - { - var baseUrl = _baseUrlMapRepository.GetBaseUrlMap(downstreamBaseUrl); - - return new OkResponse(baseUrl.Data.UpstreamBaseUrl); - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlFinder/UpstreamHostUrlFinder.cs b/src/Ocelot.Library/Infrastructure/UrlFinder/UpstreamHostUrlFinder.cs new file mode 100644 index 00000000..555eb3d2 --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/UrlFinder/UpstreamHostUrlFinder.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using Ocelot.Library.Infrastructure.HostUrlRepository; +using Ocelot.Library.Infrastructure.Responses; + +namespace Ocelot.Library.Infrastructure.UrlFinder +{ + public class UpstreamHostUrlFinder : IUpstreamHostUrlFinder + { + private readonly IHostUrlMapRepository _hostUrlMapRepository; + + public UpstreamHostUrlFinder(IHostUrlMapRepository hostUrlMapRepository) + { + _hostUrlMapRepository = hostUrlMapRepository; + } + public Response FindUpstreamHostUrl(string downstreamBaseUrl) + { + var baseUrl = _hostUrlMapRepository.GetBaseUrlMap(downstreamBaseUrl); + + if(baseUrl.IsError) + { + return new ErrorResponse(new List {new UnableToFindUpstreamHostUrl()}); + } + + return new OkResponse(baseUrl.Data.UpstreamHostUrl); + } + } +} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlPathRepository/DownstreamUrlPathAlreadyExists.cs b/src/Ocelot.Library/Infrastructure/UrlPathRepository/DownstreamUrlPathAlreadyExists.cs deleted file mode 100644 index efb3fc9e..00000000 --- a/src/Ocelot.Library/Infrastructure/UrlPathRepository/DownstreamUrlPathAlreadyExists.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.UrlPathRepository -{ - public class DownstreamUrlPathTemplateAlreadyExists : Error - { - public DownstreamUrlPathTemplateAlreadyExists() - : base("This key has already been used") - { - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlPathRepository/DownstreamUrlPathDoesNotExist.cs b/src/Ocelot.Library/Infrastructure/UrlPathRepository/DownstreamUrlPathDoesNotExist.cs deleted file mode 100644 index 47d1f768..00000000 --- a/src/Ocelot.Library/Infrastructure/UrlPathRepository/DownstreamUrlPathDoesNotExist.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.UrlPathRepository -{ - public class DownstreamUrlPathTemplateDoesNotExist : Error - { - public DownstreamUrlPathTemplateDoesNotExist() - : base("This key does not exist") - { - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Middleware/ProxyMiddleware.cs b/src/Ocelot.Library/Middleware/ProxyMiddleware.cs index a8698273..ddc94fcf 100644 --- a/src/Ocelot.Library/Middleware/ProxyMiddleware.cs +++ b/src/Ocelot.Library/Middleware/ProxyMiddleware.cs @@ -14,6 +14,13 @@ namespace Ocelot.Library.Middleware public async Task Invoke(HttpContext context) { + //get the downstream host from the request context + //get the upstream host from the host repository + //if no upstream host fail this request + //get the downstream path from the request context + //get the downstream path template from the path template finder + //todo think about variables.. + //add any query string.. await _next.Invoke(context); } } diff --git a/test/Ocelot.UnitTests/BaseUrlMapRepositoryTests.cs b/test/Ocelot.UnitTests/HostUrlMapRepositoryTests.cs similarity index 83% rename from test/Ocelot.UnitTests/BaseUrlMapRepositoryTests.cs rename to test/Ocelot.UnitTests/HostUrlMapRepositoryTests.cs index 721fd3ca..b22a6fb1 100644 --- a/test/Ocelot.UnitTests/BaseUrlMapRepositoryTests.cs +++ b/test/Ocelot.UnitTests/HostUrlMapRepositoryTests.cs @@ -1,21 +1,21 @@ using Ocelot.Library.Infrastructure.Responses; -using Ocelot.Library.Infrastructure.BaseUrlRepository; +using Ocelot.Library.Infrastructure.HostUrlRepository; using Shouldly; using Xunit; namespace Ocelot.UnitTests { - public class BaseUrlMapRepositoryTests + public class HostUrlMapRepositoryTests { private string _upstreamBaseUrl; private string _downstreamBaseUrl; - private IBaseUrlMapRepository _repository; + private IHostUrlMapRepository _repository; private Response _response; - private Response _getRouteResponse; + private Response _getRouteResponse; - public BaseUrlMapRepositoryTests() + public HostUrlMapRepositoryTests() { - _repository = new InMemoryBaseUrlMapRepository(); + _repository = new InMemoryHostUrlMapRepository(); } [Fact] @@ -66,7 +66,7 @@ namespace Ocelot.UnitTests private void ThenTheKeyDoesNotExist() { _getRouteResponse.ShouldNotBeNull(); - _getRouteResponse.ShouldBeOfType>(); + _getRouteResponse.ShouldBeOfType>(); _getRouteResponse.Errors[0].Message.ShouldBe("This key does not exist"); } @@ -77,8 +77,8 @@ namespace Ocelot.UnitTests private void ThenTheRouteIsReturned() { - _getRouteResponse.Data.DownstreamBaseUrl.ShouldBe(_downstreamBaseUrl); - _getRouteResponse.Data.UpstreamBaseUrl.ShouldBe(_upstreamBaseUrl); + _getRouteResponse.Data.DownstreamHostUrl.ShouldBe(_downstreamBaseUrl); + _getRouteResponse.Data.UpstreamHostUrl.ShouldBe(_upstreamBaseUrl); } private void GivenIHaveSetUpAnApiKeyAndUpstreamUrl(string apiKey, string upstreamUrl) @@ -100,7 +100,7 @@ namespace Ocelot.UnitTests private void WhenIAddTheConfiguration() { - _response = _repository.AddBaseUrlMap(new BaseUrlMap(_downstreamBaseUrl, _upstreamBaseUrl)); + _response = _repository.AddBaseUrlMap(new HostUrlMap(_downstreamBaseUrl, _upstreamBaseUrl)); } private void ThenTheResponseIsSuccesful() diff --git a/test/Ocelot.UnitTests/UpstreamBaseUrlFinderTests.cs b/test/Ocelot.UnitTests/UpstreamBaseUrlFinderTests.cs index 6c794cef..1cec5f18 100644 --- a/test/Ocelot.UnitTests/UpstreamBaseUrlFinderTests.cs +++ b/test/Ocelot.UnitTests/UpstreamBaseUrlFinderTests.cs @@ -1,4 +1,4 @@ -using Ocelot.Library.Infrastructure.BaseUrlRepository; +using Ocelot.Library.Infrastructure.HostUrlRepository; using Ocelot.Library.Infrastructure.UrlFinder; using Ocelot.Library.Infrastructure.Responses; using Xunit; @@ -9,28 +9,41 @@ namespace Ocelot.UnitTests { public class UpstreamBaseUrlFinderTests { - private IUpstreamBaseUrlFinder _upstreamBaseUrlFinder; - private IBaseUrlMapRepository _baseUrlMapRepository; + private IUpstreamHostUrlFinder _upstreamBaseUrlFinder; + private IHostUrlMapRepository _hostUrlMapRepository; private string _downstreamBaseUrl; private Response _result; public UpstreamBaseUrlFinderTests() { - _baseUrlMapRepository = new InMemoryBaseUrlMapRepository(); - _upstreamBaseUrlFinder = new UpstreamBaseUrlFinder(_baseUrlMapRepository); + _hostUrlMapRepository = new InMemoryHostUrlMapRepository(); + _upstreamBaseUrlFinder = new UpstreamHostUrlFinder(_hostUrlMapRepository); } [Fact] public void can_find_base_url() { - GivenTheBaseUrlMapExists(new BaseUrlMap("api.tom.com", "api.laura.com")); + GivenTheBaseUrlMapExists(new HostUrlMap("api.tom.com", "api.laura.com")); GivenTheDownstreamBaseUrlIs("api.tom.com"); WhenIFindTheMatchingUpstreamBaseUrl(); ThenTheFollowingIsReturned("api.laura.com"); } - private void GivenTheBaseUrlMapExists(BaseUrlMap baseUrlMap) + [Fact] + public void cant_find_base_url() { - _baseUrlMapRepository.AddBaseUrlMap(baseUrlMap); + GivenTheDownstreamBaseUrlIs("api.tom.com"); + WhenIFindTheMatchingUpstreamBaseUrl(); + ThenAnErrorIsReturned(); + } + + private void ThenAnErrorIsReturned() + { + _result.Errors.Count.ShouldBe(1); + } + + private void GivenTheBaseUrlMapExists(HostUrlMap baseUrlMap) + { + _hostUrlMapRepository.AddBaseUrlMap(baseUrlMap); } @@ -41,7 +54,7 @@ namespace Ocelot.UnitTests private void WhenIFindTheMatchingUpstreamBaseUrl() { - _result = _upstreamBaseUrlFinder.FindUpstreamBaseUrl(_downstreamBaseUrl); + _result = _upstreamBaseUrlFinder.FindUpstreamHostUrl(_downstreamBaseUrl); }