mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 20:10:50 +08:00 
			
		
		
		
	Remove Ocelot specific Middleware to make Ocelot more compatible with kestrel middleware and get ready for YARP
This commit is contained in:
		@@ -1,194 +1,194 @@
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Infrastructure.Claims.Parser;
 | 
			
		||||
using Ocelot.QueryStrings;
 | 
			
		||||
using Ocelot.Request.Middleware;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Security.Claims;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.QueryStrings
 | 
			
		||||
{
 | 
			
		||||
    public class AddQueriesToRequestTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly AddQueriesToRequest _addQueriesToRequest;
 | 
			
		||||
        private DownstreamRequest _downstreamRequest;
 | 
			
		||||
        private readonly Mock<IClaimsParser> _parser;
 | 
			
		||||
        private List<ClaimToThing> _configuration;
 | 
			
		||||
        private List<Claim> _claims;
 | 
			
		||||
        private Response _result;
 | 
			
		||||
        private Response<string> _claimValue;
 | 
			
		||||
        private HttpRequestMessage _request;
 | 
			
		||||
 | 
			
		||||
        public AddQueriesToRequestTests()
 | 
			
		||||
        {
 | 
			
		||||
            _request = new HttpRequestMessage(HttpMethod.Post, "http://my.url/abc?q=123");
 | 
			
		||||
            _parser = new Mock<IClaimsParser>();
 | 
			
		||||
            _addQueriesToRequest = new AddQueriesToRequest(_parser.Object);
 | 
			
		||||
            _downstreamRequest = new DownstreamRequest(_request);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_add_new_queries_to_downstream_request()
 | 
			
		||||
        {
 | 
			
		||||
            var claims = new List<Claim>
 | 
			
		||||
            {
 | 
			
		||||
                new Claim("test", "data")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(
 | 
			
		||||
                x => x.GivenAClaimToThing(new List<ClaimToThing>
 | 
			
		||||
                {
 | 
			
		||||
                    new ClaimToThing("query-key", "", "", 0)
 | 
			
		||||
                }))
 | 
			
		||||
                .Given(x => x.GivenClaims(claims))
 | 
			
		||||
                .And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
 | 
			
		||||
                .When(x => x.WhenIAddQueriesToTheRequest())
 | 
			
		||||
                .Then(x => x.ThenTheResultIsSuccess())
 | 
			
		||||
                .And(x => x.ThenTheQueryIsAdded())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_add_new_queries_to_downstream_request_and_preserve_other_queries()
 | 
			
		||||
        {
 | 
			
		||||
            var claims = new List<Claim>
 | 
			
		||||
            {
 | 
			
		||||
                new Claim("test", "data")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(
 | 
			
		||||
                x => x.GivenAClaimToThing(new List<ClaimToThing>
 | 
			
		||||
                {
 | 
			
		||||
                    new ClaimToThing("query-key", "", "", 0)
 | 
			
		||||
                }))
 | 
			
		||||
                .Given(x => x.GivenClaims(claims))
 | 
			
		||||
                .And(x => GivenTheDownstreamRequestHasQueryString("?test=1&test=2"))
 | 
			
		||||
                .And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
 | 
			
		||||
                .When(x => x.WhenIAddQueriesToTheRequest())
 | 
			
		||||
                .Then(x => x.ThenTheResultIsSuccess())
 | 
			
		||||
                .And(x => x.ThenTheQueryIsAdded())
 | 
			
		||||
                .And(x => TheTheQueryStringIs("?test=1&test=2&query-key=value"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void TheTheQueryStringIs(string expected)
 | 
			
		||||
        {
 | 
			
		||||
            _downstreamRequest.Query.ShouldBe(expected);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_replace_existing_queries_on_downstream_request()
 | 
			
		||||
        {
 | 
			
		||||
            var claims = new List<Claim>
 | 
			
		||||
            {
 | 
			
		||||
                new Claim("test", "data")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(
 | 
			
		||||
                x => x.GivenAClaimToThing(new List<ClaimToThing>
 | 
			
		||||
                {
 | 
			
		||||
                    new ClaimToThing("query-key", "", "", 0)
 | 
			
		||||
                }))
 | 
			
		||||
                .And(x => x.GivenClaims(claims))
 | 
			
		||||
                .And(x => x.GivenTheDownstreamRequestHasQueryString("query-key", "initial"))
 | 
			
		||||
                .And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
 | 
			
		||||
                .When(x => x.WhenIAddQueriesToTheRequest())
 | 
			
		||||
                .Then(x => x.ThenTheResultIsSuccess())
 | 
			
		||||
                .And(x => x.ThenTheQueryIsAdded())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_error()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(
 | 
			
		||||
               x => x.GivenAClaimToThing(new List<ClaimToThing>
 | 
			
		||||
               {
 | 
			
		||||
                    new ClaimToThing("", "", "", 0)
 | 
			
		||||
               }))
 | 
			
		||||
               .Given(x => x.GivenClaims(new List<Claim>()))
 | 
			
		||||
               .And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
 | 
			
		||||
               {
 | 
			
		||||
                   new AnyError()
 | 
			
		||||
               })))
 | 
			
		||||
               .When(x => x.WhenIAddQueriesToTheRequest())
 | 
			
		||||
               .Then(x => x.ThenTheResultIsError())
 | 
			
		||||
               .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheQueryIsAdded()
 | 
			
		||||
        {
 | 
			
		||||
            var queries = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(_downstreamRequest.ToHttpRequestMessage().RequestUri.OriginalString);
 | 
			
		||||
            var query = queries.First(x => x.Key == "query-key");
 | 
			
		||||
            query.Value.First().ShouldBe(_claimValue.Data);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenAClaimToThing(List<ClaimToThing> configuration)
 | 
			
		||||
        {
 | 
			
		||||
            _configuration = configuration;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenClaims(List<Claim> claims)
 | 
			
		||||
        {
 | 
			
		||||
            _claims = claims;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheDownstreamRequestHasQueryString(string queryString)
 | 
			
		||||
        {
 | 
			
		||||
            _request = new HttpRequestMessage(HttpMethod.Post, $"http://my.url/abc{queryString}");
 | 
			
		||||
            _downstreamRequest = new DownstreamRequest(_request);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheDownstreamRequestHasQueryString(string key, string value)
 | 
			
		||||
        {
 | 
			
		||||
            var newUri = Microsoft.AspNetCore.WebUtilities.QueryHelpers
 | 
			
		||||
                .AddQueryString(_downstreamRequest.ToHttpRequestMessage().RequestUri.OriginalString, key, value);
 | 
			
		||||
 | 
			
		||||
            _request.RequestUri = new Uri(newUri);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheClaimParserReturns(Response<string> claimValue)
 | 
			
		||||
        {
 | 
			
		||||
            _claimValue = claimValue;
 | 
			
		||||
            _parser
 | 
			
		||||
                .Setup(
 | 
			
		||||
                    x =>
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Infrastructure.Claims.Parser;
 | 
			
		||||
using Ocelot.QueryStrings;
 | 
			
		||||
using Ocelot.Request.Middleware;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Security.Claims;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.QueryStrings
 | 
			
		||||
{
 | 
			
		||||
    public class AddQueriesToRequestTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly AddQueriesToRequest _addQueriesToRequest;
 | 
			
		||||
        private DownstreamRequest _downstreamRequest;
 | 
			
		||||
        private readonly Mock<IClaimsParser> _parser;
 | 
			
		||||
        private List<ClaimToThing> _configuration;
 | 
			
		||||
        private List<Claim> _claims;
 | 
			
		||||
        private Response _result;
 | 
			
		||||
        private Response<string> _claimValue;
 | 
			
		||||
        private HttpRequestMessage _request;
 | 
			
		||||
 | 
			
		||||
        public AddQueriesToRequestTests()
 | 
			
		||||
        {
 | 
			
		||||
            _request = new HttpRequestMessage(HttpMethod.Post, "http://my.url/abc?q=123");
 | 
			
		||||
            _parser = new Mock<IClaimsParser>();
 | 
			
		||||
            _addQueriesToRequest = new AddQueriesToRequest(_parser.Object);
 | 
			
		||||
            _downstreamRequest = new DownstreamRequest(_request);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_add_new_queries_to_downstream_request()
 | 
			
		||||
        {
 | 
			
		||||
            var claims = new List<Claim>
 | 
			
		||||
            {
 | 
			
		||||
                new Claim("test", "data")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(
 | 
			
		||||
                x => x.GivenAClaimToThing(new List<ClaimToThing>
 | 
			
		||||
                {
 | 
			
		||||
                    new ClaimToThing("query-key", "", "", 0)
 | 
			
		||||
                }))
 | 
			
		||||
                .Given(x => x.GivenClaims(claims))
 | 
			
		||||
                .And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
 | 
			
		||||
                .When(x => x.WhenIAddQueriesToTheRequest())
 | 
			
		||||
                .Then(x => x.ThenTheResultIsSuccess())
 | 
			
		||||
                .And(x => x.ThenTheQueryIsAdded())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_add_new_queries_to_downstream_request_and_preserve_other_queries()
 | 
			
		||||
        {
 | 
			
		||||
            var claims = new List<Claim>
 | 
			
		||||
            {
 | 
			
		||||
                new Claim("test", "data")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(
 | 
			
		||||
                x => x.GivenAClaimToThing(new List<ClaimToThing>
 | 
			
		||||
                {
 | 
			
		||||
                    new ClaimToThing("query-key", "", "", 0)
 | 
			
		||||
                }))
 | 
			
		||||
                .Given(x => x.GivenClaims(claims))
 | 
			
		||||
                .And(x => GivenTheDownstreamRequestHasQueryString("?test=1&test=2"))
 | 
			
		||||
                .And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
 | 
			
		||||
                .When(x => x.WhenIAddQueriesToTheRequest())
 | 
			
		||||
                .Then(x => x.ThenTheResultIsSuccess())
 | 
			
		||||
                .And(x => x.ThenTheQueryIsAdded())
 | 
			
		||||
                .And(x => TheTheQueryStringIs("?test=1&test=2&query-key=value"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void TheTheQueryStringIs(string expected)
 | 
			
		||||
        {
 | 
			
		||||
            _downstreamRequest.Query.ShouldBe(expected);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_replace_existing_queries_on_downstream_request()
 | 
			
		||||
        {
 | 
			
		||||
            var claims = new List<Claim>
 | 
			
		||||
            {
 | 
			
		||||
                new Claim("test", "data")
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(
 | 
			
		||||
                x => x.GivenAClaimToThing(new List<ClaimToThing>
 | 
			
		||||
                {
 | 
			
		||||
                    new ClaimToThing("query-key", "", "", 0)
 | 
			
		||||
                }))
 | 
			
		||||
                .And(x => x.GivenClaims(claims))
 | 
			
		||||
                .And(x => x.GivenTheDownstreamRequestHasQueryString("query-key", "initial"))
 | 
			
		||||
                .And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
 | 
			
		||||
                .When(x => x.WhenIAddQueriesToTheRequest())
 | 
			
		||||
                .Then(x => x.ThenTheResultIsSuccess())
 | 
			
		||||
                .And(x => x.ThenTheQueryIsAdded())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_error()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(
 | 
			
		||||
               x => x.GivenAClaimToThing(new List<ClaimToThing>
 | 
			
		||||
               {
 | 
			
		||||
                    new ClaimToThing("", "", "", 0)
 | 
			
		||||
               }))
 | 
			
		||||
               .Given(x => x.GivenClaims(new List<Claim>()))
 | 
			
		||||
               .And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
 | 
			
		||||
               {
 | 
			
		||||
                   new AnyError()
 | 
			
		||||
               })))
 | 
			
		||||
               .When(x => x.WhenIAddQueriesToTheRequest())
 | 
			
		||||
               .Then(x => x.ThenTheResultIsError())
 | 
			
		||||
               .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheQueryIsAdded()
 | 
			
		||||
        {
 | 
			
		||||
            var queries = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(_downstreamRequest.ToHttpRequestMessage().RequestUri.OriginalString);
 | 
			
		||||
            var query = queries.First(x => x.Key == "query-key");
 | 
			
		||||
            query.Value.First().ShouldBe(_claimValue.Data);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenAClaimToThing(List<ClaimToThing> configuration)
 | 
			
		||||
        {
 | 
			
		||||
            _configuration = configuration;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenClaims(List<Claim> claims)
 | 
			
		||||
        {
 | 
			
		||||
            _claims = claims;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheDownstreamRequestHasQueryString(string queryString)
 | 
			
		||||
        {
 | 
			
		||||
            _request = new HttpRequestMessage(HttpMethod.Post, $"http://my.url/abc{queryString}");
 | 
			
		||||
            _downstreamRequest = new DownstreamRequest(_request);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheDownstreamRequestHasQueryString(string key, string value)
 | 
			
		||||
        {
 | 
			
		||||
            var newUri = Microsoft.AspNetCore.WebUtilities.QueryHelpers
 | 
			
		||||
                .AddQueryString(_downstreamRequest.ToHttpRequestMessage().RequestUri.OriginalString, key, value);
 | 
			
		||||
 | 
			
		||||
            _request.RequestUri = new Uri(newUri);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        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 WhenIAddQueriesToTheRequest()
 | 
			
		||||
        {
 | 
			
		||||
            _result = _addQueriesToRequest.SetQueriesOnDownstreamRequest(_configuration, _claims, _downstreamRequest);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheResultIsSuccess()
 | 
			
		||||
        {
 | 
			
		||||
            _result.IsError.ShouldBe(false);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheResultIsError()
 | 
			
		||||
        {
 | 
			
		||||
            _result.IsError.ShouldBe(true);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private class AnyError : Error
 | 
			
		||||
        {
 | 
			
		||||
                        It.IsAny<string>(),
 | 
			
		||||
                        It.IsAny<int>()))
 | 
			
		||||
                .Returns(_claimValue);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIAddQueriesToTheRequest()
 | 
			
		||||
        {
 | 
			
		||||
            _result = _addQueriesToRequest.SetQueriesOnDownstreamRequest(_configuration, _claims, _downstreamRequest);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheResultIsSuccess()
 | 
			
		||||
        {
 | 
			
		||||
            _result.IsError.ShouldBe(false);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheResultIsError()
 | 
			
		||||
        {
 | 
			
		||||
            _result.IsError.ShouldBe(true);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private class AnyError : Error
 | 
			
		||||
        {
 | 
			
		||||
            public AnyError()
 | 
			
		||||
                : base("blahh", OcelotErrorCode.UnknownError)
 | 
			
		||||
            {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
                : base("blahh", OcelotErrorCode.UnknownError, 404)
 | 
			
		||||
            {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,97 +1,102 @@
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.QueryStrings
 | 
			
		||||
{
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
    using Moq;
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
    using Ocelot.Configuration.Builder;
 | 
			
		||||
    using Ocelot.DownstreamRouteFinder;
 | 
			
		||||
    using Ocelot.DownstreamRouteFinder.UrlMatcher;
 | 
			
		||||
    using Ocelot.Logging;
 | 
			
		||||
    using Ocelot.QueryStrings;
 | 
			
		||||
    using Ocelot.QueryStrings.Middleware;
 | 
			
		||||
    using Ocelot.Request.Middleware;
 | 
			
		||||
    using Ocelot.Responses;
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.Net.Http;
 | 
			
		||||
    using System.Security.Claims;
 | 
			
		||||
    using System.Threading.Tasks;
 | 
			
		||||
    using TestStack.BDDfy;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.QueryStrings
 | 
			
		||||
{
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
    using Moq;
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
    using Ocelot.Configuration.Builder;
 | 
			
		||||
    using Ocelot.DownstreamRouteFinder;
 | 
			
		||||
    using Ocelot.DownstreamRouteFinder.UrlMatcher;
 | 
			
		||||
    using Ocelot.Logging;
 | 
			
		||||
    using Ocelot.QueryStrings;
 | 
			
		||||
    using Ocelot.QueryStrings.Middleware;
 | 
			
		||||
    using Ocelot.Request.Middleware;
 | 
			
		||||
    using Ocelot.Responses;
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.Net.Http;
 | 
			
		||||
    using System.Security.Claims;
 | 
			
		||||
    using System.Threading.Tasks;
 | 
			
		||||
    using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
    using TestStack.BDDfy;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
    using Ocelot.DownstreamRouteFinder.Middleware;
 | 
			
		||||
 | 
			
		||||
    public class ClaimsToQueryStringMiddlewareTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Mock<IAddQueriesToRequest> _addQueries;
 | 
			
		||||
        private Mock<IOcelotLoggerFactory> _loggerFactory;
 | 
			
		||||
        private Mock<IOcelotLogger> _logger;
 | 
			
		||||
        private ClaimsToQueryStringMiddleware _middleware;
 | 
			
		||||
        private DownstreamContext _downstreamContext;
 | 
			
		||||
        private OcelotRequestDelegate _next;
 | 
			
		||||
 | 
			
		||||
        public ClaimsToQueryStringMiddlewareTests()
 | 
			
		||||
        {
 | 
			
		||||
            _downstreamContext = new DownstreamContext(new DefaultHttpContext());
 | 
			
		||||
            _loggerFactory = new Mock<IOcelotLoggerFactory>();
 | 
			
		||||
            _logger = new Mock<IOcelotLogger>();
 | 
			
		||||
            _loggerFactory.Setup(x => x.CreateLogger<ClaimsToQueryStringMiddleware>()).Returns(_logger.Object);
 | 
			
		||||
            _next = context => Task.CompletedTask;
 | 
			
		||||
    public class ClaimsToQueryStringMiddlewareTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Mock<IAddQueriesToRequest> _addQueries;
 | 
			
		||||
        private Mock<IOcelotLoggerFactory> _loggerFactory;
 | 
			
		||||
        private Mock<IOcelotLogger> _logger;
 | 
			
		||||
        private ClaimsToQueryStringMiddleware _middleware;
 | 
			
		||||
        private RequestDelegate _next;
 | 
			
		||||
        private HttpContext _httpContext;
 | 
			
		||||
        private Mock<IRequestScopedDataRepository> _repo;
 | 
			
		||||
 | 
			
		||||
        public ClaimsToQueryStringMiddlewareTests()
 | 
			
		||||
        {
 | 
			
		||||
            _repo = new Mock<IRequestScopedDataRepository>();
 | 
			
		||||
            _httpContext = new DefaultHttpContext();
 | 
			
		||||
            _loggerFactory = new Mock<IOcelotLoggerFactory>();
 | 
			
		||||
            _logger = new Mock<IOcelotLogger>();
 | 
			
		||||
            _loggerFactory.Setup(x => x.CreateLogger<ClaimsToQueryStringMiddleware>()).Returns(_logger.Object);
 | 
			
		||||
            _next = context => Task.CompletedTask;
 | 
			
		||||
            _addQueries = new Mock<IAddQueriesToRequest>();
 | 
			
		||||
            _downstreamContext.DownstreamRequest = new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com"));
 | 
			
		||||
            _middleware = new ClaimsToQueryStringMiddleware(_next, _loggerFactory.Object, _addQueries.Object);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_call_add_queries_correctly()
 | 
			
		||||
            _httpContext.Items.UpsertDownstreamRequest(new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com")));
 | 
			
		||||
            _middleware = new ClaimsToQueryStringMiddleware(_next, _loggerFactory.Object, _addQueries.Object);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_call_add_queries_correctly()
 | 
			
		||||
        {
 | 
			
		||||
            var downstreamRoute = new DownstreamRoute(new List<PlaceholderNameAndValue>(),
 | 
			
		||||
                new ReRouteBuilder()
 | 
			
		||||
                    .WithDownstreamReRoute(new DownstreamReRouteBuilder()
 | 
			
		||||
                        .WithDownstreamPathTemplate("any old string")
 | 
			
		||||
                        .WithClaimsToQueries(new List<ClaimToThing>
 | 
			
		||||
                        {
 | 
			
		||||
                            new ClaimToThing("UserId", "Subject", "", 0)
 | 
			
		||||
                        })
 | 
			
		||||
                        .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
			
		||||
                        .Build())
 | 
			
		||||
                    .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
			
		||||
                    .Build());
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
 | 
			
		||||
                .And(x => x.GivenTheAddHeadersToRequestReturnsOk())
 | 
			
		||||
                .When(x => x.WhenICallTheMiddleware())
 | 
			
		||||
                .Then(x => x.ThenTheAddQueriesToRequestIsCalledCorrectly())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenICallTheMiddleware()
 | 
			
		||||
        {
 | 
			
		||||
            _middleware.Invoke(_httpContext).GetAwaiter().GetResult();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheAddHeadersToRequestReturnsOk()
 | 
			
		||||
        {
 | 
			
		||||
            _addQueries
 | 
			
		||||
                .Setup(x => x.SetQueriesOnDownstreamRequest(
 | 
			
		||||
                    It.IsAny<List<ClaimToThing>>(),
 | 
			
		||||
                    It.IsAny<IEnumerable<Claim>>(),
 | 
			
		||||
                    It.IsAny<DownstreamRequest>()))
 | 
			
		||||
                .Returns(new OkResponse());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheAddQueriesToRequestIsCalledCorrectly()
 | 
			
		||||
        {
 | 
			
		||||
            _addQueries
 | 
			
		||||
                .Verify(x => x.SetQueriesOnDownstreamRequest(
 | 
			
		||||
                    It.IsAny<List<ClaimToThing>>(),
 | 
			
		||||
                    It.IsAny<IEnumerable<Claim>>(),
 | 
			
		||||
                    _httpContext.Items.DownstreamRequest()), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
 | 
			
		||||
        {
 | 
			
		||||
            var downstreamRoute = new DownstreamRoute(new List<PlaceholderNameAndValue>(),
 | 
			
		||||
                new ReRouteBuilder()
 | 
			
		||||
                    .WithDownstreamReRoute(new DownstreamReRouteBuilder()
 | 
			
		||||
                        .WithDownstreamPathTemplate("any old string")
 | 
			
		||||
                        .WithClaimsToQueries(new List<ClaimToThing>
 | 
			
		||||
                        {
 | 
			
		||||
                            new ClaimToThing("UserId", "Subject", "", 0)
 | 
			
		||||
                        })
 | 
			
		||||
                        .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
			
		||||
                        .Build())
 | 
			
		||||
                    .WithUpstreamHttpMethod(new List<string> { "Get" })
 | 
			
		||||
                    .Build());
 | 
			
		||||
            _httpContext.Items.UpsertTemplatePlaceholderNameAndValues(downstreamRoute.TemplatePlaceholderNameAndValues);
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
 | 
			
		||||
                .And(x => x.GivenTheAddHeadersToRequestReturnsOk())
 | 
			
		||||
                .When(x => x.WhenICallTheMiddleware())
 | 
			
		||||
                .Then(x => x.ThenTheAddQueriesToRequestIsCalledCorrectly())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenICallTheMiddleware()
 | 
			
		||||
        {
 | 
			
		||||
            _middleware.Invoke(_downstreamContext).GetAwaiter().GetResult();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheAddHeadersToRequestReturnsOk()
 | 
			
		||||
        {
 | 
			
		||||
            _addQueries
 | 
			
		||||
                .Setup(x => x.SetQueriesOnDownstreamRequest(
 | 
			
		||||
                    It.IsAny<List<ClaimToThing>>(),
 | 
			
		||||
                    It.IsAny<IEnumerable<Claim>>(),
 | 
			
		||||
                    It.IsAny<DownstreamRequest>()))
 | 
			
		||||
                .Returns(new OkResponse());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheAddQueriesToRequestIsCalledCorrectly()
 | 
			
		||||
        {
 | 
			
		||||
            _addQueries
 | 
			
		||||
                .Verify(x => x.SetQueriesOnDownstreamRequest(
 | 
			
		||||
                    It.IsAny<List<ClaimToThing>>(),
 | 
			
		||||
                    It.IsAny<IEnumerable<Claim>>(),
 | 
			
		||||
                    _downstreamContext.DownstreamRequest), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
 | 
			
		||||
        {
 | 
			
		||||
            _downstreamContext.TemplatePlaceholderNameAndValues = downstreamRoute.TemplatePlaceholderNameAndValues;
 | 
			
		||||
            _downstreamContext.DownstreamReRoute = downstreamRoute.ReRoute.DownstreamReRoute[0];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
            _httpContext.Items.UpsertDownstreamReRoute(downstreamRoute.ReRoute.DownstreamReRoute[0]);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user