mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 09:52:50 +08:00
added a base url finder
This commit is contained in:
parent
5b417ad466
commit
711a3d6a91
49
.vscode/launch.json
vendored
Normal file
49
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": ".NET Core Launch (console)",
|
||||||
|
"type": "coreclr",
|
||||||
|
"request": "launch",
|
||||||
|
"preLaunchTask": "build",
|
||||||
|
"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
17
.vscode/tasks.json
vendored
Normal file
17
.vscode/tasks.json
vendored
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -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;}
|
||||||
|
}
|
||||||
|
}
|
@ -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")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
|
|
||||||
|
namespace Ocelot.Library.Infrastructure.BaseUrlRepository
|
||||||
|
{
|
||||||
|
public interface IBaseUrlMapRepository
|
||||||
|
{
|
||||||
|
Response AddBaseUrlMap(BaseUrlMap baseUrlMap);
|
||||||
|
Response<BaseUrlMap> GetBaseUrlMap(string downstreamUrl);
|
||||||
|
}
|
||||||
|
}
|
@ -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<string, string> _routes;
|
||||||
|
public InMemoryBaseUrlMapRepository()
|
||||||
|
{
|
||||||
|
_routes = new Dictionary<string,string>();
|
||||||
|
}
|
||||||
|
public Response AddBaseUrlMap(BaseUrlMap baseUrlMap)
|
||||||
|
{
|
||||||
|
if(_routes.ContainsKey(baseUrlMap.DownstreamBaseUrl))
|
||||||
|
{
|
||||||
|
return new ErrorResponse(new List<Error>(){new BaseUrlMapKeyAlreadyExists()});
|
||||||
|
}
|
||||||
|
|
||||||
|
_routes.Add(baseUrlMap.DownstreamBaseUrl, baseUrlMap.UpstreamBaseUrl);
|
||||||
|
|
||||||
|
return new OkResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Response<BaseUrlMap> GetBaseUrlMap(string downstreamUrl)
|
||||||
|
{
|
||||||
|
string upstreamUrl = null;
|
||||||
|
|
||||||
|
if(_routes.TryGetValue(downstreamUrl, out upstreamUrl))
|
||||||
|
{
|
||||||
|
return new OkResponse<BaseUrlMap>(new BaseUrlMap(downstreamUrl, upstreamUrl));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ErrorResponse<BaseUrlMap>(new List<Error>(){new BaseUrlMapKeyDoesNotExist()});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<Route> GetRoute(string downstreamUrl);
|
|
||||||
}
|
|
||||||
}
|
|
@ -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<string, string> _routes;
|
|
||||||
public InMemoryUpstreamRouter()
|
|
||||||
{
|
|
||||||
_routes = new Dictionary<string,string>();
|
|
||||||
}
|
|
||||||
public Response AddRoute(string downstreamUrl, string upstreamUrl)
|
|
||||||
{
|
|
||||||
if(_routes.ContainsKey(downstreamUrl))
|
|
||||||
{
|
|
||||||
return new ErrorResponse(new List<Error>(){new RouteKeyAlreadyExists()});
|
|
||||||
}
|
|
||||||
|
|
||||||
_routes.Add(downstreamUrl, upstreamUrl);
|
|
||||||
|
|
||||||
return new OkResponse();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Response<Route> GetRoute(string downstreamUrl)
|
|
||||||
{
|
|
||||||
string upstreamUrl = null;
|
|
||||||
|
|
||||||
if(_routes.TryGetValue(downstreamUrl, out upstreamUrl))
|
|
||||||
{
|
|
||||||
return new OkResponse<Route>(new Route(downstreamUrl, upstreamUrl));
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ErrorResponse<Route>(new List<Error>(){new RouteKeyDoesNotExist()});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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;}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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")
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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")
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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<UrlPath> GetRoute(string downstreamUrlPathTemplate);
|
|
||||||
}
|
|
||||||
}
|
|
@ -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<string, string> _routes;
|
|
||||||
public InMemoryUrlPathRouter()
|
|
||||||
{
|
|
||||||
_routes = new Dictionary<string,string>();
|
|
||||||
}
|
|
||||||
public Response AddRoute(string downstreamUrlPathTemplate, string upstreamUrlPathTemplate)
|
|
||||||
{
|
|
||||||
if(_routes.ContainsKey(downstreamUrlPathTemplate))
|
|
||||||
{
|
|
||||||
return new ErrorResponse(new List<Error>(){new DownstreamUrlPathTemplateAlreadyExists()});
|
|
||||||
}
|
|
||||||
|
|
||||||
_routes.Add(downstreamUrlPathTemplate, upstreamUrlPathTemplate);
|
|
||||||
|
|
||||||
return new OkResponse();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Response<UrlPath> GetRoute(string downstreamUrlPathTemplate)
|
|
||||||
{
|
|
||||||
string upstreamUrlPathTemplate = null;
|
|
||||||
|
|
||||||
if(_routes.TryGetValue(downstreamUrlPathTemplate, out upstreamUrlPathTemplate))
|
|
||||||
{
|
|
||||||
return new OkResponse<UrlPath>(new UrlPath(downstreamUrlPathTemplate, upstreamUrlPathTemplate));
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ErrorResponse<UrlPath>(new List<Error>(){new DownstreamUrlPathTemplateDoesNotExist()});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,9 @@
|
|||||||
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
|
|
||||||
|
namespace Ocelot.Library.Infrastructure.UrlFinder
|
||||||
|
{
|
||||||
|
public interface IUpstreamBaseUrlFinder
|
||||||
|
{
|
||||||
|
Response<string> FindUpstreamBaseUrl(string downstreamBaseUrl);
|
||||||
|
}
|
||||||
|
}
|
@ -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<string> FindUpstreamBaseUrl(string downstreamBaseUrl)
|
||||||
|
{
|
||||||
|
var baseUrl = _baseUrlMapRepository.GetBaseUrlMap(downstreamBaseUrl);
|
||||||
|
|
||||||
|
return new OkResponse<string>(baseUrl.Data.UpstreamBaseUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
namespace Ocelot.Library.Infrastructure.Router.UrlPathMatcher
|
namespace Ocelot.Library.Infrastructure.UrlPathMatcher
|
||||||
{
|
{
|
||||||
public interface IUrlPathToUrlPathTemplateMatcher
|
public interface IUrlPathToUrlPathTemplateMatcher
|
||||||
{
|
{
|
@ -1,4 +1,4 @@
|
|||||||
namespace Ocelot.Library.Infrastructure.Router.UrlPathMatcher
|
namespace Ocelot.Library.Infrastructure.UrlPathMatcher
|
||||||
{
|
{
|
||||||
public class UrlPathToUrlPathTemplateMatcher : IUrlPathToUrlPathTemplateMatcher
|
public class UrlPathToUrlPathTemplateMatcher : IUrlPathToUrlPathTemplateMatcher
|
||||||
{
|
{
|
@ -1,6 +1,6 @@
|
|||||||
using Ocelot.Library.Infrastructure.Responses;
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
|
|
||||||
namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter
|
namespace Ocelot.Library.Infrastructure.UrlPathRepository
|
||||||
{
|
{
|
||||||
public class DownstreamUrlPathTemplateAlreadyExists : Error
|
public class DownstreamUrlPathTemplateAlreadyExists : Error
|
||||||
{
|
{
|
@ -1,6 +1,6 @@
|
|||||||
using Ocelot.Library.Infrastructure.Responses;
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
|
|
||||||
namespace Ocelot.Library.Infrastructure.Router.UrlPathRouter
|
namespace Ocelot.Library.Infrastructure.UrlPathRepository
|
||||||
{
|
{
|
||||||
public class DownstreamUrlPathTemplateDoesNotExist : Error
|
public class DownstreamUrlPathTemplateDoesNotExist : Error
|
||||||
{
|
{
|
@ -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")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
|
|
||||||
|
namespace Ocelot.Library.Infrastructure.UrlPathTemplateRepository
|
||||||
|
{
|
||||||
|
public interface IUrlPathTemplateMapRepository
|
||||||
|
{
|
||||||
|
Response AddUrlPathTemplateMap(UrlPathTemplateMap urlPathMap);
|
||||||
|
Response<UrlPathTemplateMap> GetUrlPathTemplateMap(string downstreamUrlPathTemplate);
|
||||||
|
}
|
||||||
|
}
|
@ -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<string, string> _routes;
|
||||||
|
public InMemoryUrlPathTemplateMapRepository()
|
||||||
|
{
|
||||||
|
_routes = new Dictionary<string,string>();
|
||||||
|
}
|
||||||
|
public Response AddUrlPathTemplateMap(UrlPathTemplateMap urlPathMap)
|
||||||
|
{
|
||||||
|
if(_routes.ContainsKey(urlPathMap.DownstreamUrlPathTemplate))
|
||||||
|
{
|
||||||
|
return new ErrorResponse(new List<Error>(){new DownstreamUrlPathTemplateAlreadyExists()});
|
||||||
|
}
|
||||||
|
|
||||||
|
_routes.Add(urlPathMap.DownstreamUrlPathTemplate, urlPathMap.UpstreamUrlPathTemplate);
|
||||||
|
|
||||||
|
return new OkResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Response<UrlPathTemplateMap> GetUrlPathTemplateMap(string downstreamUrlPathTemplate)
|
||||||
|
{
|
||||||
|
string upstreamUrlPathTemplate = null;
|
||||||
|
|
||||||
|
if(_routes.TryGetValue(downstreamUrlPathTemplate, out upstreamUrlPathTemplate))
|
||||||
|
{
|
||||||
|
return new OkResponse<UrlPathTemplateMap>(new UrlPathTemplateMap(downstreamUrlPathTemplate, upstreamUrlPathTemplate));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ErrorResponse<UrlPathTemplateMap>(new List<Error>(){new DownstreamUrlPathTemplateDoesNotExist()});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
DownstreamUrlPathTemplate = downstreamUrlPathTemplate;
|
||||||
UpstreamUrlPathTemplate = upstreamUrlPathTemplate;
|
UpstreamUrlPathTemplate = upstreamUrlPathTemplate;
|
@ -1,28 +1,28 @@
|
|||||||
using Ocelot.Library.Infrastructure.Responses;
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
using Ocelot.Library.Infrastructure.Router.UpstreamRouter;
|
using Ocelot.Library.Infrastructure.BaseUrlRepository;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Ocelot.UnitTests
|
namespace Ocelot.UnitTests
|
||||||
{
|
{
|
||||||
public class RouterTests
|
public class BaseUrlMapRepositoryTests
|
||||||
{
|
{
|
||||||
private string _upstreamApiUrl;
|
private string _upstreamBaseUrl;
|
||||||
private string _apiKey;
|
private string _downstreamBaseUrl;
|
||||||
private IUpstreamRouter _router;
|
private IBaseUrlMapRepository _repository;
|
||||||
private Response _response;
|
private Response _response;
|
||||||
private Response<Route> _getRouteResponse;
|
private Response<BaseUrlMap> _getRouteResponse;
|
||||||
|
|
||||||
public RouterTests()
|
public BaseUrlMapRepositoryTests()
|
||||||
{
|
{
|
||||||
_router = new InMemoryUpstreamRouter();
|
_repository = new InMemoryBaseUrlMapRepository();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void can_add_route()
|
public void can_add_route()
|
||||||
{
|
{
|
||||||
GivenIHaveAnUpstreamApi("http://www.someapi.com/api1");
|
GivenIHaveAnUpstreamBaseUrl("www.someapi.com");
|
||||||
GivenIWantToRouteRequestsToMyUpstreamApi("api");
|
GivenIWantToRouteRequestsFromMyDownstreamBaseUrl("api");
|
||||||
WhenIAddTheConfiguration();
|
WhenIAddTheConfiguration();
|
||||||
ThenTheResponseIsSuccesful();
|
ThenTheResponseIsSuccesful();
|
||||||
}
|
}
|
||||||
@ -30,7 +30,7 @@ namespace Ocelot.UnitTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void can_get_route_by_key()
|
public void can_get_route_by_key()
|
||||||
{
|
{
|
||||||
GivenIHaveSetUpAnApiKeyAndUpstreamUrl("api2", "http://www.someapi.com/api2");
|
GivenIHaveSetUpAnApiKeyAndUpstreamUrl("api2", "www.someapi.com");
|
||||||
WhenIRetrieveTheRouteByKey();
|
WhenIRetrieveTheRouteByKey();
|
||||||
ThenTheRouteIsReturned();
|
ThenTheRouteIsReturned();
|
||||||
}
|
}
|
||||||
@ -38,7 +38,7 @@ namespace Ocelot.UnitTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_error_response_when_key_already_used()
|
public void should_return_error_response_when_key_already_used()
|
||||||
{
|
{
|
||||||
GivenIHaveSetUpAnApiKeyAndUpstreamUrl("api2", "http://www.someapi.com/api2");
|
GivenIHaveSetUpAnApiKeyAndUpstreamUrl("api2", "www.someapi.com");
|
||||||
WhenITryToUseTheSameKey();
|
WhenITryToUseTheSameKey();
|
||||||
ThenTheKeyHasAlreadyBeenUsed();
|
ThenTheKeyHasAlreadyBeenUsed();
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ namespace Ocelot.UnitTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_error_response_if_key_doesnt_exist()
|
public void should_return_error_response_if_key_doesnt_exist()
|
||||||
{
|
{
|
||||||
GivenIWantToRouteRequestsToMyUpstreamApi("api");
|
GivenIWantToRouteRequestsFromMyDownstreamBaseUrl("api");
|
||||||
WhenIRetrieveTheRouteByKey();
|
WhenIRetrieveTheRouteByKey();
|
||||||
ThenTheKeyDoesNotExist();
|
ThenTheKeyDoesNotExist();
|
||||||
}
|
}
|
||||||
@ -66,41 +66,41 @@ namespace Ocelot.UnitTests
|
|||||||
private void ThenTheKeyDoesNotExist()
|
private void ThenTheKeyDoesNotExist()
|
||||||
{
|
{
|
||||||
_getRouteResponse.ShouldNotBeNull();
|
_getRouteResponse.ShouldNotBeNull();
|
||||||
_getRouteResponse.ShouldBeOfType<ErrorResponse<Route>>();
|
_getRouteResponse.ShouldBeOfType<ErrorResponse<BaseUrlMap>>();
|
||||||
_getRouteResponse.Errors[0].Message.ShouldBe("This key does not exist");
|
_getRouteResponse.Errors[0].Message.ShouldBe("This key does not exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenIRetrieveTheRouteByKey()
|
private void WhenIRetrieveTheRouteByKey()
|
||||||
{
|
{
|
||||||
_getRouteResponse = _router.GetRoute(_apiKey);
|
_getRouteResponse = _repository.GetBaseUrlMap(_downstreamBaseUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheRouteIsReturned()
|
private void ThenTheRouteIsReturned()
|
||||||
{
|
{
|
||||||
_getRouteResponse.Data.DownstreamUrl.ShouldBe(_apiKey);
|
_getRouteResponse.Data.DownstreamBaseUrl.ShouldBe(_downstreamBaseUrl);
|
||||||
_getRouteResponse.Data.UpstreamUrl.ShouldBe(_upstreamApiUrl);
|
_getRouteResponse.Data.UpstreamBaseUrl.ShouldBe(_upstreamBaseUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenIHaveSetUpAnApiKeyAndUpstreamUrl(string apiKey, string upstreamUrl)
|
private void GivenIHaveSetUpAnApiKeyAndUpstreamUrl(string apiKey, string upstreamUrl)
|
||||||
{
|
{
|
||||||
GivenIHaveAnUpstreamApi(upstreamUrl);
|
GivenIHaveAnUpstreamBaseUrl(upstreamUrl);
|
||||||
GivenIWantToRouteRequestsToMyUpstreamApi(apiKey);
|
GivenIWantToRouteRequestsFromMyDownstreamBaseUrl(apiKey);
|
||||||
WhenIAddTheConfiguration();
|
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()
|
private void WhenIAddTheConfiguration()
|
||||||
{
|
{
|
||||||
_response = _router.AddRoute(_apiKey, _upstreamApiUrl);
|
_response = _repository.AddBaseUrlMap(new BaseUrlMap(_downstreamBaseUrl, _upstreamBaseUrl));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheResponseIsSuccesful()
|
private void ThenTheResponseIsSuccesful()
|
53
test/Ocelot.UnitTests/UpstreamBaseUrlFinderTests.cs
Normal file
53
test/Ocelot.UnitTests/UpstreamBaseUrlFinderTests.cs
Normal file
@ -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<string> _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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,28 +1,28 @@
|
|||||||
using Ocelot.Library.Infrastructure.Responses;
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
using Ocelot.Library.Infrastructure.Router.UrlPathRouter;
|
using Ocelot.Library.Infrastructure.UrlPathTemplateRepository;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Ocelot.UnitTests
|
namespace Ocelot.UnitTests
|
||||||
{
|
{
|
||||||
public class UrlPathRouterTests
|
public class UrlPathTemplateMapRepositoryTests
|
||||||
{
|
{
|
||||||
private string _upstreamUrlPath;
|
private string _upstreamUrlPath;
|
||||||
private string _downstreamUrlPath;
|
private string _downstreamUrlPath;
|
||||||
private IUrlPathRouter _router;
|
private IUrlPathTemplateMapRepository _repository;
|
||||||
private Response _response;
|
private Response _response;
|
||||||
private Response<UrlPath> _getResponse;
|
private Response<UrlPathTemplateMap> _getResponse;
|
||||||
|
|
||||||
public UrlPathRouterTests()
|
public UrlPathTemplateMapRepositoryTests()
|
||||||
{
|
{
|
||||||
_router = new InMemoryUrlPathRouter();
|
_repository = new InMemoryUrlPathTemplateMapRepository();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void can_add_url_path()
|
public void can_add_url_path()
|
||||||
{
|
{
|
||||||
GivenIHaveAnUpstreamUrlPath("api/products/products/{productId}");
|
GivenIHaveAnUpstreamUrlPath("/api/products/products/{productId}");
|
||||||
GivenIWantToRouteRequestsToMyUpstreamUrlPath("api/products/{productId}");
|
GivenIWantToRouteRequestsToMyUpstreamUrlPath("/api/products/{productId}");
|
||||||
WhenIAddTheConfiguration();
|
WhenIAddTheConfiguration();
|
||||||
ThenTheResponseIsSuccesful();
|
ThenTheResponseIsSuccesful();
|
||||||
}
|
}
|
||||||
@ -30,7 +30,7 @@ namespace Ocelot.UnitTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void can_get_url_path()
|
public void can_get_url_path()
|
||||||
{
|
{
|
||||||
GivenIHaveSetUpADownstreamUrlPathAndAnUpstreamUrlPath("api2", "http://www.someapi.com/api2");
|
GivenIHaveSetUpADownstreamUrlPathAndAnUpstreamUrlPath("/api2", "http://www.someapi.com/api2");
|
||||||
WhenIRetrieveTheUrlPathByDownstreamUrl();
|
WhenIRetrieveTheUrlPathByDownstreamUrl();
|
||||||
ThenTheUrlPathIsReturned();
|
ThenTheUrlPathIsReturned();
|
||||||
}
|
}
|
||||||
@ -38,7 +38,7 @@ namespace Ocelot.UnitTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_error_response_when_url_path_already_used()
|
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();
|
WhenITryToUseTheSameDownstreamUrl();
|
||||||
ThenTheDownstreamUrlAlreadyBeenUsed();
|
ThenTheDownstreamUrlAlreadyBeenUsed();
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ namespace Ocelot.UnitTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_error_response_if_key_doesnt_exist()
|
public void should_return_error_response_if_key_doesnt_exist()
|
||||||
{
|
{
|
||||||
GivenIWantToRouteRequestsToMyUpstreamUrlPath("api");
|
GivenIWantToRouteRequestsToMyUpstreamUrlPath("/api");
|
||||||
WhenIRetrieveTheUrlPathByDownstreamUrl();
|
WhenIRetrieveTheUrlPathByDownstreamUrl();
|
||||||
ThenTheKeyDoesNotExist();
|
ThenTheKeyDoesNotExist();
|
||||||
}
|
}
|
||||||
@ -66,13 +66,13 @@ namespace Ocelot.UnitTests
|
|||||||
private void ThenTheKeyDoesNotExist()
|
private void ThenTheKeyDoesNotExist()
|
||||||
{
|
{
|
||||||
_getResponse.ShouldNotBeNull();
|
_getResponse.ShouldNotBeNull();
|
||||||
_getResponse.ShouldBeOfType<ErrorResponse<UrlPath>>();
|
_getResponse.ShouldBeOfType<ErrorResponse<UrlPathTemplateMap>>();
|
||||||
_getResponse.Errors[0].Message.ShouldBe("This key does not exist");
|
_getResponse.Errors[0].Message.ShouldBe("This key does not exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenIRetrieveTheUrlPathByDownstreamUrl()
|
private void WhenIRetrieveTheUrlPathByDownstreamUrl()
|
||||||
{
|
{
|
||||||
_getResponse = _router.GetRoute(_downstreamUrlPath);
|
_getResponse = _repository.GetUrlPathTemplateMap(_downstreamUrlPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheUrlPathIsReturned()
|
private void ThenTheUrlPathIsReturned()
|
||||||
@ -100,7 +100,7 @@ namespace Ocelot.UnitTests
|
|||||||
|
|
||||||
private void WhenIAddTheConfiguration()
|
private void WhenIAddTheConfiguration()
|
||||||
{
|
{
|
||||||
_response = _router.AddRoute(_downstreamUrlPath, _upstreamUrlPath);
|
_response = _repository.AddUrlPathTemplateMap(new UrlPathTemplateMap(_downstreamUrlPath, _upstreamUrlPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheResponseIsSuccesful()
|
private void ThenTheResponseIsSuccesful()
|
@ -1,7 +1,4 @@
|
|||||||
using System;
|
using Ocelot.Library.Infrastructure.UrlPathMatcher;
|
||||||
using Ocelot.Library.Infrastructure.Responses;
|
|
||||||
using Ocelot.Library.Infrastructure.Router.UpstreamRouter;
|
|
||||||
using Ocelot.Library.Infrastructure.Router.UrlPathMatcher;
|
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user