mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 19:58:14 +08:00
Added ability to strip claims and forward to downstream service as headers
This commit is contained in:
152
test/Ocelot.UnitTests/RequestBuilder/AddHeadersToRequestTests.cs
Normal file
152
test/Ocelot.UnitTests/RequestBuilder/AddHeadersToRequestTests.cs
Normal file
@ -0,0 +1,152 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Moq;
|
||||
using Ocelot.Library.Errors;
|
||||
using Ocelot.Library.RequestBuilder;
|
||||
using Ocelot.Library.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.RequestBuilder
|
||||
{
|
||||
public class AddHeadersToRequestTests
|
||||
{
|
||||
private readonly AddHeadersToRequest _addHeadersToRequest;
|
||||
private readonly Mock<IClaimsParser> _parser;
|
||||
private List<ConfigurationHeaderExtractorProperties> _configuration;
|
||||
private HttpContext _context;
|
||||
private Response _result;
|
||||
private Response<string> _claimValue;
|
||||
|
||||
public AddHeadersToRequestTests()
|
||||
{
|
||||
_parser = new Mock<IClaimsParser>();
|
||||
_addHeadersToRequest = new AddHeadersToRequest(_parser.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_add_headers_to_context()
|
||||
{
|
||||
var context = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim("test", "data")
|
||||
}))
|
||||
};
|
||||
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
|
||||
{
|
||||
new ConfigurationHeaderExtractorProperties("header-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.When(x => x.WhenIAddHeadersToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.And(x => x.ThenTheHeaderIsAdded())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void if_header_exists_should_replace_it()
|
||||
{
|
||||
var context = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim("test", "data")
|
||||
})),
|
||||
};
|
||||
|
||||
context.Request.Headers.Add("header-key", new StringValues("initial"));
|
||||
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
|
||||
{
|
||||
new ConfigurationHeaderExtractorProperties("header-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.When(x => x.WhenIAddHeadersToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.And(x => x.ThenTheHeaderIsAdded())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
|
||||
{
|
||||
new ConfigurationHeaderExtractorProperties("", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(new DefaultHttpContext()))
|
||||
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.When(x => x.WhenIAddHeadersToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsError())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheHeaderIsAdded()
|
||||
{
|
||||
var header = _context.Request.Headers.First(x => x.Key == "header-key");
|
||||
header.Value.First().ShouldBe(_claimValue.Data);
|
||||
}
|
||||
|
||||
private void GivenConfigurationHeaderExtractorProperties(List<ConfigurationHeaderExtractorProperties> configuration)
|
||||
{
|
||||
_configuration = 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 WhenIAddHeadersToTheRequest()
|
||||
{
|
||||
_result = _addHeadersToRequest.SetHeadersOnContext(_configuration, _context);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsSuccess()
|
||||
{
|
||||
_result.IsError.ShouldBe(false);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsError()
|
||||
{
|
||||
|
||||
_result.IsError.ShouldBe(true);
|
||||
}
|
||||
|
||||
class AnyError : Error
|
||||
{
|
||||
public AnyError()
|
||||
: base("blahh", OcelotErrorCode.UnknownError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
123
test/Ocelot.UnitTests/RequestBuilder/ClaimParserTests.cs
Normal file
123
test/Ocelot.UnitTests/RequestBuilder/ClaimParserTests.cs
Normal file
@ -0,0 +1,123 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Ocelot.Library.Errors;
|
||||
using Ocelot.Library.RequestBuilder;
|
||||
using Ocelot.Library.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.RequestBuilder
|
||||
{
|
||||
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 Library.RequestBuilder.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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ocelot.Library.Errors;
|
||||
using Ocelot.Library.RequestBuilder;
|
||||
using Ocelot.Library.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.RequestBuilder
|
||||
{
|
||||
public class ConfigurationHeadersExtractorTests
|
||||
{
|
||||
private Dictionary<string, string> _dictionary;
|
||||
private readonly IConfigurationHeaderExtrator _configurationHeaderExtrator;
|
||||
private Response<ConfigurationHeaderExtractorProperties> _result;
|
||||
|
||||
public ConfigurationHeadersExtractorTests()
|
||||
{
|
||||
_configurationHeaderExtrator = new ConfigurationHeaderExtrator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void returns_no_instructions_error()
|
||||
{
|
||||
this.Given(x => x.GivenTheDictionaryIs(new Dictionary<string, string>()
|
||||
{
|
||||
{"CustomerId", ""},
|
||||
}))
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenAnErrorIsReturned(new ErrorResponse<ConfigurationHeaderExtractorProperties>(
|
||||
new List<Error>
|
||||
{
|
||||
new NoInstructionsError(">")
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void returns_no_instructions_not_for_claims_error()
|
||||
{
|
||||
this.Given(x => x.GivenTheDictionaryIs(new Dictionary<string, string>()
|
||||
{
|
||||
{"CustomerId", "Cheese[CustomerId] > value"},
|
||||
}))
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenAnErrorIsReturned(new ErrorResponse<ConfigurationHeaderExtractorProperties>(
|
||||
new List<Error>
|
||||
{
|
||||
new InstructionNotForClaimsError()
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_parse_entry_to_work_out_properties_with_key()
|
||||
{
|
||||
this.Given(x => x.GivenTheDictionaryIs(new Dictionary<string, string>()
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
}))
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenTheClaimParserPropertiesAreReturned(
|
||||
new OkResponse<ConfigurationHeaderExtractorProperties>(
|
||||
new ConfigurationHeaderExtractorProperties("CustomerId", "CustomerId", "", 0))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_parse_entry_to_work_out_properties_with_key_delimiter_and_index()
|
||||
{
|
||||
this.Given(x => x.GivenTheDictionaryIs(new Dictionary<string, string>()
|
||||
{
|
||||
{"UserId", "Claims[Subject] > value[0] > |"},
|
||||
}))
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenTheClaimParserPropertiesAreReturned(
|
||||
new OkResponse<ConfigurationHeaderExtractorProperties>(
|
||||
new ConfigurationHeaderExtractorProperties("UserId", "Subject", "|", 0))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenAnErrorIsReturned(Response<ConfigurationHeaderExtractorProperties> expected)
|
||||
{
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
_result.Errors[0].ShouldBeOfType(expected.Errors[0].GetType());
|
||||
}
|
||||
|
||||
private void ThenTheClaimParserPropertiesAreReturned(Response<ConfigurationHeaderExtractorProperties> expected)
|
||||
{
|
||||
_result.Data.ClaimKey.ShouldBe(expected.Data.ClaimKey);
|
||||
_result.Data.Delimiter.ShouldBe(expected.Data.Delimiter);
|
||||
_result.Data.Index.ShouldBe(expected.Data.Index);
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
}
|
||||
|
||||
private void WhenICallTheExtractor()
|
||||
{
|
||||
var first = _dictionary.First();
|
||||
_result = _configurationHeaderExtrator.Extract(first.Key, first.Value);
|
||||
}
|
||||
|
||||
private void GivenTheDictionaryIs(Dictionary<string, string> dictionary)
|
||||
{
|
||||
_dictionary = dictionary;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user