mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 11:58:15 +08:00
added a base url finder
This commit is contained in:
@ -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<Route> _getRouteResponse;
|
||||
|
||||
public RouterTests()
|
||||
private Response<BaseUrlMap> _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<ErrorResponse<Route>>();
|
||||
_getRouteResponse.ShouldBeOfType<ErrorResponse<BaseUrlMap>>();
|
||||
_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()
|
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.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<UrlPath> _getResponse;
|
||||
private Response<UrlPathTemplateMap> _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<ErrorResponse<UrlPath>>();
|
||||
_getResponse.ShouldBeOfType<ErrorResponse<UrlPathTemplateMap>>();
|
||||
_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()
|
@ -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
|
||||
|
Reference in New Issue
Block a user