From 711a3d6a911a9c624c3ebe586806703568436d39 Mon Sep 17 00:00:00 2001 From: Tom Gardham-Pallister Date: Sun, 10 Jul 2016 17:11:12 +0100 Subject: [PATCH] added a base url finder --- .vscode/launch.json | 49 +++++++++++++++++ .vscode/tasks.json | 17 ++++++ .../BaseUrlRepository/BaseUrlMap.cs | 14 +++++ .../BaseUrlMapKeyAlreadyExists.cs | 12 +++++ .../BaseUrlMapKeyDoesNotExist.cs | 12 +++++ .../IBaseUrlMapRepository.cs | 10 ++++ .../InMemoryBaseUrlMapRepository.cs | 37 +++++++++++++ .../Router/UpstreamRouter/IUpstreamRouter.cs | 10 ---- .../UpstreamRouter/InMemoryUpstreamRouter.cs | 38 ------------- .../Router/UpstreamRouter/Route.cs | 14 ----- .../UpstreamRouter/RouteKeyAlreadyExists.cs | 12 ----- .../UpstreamRouter/RouteKeyDoesNotExist.cs | 12 ----- .../Router/UrlPathRouter/IUrlPathRouter.cs | 10 ---- .../UrlPathRouter/InMemoryUrlPathRouter.cs | 38 ------------- .../UrlFinder/IUpstreamBaseUrlFinder.cs | 9 ++++ .../UrlFinder/UpstreamBaseUrlFinder.cs | 22 ++++++++ .../IUrlPathToUrlPathTemplateMatcher.cs | 2 +- .../UrlPathToUrlPathTemplateMatcher.cs | 2 +- .../DownstreamUrlPathAlreadyExists.cs | 2 +- .../DownstreamUrlPathDoesNotExist.cs | 2 +- .../DownstreamUrlPathAlreadyExists.cs | 12 +++++ .../DownstreamUrlPathDoesNotExist.cs | 12 +++++ .../IUrlPathTemplateMapRepository.cs | 10 ++++ .../InMemoryUrlPathTemplateMapRepository.cs | 38 +++++++++++++ .../UrlPathTemplateMap.cs} | 6 +-- ...rTests.cs => BaseUrlMapRepositoryTests.cs} | 50 ++++++++--------- .../UpstreamBaseUrlFinderTests.cs | 53 +++++++++++++++++++ ...s => UrlPathTemplateMapRepositoryTests.cs} | 30 +++++------ .../UrlPathToUrlPathTemplateMatcherTests.cs | 7 +-- 29 files changed, 356 insertions(+), 186 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMap.cs create mode 100644 src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMapKeyAlreadyExists.cs create mode 100644 src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMapKeyDoesNotExist.cs create mode 100644 src/Ocelot.Library/Infrastructure/BaseUrlRepository/IBaseUrlMapRepository.cs create mode 100644 src/Ocelot.Library/Infrastructure/BaseUrlRepository/InMemoryBaseUrlMapRepository.cs delete mode 100644 src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/IUpstreamRouter.cs delete mode 100644 src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/InMemoryUpstreamRouter.cs delete mode 100644 src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/Route.cs delete mode 100644 src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/RouteKeyAlreadyExists.cs delete mode 100644 src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/RouteKeyDoesNotExist.cs delete mode 100644 src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/IUrlPathRouter.cs delete mode 100644 src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/InMemoryUrlPathRouter.cs create mode 100644 src/Ocelot.Library/Infrastructure/UrlFinder/IUpstreamBaseUrlFinder.cs create mode 100644 src/Ocelot.Library/Infrastructure/UrlFinder/UpstreamBaseUrlFinder.cs rename src/Ocelot.Library/Infrastructure/{Router => }/UrlPathMatcher/IUrlPathToUrlPathTemplateMatcher.cs (68%) rename src/Ocelot.Library/Infrastructure/{Router => }/UrlPathMatcher/UrlPathToUrlPathTemplateMatcher.cs (96%) rename src/Ocelot.Library/Infrastructure/{Router/UrlPathRouter => UrlPathRepository}/DownstreamUrlPathAlreadyExists.cs (80%) rename src/Ocelot.Library/Infrastructure/{Router/UrlPathRouter => UrlPathRepository}/DownstreamUrlPathDoesNotExist.cs (80%) create mode 100644 src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/DownstreamUrlPathAlreadyExists.cs create mode 100644 src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/DownstreamUrlPathDoesNotExist.cs create mode 100644 src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/IUrlPathTemplateMapRepository.cs create mode 100644 src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/InMemoryUrlPathTemplateMapRepository.cs rename src/Ocelot.Library/Infrastructure/{Router/UrlPathRouter/UrlPath.cs => UrlPathTemplateRepository/UrlPathTemplateMap.cs} (59%) rename test/Ocelot.UnitTests/{UpstreamRouterTests.cs => BaseUrlMapRepositoryTests.cs} (55%) create mode 100644 test/Ocelot.UnitTests/UpstreamBaseUrlFinderTests.cs rename test/Ocelot.UnitTests/{UrlPathRouterTests.cs => UrlPathTemplateMapRepositoryTests.cs} (73%) diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..63dc596d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,49 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "${workspaceRoot}/bin/Debug//", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "externalConsole": false + }, + { + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "${workspaceRoot}/src/Ocelot/bin/Debug/netcoreapp1.0/Ocelot.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "launchBrowser": { + "enabled": true, + "args": "${auto-detect-url}", + "windows": { + "command": "cmd.exe", + "args": "/C start ${auto-detect-url}" + }, + "osx": { + "command": "open" + }, + "linux": { + "command": "xdg-open" + } + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": 0 + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000..33256db7 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,17 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "0.1.0", + "command": "dotnet", + "isShellCommand": true, + "args": [], + "tasks": [ + { + "taskName": "build", + "args": [ ], + "isBuildCommand": true, + "showOutput": "silent", + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMap.cs b/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMap.cs new file mode 100644 index 00000000..564bb26e --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMap.cs @@ -0,0 +1,14 @@ +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 new file mode 100644 index 00000000..91b4ae4a --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMapKeyAlreadyExists.cs @@ -0,0 +1,12 @@ +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 new file mode 100644 index 00000000..d8367abf --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/BaseUrlRepository/BaseUrlMapKeyDoesNotExist.cs @@ -0,0 +1,12 @@ +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 new file mode 100644 index 00000000..2e76444d --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/BaseUrlRepository/IBaseUrlMapRepository.cs @@ -0,0 +1,10 @@ +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 new file mode 100644 index 00000000..d43fd23c --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/BaseUrlRepository/InMemoryBaseUrlMapRepository.cs @@ -0,0 +1,37 @@ +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/Router/UpstreamRouter/IUpstreamRouter.cs b/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/IUpstreamRouter.cs deleted file mode 100644 index 25323b8b..00000000 --- a/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/IUpstreamRouter.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter -{ - public interface IUpstreamRouter - { - Response AddRoute(string downstreamUrl, string upstreamUrl); - Response GetRoute(string downstreamUrl); - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/InMemoryUpstreamRouter.cs b/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/InMemoryUpstreamRouter.cs deleted file mode 100644 index 7767dc41..00000000 --- a/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/InMemoryUpstreamRouter.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Collections.Generic; -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter -{ - public class InMemoryUpstreamRouter : IUpstreamRouter - { - private readonly Dictionary _routes; - public InMemoryUpstreamRouter() - { - _routes = new Dictionary(); - } - public Response AddRoute(string downstreamUrl, string upstreamUrl) - { - if(_routes.ContainsKey(downstreamUrl)) - { - return new ErrorResponse(new List(){new RouteKeyAlreadyExists()}); - } - - _routes.Add(downstreamUrl, upstreamUrl); - - return new OkResponse(); - } - - public Response GetRoute(string downstreamUrl) - { - string upstreamUrl = null; - - if(_routes.TryGetValue(downstreamUrl, out upstreamUrl)) - { - return new OkResponse(new Route(downstreamUrl, upstreamUrl)); - } - - return new ErrorResponse(new List(){new RouteKeyDoesNotExist()}); - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/Route.cs b/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/Route.cs deleted file mode 100644 index 6b14d307..00000000 --- a/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/Route.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter -{ - public class Route - { - public Route(string downstreamUrl, string upstreamUrl) - { - DownstreamUrl = downstreamUrl; - UpstreamUrl = upstreamUrl; - } - - public string DownstreamUrl {get;private set;} - public string UpstreamUrl {get;private set;} - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/RouteKeyAlreadyExists.cs b/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/RouteKeyAlreadyExists.cs deleted file mode 100644 index 19a436cc..00000000 --- a/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/RouteKeyAlreadyExists.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter -{ - public class RouteKeyAlreadyExists : Error - { - public RouteKeyAlreadyExists() - : base("This key has already been used") - { - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/RouteKeyDoesNotExist.cs b/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/RouteKeyDoesNotExist.cs deleted file mode 100644 index fe4c90ba..00000000 --- a/src/Ocelot.Library/Infrastructure/Router/UpstreamRouter/RouteKeyDoesNotExist.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.Router.UpstreamRouter -{ - public class RouteKeyDoesNotExist : Error - { - public RouteKeyDoesNotExist() - : base("This key does not exist") - { - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/IUrlPathRouter.cs b/src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/IUrlPathRouter.cs deleted file mode 100644 index c39be36b..00000000 --- a/src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/IUrlPathRouter.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter -{ - public interface IUrlPathRouter - { - Response AddRoute(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate); - Response GetRoute(string downstreamUrlPathTemplate); - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/InMemoryUrlPathRouter.cs b/src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/InMemoryUrlPathRouter.cs deleted file mode 100644 index 73c358f2..00000000 --- a/src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/InMemoryUrlPathRouter.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Collections.Generic; -using Ocelot.Library.Infrastructure.Responses; - -namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter -{ - public class InMemoryUrlPathRouter : IUrlPathRouter - { - private readonly Dictionary _routes; - public InMemoryUrlPathRouter() - { - _routes = new Dictionary(); - } - public Response AddRoute(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate) - { - if(_routes.ContainsKey(downstreamUrlPathTemplate)) - { - return new ErrorResponse(new List(){new DownstreamUrlPathTemplateAlreadyExists()}); - } - - _routes.Add(downstreamUrlPathTemplate, upstreamUrlPathTemplate); - - return new OkResponse(); - } - - public Response GetRoute(string downstreamUrlPathTemplate) - { - string upstreamUrlPathTemplate = null; - - if(_routes.TryGetValue(downstreamUrlPathTemplate, out upstreamUrlPathTemplate)) - { - return new OkResponse(new UrlPath(downstreamUrlPathTemplate, upstreamUrlPathTemplate)); - } - - return new ErrorResponse(new List(){new DownstreamUrlPathTemplateDoesNotExist()}); - } - } -} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlFinder/IUpstreamBaseUrlFinder.cs b/src/Ocelot.Library/Infrastructure/UrlFinder/IUpstreamBaseUrlFinder.cs new file mode 100644 index 00000000..c4623939 --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/UrlFinder/IUpstreamBaseUrlFinder.cs @@ -0,0 +1,9 @@ +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/UpstreamBaseUrlFinder.cs b/src/Ocelot.Library/Infrastructure/UrlFinder/UpstreamBaseUrlFinder.cs new file mode 100644 index 00000000..135c8fcc --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/UrlFinder/UpstreamBaseUrlFinder.cs @@ -0,0 +1,22 @@ +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/Router/UrlPathMatcher/IUrlPathToUrlPathTemplateMatcher.cs b/src/Ocelot.Library/Infrastructure/UrlPathMatcher/IUrlPathToUrlPathTemplateMatcher.cs similarity index 68% rename from src/Ocelot.Library/Infrastructure/Router/UrlPathMatcher/IUrlPathToUrlPathTemplateMatcher.cs rename to src/Ocelot.Library/Infrastructure/UrlPathMatcher/IUrlPathToUrlPathTemplateMatcher.cs index 5fb89c15..7a617e13 100644 --- a/src/Ocelot.Library/Infrastructure/Router/UrlPathMatcher/IUrlPathToUrlPathTemplateMatcher.cs +++ b/src/Ocelot.Library/Infrastructure/UrlPathMatcher/IUrlPathToUrlPathTemplateMatcher.cs @@ -1,4 +1,4 @@ -namespace Ocelot.Library.Infrastructure.Router.UrlPathMatcher +namespace Ocelot.Library.Infrastructure.UrlPathMatcher { public interface IUrlPathToUrlPathTemplateMatcher { diff --git a/src/Ocelot.Library/Infrastructure/Router/UrlPathMatcher/UrlPathToUrlPathTemplateMatcher.cs b/src/Ocelot.Library/Infrastructure/UrlPathMatcher/UrlPathToUrlPathTemplateMatcher.cs similarity index 96% rename from src/Ocelot.Library/Infrastructure/Router/UrlPathMatcher/UrlPathToUrlPathTemplateMatcher.cs rename to src/Ocelot.Library/Infrastructure/UrlPathMatcher/UrlPathToUrlPathTemplateMatcher.cs index fb543fb3..04189704 100644 --- a/src/Ocelot.Library/Infrastructure/Router/UrlPathMatcher/UrlPathToUrlPathTemplateMatcher.cs +++ b/src/Ocelot.Library/Infrastructure/UrlPathMatcher/UrlPathToUrlPathTemplateMatcher.cs @@ -1,4 +1,4 @@ -namespace Ocelot.Library.Infrastructure.Router.UrlPathMatcher +namespace Ocelot.Library.Infrastructure.UrlPathMatcher { public class UrlPathToUrlPathTemplateMatcher : IUrlPathToUrlPathTemplateMatcher { diff --git a/src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/DownstreamUrlPathAlreadyExists.cs b/src/Ocelot.Library/Infrastructure/UrlPathRepository/DownstreamUrlPathAlreadyExists.cs similarity index 80% rename from src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/DownstreamUrlPathAlreadyExists.cs rename to src/Ocelot.Library/Infrastructure/UrlPathRepository/DownstreamUrlPathAlreadyExists.cs index 3dc099a7..efb3fc9e 100644 --- a/src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/DownstreamUrlPathAlreadyExists.cs +++ b/src/Ocelot.Library/Infrastructure/UrlPathRepository/DownstreamUrlPathAlreadyExists.cs @@ -1,6 +1,6 @@ using Ocelot.Library.Infrastructure.Responses; -namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter +namespace Ocelot.Library.Infrastructure.UrlPathRepository { public class DownstreamUrlPathTemplateAlreadyExists : Error { diff --git a/src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/DownstreamUrlPathDoesNotExist.cs b/src/Ocelot.Library/Infrastructure/UrlPathRepository/DownstreamUrlPathDoesNotExist.cs similarity index 80% rename from src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/DownstreamUrlPathDoesNotExist.cs rename to src/Ocelot.Library/Infrastructure/UrlPathRepository/DownstreamUrlPathDoesNotExist.cs index 4c05e3d2..47d1f768 100644 --- a/src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/DownstreamUrlPathDoesNotExist.cs +++ b/src/Ocelot.Library/Infrastructure/UrlPathRepository/DownstreamUrlPathDoesNotExist.cs @@ -1,6 +1,6 @@ using Ocelot.Library.Infrastructure.Responses; -namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter +namespace Ocelot.Library.Infrastructure.UrlPathRepository { public class DownstreamUrlPathTemplateDoesNotExist : Error { diff --git a/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/DownstreamUrlPathAlreadyExists.cs b/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/DownstreamUrlPathAlreadyExists.cs new file mode 100644 index 00000000..05a552ac --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/DownstreamUrlPathAlreadyExists.cs @@ -0,0 +1,12 @@ +using Ocelot.Library.Infrastructure.Responses; + +namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository +{ + 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/UrlPathTemplateRepository/DownstreamUrlPathDoesNotExist.cs b/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/DownstreamUrlPathDoesNotExist.cs new file mode 100644 index 00000000..7d89beeb --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/DownstreamUrlPathDoesNotExist.cs @@ -0,0 +1,12 @@ +using Ocelot.Library.Infrastructure.Responses; + +namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository +{ + public class DownstreamUrlPathTemplateDoesNotExist : Error + { + public DownstreamUrlPathTemplateDoesNotExist() + : base("This key does not exist") + { + } + } +} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/IUrlPathTemplateMapRepository.cs b/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/IUrlPathTemplateMapRepository.cs new file mode 100644 index 00000000..f847c7ba --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/IUrlPathTemplateMapRepository.cs @@ -0,0 +1,10 @@ +using Ocelot.Library.Infrastructure.Responses; + +namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository +{ + public interface IUrlPathTemplateMapRepository + { + Response AddUrlPathTemplateMap(UrlPathTemplateMap urlPathMap); + Response GetUrlPathTemplateMap(string downstreamUrlPathTemplate); + } +} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/InMemoryUrlPathTemplateMapRepository.cs b/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/InMemoryUrlPathTemplateMapRepository.cs new file mode 100644 index 00000000..257f1d3f --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/InMemoryUrlPathTemplateMapRepository.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using Ocelot.Library.Infrastructure.Responses; + +namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository +{ + public class InMemoryUrlPathTemplateMapRepository : IUrlPathTemplateMapRepository + { + private readonly Dictionary _routes; + public InMemoryUrlPathTemplateMapRepository() + { + _routes = new Dictionary(); + } + public Response AddUrlPathTemplateMap(UrlPathTemplateMap urlPathMap) + { + if(_routes.ContainsKey(urlPathMap.DownstreamUrlPathTemplate)) + { + return new ErrorResponse(new List(){new DownstreamUrlPathTemplateAlreadyExists()}); + } + + _routes.Add(urlPathMap.DownstreamUrlPathTemplate, urlPathMap.UpstreamUrlPathTemplate); + + return new OkResponse(); + } + + public Response GetUrlPathTemplateMap(string downstreamUrlPathTemplate) + { + string upstreamUrlPathTemplate = null; + + if(_routes.TryGetValue(downstreamUrlPathTemplate, out upstreamUrlPathTemplate)) + { + return new OkResponse(new UrlPathTemplateMap(downstreamUrlPathTemplate, upstreamUrlPathTemplate)); + } + + return new ErrorResponse(new List(){new DownstreamUrlPathTemplateDoesNotExist()}); + } + } +} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/UrlPath.cs b/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/UrlPathTemplateMap.cs similarity index 59% rename from src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/UrlPath.cs rename to src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/UrlPathTemplateMap.cs index b1ea65aa..1471813c 100644 --- a/src/Ocelot.Library/Infrastructure/Router/UrlPathRouter/UrlPath.cs +++ b/src/Ocelot.Library/Infrastructure/UrlPathTemplateRepository/UrlPathTemplateMap.cs @@ -1,8 +1,8 @@ -namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter +namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository { - public class UrlPath + public class UrlPathTemplateMap { - public UrlPath(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate) + public UrlPathTemplateMap(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate) { DownstreamUrlPathTemplate = downstreamUrlPathTemplate; UpstreamUrlPathTemplate = upstreamUrlPathTemplate; diff --git a/test/Ocelot.UnitTests/UpstreamRouterTests.cs b/test/Ocelot.UnitTests/BaseUrlMapRepositoryTests.cs similarity index 55% rename from test/Ocelot.UnitTests/UpstreamRouterTests.cs rename to test/Ocelot.UnitTests/BaseUrlMapRepositoryTests.cs index e2060f6e..721fd3ca 100644 --- a/test/Ocelot.UnitTests/UpstreamRouterTests.cs +++ b/test/Ocelot.UnitTests/BaseUrlMapRepositoryTests.cs @@ -1,28 +1,28 @@ using Ocelot.Library.Infrastructure.Responses; -using Ocelot.Library.Infrastructure.Router.UpstreamRouter; +using Ocelot.Library.Infrastructure.BaseUrlRepository; using Shouldly; using Xunit; namespace Ocelot.UnitTests { - public class RouterTests + public class BaseUrlMapRepositoryTests { - private string _upstreamApiUrl; - private string _apiKey; - private IUpstreamRouter _router; + private string _upstreamBaseUrl; + private string _downstreamBaseUrl; + private IBaseUrlMapRepository _repository; private Response _response; - private Response _getRouteResponse; - - public RouterTests() + private Response _getRouteResponse; + + public BaseUrlMapRepositoryTests() { - _router = new InMemoryUpstreamRouter(); + _repository = new InMemoryBaseUrlMapRepository(); } [Fact] public void can_add_route() { - GivenIHaveAnUpstreamApi("http://www.someapi.com/api1"); - GivenIWantToRouteRequestsToMyUpstreamApi("api"); + GivenIHaveAnUpstreamBaseUrl("www.someapi.com"); + GivenIWantToRouteRequestsFromMyDownstreamBaseUrl("api"); WhenIAddTheConfiguration(); ThenTheResponseIsSuccesful(); } @@ -30,7 +30,7 @@ namespace Ocelot.UnitTests [Fact] public void can_get_route_by_key() { - GivenIHaveSetUpAnApiKeyAndUpstreamUrl("api2", "http://www.someapi.com/api2"); + GivenIHaveSetUpAnApiKeyAndUpstreamUrl("api2", "www.someapi.com"); WhenIRetrieveTheRouteByKey(); ThenTheRouteIsReturned(); } @@ -38,7 +38,7 @@ namespace Ocelot.UnitTests [Fact] public void should_return_error_response_when_key_already_used() { - GivenIHaveSetUpAnApiKeyAndUpstreamUrl("api2", "http://www.someapi.com/api2"); + GivenIHaveSetUpAnApiKeyAndUpstreamUrl("api2", "www.someapi.com"); WhenITryToUseTheSameKey(); ThenTheKeyHasAlreadyBeenUsed(); } @@ -46,7 +46,7 @@ namespace Ocelot.UnitTests [Fact] public void should_return_error_response_if_key_doesnt_exist() { - GivenIWantToRouteRequestsToMyUpstreamApi("api"); + GivenIWantToRouteRequestsFromMyDownstreamBaseUrl("api"); WhenIRetrieveTheRouteByKey(); ThenTheKeyDoesNotExist(); } @@ -66,41 +66,41 @@ namespace Ocelot.UnitTests private void ThenTheKeyDoesNotExist() { _getRouteResponse.ShouldNotBeNull(); - _getRouteResponse.ShouldBeOfType>(); + _getRouteResponse.ShouldBeOfType>(); _getRouteResponse.Errors[0].Message.ShouldBe("This key does not exist"); } private void WhenIRetrieveTheRouteByKey() { - _getRouteResponse = _router.GetRoute(_apiKey); + _getRouteResponse = _repository.GetBaseUrlMap(_downstreamBaseUrl); } private void ThenTheRouteIsReturned() { - _getRouteResponse.Data.DownstreamUrl.ShouldBe(_apiKey); - _getRouteResponse.Data.UpstreamUrl.ShouldBe(_upstreamApiUrl); + _getRouteResponse.Data.DownstreamBaseUrl.ShouldBe(_downstreamBaseUrl); + _getRouteResponse.Data.UpstreamBaseUrl.ShouldBe(_upstreamBaseUrl); } private void GivenIHaveSetUpAnApiKeyAndUpstreamUrl(string apiKey, string upstreamUrl) { - GivenIHaveAnUpstreamApi(upstreamUrl); - GivenIWantToRouteRequestsToMyUpstreamApi(apiKey); + GivenIHaveAnUpstreamBaseUrl(upstreamUrl); + GivenIWantToRouteRequestsFromMyDownstreamBaseUrl(apiKey); WhenIAddTheConfiguration(); } - private void GivenIHaveAnUpstreamApi(string upstreamApiUrl) + private void GivenIHaveAnUpstreamBaseUrl(string upstreamApiUrl) { - _upstreamApiUrl = upstreamApiUrl; + _upstreamBaseUrl = upstreamApiUrl; } - private void GivenIWantToRouteRequestsToMyUpstreamApi(string apiKey) + private void GivenIWantToRouteRequestsFromMyDownstreamBaseUrl(string downstreamBaseUrl) { - _apiKey = apiKey; + _downstreamBaseUrl = downstreamBaseUrl; } private void WhenIAddTheConfiguration() { - _response = _router.AddRoute(_apiKey, _upstreamApiUrl); + _response = _repository.AddBaseUrlMap(new BaseUrlMap(_downstreamBaseUrl, _upstreamBaseUrl)); } private void ThenTheResponseIsSuccesful() diff --git a/test/Ocelot.UnitTests/UpstreamBaseUrlFinderTests.cs b/test/Ocelot.UnitTests/UpstreamBaseUrlFinderTests.cs new file mode 100644 index 00000000..6c794cef --- /dev/null +++ b/test/Ocelot.UnitTests/UpstreamBaseUrlFinderTests.cs @@ -0,0 +1,53 @@ +using Ocelot.Library.Infrastructure.BaseUrlRepository; +using Ocelot.Library.Infrastructure.UrlFinder; +using Ocelot.Library.Infrastructure.Responses; +using Xunit; +using Shouldly; +using System; + +namespace Ocelot.UnitTests +{ + public class UpstreamBaseUrlFinderTests + { + private IUpstreamBaseUrlFinder _upstreamBaseUrlFinder; + private IBaseUrlMapRepository _baseUrlMapRepository; + private string _downstreamBaseUrl; + private Response _result; + public UpstreamBaseUrlFinderTests() + { + _baseUrlMapRepository = new InMemoryBaseUrlMapRepository(); + _upstreamBaseUrlFinder = new UpstreamBaseUrlFinder(_baseUrlMapRepository); + } + + [Fact] + public void can_find_base_url() + { + GivenTheBaseUrlMapExists(new BaseUrlMap("api.tom.com", "api.laura.com")); + GivenTheDownstreamBaseUrlIs("api.tom.com"); + WhenIFindTheMatchingUpstreamBaseUrl(); + ThenTheFollowingIsReturned("api.laura.com"); + } + + private void GivenTheBaseUrlMapExists(BaseUrlMap baseUrlMap) + { + _baseUrlMapRepository.AddBaseUrlMap(baseUrlMap); + + } + + private void GivenTheDownstreamBaseUrlIs(string downstreamBaseUrl) + { + _downstreamBaseUrl = downstreamBaseUrl; + } + + private void WhenIFindTheMatchingUpstreamBaseUrl() + { + _result = _upstreamBaseUrlFinder.FindUpstreamBaseUrl(_downstreamBaseUrl); + + } + + private void ThenTheFollowingIsReturned(string expectedBaseUrl) + { + _result.Data.ShouldBe(expectedBaseUrl); + } + } +} \ No newline at end of file diff --git a/test/Ocelot.UnitTests/UrlPathRouterTests.cs b/test/Ocelot.UnitTests/UrlPathTemplateMapRepositoryTests.cs similarity index 73% rename from test/Ocelot.UnitTests/UrlPathRouterTests.cs rename to test/Ocelot.UnitTests/UrlPathTemplateMapRepositoryTests.cs index 73cdc06e..71ddd2c3 100644 --- a/test/Ocelot.UnitTests/UrlPathRouterTests.cs +++ b/test/Ocelot.UnitTests/UrlPathTemplateMapRepositoryTests.cs @@ -1,28 +1,28 @@ using Ocelot.Library.Infrastructure.Responses; -using Ocelot.Library.Infrastructure.Router.UrlPathRouter; +using Ocelot.Library.Infrastructure.UrlPathTemplateRepository; using Shouldly; using Xunit; namespace Ocelot.UnitTests { - public class UrlPathRouterTests + public class UrlPathTemplateMapRepositoryTests { - private string _upstreamUrlPath; + private string _upstreamUrlPath; private string _downstreamUrlPath; - private IUrlPathRouter _router; + private IUrlPathTemplateMapRepository _repository; private Response _response; - private Response _getResponse; + private Response _getResponse; - public UrlPathRouterTests() + public UrlPathTemplateMapRepositoryTests() { - _router = new InMemoryUrlPathRouter(); + _repository = new InMemoryUrlPathTemplateMapRepository(); } [Fact] public void can_add_url_path() { - GivenIHaveAnUpstreamUrlPath("api/products/products/{productId}"); - GivenIWantToRouteRequestsToMyUpstreamUrlPath("api/products/{productId}"); + GivenIHaveAnUpstreamUrlPath("/api/products/products/{productId}"); + GivenIWantToRouteRequestsToMyUpstreamUrlPath("/api/products/{productId}"); WhenIAddTheConfiguration(); ThenTheResponseIsSuccesful(); } @@ -30,7 +30,7 @@ namespace Ocelot.UnitTests [Fact] public void can_get_url_path() { - GivenIHaveSetUpADownstreamUrlPathAndAnUpstreamUrlPath("api2", "http://www.someapi.com/api2"); + GivenIHaveSetUpADownstreamUrlPathAndAnUpstreamUrlPath("/api2", "http://www.someapi.com/api2"); WhenIRetrieveTheUrlPathByDownstreamUrl(); ThenTheUrlPathIsReturned(); } @@ -38,7 +38,7 @@ namespace Ocelot.UnitTests [Fact] public void should_return_error_response_when_url_path_already_used() { - GivenIHaveSetUpADownstreamUrlPathAndAnUpstreamUrlPath("api2", "http://www.someapi.com/api2"); + GivenIHaveSetUpADownstreamUrlPathAndAnUpstreamUrlPath("/api2", "http://www.someapi.com/api2"); WhenITryToUseTheSameDownstreamUrl(); ThenTheDownstreamUrlAlreadyBeenUsed(); } @@ -46,7 +46,7 @@ namespace Ocelot.UnitTests [Fact] public void should_return_error_response_if_key_doesnt_exist() { - GivenIWantToRouteRequestsToMyUpstreamUrlPath("api"); + GivenIWantToRouteRequestsToMyUpstreamUrlPath("/api"); WhenIRetrieveTheUrlPathByDownstreamUrl(); ThenTheKeyDoesNotExist(); } @@ -66,13 +66,13 @@ namespace Ocelot.UnitTests private void ThenTheKeyDoesNotExist() { _getResponse.ShouldNotBeNull(); - _getResponse.ShouldBeOfType>(); + _getResponse.ShouldBeOfType>(); _getResponse.Errors[0].Message.ShouldBe("This key does not exist"); } private void WhenIRetrieveTheUrlPathByDownstreamUrl() { - _getResponse = _router.GetRoute(_downstreamUrlPath); + _getResponse = _repository.GetUrlPathTemplateMap(_downstreamUrlPath); } private void ThenTheUrlPathIsReturned() @@ -100,7 +100,7 @@ namespace Ocelot.UnitTests private void WhenIAddTheConfiguration() { - _response = _router.AddRoute(_downstreamUrlPath, _upstreamUrlPath); + _response = _repository.AddUrlPathTemplateMap(new UrlPathTemplateMap(_downstreamUrlPath, _upstreamUrlPath)); } private void ThenTheResponseIsSuccesful() diff --git a/test/Ocelot.UnitTests/UrlPathToUrlPathTemplateMatcherTests.cs b/test/Ocelot.UnitTests/UrlPathToUrlPathTemplateMatcherTests.cs index b81bed68..2101e637 100644 --- a/test/Ocelot.UnitTests/UrlPathToUrlPathTemplateMatcherTests.cs +++ b/test/Ocelot.UnitTests/UrlPathToUrlPathTemplateMatcherTests.cs @@ -1,10 +1,7 @@ -using System; -using Ocelot.Library.Infrastructure.Responses; -using Ocelot.Library.Infrastructure.Router.UpstreamRouter; -using Ocelot.Library.Infrastructure.Router.UrlPathMatcher; +using Ocelot.Library.Infrastructure.UrlPathMatcher; using Shouldly; using Xunit; - + namespace Ocelot.UnitTests { public class UrlPathToUrlPathTemplateMatcherTests