mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 03:18:15 +08:00
Can authorise routes based on claims, there is also a claims transformation middleware
This commit is contained in:
@ -74,11 +74,15 @@ namespace Ocelot.AcceptanceTests
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
},
|
||||
AddClaims =
|
||||
AddClaimsToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
},
|
||||
RouteClaimsRequirement =
|
||||
{
|
||||
{"UserType", "registered"}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -91,26 +95,66 @@ namespace Ocelot.AcceptanceTests
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_403_authorising_route()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura"))
|
||||
.And(x => x.GivenIHaveAToken("http://localhost:51888"))
|
||||
.And(x => x.GivenThereIsAConfiguration(new YamlConfiguration
|
||||
{
|
||||
ReRoutes = new List<YamlReRoute>
|
||||
{
|
||||
new YamlReRoute
|
||||
{
|
||||
DownstreamTemplate = "http://localhost:51876/",
|
||||
UpstreamTemplate = "/",
|
||||
UpstreamHttpMethod = "Get",
|
||||
AuthenticationOptions = new YamlAuthenticationOptions
|
||||
{
|
||||
AdditionalScopes = new List<string>(),
|
||||
Provider = "IdentityServer",
|
||||
ProviderRootUrl = "http://localhost:51888",
|
||||
RequireHttps = false,
|
||||
ScopeName = "api",
|
||||
ScopeSecret = "secret"
|
||||
},
|
||||
AddHeadersToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"LocationId", "Claims[LocationId] > value"},
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
},
|
||||
AddClaimsToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
},
|
||||
RouteClaimsRequirement =
|
||||
{
|
||||
{"UserType", "registered"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheApiGatewayIsRunning())
|
||||
.And(x => x.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => x.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.Forbidden))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void WhenIGetUrlOnTheApiGateway(string url)
|
||||
{
|
||||
_response = _ocelotClient.GetAsync(url).Result;
|
||||
}
|
||||
|
||||
private void WhenIPostUrlOnTheApiGateway(string url)
|
||||
{
|
||||
_response = _ocelotClient.PostAsync(url, _postContent).Result;
|
||||
}
|
||||
|
||||
private void ThenTheResponseBodyShouldBe(string expectedBody)
|
||||
{
|
||||
_response.Content.ReadAsStringAsync().Result.ShouldBe(expectedBody);
|
||||
}
|
||||
|
||||
private void GivenThePostHasContent(string postcontent)
|
||||
{
|
||||
_postContent = new StringContent(postcontent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is annoying cos it should be in the constructor but we need to set up the yaml file before calling startup so its a step.
|
||||
/// </summary>
|
||||
@ -184,7 +228,8 @@ namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
Value = "secret".Sha256()
|
||||
}
|
||||
}
|
||||
},
|
||||
IncludeAllClaimsForUser = true
|
||||
},
|
||||
|
||||
StandardScopes.OpenId,
|
||||
|
@ -18,7 +18,9 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
public class AuthorizationMiddlewareTests : IDisposable
|
||||
using Authorisation.Middleware;
|
||||
|
||||
public class AuthorisationMiddlewareTests : IDisposable
|
||||
{
|
||||
private readonly Mock<IScopedRequestDataRepository> _scopedRepository;
|
||||
private readonly Mock<IAuthoriser> _authService;
|
||||
@ -28,7 +30,7 @@ namespace Ocelot.UnitTests.Authorization
|
||||
private HttpResponseMessage _result;
|
||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||
|
||||
public AuthorizationMiddlewareTests()
|
||||
public AuthorisationMiddlewareTests()
|
||||
{
|
||||
_url = "http://localhost:51879";
|
||||
_scopedRepository = new Mock<IScopedRequestDataRepository>();
|
||||
@ -56,18 +58,17 @@ namespace Ocelot.UnitTests.Authorization
|
||||
[Fact]
|
||||
public void happy_path()
|
||||
{
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRouteBuilder().Build())))
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), new ReRouteBuilder().WithIsAuthorised(true).Build())))
|
||||
.And(x => x.GivenTheAuthServiceReturns(new OkResponse<bool>(true)))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
//todo stick this back in
|
||||
//.Then(x => x.ThenTheAuthServiceIsCalledCorrectly())
|
||||
.Then(x => x.ThenTheAuthServiceIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheAuthServiceReturns(Response<bool> expected)
|
||||
{
|
||||
_authService
|
||||
.Setup(x => x.Authorise(It.IsAny<ClaimsPrincipal>(), It.IsAny<RouteClaimsRequirement>()))
|
||||
.Setup(x => x.Authorise(It.IsAny<ClaimsPrincipal>(), It.IsAny<Dictionary<string, string>>()))
|
||||
.Returns(expected);
|
||||
}
|
||||
|
||||
@ -75,7 +76,7 @@ namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
_authService
|
||||
.Verify(x => x.Authorise(It.IsAny<ClaimsPrincipal>(),
|
||||
It.IsAny<RouteClaimsRequirement>()), Times.Once);
|
||||
It.IsAny<Dictionary<string, string>>()), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
@ -1,7 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Ocelot.Authorisation;
|
||||
using Ocelot.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
@ -9,11 +8,13 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
|
||||
public class ClaimsAuthoriserTests
|
||||
{
|
||||
private readonly ClaimsAuthoriser _claimsAuthoriser;
|
||||
private ClaimsPrincipal _claimsPrincipal;
|
||||
private RouteClaimsRequirement _requirement;
|
||||
private Dictionary<string, string> _requirement;
|
||||
private Response<bool> _result;
|
||||
|
||||
public ClaimsAuthoriserTests()
|
||||
@ -28,10 +29,10 @@ namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
new Claim("UserType", "registered")
|
||||
}))))
|
||||
.And(x => x.GivenARouteClaimsRequirement(new RouteClaimsRequirement(new Dictionary<string, string>
|
||||
.And(x => x.GivenARouteClaimsRequirement(new Dictionary<string, string>
|
||||
{
|
||||
{"UserType", "registered"}
|
||||
})))
|
||||
}))
|
||||
.When(x => x.WhenICallTheAuthoriser())
|
||||
.Then(x => x.ThenTheUserIsAuthorised())
|
||||
.BDDfy();
|
||||
@ -41,10 +42,10 @@ namespace Ocelot.UnitTests.Authorization
|
||||
public void should_not_authorise_user()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimsPrincipal(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>()))))
|
||||
.And(x => x.GivenARouteClaimsRequirement(new RouteClaimsRequirement(new Dictionary<string, string>
|
||||
.And(x => x.GivenARouteClaimsRequirement(new Dictionary<string, string>
|
||||
{
|
||||
{ "UserType", "registered" }
|
||||
})))
|
||||
}))
|
||||
.When(x => x.WhenICallTheAuthoriser())
|
||||
.Then(x => x.ThenTheUserIsntAuthorised())
|
||||
.BDDfy();
|
||||
@ -55,7 +56,7 @@ namespace Ocelot.UnitTests.Authorization
|
||||
_claimsPrincipal = claimsPrincipal;
|
||||
}
|
||||
|
||||
private void GivenARouteClaimsRequirement(RouteClaimsRequirement requirement)
|
||||
private void GivenARouteClaimsRequirement(Dictionary<string, string> requirement)
|
||||
{
|
||||
_requirement = requirement;
|
||||
}
|
||||
|
145
test/Ocelot.UnitTests/ClaimsBuilder/AddClaimsToRequestTests.cs
Normal file
145
test/Ocelot.UnitTests/ClaimsBuilder/AddClaimsToRequestTests.cs
Normal file
@ -0,0 +1,145 @@
|
||||
namespace Ocelot.UnitTests.ClaimsBuilder
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using Errors;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using Ocelot.ClaimsBuilder;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class AddClaimsToRequestTests
|
||||
{
|
||||
private readonly AddClaimsToRequest _addClaimsToRequest;
|
||||
private readonly Mock<IClaimsParser> _parser;
|
||||
private List<ClaimToThing> _claimsToThings;
|
||||
private HttpContext _context;
|
||||
private Response _result;
|
||||
private Response<string> _claimValue;
|
||||
|
||||
public AddClaimsToRequestTests()
|
||||
{
|
||||
_parser = new Mock<IClaimsParser>();
|
||||
_addClaimsToRequest = new AddClaimsToRequest(_parser.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_add_claims_to_context()
|
||||
{
|
||||
var context = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim("test", "data")
|
||||
}))
|
||||
};
|
||||
|
||||
this.Given(
|
||||
x => x.GivenClaimsToThings(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("claim-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.When(x => x.WhenIAddClaimsToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void if_claims_exists_should_replace_it()
|
||||
{
|
||||
var context = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim("existing-key", "data"),
|
||||
new Claim("new-key", "data")
|
||||
})),
|
||||
};
|
||||
|
||||
this.Given(
|
||||
x => x.GivenClaimsToThings(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("existing-key", "new-key", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.When(x => x.WhenIAddClaimsToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
this.Given(
|
||||
x => x.GivenClaimsToThings(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(new DefaultHttpContext()))
|
||||
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.When(x => x.WhenIAddClaimsToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsError())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
private void GivenClaimsToThings(List<ClaimToThing> configuration)
|
||||
{
|
||||
_claimsToThings = configuration;
|
||||
}
|
||||
|
||||
private void GivenHttpContext(HttpContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
private void GivenTheClaimParserReturns(Response<string> claimValue)
|
||||
{
|
||||
_claimValue = claimValue;
|
||||
_parser
|
||||
.Setup(
|
||||
x =>
|
||||
x.GetValue(It.IsAny<IEnumerable<Claim>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<int>()))
|
||||
.Returns(_claimValue);
|
||||
}
|
||||
|
||||
private void WhenIAddClaimsToTheRequest()
|
||||
{
|
||||
_result = _addClaimsToRequest.SetClaimsOnContext(_claimsToThings, _context);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsSuccess()
|
||||
{
|
||||
_result.IsError.ShouldBe(false);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsError()
|
||||
{
|
||||
|
||||
_result.IsError.ShouldBe(true);
|
||||
}
|
||||
|
||||
class AnyError : Error
|
||||
{
|
||||
public AnyError()
|
||||
: base("blahh", OcelotErrorCode.UnknownError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
namespace Ocelot.UnitTests.ClaimsBuilder
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.ClaimsBuilder;
|
||||
using Ocelot.ClaimsBuilder.Middleware;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Responses;
|
||||
using ScopedData;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class ClaimsBuilderMiddlewareTests : IDisposable
|
||||
{
|
||||
private readonly Mock<IScopedRequestDataRepository> _scopedRepository;
|
||||
private readonly Mock<IAddClaimsToRequest> _addHeaders;
|
||||
private readonly string _url;
|
||||
private readonly TestServer _server;
|
||||
private readonly HttpClient _client;
|
||||
private Response<DownstreamRoute> _downstreamRoute;
|
||||
private HttpResponseMessage _result;
|
||||
|
||||
public ClaimsBuilderMiddlewareTests()
|
||||
{
|
||||
_url = "http://localhost:51879";
|
||||
_scopedRepository = new Mock<IScopedRequestDataRepository>();
|
||||
_addHeaders = new Mock<IAddClaimsToRequest>();
|
||||
var builder = new WebHostBuilder()
|
||||
.ConfigureServices(x =>
|
||||
{
|
||||
x.AddSingleton(_addHeaders.Object);
|
||||
x.AddSingleton(_scopedRepository.Object);
|
||||
})
|
||||
.UseUrls(_url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(_url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseClaimsBuilderMiddleware();
|
||||
});
|
||||
|
||||
_server = new TestServer(builder);
|
||||
_client = _server.CreateClient();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void happy_path()
|
||||
{
|
||||
var downstreamRoute = new DownstreamRoute(new List<TemplateVariableNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamTemplate("any old string")
|
||||
.WithClaimsToClaims(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("sub", "UserType", "|", 0)
|
||||
})
|
||||
.Build());
|
||||
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||
.And(x => x.GivenTheAddClaimsToRequestReturns())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheClaimsToRequestIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheAddClaimsToRequestReturns()
|
||||
{
|
||||
_addHeaders
|
||||
.Setup(x => x.SetClaimsOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<HttpContext>()))
|
||||
.Returns(new OkResponse());
|
||||
}
|
||||
|
||||
private void ThenTheClaimsToRequestIsCalledCorrectly()
|
||||
{
|
||||
_addHeaders
|
||||
.Verify(x => x.SetClaimsOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<HttpContext>()), Times.Once);
|
||||
}
|
||||
|
||||
private void WhenICallTheMiddleware()
|
||||
{
|
||||
_result = _client.GetAsync(_url).Result;
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
_scopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
_server.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -10,15 +10,15 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ConfigurationHeadersExtractorTests
|
||||
public class ClaimToThingConfigurationParserTests
|
||||
{
|
||||
private Dictionary<string, string> _dictionary;
|
||||
private readonly IClaimToHeaderConfigurationParser _claimToHeaderConfigurationParser;
|
||||
private Response<ClaimToHeader> _result;
|
||||
private readonly IClaimToThingConfigurationParser _claimToThingConfigurationParser;
|
||||
private Response<ClaimToThing> _result;
|
||||
|
||||
public ConfigurationHeadersExtractorTests()
|
||||
public ClaimToThingConfigurationParserTests()
|
||||
{
|
||||
_claimToHeaderConfigurationParser = new ClaimToHeaderConfigurationParser();
|
||||
_claimToThingConfigurationParser = new ClaimToThingConfigurationParser();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -31,7 +31,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenAnErrorIsReturned(new ErrorResponse<ClaimToHeader>(
|
||||
x.ThenAnErrorIsReturned(new ErrorResponse<ClaimToThing>(
|
||||
new List<Error>
|
||||
{
|
||||
new NoInstructionsError(">")
|
||||
@ -49,7 +49,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenAnErrorIsReturned(new ErrorResponse<ClaimToHeader>(
|
||||
x.ThenAnErrorIsReturned(new ErrorResponse<ClaimToThing>(
|
||||
new List<Error>
|
||||
{
|
||||
new InstructionNotForClaimsError()
|
||||
@ -68,8 +68,8 @@ namespace Ocelot.UnitTests.Configuration
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenTheClaimParserPropertiesAreReturned(
|
||||
new OkResponse<ClaimToHeader>(
|
||||
new ClaimToHeader("CustomerId", "CustomerId", "", 0))))
|
||||
new OkResponse<ClaimToThing>(
|
||||
new ClaimToThing("CustomerId", "CustomerId", "", 0))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -84,20 +84,20 @@ namespace Ocelot.UnitTests.Configuration
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenTheClaimParserPropertiesAreReturned(
|
||||
new OkResponse<ClaimToHeader>(
|
||||
new ClaimToHeader("UserId", "Subject", "|", 0))))
|
||||
new OkResponse<ClaimToThing>(
|
||||
new ClaimToThing("UserId", "Subject", "|", 0))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenAnErrorIsReturned(Response<ClaimToHeader> expected)
|
||||
private void ThenAnErrorIsReturned(Response<ClaimToThing> expected)
|
||||
{
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
_result.Errors[0].ShouldBeOfType(expected.Errors[0].GetType());
|
||||
}
|
||||
|
||||
private void ThenTheClaimParserPropertiesAreReturned(Response<ClaimToHeader> expected)
|
||||
private void ThenTheClaimParserPropertiesAreReturned(Response<ClaimToThing> expected)
|
||||
{
|
||||
_result.Data.ClaimKey.ShouldBe(expected.Data.ClaimKey);
|
||||
_result.Data.NewKey.ShouldBe(expected.Data.NewKey);
|
||||
_result.Data.Delimiter.ShouldBe(expected.Data.Delimiter);
|
||||
_result.Data.Index.ShouldBe(expected.Data.Index);
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
@ -106,7 +106,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
private void WhenICallTheExtractor()
|
||||
{
|
||||
var first = _dictionary.First();
|
||||
_result = _claimToHeaderConfigurationParser.Extract(first.Key, first.Value);
|
||||
_result = _claimToThingConfigurationParser.Extract(first.Key, first.Value);
|
||||
}
|
||||
|
||||
private void GivenTheDictionaryIs(Dictionary<string, string> dictionary)
|
@ -21,14 +21,14 @@ namespace Ocelot.UnitTests.Configuration
|
||||
private readonly Mock<IConfigurationValidator> _validator;
|
||||
private Response<IOcelotConfiguration> _config;
|
||||
private YamlConfiguration _yamlConfiguration;
|
||||
private readonly Mock<IClaimToHeaderConfigurationParser> _configParser;
|
||||
private readonly Mock<IClaimToThingConfigurationParser> _configParser;
|
||||
private readonly Mock<ILogger<YamlOcelotConfigurationCreator>> _logger;
|
||||
private readonly YamlOcelotConfigurationCreator _ocelotConfigurationCreator;
|
||||
|
||||
public YamlConfigurationCreatorTests()
|
||||
{
|
||||
_logger = new Mock<ILogger<YamlOcelotConfigurationCreator>>();
|
||||
_configParser = new Mock<IClaimToHeaderConfigurationParser>();
|
||||
_configParser = new Mock<IClaimToThingConfigurationParser>();
|
||||
_validator = new Mock<IConfigurationValidator>();
|
||||
_yamlConfig = new Mock<IOptions<YamlConfiguration>>();
|
||||
_ocelotConfigurationCreator = new YamlOcelotConfigurationCreator(
|
||||
@ -79,9 +79,9 @@ namespace Ocelot.UnitTests.Configuration
|
||||
.WithRequireHttps(false)
|
||||
.WithScopeSecret("secret")
|
||||
.WithAuthenticationProviderScopeName("api")
|
||||
.WithConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||
.WithClaimsToHeaders(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToHeader("CustomerId", "CustomerId", "", 0),
|
||||
new ClaimToThing("CustomerId", "CustomerId", "", 0),
|
||||
})
|
||||
.Build()
|
||||
};
|
||||
@ -112,18 +112,18 @@ namespace Ocelot.UnitTests.Configuration
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheYamlConfigIsValid())
|
||||
.And(x => x.GivenTheConfigHeaderExtractorReturns(new ClaimToHeader("CustomerId", "CustomerId", "", 0)))
|
||||
.And(x => x.GivenTheConfigHeaderExtractorReturns(new ClaimToThing("CustomerId", "CustomerId", "", 0)))
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
.Then(x => x.ThenTheReRoutesAre(expected))
|
||||
.And(x => x.ThenTheAuthenticationOptionsAre(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheConfigHeaderExtractorReturns(ClaimToHeader expected)
|
||||
private void GivenTheConfigHeaderExtractorReturns(ClaimToThing expected)
|
||||
{
|
||||
_configParser
|
||||
.Setup(x => x.Extract(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(new OkResponse<ClaimToHeader>(expected));
|
||||
.Returns(new OkResponse<ClaimToThing>(expected));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -4,7 +4,6 @@ using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Moq;
|
||||
using Ocelot.Claims.Parser;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.HeaderBuilder;
|
||||
@ -15,11 +14,13 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.HeaderBuilder
|
||||
{
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
|
||||
public class AddHeadersToRequestTests
|
||||
{
|
||||
private readonly AddHeadersToRequest _addHeadersToRequest;
|
||||
private readonly Mock<IClaimsParser> _parser;
|
||||
private List<ClaimToHeader> _configuration;
|
||||
private List<ClaimToThing> _configuration;
|
||||
private HttpContext _context;
|
||||
private Response _result;
|
||||
private Response<string> _claimValue;
|
||||
@ -42,9 +43,9 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
};
|
||||
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToHeader("header-key", "", "", 0)
|
||||
new ClaimToThing("header-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
@ -68,9 +69,9 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
context.Request.Headers.Add("header-key", new StringValues("initial"));
|
||||
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToHeader("header-key", "", "", 0)
|
||||
new ClaimToThing("header-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
@ -84,9 +85,9 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
public void should_return_error()
|
||||
{
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToHeader("", "", "", 0)
|
||||
new ClaimToThing("", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(new DefaultHttpContext()))
|
||||
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
||||
@ -104,7 +105,7 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
header.Value.First().ShouldBe(_claimValue.Data);
|
||||
}
|
||||
|
||||
private void GivenConfigurationHeaderExtractorProperties(List<ClaimToHeader> configuration)
|
||||
private void GivenConfigurationHeaderExtractorProperties(List<ClaimToThing> configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
@ -61,9 +61,9 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
var downstreamRoute = new DownstreamRoute(new List<TemplateVariableNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamTemplate("any old string")
|
||||
.WithConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||
.WithClaimsToHeaders(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToHeader("UserId", "Subject", "", 0)
|
||||
new ClaimToThing("UserId", "Subject", "", 0)
|
||||
})
|
||||
.Build());
|
||||
|
||||
@ -77,7 +77,7 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
private void GivenTheAddHeadersToRequestReturns(string claimValue)
|
||||
{
|
||||
_addHeaders
|
||||
.Setup(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToHeader>>(),
|
||||
.Setup(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<HttpContext>()))
|
||||
.Returns(new OkResponse());
|
||||
}
|
||||
@ -85,7 +85,7 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
private void ThenTheAddHeadersToRequestIsCalledCorrectly()
|
||||
{
|
||||
_addHeaders
|
||||
.Verify(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToHeader>>(),
|
||||
.Verify(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<HttpContext>()), Times.Once);
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Ocelot.Claims.Parser;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.HeaderBuilder
|
||||
namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Errors;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class ClaimParserTests
|
||||
{
|
||||
private readonly IClaimsParser _claimsParser;
|
Reference in New Issue
Block a user