mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:42:50 +08:00
removed thing that checks if route is authorised cos we dont need it
This commit is contained in:
parent
8c194a365b
commit
f545ba8620
@ -1,11 +0,0 @@
|
|||||||
namespace Ocelot.Library.Infrastructure.Authentication
|
|
||||||
{
|
|
||||||
using Responses;
|
|
||||||
public class CouldNotFindConfigurationError : Error
|
|
||||||
{
|
|
||||||
public CouldNotFindConfigurationError(string message)
|
|
||||||
: base(message)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
namespace Ocelot.Library.Infrastructure.Authentication
|
|
||||||
{
|
|
||||||
using DownstreamRouteFinder;
|
|
||||||
using Responses;
|
|
||||||
|
|
||||||
public interface IRouteRequiresAuthentication
|
|
||||||
{
|
|
||||||
Response<bool> IsAuthenticated(DownstreamRoute downstreamRoute, string httpMethod);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
namespace Ocelot.Library.Infrastructure.Authentication
|
|
||||||
{
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using Configuration;
|
|
||||||
using DownstreamRouteFinder;
|
|
||||||
using Responses;
|
|
||||||
|
|
||||||
public class RouteRequiresAuthentication : IRouteRequiresAuthentication
|
|
||||||
{
|
|
||||||
private readonly IOcelotConfiguration _configuration;
|
|
||||||
|
|
||||||
public RouteRequiresAuthentication(IOcelotConfiguration configuration)
|
|
||||||
{
|
|
||||||
_configuration = configuration;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Response<bool> IsAuthenticated(DownstreamRoute downstreamRoute, string httpMethod)
|
|
||||||
{
|
|
||||||
var reRoute =
|
|
||||||
_configuration.ReRoutes.FirstOrDefault(
|
|
||||||
x =>
|
|
||||||
x.DownstreamTemplate == downstreamRoute.DownstreamUrlTemplate &&
|
|
||||||
string.Equals(x.UpstreamHttpMethod, httpMethod, StringComparison.CurrentCultureIgnoreCase));
|
|
||||||
|
|
||||||
if (reRoute == null)
|
|
||||||
{
|
|
||||||
return new ErrorResponse<bool>(new List<Error> {new CouldNotFindConfigurationError($"Could not find configuration for {downstreamRoute.DownstreamUrlTemplate} using method {httpMethod}")});
|
|
||||||
}
|
|
||||||
|
|
||||||
return new OkResponse<bool>(reRoute.IsAuthenticated);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
47
src/Ocelot.Library/Infrastructure/Builder/ReRouteBuilder.cs
Normal file
47
src/Ocelot.Library/Infrastructure/Builder/ReRouteBuilder.cs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
namespace Ocelot.Library.Infrastructure.Builder
|
||||||
|
{
|
||||||
|
using Configuration;
|
||||||
|
|
||||||
|
public class ReRouteBuilder
|
||||||
|
{
|
||||||
|
private string _downstreamTemplate;
|
||||||
|
private string _upstreamTemplate;
|
||||||
|
private string _upstreamTemplatePattern;
|
||||||
|
private string _upstreamHttpMethod;
|
||||||
|
private bool _isAuthenticated;
|
||||||
|
private string _authenticationProvider;
|
||||||
|
|
||||||
|
public void WithDownstreamTemplate(string input)
|
||||||
|
{
|
||||||
|
_downstreamTemplate = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WithUpstreamTemplate(string input)
|
||||||
|
{
|
||||||
|
_upstreamTemplate = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WithUpstreamTemplatePattern(string input)
|
||||||
|
{
|
||||||
|
_upstreamTemplatePattern = input;
|
||||||
|
}
|
||||||
|
public void WithUpstreamHttpMethod(string input)
|
||||||
|
{
|
||||||
|
_upstreamHttpMethod = input;
|
||||||
|
}
|
||||||
|
public void WithIsAuthenticated(bool input)
|
||||||
|
{
|
||||||
|
_isAuthenticated = input;
|
||||||
|
|
||||||
|
}
|
||||||
|
public void WithAuthenticationProvider(string input)
|
||||||
|
{
|
||||||
|
_authenticationProvider = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReRoute Build()
|
||||||
|
{
|
||||||
|
return new ReRoute(_downstreamTemplate, _upstreamTemplate, _upstreamHttpMethod, _upstreamTemplatePattern, _isAuthenticated, _authenticationProvider);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -45,7 +45,7 @@ namespace Ocelot.Library.Infrastructure.Configuration
|
|||||||
|
|
||||||
var isAuthenticated = !string.IsNullOrEmpty(reRoute.Authentication);
|
var isAuthenticated = !string.IsNullOrEmpty(reRoute.Authentication);
|
||||||
|
|
||||||
_reRoutes.Add(new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated));
|
_reRoutes.Add(new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated, reRoute.Authentication));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,13 +2,14 @@
|
|||||||
{
|
{
|
||||||
public class ReRoute
|
public class ReRoute
|
||||||
{
|
{
|
||||||
public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated)
|
public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated, string authenticationProvider)
|
||||||
{
|
{
|
||||||
DownstreamTemplate = downstreamTemplate;
|
DownstreamTemplate = downstreamTemplate;
|
||||||
UpstreamTemplate = upstreamTemplate;
|
UpstreamTemplate = upstreamTemplate;
|
||||||
UpstreamHttpMethod = upstreamHttpMethod;
|
UpstreamHttpMethod = upstreamHttpMethod;
|
||||||
UpstreamTemplatePattern = upstreamTemplatePattern;
|
UpstreamTemplatePattern = upstreamTemplatePattern;
|
||||||
IsAuthenticated = isAuthenticated;
|
IsAuthenticated = isAuthenticated;
|
||||||
|
AuthenticationProvider = authenticationProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string DownstreamTemplate { get; private set; }
|
public string DownstreamTemplate { get; private set; }
|
||||||
@ -16,5 +17,6 @@
|
|||||||
public string UpstreamTemplatePattern { get; private set; }
|
public string UpstreamTemplatePattern { get; private set; }
|
||||||
public string UpstreamHttpMethod { get; private set; }
|
public string UpstreamHttpMethod { get; private set; }
|
||||||
public bool IsAuthenticated { get; private set; }
|
public bool IsAuthenticated { get; private set; }
|
||||||
|
public string AuthenticationProvider { get; private set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,14 +3,16 @@ using Ocelot.Library.Infrastructure.UrlMatcher;
|
|||||||
|
|
||||||
namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder
|
namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder
|
||||||
{
|
{
|
||||||
|
using Configuration;
|
||||||
|
|
||||||
public class DownstreamRoute
|
public class DownstreamRoute
|
||||||
{
|
{
|
||||||
public DownstreamRoute(List<TemplateVariableNameAndValue> templateVariableNameAndValues, string downstreamUrlTemplate)
|
public DownstreamRoute(List<TemplateVariableNameAndValue> templateVariableNameAndValues, ReRoute reRoute)
|
||||||
{
|
{
|
||||||
TemplateVariableNameAndValues = templateVariableNameAndValues;
|
TemplateVariableNameAndValues = templateVariableNameAndValues;
|
||||||
DownstreamUrlTemplate = downstreamUrlTemplate;
|
ReRoute = reRoute;
|
||||||
}
|
}
|
||||||
public List<TemplateVariableNameAndValue> TemplateVariableNameAndValues { get; private set; }
|
public List<TemplateVariableNameAndValue> TemplateVariableNameAndValues { get; private set; }
|
||||||
public string DownstreamUrlTemplate { get; private set; }
|
public ReRoute ReRoute { get; private set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -32,7 +32,7 @@ namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder
|
|||||||
var templateVariableNameAndValues = _templateVariableNameAndValueFinder.Find(upstreamUrlPath,
|
var templateVariableNameAndValues = _templateVariableNameAndValueFinder.Find(upstreamUrlPath,
|
||||||
template.UpstreamTemplate);
|
template.UpstreamTemplate);
|
||||||
|
|
||||||
return new OkResponse<DownstreamRoute>(new DownstreamRoute(templateVariableNameAndValues.Data, template.DownstreamTemplate));
|
return new OkResponse<DownstreamRoute>(new DownstreamRoute(templateVariableNameAndValues.Data, template));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer
|
|||||||
{
|
{
|
||||||
var upstreamUrl = new StringBuilder();
|
var upstreamUrl = new StringBuilder();
|
||||||
|
|
||||||
upstreamUrl.Append(downstreamRoute.DownstreamUrlTemplate);
|
upstreamUrl.Append(downstreamRoute.ReRoute.DownstreamTemplate);
|
||||||
|
|
||||||
foreach (var templateVarAndValue in downstreamRoute.TemplateVariableNameAndValues)
|
foreach (var templateVarAndValue in downstreamRoute.TemplateVariableNameAndValues)
|
||||||
{
|
{
|
||||||
|
@ -1,41 +1,35 @@
|
|||||||
namespace Ocelot.Library.Middleware
|
namespace Ocelot.Library.Middleware
|
||||||
{
|
{
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Infrastructure.Authentication;
|
using Infrastructure.Configuration;
|
||||||
using Infrastructure.DownstreamRouteFinder;
|
using Infrastructure.DownstreamRouteFinder;
|
||||||
using Infrastructure.Repository;
|
using Infrastructure.Repository;
|
||||||
using Infrastructure.Responses;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
public class AuthenticationMiddleware : OcelotMiddleware
|
public class AuthenticationMiddleware : OcelotMiddleware
|
||||||
{
|
{
|
||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
|
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
|
||||||
private readonly IRouteRequiresAuthentication _requiresAuthentication;
|
|
||||||
|
|
||||||
public AuthenticationMiddleware(RequestDelegate next,
|
public AuthenticationMiddleware(RequestDelegate next,
|
||||||
IScopedRequestDataRepository scopedRequestDataRepository,
|
IScopedRequestDataRepository scopedRequestDataRepository)
|
||||||
IRouteRequiresAuthentication requiresAuthentication)
|
|
||||||
: base(scopedRequestDataRepository)
|
: base(scopedRequestDataRepository)
|
||||||
{
|
{
|
||||||
_next = next;
|
_next = next;
|
||||||
_scopedRequestDataRepository = scopedRequestDataRepository;
|
_scopedRequestDataRepository = scopedRequestDataRepository;
|
||||||
_requiresAuthentication = requiresAuthentication;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Invoke(HttpContext context)
|
public async Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
var downstreamRoute = _scopedRequestDataRepository.Get<DownstreamRoute>("DownstreamRoute");
|
var downstreamRoute = _scopedRequestDataRepository.Get<DownstreamRoute>("DownstreamRoute");
|
||||||
|
|
||||||
var isAuthenticated = _requiresAuthentication.IsAuthenticated(downstreamRoute.Data, context.Request.Method);
|
if (downstreamRoute.IsError)
|
||||||
|
|
||||||
if (isAuthenticated.IsError)
|
|
||||||
{
|
{
|
||||||
SetPipelineError(downstreamRoute.Errors);
|
SetPipelineError(downstreamRoute.Errors);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsAuthenticatedRoute(isAuthenticated))
|
if (IsAuthenticatedRoute(downstreamRoute.Data.ReRoute))
|
||||||
{
|
{
|
||||||
//todo - build auth pipeline and then call normal pipeline if all good?
|
//todo - build auth pipeline and then call normal pipeline if all good?
|
||||||
await _next.Invoke(context);
|
await _next.Invoke(context);
|
||||||
@ -46,9 +40,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsAuthenticatedRoute(Response<bool> isAuthenticated)
|
private static bool IsAuthenticatedRoute(ReRoute reRoute)
|
||||||
{
|
{
|
||||||
return isAuthenticated.Data;
|
return reRoute.IsAuthenticated;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,6 @@ using Ocelot.Library.Middleware;
|
|||||||
|
|
||||||
namespace Ocelot
|
namespace Ocelot
|
||||||
{
|
{
|
||||||
using Library.Infrastructure.Authentication;
|
|
||||||
|
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
public Startup(IHostingEnvironment env)
|
public Startup(IHostingEnvironment env)
|
||||||
@ -54,7 +52,6 @@ namespace Ocelot
|
|||||||
services.AddSingleton<IHttpRequester, HttpClientHttpRequester>();
|
services.AddSingleton<IHttpRequester, HttpClientHttpRequester>();
|
||||||
services.AddSingleton<IHttpResponder, HttpContextResponder>();
|
services.AddSingleton<IHttpResponder, HttpContextResponder>();
|
||||||
services.AddSingleton<IRequestBuilder, HttpRequestBuilder>();
|
services.AddSingleton<IRequestBuilder, HttpRequestBuilder>();
|
||||||
services.AddSingleton<IRouteRequiresAuthentication, RouteRequiresAuthentication>();
|
|
||||||
|
|
||||||
// see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc
|
// see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc
|
||||||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||||
|
@ -1,96 +0,0 @@
|
|||||||
namespace Ocelot.UnitTests.Authentication
|
|
||||||
{
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Library.Infrastructure.Authentication;
|
|
||||||
using Library.Infrastructure.Configuration;
|
|
||||||
using Library.Infrastructure.DownstreamRouteFinder;
|
|
||||||
using Library.Infrastructure.Responses;
|
|
||||||
using Library.Infrastructure.UrlMatcher;
|
|
||||||
using Moq;
|
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
public class RequiresAuthenticationTests
|
|
||||||
{
|
|
||||||
private readonly RouteRequiresAuthentication _routeRequiresAuthentication;
|
|
||||||
private string _url;
|
|
||||||
private readonly Mock<IOcelotConfiguration> _config;
|
|
||||||
private Response<bool> _result;
|
|
||||||
private string _httpMethod;
|
|
||||||
|
|
||||||
public RequiresAuthenticationTests()
|
|
||||||
{
|
|
||||||
_config = new Mock<IOcelotConfiguration>();
|
|
||||||
_routeRequiresAuthentication = new RouteRequiresAuthentication(_config.Object);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_return_true_if_route_requires_authentication()
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenIHaveADownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(
|
|
||||||
x =>
|
|
||||||
x.GivenTheConfigurationForTheRouteIs(new ReRoute("http://www.bbc.co.uk", "/api/poo", "get",
|
|
||||||
"/api/poo$", true)))
|
|
||||||
.When(x => x.WhenICheckToSeeIfTheRouteShouldBeAuthenticated())
|
|
||||||
.Then(x => x.ThenTheResultIs(true))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_return_false_if_route_requires_authentication()
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenIHaveADownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(
|
|
||||||
x =>
|
|
||||||
x.GivenTheConfigurationForTheRouteIs(new ReRoute("http://www.bbc.co.uk", "/api/poo", "get",
|
|
||||||
"/api/poo$", false)))
|
|
||||||
.When(x => x.WhenICheckToSeeIfTheRouteShouldBeAuthenticated())
|
|
||||||
.Then(x => x.ThenTheResultIs(false))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_return_error_if_no_matching_config()
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenIHaveADownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(x => x.GivenTheConfigurationForTheRouteIs(new ReRoute(string.Empty, string.Empty, string.Empty, string.Empty,false)))
|
|
||||||
.When(x => x.WhenICheckToSeeIfTheRouteShouldBeAuthenticated())
|
|
||||||
.Then(x => x.ThenAnErrorIsReturned())
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenAnErrorIsReturned()
|
|
||||||
{
|
|
||||||
_result.IsError.ShouldBeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void GivenIHaveADownstreamUrl(string url)
|
|
||||||
{
|
|
||||||
_url = url;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheConfigurationForTheRouteIs(ReRoute reRoute)
|
|
||||||
{
|
|
||||||
_httpMethod = reRoute.UpstreamHttpMethod;
|
|
||||||
|
|
||||||
_config
|
|
||||||
.Setup(x => x.ReRoutes)
|
|
||||||
.Returns(new List<ReRoute>
|
|
||||||
{
|
|
||||||
reRoute
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WhenICheckToSeeIfTheRouteShouldBeAuthenticated()
|
|
||||||
{
|
|
||||||
_result = _routeRequiresAuthentication.IsAuthenticated(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), _url), _httpMethod);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheResultIs(bool expected)
|
|
||||||
{
|
|
||||||
_result.Data.ShouldBe(expected);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -38,7 +38,7 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||||
{
|
{
|
||||||
new ReRoute("/products/{productId}","/api/products/{productId}", "Get", "/api/products/.*$", false)
|
new ReRoute("/products/{productId}","/api/products/{productId}", "Get", "/api/products/.*$", false, "")
|
||||||
}))
|
}))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
@ -61,7 +61,7 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||||
{
|
{
|
||||||
new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}", "Get", "/api/products/.*/variants/.*$", false)
|
new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}", "Get", "/api/products/.*/variants/.*$", false, "")
|
||||||
}))
|
}))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
@ -84,7 +84,7 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||||
{
|
{
|
||||||
new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}/", "Get", "/api/products/.*/variants/.*/$", false)
|
new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}/", "Get", "/api/products/.*/variants/.*/$", false, "")
|
||||||
}))
|
}))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
@ -107,7 +107,7 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||||
{
|
{
|
||||||
new ReRoute("/api/products/","/", "Get", "/$", false)
|
new ReRoute("/api/products/","/", "Get", "/$", false, "")
|
||||||
}))
|
}))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
@ -37,14 +37,14 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
|
|||||||
.And(x => x.GivenTheTemplateVariableAndNameFinderReturns(new OkResponse<List<TemplateVariableNameAndValue>>(new List<TemplateVariableNameAndValue>())))
|
.And(x => x.GivenTheTemplateVariableAndNameFinderReturns(new OkResponse<List<TemplateVariableNameAndValue>>(new List<TemplateVariableNameAndValue>())))
|
||||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||||
{
|
{
|
||||||
new ReRoute("someDownstreamPath","someUpstreamPath", "Get", "someUpstreamPath", false)
|
new ReRoute("someDownstreamPath","someUpstreamPath", "Get", "someUpstreamPath", false, "")
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Get"))
|
.And(x => x.GivenTheUpstreamHttpMethodIs("Get"))
|
||||||
.When(x => x.WhenICallTheFinder())
|
.When(x => x.WhenICallTheFinder())
|
||||||
.Then(
|
.Then(
|
||||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "someDownstreamPath")))
|
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRoute("someDownstreamPath","","","",false, ""))))
|
||||||
.And(x => x.ThenTheUrlMatcherIsCalledCorrectly())
|
.And(x => x.ThenTheUrlMatcherIsCalledCorrectly())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
@ -56,15 +56,15 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
|
|||||||
.And(x => x.GivenTheTemplateVariableAndNameFinderReturns(new OkResponse<List<TemplateVariableNameAndValue>>(new List<TemplateVariableNameAndValue>())))
|
.And(x => x.GivenTheTemplateVariableAndNameFinderReturns(new OkResponse<List<TemplateVariableNameAndValue>>(new List<TemplateVariableNameAndValue>())))
|
||||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||||
{
|
{
|
||||||
new ReRoute("someDownstreamPath", "someUpstreamPath", "Get", string.Empty, false),
|
new ReRoute("someDownstreamPath", "someUpstreamPath", "Get", string.Empty, false, ""),
|
||||||
new ReRoute("someDownstreamPathForAPost", "someUpstreamPath", "Post", string.Empty, false)
|
new ReRoute("someDownstreamPathForAPost", "someUpstreamPath", "Post", string.Empty, false, "")
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||||
.When(x => x.WhenICallTheFinder())
|
.When(x => x.WhenICallTheFinder())
|
||||||
.Then(
|
.Then(
|
||||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "someDownstreamPathForAPost")))
|
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRoute("someDownstreamPathForAPost", "","","",false, ""))))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
|
|||||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("somePath"))
|
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("somePath"))
|
||||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||||
{
|
{
|
||||||
new ReRoute("somPath", "somePath", "Get", "somePath", false)
|
new ReRoute("somPath", "somePath", "Get", "somePath", false, "")
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(false))))
|
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(false))))
|
||||||
@ -137,7 +137,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
|
|||||||
|
|
||||||
private void ThenTheFollowingIsReturned(DownstreamRoute expected)
|
private void ThenTheFollowingIsReturned(DownstreamRoute expected)
|
||||||
{
|
{
|
||||||
_result.Data.DownstreamUrlTemplate.ShouldBe(expected.DownstreamUrlTemplate);
|
_result.Data.ReRoute.DownstreamTemplate.ShouldBe(expected.ReRoute.DownstreamTemplate);
|
||||||
|
|
||||||
for (int i = 0; i < _result.Data.TemplateVariableNameAndValues.Count; i++)
|
for (int i = 0; i < _result.Data.TemplateVariableNameAndValues.Count; i++)
|
||||||
{
|
{
|
||||||
|
@ -6,7 +6,6 @@ using Microsoft.AspNetCore.Hosting;
|
|||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Library.Infrastructure.Authentication;
|
|
||||||
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
|
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
|
||||||
using Ocelot.Library.Infrastructure.Repository;
|
using Ocelot.Library.Infrastructure.Repository;
|
||||||
using Ocelot.Library.Infrastructure.Responses;
|
using Ocelot.Library.Infrastructure.Responses;
|
||||||
@ -17,9 +16,10 @@ using Xunit;
|
|||||||
|
|
||||||
namespace Ocelot.UnitTests.Middleware
|
namespace Ocelot.UnitTests.Middleware
|
||||||
{
|
{
|
||||||
|
using Library.Infrastructure.Configuration;
|
||||||
|
|
||||||
public class AuthenticationMiddlewareTests : IDisposable
|
public class AuthenticationMiddlewareTests : IDisposable
|
||||||
{
|
{
|
||||||
private readonly Mock<IRouteRequiresAuthentication> _requiresAuth;
|
|
||||||
private readonly Mock<IScopedRequestDataRepository> _scopedRepository;
|
private readonly Mock<IScopedRequestDataRepository> _scopedRepository;
|
||||||
private readonly string _url;
|
private readonly string _url;
|
||||||
private readonly TestServer _server;
|
private readonly TestServer _server;
|
||||||
@ -30,13 +30,11 @@ namespace Ocelot.UnitTests.Middleware
|
|||||||
public AuthenticationMiddlewareTests()
|
public AuthenticationMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
_url = "http://localhost:51879";
|
||||||
_requiresAuth = new Mock<IRouteRequiresAuthentication>();
|
|
||||||
_scopedRepository = new Mock<IScopedRequestDataRepository>();
|
_scopedRepository = new Mock<IScopedRequestDataRepository>();
|
||||||
|
|
||||||
var builder = new WebHostBuilder()
|
var builder = new WebHostBuilder()
|
||||||
.ConfigureServices(x =>
|
.ConfigureServices(x =>
|
||||||
{
|
{
|
||||||
x.AddSingleton(_requiresAuth.Object);
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
x.AddSingleton(_scopedRepository.Object);
|
||||||
})
|
})
|
||||||
.UseUrls(_url)
|
.UseUrls(_url)
|
||||||
@ -56,8 +54,7 @@ namespace Ocelot.UnitTests.Middleware
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void happy_path()
|
public void happy_path()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "any old string")))
|
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRoute("","","","",false, ""))))
|
||||||
.And(x => x.GivenTheRouteIsNotAuthenticated())
|
|
||||||
.When(x => x.WhenICallTheMiddleware())
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
.Then(x => x.ThenNoExceptionsAreThrown())
|
.Then(x => x.ThenNoExceptionsAreThrown())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
@ -76,13 +73,6 @@ namespace Ocelot.UnitTests.Middleware
|
|||||||
.Returns(_downstreamRoute);
|
.Returns(_downstreamRoute);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheRouteIsNotAuthenticated()
|
|
||||||
{
|
|
||||||
_requiresAuth
|
|
||||||
.Setup(x => x.IsAuthenticated(It.IsAny<DownstreamRoute>(), It.IsAny<string>()))
|
|
||||||
.Returns(new OkResponse<bool>(false));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
private void WhenICallTheMiddleware()
|
||||||
{
|
{
|
||||||
_result = _client.GetAsync(_url).Result;
|
_result = _client.GetAsync(_url).Result;
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using Library.Infrastructure.Configuration;
|
||||||
using Library.Infrastructure.DownstreamRouteFinder;
|
using Library.Infrastructure.DownstreamRouteFinder;
|
||||||
using Library.Infrastructure.Repository;
|
using Library.Infrastructure.Repository;
|
||||||
using Library.Infrastructure.Responses;
|
using Library.Infrastructure.Responses;
|
||||||
@ -55,7 +56,7 @@
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void happy_path()
|
public void happy_path()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenTheDownStreamRouteFinderReturns(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "any old string")))
|
this.Given(x => x.GivenTheDownStreamRouteFinderReturns(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRoute("any old string", "", "", "",false, ""))))
|
||||||
.When(x => x.WhenICallTheMiddleware())
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using Library.Infrastructure.Configuration;
|
||||||
using Library.Infrastructure.DownstreamRouteFinder;
|
using Library.Infrastructure.DownstreamRouteFinder;
|
||||||
using Library.Infrastructure.Repository;
|
using Library.Infrastructure.Repository;
|
||||||
using Library.Infrastructure.Responses;
|
using Library.Infrastructure.Responses;
|
||||||
@ -57,7 +58,7 @@
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void happy_path()
|
public void happy_path()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "any old string")))
|
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRoute("any old string", "", "", "", false, ""))))
|
||||||
.And(x => x.TheUrlReplacerReturns("any old string"))
|
.And(x => x.TheUrlReplacerReturns("any old string"))
|
||||||
.When(x => x.WhenICallTheMiddleware())
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||||
|
@ -9,6 +9,8 @@ using Xunit;
|
|||||||
|
|
||||||
namespace Ocelot.UnitTests.UrlTemplateReplacer
|
namespace Ocelot.UnitTests.UrlTemplateReplacer
|
||||||
{
|
{
|
||||||
|
using Library.Infrastructure.Configuration;
|
||||||
|
|
||||||
public class UpstreamUrlPathTemplateVariableReplacerTests
|
public class UpstreamUrlPathTemplateVariableReplacerTests
|
||||||
{
|
{
|
||||||
private DownstreamRoute _downstreamRoute;
|
private DownstreamRoute _downstreamRoute;
|
||||||
@ -23,7 +25,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void can_replace_no_template_variables()
|
public void can_replace_no_template_variables()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "")))
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRoute("", "", "", "", false, ""))))
|
||||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned(""))
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned(""))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
@ -32,7 +34,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void can_replace_no_template_variables_with_slash()
|
public void can_replace_no_template_variables_with_slash()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "/")))
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRoute("/", "", "", "", false, ""))))
|
||||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("/"))
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("/"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
@ -41,7 +43,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void can_replace_url_no_slash()
|
public void can_replace_url_no_slash()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "api")))
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRoute("api", "", "", "", false, ""))))
|
||||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api"))
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
@ -50,7 +52,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void can_replace_url_one_slash()
|
public void can_replace_url_one_slash()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "api/")))
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRoute("api/", "", "", "", false, ""))))
|
||||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/"))
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
@ -59,7 +61,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void can_replace_url_multiple_slash()
|
public void can_replace_url_multiple_slash()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "api/product/products/")))
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRoute("api/product/products/", "", "", "", false, ""))))
|
||||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/product/products/"))
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/product/products/"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
@ -73,7 +75,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer
|
|||||||
new TemplateVariableNameAndValue("{productId}", "1")
|
new TemplateVariableNameAndValue("{productId}", "1")
|
||||||
};
|
};
|
||||||
|
|
||||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, "productservice/products/{productId}/")))
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, new ReRoute("productservice/products/{productId}/", "", "", "", false, ""))))
|
||||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/"))
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
@ -87,7 +89,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer
|
|||||||
new TemplateVariableNameAndValue("{productId}", "1")
|
new TemplateVariableNameAndValue("{productId}", "1")
|
||||||
};
|
};
|
||||||
|
|
||||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, "productservice/products/{productId}/variants")))
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, new ReRoute("productservice/products/{productId}/variants", "", "", "", false, ""))))
|
||||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants"))
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
@ -102,7 +104,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer
|
|||||||
new TemplateVariableNameAndValue("{variantId}", "12")
|
new TemplateVariableNameAndValue("{variantId}", "12")
|
||||||
};
|
};
|
||||||
|
|
||||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, "productservice/products/{productId}/variants/{variantId}")))
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, new ReRoute("productservice/products/{productId}/variants/{variantId}", "", "", "", false, ""))))
|
||||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants/12"))
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants/12"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
@ -118,7 +120,7 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer
|
|||||||
new TemplateVariableNameAndValue("{categoryId}", "34")
|
new TemplateVariableNameAndValue("{categoryId}", "34")
|
||||||
};
|
};
|
||||||
|
|
||||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, "productservice/category/{categoryId}/products/{productId}/variants/{variantId}")))
|
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables, new ReRoute("productservice/category/{categoryId}/products/{productId}/variants/{variantId}", "", "", "", false, ""))))
|
||||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/category/34/products/1/variants/12"))
|
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/category/34/products/1/variants/12"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
@ -138,6 +140,5 @@ namespace Ocelot.UnitTests.UrlTemplateReplacer
|
|||||||
{
|
{
|
||||||
_result.Data.ShouldBe(expected);
|
_result.Data.ShouldBe(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user