mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 12:18:16 +08:00
hacked together load balancing reroutes in fileconfig (#211)
* hacked together load balancing reroutes in fileconfig * some renaming and refactoring * more renames * hacked away the old config json * test for issue 213 * renamed key * dont share ports * oops * updated docs * mvoed docs around * port being used
This commit is contained in:
@ -1,124 +1,124 @@
|
||||
using Ocelot.Errors;
|
||||
|
||||
namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class ClaimParserTests
|
||||
{
|
||||
private readonly IClaimsParser _claimsParser;
|
||||
private readonly List<Claim> _claims;
|
||||
private string _key;
|
||||
private Response<string> _result;
|
||||
private string _delimiter;
|
||||
private int _index;
|
||||
|
||||
public ClaimParserTests()
|
||||
{
|
||||
_claims = new List<Claim>();
|
||||
_claimsParser = new ClaimsParser();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_parse_claims_dictionary_access_string_returning_value_to_function()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("CustomerId", "1234")))
|
||||
.And(x => x.GivenTheKeyIs("CustomerId"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new OkResponse<string>("1234")))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_response_when_cannot_find_requested_claim()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("BallsId", "1234")))
|
||||
.And(x => x.GivenTheKeyIs("CustomerId"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new CannotFindClaimError($"Cannot find claim for key: {_key}")
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_parse_claims_dictionary_access_string_using_delimiter_and_retuning_at_correct_index()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("Subject", "registered|4321")))
|
||||
.And(x => x.GivenTheDelimiterIs("|"))
|
||||
.And(x => x.GivenTheIndexIs(1))
|
||||
.And(x => x.GivenTheKeyIs("Subject"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new OkResponse<string>("4321")))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_response_if_index_too_large()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("Subject", "registered|4321")))
|
||||
.And(x => x.GivenTheDelimiterIs("|"))
|
||||
.And(x => x.GivenTheIndexIs(24))
|
||||
.And(x => x.GivenTheKeyIs("Subject"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new CannotFindClaimError($"Cannot find claim for key: {_key}, delimiter: {_delimiter}, index: {_index}")
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_response_if_index_too_small()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("Subject", "registered|4321")))
|
||||
.And(x => x.GivenTheDelimiterIs("|"))
|
||||
.And(x => x.GivenTheIndexIs(-1))
|
||||
.And(x => x.GivenTheKeyIs("Subject"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new CannotFindClaimError($"Cannot find claim for key: {_key}, delimiter: {_delimiter}, index: {_index}")
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheIndexIs(int index)
|
||||
{
|
||||
_index = index;
|
||||
}
|
||||
|
||||
private void GivenTheDelimiterIs(string delimiter)
|
||||
{
|
||||
_delimiter = delimiter;
|
||||
}
|
||||
|
||||
private void GivenAClaimOf(Claim claim)
|
||||
{
|
||||
_claims.Add(claim);
|
||||
}
|
||||
|
||||
private void GivenTheKeyIs(string key)
|
||||
{
|
||||
_key = key;
|
||||
}
|
||||
|
||||
private void WhenICallTheParser()
|
||||
{
|
||||
_result = _claimsParser.GetValue(_claims, _key, _delimiter, _index);
|
||||
}
|
||||
|
||||
private void ThenTheResultIs(Response<string> expected)
|
||||
{
|
||||
_result.Data.ShouldBe(expected.Data);
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
}
|
||||
}
|
||||
}
|
||||
using Ocelot.Errors;
|
||||
|
||||
namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class ClaimParserTests
|
||||
{
|
||||
private readonly IClaimsParser _claimsParser;
|
||||
private readonly List<Claim> _claims;
|
||||
private string _key;
|
||||
private Response<string> _result;
|
||||
private string _delimiter;
|
||||
private int _index;
|
||||
|
||||
public ClaimParserTests()
|
||||
{
|
||||
_claims = new List<Claim>();
|
||||
_claimsParser = new ClaimsParser();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_parse_claims_dictionary_access_string_returning_value_to_function()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("CustomerId", "1234")))
|
||||
.And(x => x.GivenTheKeyIs("CustomerId"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new OkResponse<string>("1234")))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_response_when_cannot_find_requested_claim()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("BallsId", "1234")))
|
||||
.And(x => x.GivenTheKeyIs("CustomerId"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new CannotFindClaimError($"Cannot find claim for key: {_key}")
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_parse_claims_dictionary_access_string_using_delimiter_and_retuning_at_correct_index()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("Subject", "registered|4321")))
|
||||
.And(x => x.GivenTheDelimiterIs("|"))
|
||||
.And(x => x.GivenTheIndexIs(1))
|
||||
.And(x => x.GivenTheKeyIs("Subject"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new OkResponse<string>("4321")))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_response_if_index_too_large()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("Subject", "registered|4321")))
|
||||
.And(x => x.GivenTheDelimiterIs("|"))
|
||||
.And(x => x.GivenTheIndexIs(24))
|
||||
.And(x => x.GivenTheKeyIs("Subject"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new CannotFindClaimError($"Cannot find claim for key: {_key}, delimiter: {_delimiter}, index: {_index}")
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_response_if_index_too_small()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("Subject", "registered|4321")))
|
||||
.And(x => x.GivenTheDelimiterIs("|"))
|
||||
.And(x => x.GivenTheIndexIs(-1))
|
||||
.And(x => x.GivenTheKeyIs("Subject"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new CannotFindClaimError($"Cannot find claim for key: {_key}, delimiter: {_delimiter}, index: {_index}")
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheIndexIs(int index)
|
||||
{
|
||||
_index = index;
|
||||
}
|
||||
|
||||
private void GivenTheDelimiterIs(string delimiter)
|
||||
{
|
||||
_delimiter = delimiter;
|
||||
}
|
||||
|
||||
private void GivenAClaimOf(Claim claim)
|
||||
{
|
||||
_claims.Add(claim);
|
||||
}
|
||||
|
||||
private void GivenTheKeyIs(string key)
|
||||
{
|
||||
_key = key;
|
||||
}
|
||||
|
||||
private void WhenICallTheParser()
|
||||
{
|
||||
_result = _claimsParser.GetValue(_claims, _key, _delimiter, _index);
|
||||
}
|
||||
|
||||
private void ThenTheResultIs(Response<string> expected)
|
||||
{
|
||||
_result.Data.ShouldBe(expected.Data);
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,124 +1,124 @@
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using Ocelot.Authorisation;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Ocelot.Errors;
|
||||
|
||||
namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
public class ScopesAuthoriserTests
|
||||
{
|
||||
private ScopesAuthoriser _authoriser;
|
||||
public Mock<IClaimsParser> _parser;
|
||||
private ClaimsPrincipal _principal;
|
||||
private List<string> _allowedScopes;
|
||||
private Response<bool> _result;
|
||||
|
||||
public ScopesAuthoriserTests()
|
||||
{
|
||||
_parser = new Mock<IClaimsParser>();
|
||||
_authoriser = new ScopesAuthoriser(_parser.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_ok_if_no_allowed_scopes()
|
||||
{
|
||||
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
|
||||
.And(_ => GivenTheFollowing(new List<string>()))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new OkResponse<bool>(true)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_return_ok_if_null_allowed_scopes()
|
||||
{
|
||||
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
|
||||
.And(_ => GivenTheFollowing((List<string>)null))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new OkResponse<bool>(true)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_claims_parser_returns_error()
|
||||
{
|
||||
var fakeError = new FakeError();
|
||||
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
|
||||
.And(_ => GivenTheParserReturns(new ErrorResponse<List<string>>(fakeError)))
|
||||
.And(_ => GivenTheFollowing(new List<string>(){"doesntmatter"}))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new ErrorResponse<bool>(fakeError)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_match_scopes_and_return_ok_result()
|
||||
{
|
||||
var claimsPrincipal = new ClaimsPrincipal();
|
||||
var allowedScopes = new List<string>(){"someScope"};
|
||||
|
||||
this.Given(_ => GivenTheFollowing(claimsPrincipal))
|
||||
.And(_ => GivenTheParserReturns(new OkResponse<List<string>>(allowedScopes)))
|
||||
.And(_ => GivenTheFollowing(allowedScopes))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new OkResponse<bool>(true)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_match_scopes_and_return_error_result()
|
||||
{
|
||||
var fakeError = new FakeError();
|
||||
var claimsPrincipal = new ClaimsPrincipal();
|
||||
var allowedScopes = new List<string>(){"someScope"};
|
||||
var userScopes = new List<string>(){"anotherScope"};
|
||||
|
||||
this.Given(_ => GivenTheFollowing(claimsPrincipal))
|
||||
.And(_ => GivenTheParserReturns(new OkResponse<List<string>>(userScopes)))
|
||||
.And(_ => GivenTheFollowing(allowedScopes))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new ErrorResponse<bool>(fakeError)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheParserReturns(Response<List<string>> response)
|
||||
{
|
||||
_parser.Setup(x => x.GetValuesByClaimType(It.IsAny<IEnumerable<Claim>>(), It.IsAny<string>())).Returns(response);
|
||||
}
|
||||
|
||||
private void GivenTheFollowing(ClaimsPrincipal principal)
|
||||
{
|
||||
_principal = principal;
|
||||
}
|
||||
|
||||
private void GivenTheFollowing(List<string> allowedScopes)
|
||||
{
|
||||
_allowedScopes = allowedScopes;
|
||||
}
|
||||
|
||||
private void WhenIAuthorise()
|
||||
{
|
||||
_result = _authoriser.Authorise(_principal, _allowedScopes);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(Response<bool> expected)
|
||||
{
|
||||
_result.Data.ShouldBe(expected.Data);
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
}
|
||||
}
|
||||
|
||||
public class FakeError : Error
|
||||
{
|
||||
public FakeError() : base("fake error", OcelotErrorCode.CannotAddDataError)
|
||||
{
|
||||
}
|
||||
}
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using Ocelot.Authorisation;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Ocelot.Errors;
|
||||
|
||||
namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
public class ScopesAuthoriserTests
|
||||
{
|
||||
private ScopesAuthoriser _authoriser;
|
||||
public Mock<IClaimsParser> _parser;
|
||||
private ClaimsPrincipal _principal;
|
||||
private List<string> _allowedScopes;
|
||||
private Response<bool> _result;
|
||||
|
||||
public ScopesAuthoriserTests()
|
||||
{
|
||||
_parser = new Mock<IClaimsParser>();
|
||||
_authoriser = new ScopesAuthoriser(_parser.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_ok_if_no_allowed_scopes()
|
||||
{
|
||||
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
|
||||
.And(_ => GivenTheFollowing(new List<string>()))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new OkResponse<bool>(true)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_return_ok_if_null_allowed_scopes()
|
||||
{
|
||||
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
|
||||
.And(_ => GivenTheFollowing((List<string>)null))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new OkResponse<bool>(true)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_claims_parser_returns_error()
|
||||
{
|
||||
var fakeError = new FakeError();
|
||||
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
|
||||
.And(_ => GivenTheParserReturns(new ErrorResponse<List<string>>(fakeError)))
|
||||
.And(_ => GivenTheFollowing(new List<string>(){"doesntmatter"}))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new ErrorResponse<bool>(fakeError)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_match_scopes_and_return_ok_result()
|
||||
{
|
||||
var claimsPrincipal = new ClaimsPrincipal();
|
||||
var allowedScopes = new List<string>(){"someScope"};
|
||||
|
||||
this.Given(_ => GivenTheFollowing(claimsPrincipal))
|
||||
.And(_ => GivenTheParserReturns(new OkResponse<List<string>>(allowedScopes)))
|
||||
.And(_ => GivenTheFollowing(allowedScopes))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new OkResponse<bool>(true)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_match_scopes_and_return_error_result()
|
||||
{
|
||||
var fakeError = new FakeError();
|
||||
var claimsPrincipal = new ClaimsPrincipal();
|
||||
var allowedScopes = new List<string>(){"someScope"};
|
||||
var userScopes = new List<string>(){"anotherScope"};
|
||||
|
||||
this.Given(_ => GivenTheFollowing(claimsPrincipal))
|
||||
.And(_ => GivenTheParserReturns(new OkResponse<List<string>>(userScopes)))
|
||||
.And(_ => GivenTheFollowing(allowedScopes))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new ErrorResponse<bool>(fakeError)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheParserReturns(Response<List<string>> response)
|
||||
{
|
||||
_parser.Setup(x => x.GetValuesByClaimType(It.IsAny<IEnumerable<Claim>>(), It.IsAny<string>())).Returns(response);
|
||||
}
|
||||
|
||||
private void GivenTheFollowing(ClaimsPrincipal principal)
|
||||
{
|
||||
_principal = principal;
|
||||
}
|
||||
|
||||
private void GivenTheFollowing(List<string> allowedScopes)
|
||||
{
|
||||
_allowedScopes = allowedScopes;
|
||||
}
|
||||
|
||||
private void WhenIAuthorise()
|
||||
{
|
||||
_result = _authoriser.Authorise(_principal, _allowedScopes);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(Response<bool> expected)
|
||||
{
|
||||
_result.Data.ShouldBe(expected.Data);
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
}
|
||||
}
|
||||
|
||||
public class FakeError : Error
|
||||
{
|
||||
public FakeError() : base("fake error", OcelotErrorCode.CannotAddDataError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user