mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:42:50 +08:00
Fixed some unit tests
This commit is contained in:
parent
8b93f44077
commit
7c1a277147
@ -27,14 +27,14 @@ namespace Ocelot.Cache.Middleware
|
|||||||
|
|
||||||
public async Task Invoke(HttpContext context)
|
public async Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
var downstreamUrlKey = DownstreamRequest.RequestUri.OriginalString;
|
|
||||||
|
|
||||||
if (!DownstreamRoute.ReRoute.IsCached)
|
if (!DownstreamRoute.ReRoute.IsCached)
|
||||||
{
|
{
|
||||||
await _next.Invoke(context);
|
await _next.Invoke(context);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var downstreamUrlKey = DownstreamRequest.RequestUri.OriginalString;
|
||||||
|
|
||||||
_logger.LogDebug("started checking cache for {downstreamUrlKey}", downstreamUrlKey);
|
_logger.LogDebug("started checking cache for {downstreamUrlKey}", downstreamUrlKey);
|
||||||
|
|
||||||
var cached = _outputCache.Get(downstreamUrlKey);
|
var cached = _outputCache.Get(downstreamUrlKey);
|
||||||
|
@ -51,7 +51,6 @@ namespace Ocelot.QueryStrings
|
|||||||
|
|
||||||
public Response SetQueriesOnDownstreamRequest(List<ClaimToThing> claimsToThings, IEnumerable<Claim> claims, HttpRequestMessage downstreamRequest)
|
public Response SetQueriesOnDownstreamRequest(List<ClaimToThing> claimsToThings, IEnumerable<Claim> claims, HttpRequestMessage downstreamRequest)
|
||||||
{
|
{
|
||||||
|
|
||||||
var queryDictionary = ConvertQueryStringToDictionary(downstreamRequest.RequestUri.Query);
|
var queryDictionary = ConvertQueryStringToDictionary(downstreamRequest.RequestUri.Query);
|
||||||
|
|
||||||
foreach (var config in claimsToThings)
|
foreach (var config in claimsToThings)
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
using System.IO;
|
using System.Threading.Tasks;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Ocelot.Responses;
|
using Ocelot.Responses;
|
||||||
using Ocelot.Configuration;
|
|
||||||
using Ocelot.Requester.QoS;
|
using Ocelot.Requester.QoS;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
|
||||||
@ -10,32 +7,6 @@ namespace Ocelot.Request.Builder
|
|||||||
{
|
{
|
||||||
public sealed class HttpRequestCreator : IRequestCreator
|
public sealed class HttpRequestCreator : IRequestCreator
|
||||||
{
|
{
|
||||||
//public async Task<Response<Request>> Build(
|
|
||||||
// string httpMethod,
|
|
||||||
// string downstreamUrl,
|
|
||||||
// Stream content,
|
|
||||||
// IHeaderDictionary headers,
|
|
||||||
// QueryString queryString,
|
|
||||||
// string contentType,
|
|
||||||
// RequestId.RequestId requestId,
|
|
||||||
// bool isQos,
|
|
||||||
// IQoSProvider qosProvider)
|
|
||||||
//{
|
|
||||||
// var request = await new RequestBuilder()
|
|
||||||
// .WithHttpMethod(httpMethod)
|
|
||||||
// .WithDownstreamUrl(downstreamUrl)
|
|
||||||
// .WithQueryString(queryString)
|
|
||||||
// .WithContent(content)
|
|
||||||
// .WithContentType(contentType)
|
|
||||||
// .WithHeaders(headers)
|
|
||||||
// .WithRequestId(requestId)
|
|
||||||
// .WithIsQos(isQos)
|
|
||||||
// .WithQos(qosProvider)
|
|
||||||
// .Build();
|
|
||||||
|
|
||||||
// return new OkResponse<Request>(request);
|
|
||||||
//}
|
|
||||||
|
|
||||||
public async Task<Response<Request>> Build(
|
public async Task<Response<Request>> Build(
|
||||||
HttpRequestMessage httpRequestMessage,
|
HttpRequestMessage httpRequestMessage,
|
||||||
bool isQos,
|
bool isQos,
|
||||||
|
@ -2,11 +2,9 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using CacheManager.Core;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Cache;
|
using Ocelot.Cache;
|
||||||
using Ocelot.Cache.Middleware;
|
using Ocelot.Cache.Middleware;
|
||||||
@ -37,7 +35,6 @@ namespace Ocelot.UnitTests.Cache
|
|||||||
_cacheManager = new Mock<IOcelotCache<HttpResponseMessage>>();
|
_cacheManager = new Mock<IOcelotCache<HttpResponseMessage>>();
|
||||||
_scopedRepo = new Mock<IRequestScopedDataRepository>();
|
_scopedRepo = new Mock<IRequestScopedDataRepository>();
|
||||||
|
|
||||||
|
|
||||||
_url = "http://localhost:51879";
|
_url = "http://localhost:51879";
|
||||||
var builder = new WebHostBuilder()
|
var builder = new WebHostBuilder()
|
||||||
.ConfigureServices(x =>
|
.ConfigureServices(x =>
|
||||||
@ -57,6 +54,10 @@ namespace Ocelot.UnitTests.Cache
|
|||||||
app.UseOutputCacheMiddleware();
|
app.UseOutputCacheMiddleware();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_scopedRepo
|
||||||
|
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||||
|
.Returns(new OkResponse<HttpRequestMessage>(new HttpRequestMessage(HttpMethod.Get, "https://some.url/blah?abcd=123")));
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
_server = new TestServer(builder);
|
||||||
_client = _server.CreateClient();
|
_client = _server.CreateClient();
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,9 @@ using System.Net.Http;
|
|||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Configuration;
|
|
||||||
using Ocelot.Configuration.Builder;
|
using Ocelot.Configuration.Builder;
|
||||||
using Ocelot.DownstreamRouteFinder;
|
using Ocelot.DownstreamRouteFinder;
|
||||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
using Ocelot.DownstreamUrlCreator;
|
using Ocelot.DownstreamUrlCreator;
|
||||||
using Ocelot.DownstreamUrlCreator.Middleware;
|
using Ocelot.DownstreamUrlCreator.Middleware;
|
||||||
@ -63,6 +60,10 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
|
|||||||
app.UseDownstreamUrlCreatorMiddleware();
|
app.UseDownstreamUrlCreatorMiddleware();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_scopedRepository
|
||||||
|
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||||
|
.Returns(new OkResponse<HttpRequestMessage>(new HttpRequestMessage(HttpMethod.Get, "https://my.url/abc/?q=123")));
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
_server = new TestServer(builder);
|
||||||
_client = _server.CreateClient();
|
_client = _server.CreateClient();
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ using Ocelot.Responses;
|
|||||||
using Ocelot.Values;
|
using Ocelot.Values;
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Shouldly;
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.LoadBalancer
|
namespace Ocelot.UnitTests.LoadBalancer
|
||||||
{
|
{
|
||||||
@ -33,6 +34,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||||
private ErrorResponse<ILoadBalancer> _getLoadBalancerHouseError;
|
private ErrorResponse<ILoadBalancer> _getLoadBalancerHouseError;
|
||||||
private ErrorResponse<HostAndPort> _getHostAndPortError;
|
private ErrorResponse<HostAndPort> _getHostAndPortError;
|
||||||
|
private HttpRequestMessage _downstreamRequest;
|
||||||
|
|
||||||
public LoadBalancerMiddlewareTests()
|
public LoadBalancerMiddlewareTests()
|
||||||
{
|
{
|
||||||
@ -59,6 +61,10 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
app.UseLoadBalancingMiddleware();
|
app.UseLoadBalancingMiddleware();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "");
|
||||||
|
_scopedRepository
|
||||||
|
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||||
|
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||||
_server = new TestServer(builder);
|
_server = new TestServer(builder);
|
||||||
_client = _server.CreateClient();
|
_client = _server.CreateClient();
|
||||||
}
|
}
|
||||||
@ -71,12 +77,12 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.WithUpstreamHttpMethod("Get")
|
.WithUpstreamHttpMethod("Get")
|
||||||
.Build());
|
.Build());
|
||||||
|
|
||||||
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
|
this.Given(x => x.GivenTheDownStreamUrlIs("http://my.url/abc?q=123"))
|
||||||
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||||
.And(x => x.GivenTheLoadBalancerHouseReturns())
|
.And(x => x.GivenTheLoadBalancerHouseReturns())
|
||||||
.And(x => x.GivenTheLoadBalancerReturns())
|
.And(x => x.GivenTheLoadBalancerReturns())
|
||||||
.When(x => x.WhenICallTheMiddleware())
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
.Then(x => x.ThenTheDownstreamUrlIsReplacedWith("http://127.0.0.1:80/abc?q=123"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,7 +94,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.WithUpstreamHttpMethod("Get")
|
.WithUpstreamHttpMethod("Get")
|
||||||
.Build());
|
.Build());
|
||||||
|
|
||||||
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
|
this.Given(x => x.GivenTheDownStreamUrlIs("http://my.url/abc?q=123"))
|
||||||
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||||
.And(x => x.GivenTheLoadBalancerHouseReturnsAnError())
|
.And(x => x.GivenTheLoadBalancerHouseReturnsAnError())
|
||||||
.When(x => x.WhenICallTheMiddleware())
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
@ -104,7 +110,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.WithUpstreamHttpMethod("Get")
|
.WithUpstreamHttpMethod("Get")
|
||||||
.Build());
|
.Build());
|
||||||
|
|
||||||
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
|
this.Given(x => x.GivenTheDownStreamUrlIs("http://my.url/abc?q=123"))
|
||||||
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||||
.And(x => x.GivenTheLoadBalancerHouseReturns())
|
.And(x => x.GivenTheLoadBalancerHouseReturns())
|
||||||
.And(x => x.GivenTheLoadBalancerReturnsAnError())
|
.And(x => x.GivenTheLoadBalancerReturnsAnError())
|
||||||
@ -113,6 +119,11 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void GivenTheDownStreamUrlIs(string downstreamUrl)
|
||||||
|
{
|
||||||
|
_downstreamRequest.RequestUri = new System.Uri(downstreamUrl);
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheLoadBalancerReturnsAnError()
|
private void GivenTheLoadBalancerReturnsAnError()
|
||||||
{
|
{
|
||||||
_getHostAndPortError = new ErrorResponse<HostAndPort>(new List<Error>() { new ServicesAreNullError($"services were null for bah") });
|
_getHostAndPortError = new ErrorResponse<HostAndPort>(new List<Error>() { new ServicesAreNullError($"services were null for bah") });
|
||||||
@ -157,10 +168,9 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.Returns(_getLoadBalancerHouseError);
|
.Returns(_getLoadBalancerHouseError);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
|
private void WhenICallTheMiddleware()
|
||||||
{
|
{
|
||||||
_scopedRepository
|
_result = _client.GetAsync(_url).Result;
|
||||||
.Verify(x => x.Add("HostAndPort", _hostAndPort), Times.Once());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline()
|
private void ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline()
|
||||||
@ -190,17 +200,11 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.Verify(x => x.Add("OcelotMiddlewareErrors", _getHostAndPortError.Errors), Times.Once);
|
.Verify(x => x.Add("OcelotMiddlewareErrors", _getHostAndPortError.Errors), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheDownStreamUrlIs(string downstreamUrl)
|
|
||||||
|
private void ThenTheDownstreamUrlIsReplacedWith(string expectedUri)
|
||||||
{
|
{
|
||||||
_downstreamUrl = new OkResponse<string>(downstreamUrl);
|
_downstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
|
||||||
_scopedRepository
|
|
||||||
.Setup(x => x.Get<string>(It.IsAny<string>()))
|
|
||||||
.Returns(_downstreamUrl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
@ -11,15 +11,18 @@ using Ocelot.Responses;
|
|||||||
using Shouldly;
|
using Shouldly;
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.QueryStrings
|
namespace Ocelot.UnitTests.QueryStrings
|
||||||
{
|
{
|
||||||
public class AddQueriesToRequestTests
|
public class AddQueriesToRequestTests
|
||||||
{
|
{
|
||||||
private readonly AddQueriesToRequest _addQueriesToRequest;
|
private readonly AddQueriesToRequest _addQueriesToRequest;
|
||||||
|
private readonly HttpRequestMessage _downstreamRequest;
|
||||||
private readonly Mock<IClaimsParser> _parser;
|
private readonly Mock<IClaimsParser> _parser;
|
||||||
private List<ClaimToThing> _configuration;
|
private List<ClaimToThing> _configuration;
|
||||||
private HttpContext _context;
|
private List<Claim> _claims;
|
||||||
private Response _result;
|
private Response _result;
|
||||||
private Response<string> _claimValue;
|
private Response<string> _claimValue;
|
||||||
|
|
||||||
@ -27,17 +30,15 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
{
|
{
|
||||||
_parser = new Mock<IClaimsParser>();
|
_parser = new Mock<IClaimsParser>();
|
||||||
_addQueriesToRequest = new AddQueriesToRequest(_parser.Object);
|
_addQueriesToRequest = new AddQueriesToRequest(_parser.Object);
|
||||||
|
_downstreamRequest = new HttpRequestMessage(HttpMethod.Post, "http://my.url/abc?q=123");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_add_queries_to_context()
|
public void should_add_new_queries_to_downstream_request()
|
||||||
{
|
{
|
||||||
var context = new DefaultHttpContext
|
var claims = new List<Claim>
|
||||||
{
|
|
||||||
User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
|
||||||
{
|
{
|
||||||
new Claim("test", "data")
|
new Claim("test", "data")
|
||||||
}))
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.Given(
|
this.Given(
|
||||||
@ -45,7 +46,7 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
{
|
{
|
||||||
new ClaimToThing("query-key", "", "", 0)
|
new ClaimToThing("query-key", "", "", 0)
|
||||||
}))
|
}))
|
||||||
.Given(x => x.GivenHttpContext(context))
|
.Given(x => x.GivenClaims(claims))
|
||||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||||
.When(x => x.WhenIAddQueriesToTheRequest())
|
.When(x => x.WhenIAddQueriesToTheRequest())
|
||||||
.Then(x => x.ThenTheResultIsSuccess())
|
.Then(x => x.ThenTheResultIsSuccess())
|
||||||
@ -54,24 +55,20 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void if_query_exists_should_replace_it()
|
public void should_replace_existing_queries_on_downstream_request()
|
||||||
{
|
{
|
||||||
var context = new DefaultHttpContext
|
var claims = new List<Claim>
|
||||||
{
|
|
||||||
User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
|
||||||
{
|
{
|
||||||
new Claim("test", "data")
|
new Claim("test", "data")
|
||||||
})),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
context.Request.QueryString = context.Request.QueryString.Add("query-key", "initial");
|
|
||||||
|
|
||||||
this.Given(
|
this.Given(
|
||||||
x => x.GivenAClaimToThing(new List<ClaimToThing>
|
x => x.GivenAClaimToThing(new List<ClaimToThing>
|
||||||
{
|
{
|
||||||
new ClaimToThing("query-key", "", "", 0)
|
new ClaimToThing("query-key", "", "", 0)
|
||||||
}))
|
}))
|
||||||
.Given(x => x.GivenHttpContext(context))
|
.And(x => x.GivenClaims(claims))
|
||||||
|
.And(x => x.GivenTheDownstreamRequestHasQueryString("query-key", "initial"))
|
||||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||||
.When(x => x.WhenIAddQueriesToTheRequest())
|
.When(x => x.WhenIAddQueriesToTheRequest())
|
||||||
.Then(x => x.ThenTheResultIsSuccess())
|
.Then(x => x.ThenTheResultIsSuccess())
|
||||||
@ -87,7 +84,7 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
{
|
{
|
||||||
new ClaimToThing("", "", "", 0)
|
new ClaimToThing("", "", "", 0)
|
||||||
}))
|
}))
|
||||||
.Given(x => x.GivenHttpContext(new DefaultHttpContext()))
|
.Given(x => x.GivenClaims(new List<Claim>()))
|
||||||
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
||||||
{
|
{
|
||||||
new AnyError()
|
new AnyError()
|
||||||
@ -99,7 +96,8 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
|
|
||||||
private void ThenTheQueryIsAdded()
|
private void ThenTheQueryIsAdded()
|
||||||
{
|
{
|
||||||
var query = _context.Request.Query.First(x => x.Key == "query-key");
|
var queries = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(_downstreamRequest.RequestUri.OriginalString);
|
||||||
|
var query = queries.First(x => x.Key == "query-key");
|
||||||
query.Value.First().ShouldBe(_claimValue.Data);
|
query.Value.First().ShouldBe(_claimValue.Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,9 +106,17 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenHttpContext(HttpContext context)
|
private void GivenClaims(List<Claim> claims)
|
||||||
{
|
{
|
||||||
_context = context;
|
_claims = claims;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheDownstreamRequestHasQueryString(string key, string value)
|
||||||
|
{
|
||||||
|
var newUri = Microsoft.AspNetCore.WebUtilities.QueryHelpers
|
||||||
|
.AddQueryString(_downstreamRequest.RequestUri.OriginalString, key, value);
|
||||||
|
|
||||||
|
_downstreamRequest.RequestUri = new Uri(newUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheClaimParserReturns(Response<string> claimValue)
|
private void GivenTheClaimParserReturns(Response<string> claimValue)
|
||||||
@ -128,9 +134,7 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
|
|
||||||
private void WhenIAddQueriesToTheRequest()
|
private void WhenIAddQueriesToTheRequest()
|
||||||
{
|
{
|
||||||
//_result = _addQueriesToRequest.SetQueriesOnContext(_configuration, _context);
|
_result = _addQueriesToRequest.SetQueriesOnDownstreamRequest(_configuration, _claims, _downstreamRequest);
|
||||||
//TODO: set downstreamRequest
|
|
||||||
_result = _addQueriesToRequest.SetQueriesOnDownstreamRequest(_configuration, _context.User.Claims, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheResultIsSuccess()
|
private void ThenTheResultIsSuccess()
|
||||||
|
@ -3,16 +3,13 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Configuration;
|
using Ocelot.Configuration;
|
||||||
using Ocelot.Configuration.Builder;
|
using Ocelot.Configuration.Builder;
|
||||||
using Ocelot.DownstreamRouteFinder;
|
using Ocelot.DownstreamRouteFinder;
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
using Ocelot.Headers.Middleware;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
using Ocelot.Infrastructure.RequestData;
|
||||||
using Ocelot.Logging;
|
using Ocelot.Logging;
|
||||||
using Ocelot.QueryStrings;
|
using Ocelot.QueryStrings;
|
||||||
@ -31,6 +28,7 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
private readonly string _url;
|
private readonly string _url;
|
||||||
private readonly TestServer _server;
|
private readonly TestServer _server;
|
||||||
private readonly HttpClient _client;
|
private readonly HttpClient _client;
|
||||||
|
private readonly HttpRequestMessage _downstreamRequest;
|
||||||
private Response<DownstreamRoute> _downstreamRoute;
|
private Response<DownstreamRoute> _downstreamRoute;
|
||||||
private HttpResponseMessage _result;
|
private HttpResponseMessage _result;
|
||||||
|
|
||||||
@ -57,6 +55,11 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
app.UseQueryStringBuilderMiddleware();
|
app.UseQueryStringBuilderMiddleware();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_downstreamRequest = new HttpRequestMessage();
|
||||||
|
|
||||||
|
_scopedRepository.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||||
|
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
_server = new TestServer(builder);
|
||||||
_client = _server.CreateClient();
|
_client = _server.CreateClient();
|
||||||
}
|
}
|
||||||
@ -75,18 +78,14 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
.Build());
|
.Build());
|
||||||
|
|
||||||
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||||
.And(x => x.GivenTheAddHeadersToRequestReturns())
|
.And(x => x.GivenTheAddHeadersToRequestReturnsOk())
|
||||||
.When(x => x.WhenICallTheMiddleware())
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
.Then(x => x.ThenTheAddQueriesToRequestIsCalledCorrectly())
|
.Then(x => x.ThenTheAddQueriesToRequestIsCalledCorrectly())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheAddHeadersToRequestReturns()
|
private void GivenTheAddHeadersToRequestReturnsOk()
|
||||||
{
|
{
|
||||||
//_addQueries
|
|
||||||
// .Setup(x => x.SetQueriesOnContext(It.IsAny<List<ClaimToThing>>(),
|
|
||||||
// It.IsAny<HttpContext>()))
|
|
||||||
// .Returns(new OkResponse());
|
|
||||||
_addQueries
|
_addQueries
|
||||||
.Setup(x => x.SetQueriesOnDownstreamRequest(
|
.Setup(x => x.SetQueriesOnDownstreamRequest(
|
||||||
It.IsAny<List<ClaimToThing>>(),
|
It.IsAny<List<ClaimToThing>>(),
|
||||||
@ -97,14 +96,11 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
|
|
||||||
private void ThenTheAddQueriesToRequestIsCalledCorrectly()
|
private void ThenTheAddQueriesToRequestIsCalledCorrectly()
|
||||||
{
|
{
|
||||||
//_addQueries
|
|
||||||
// .Verify(x => x.SetQueriesOnContext(It.IsAny<List<ClaimToThing>>(),
|
|
||||||
// It.IsAny<HttpContext>()), Times.Once);
|
|
||||||
_addQueries
|
_addQueries
|
||||||
.Verify(x => x.SetQueriesOnDownstreamRequest(
|
.Verify(x => x.SetQueriesOnDownstreamRequest(
|
||||||
It.IsAny<List<ClaimToThing>>(),
|
It.IsAny<List<ClaimToThing>>(),
|
||||||
It.IsAny<IEnumerable<Claim>>(),
|
It.IsAny<IEnumerable<Claim>>(),
|
||||||
It.IsAny<HttpRequestMessage>()), Times.Once);
|
_downstreamRequest), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
private void WhenICallTheMiddleware()
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Configuration.Builder;
|
using Ocelot.Configuration.Builder;
|
||||||
using Ocelot.DownstreamRouteFinder;
|
using Ocelot.DownstreamRouteFinder;
|
||||||
@ -19,7 +16,6 @@ using Ocelot.Request.Middleware;
|
|||||||
using Ocelot.Responses;
|
using Ocelot.Responses;
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Ocelot.Configuration;
|
|
||||||
using Ocelot.Requester.QoS;
|
using Ocelot.Requester.QoS;
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Request
|
namespace Ocelot.UnitTests.Request
|
||||||
|
56
test/Ocelot.UnitTests/Request/HttpRequestCreatorTests.cs
Normal file
56
test/Ocelot.UnitTests/Request/HttpRequestCreatorTests.cs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
namespace Ocelot.UnitTests.Request
|
||||||
|
{
|
||||||
|
using System.Net.Http;
|
||||||
|
|
||||||
|
using Ocelot.Request.Builder;
|
||||||
|
using Ocelot.Requester.QoS;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using Shouldly;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class HttpRequestCreatorTests
|
||||||
|
{
|
||||||
|
private readonly IRequestCreator _requestCreator;
|
||||||
|
private readonly bool _isQos;
|
||||||
|
private readonly IQoSProvider _qoSProvider;
|
||||||
|
private readonly HttpRequestMessage _requestMessage;
|
||||||
|
private Response<Ocelot.Request.Request> _response;
|
||||||
|
|
||||||
|
public HttpRequestCreatorTests()
|
||||||
|
{
|
||||||
|
_requestCreator = new HttpRequestCreator();
|
||||||
|
_isQos = true;
|
||||||
|
_qoSProvider = new NoQoSProvider();
|
||||||
|
_requestMessage = new HttpRequestMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ShouldBuildRequest()
|
||||||
|
{
|
||||||
|
this.When(x => x.WhenIBuildARequest())
|
||||||
|
.Then(x => x.ThenTheRequestContainsTheRequestMessage())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenIBuildARequest()
|
||||||
|
{
|
||||||
|
_response = _requestCreator.Build(_requestMessage, _isQos, _qoSProvider).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheRequestContainsTheRequestMessage()
|
||||||
|
{
|
||||||
|
_response.Data.HttpRequestMessage.ShouldBe(_requestMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheRequestContainsTheIsQos()
|
||||||
|
{
|
||||||
|
_response.Data.IsQos.ShouldBe(_isQos);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheRequestContainsTheQosProvider()
|
||||||
|
{
|
||||||
|
_response.Data.QosProvider.ShouldBe(_qoSProvider);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,313 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Http.Internal;
|
|
||||||
using Ocelot.Request.Builder;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
using Ocelot.Configuration;
|
|
||||||
using Ocelot.Requester.QoS;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Request
|
|
||||||
{
|
|
||||||
public class RequestBuilderTests
|
|
||||||
{
|
|
||||||
private string _httpMethod;
|
|
||||||
private string _downstreamUrl;
|
|
||||||
private HttpContent _content;
|
|
||||||
private IHeaderDictionary _headers;
|
|
||||||
private IRequestCookieCollection _cookies;
|
|
||||||
private QueryString _query;
|
|
||||||
private string _contentType;
|
|
||||||
private readonly IRequestCreator _requestCreator;
|
|
||||||
private Response<Ocelot.Request.Request> _result;
|
|
||||||
private Ocelot.RequestId.RequestId _requestId;
|
|
||||||
private bool _isQos;
|
|
||||||
private IQoSProvider _qoSProvider;
|
|
||||||
|
|
||||||
public RequestBuilderTests()
|
|
||||||
{
|
|
||||||
_content = new StringContent(string.Empty);
|
|
||||||
_requestCreator = new HttpRequestCreator();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_user_downstream_url()
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenIHaveHttpMethod("GET"))
|
|
||||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(x=> x.GivenTheQos(true, new NoQoSProvider()))
|
|
||||||
.When(x => x.WhenICreateARequest())
|
|
||||||
.And(x => x.ThenTheCorrectDownstreamUrlIsUsed("http://www.bbc.co.uk/"))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_use_http_method()
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenIHaveHttpMethod("POST"))
|
|
||||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(x => x.GivenTheQos(true, new NoQoSProvider()))
|
|
||||||
|
|
||||||
.When(x => x.WhenICreateARequest())
|
|
||||||
.And(x => x.ThenTheCorrectHttpMethodIsUsed(HttpMethod.Post))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_use_http_content()
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenIHaveHttpMethod("POST"))
|
|
||||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(x => x.GivenIHaveTheHttpContent(new StringContent("Hi from Tom")))
|
|
||||||
.And(x => x.GivenTheContentTypeIs("application/json"))
|
|
||||||
.And(x => x.GivenTheQos(true, new NoQoSProvider()))
|
|
||||||
|
|
||||||
.When(x => x.WhenICreateARequest())
|
|
||||||
.And(x => x.ThenTheCorrectContentIsUsed(new StringContent("Hi from Tom")))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_use_http_content_headers()
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenIHaveHttpMethod("POST"))
|
|
||||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(x => x.GivenIHaveTheHttpContent(new StringContent("Hi from Tom")))
|
|
||||||
.And(x => x.GivenTheContentTypeIs("application/json"))
|
|
||||||
.And(x => x.GivenTheQos(true, new NoQoSProvider()))
|
|
||||||
|
|
||||||
.When(x => x.WhenICreateARequest())
|
|
||||||
.And(x => x.ThenTheCorrectContentHeadersAreUsed(new HeaderDictionary
|
|
||||||
{
|
|
||||||
{
|
|
||||||
"Content-Type", "application/json"
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_use_unvalidated_http_content_headers()
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenIHaveHttpMethod("POST"))
|
|
||||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(x => x.GivenIHaveTheHttpContent(new StringContent("Hi from Tom")))
|
|
||||||
.And(x => x.GivenTheContentTypeIs("application/json; charset=utf-8"))
|
|
||||||
.And(x => x.GivenTheQos(true, new NoQoSProvider()))
|
|
||||||
|
|
||||||
.When(x => x.WhenICreateARequest())
|
|
||||||
.And(x => x.ThenTheCorrectContentHeadersAreUsed(new HeaderDictionary
|
|
||||||
{
|
|
||||||
{
|
|
||||||
"Content-Type", "application/json; charset=utf-8"
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_use_headers()
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenIHaveHttpMethod("GET"))
|
|
||||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(x => x.GivenTheHttpHeadersAre(new HeaderDictionary
|
|
||||||
{
|
|
||||||
{"ChopSticks", "Bubbles" }
|
|
||||||
}))
|
|
||||||
.And(x => x.GivenTheQos(true, new NoQoSProvider()))
|
|
||||||
|
|
||||||
.When(x => x.WhenICreateARequest())
|
|
||||||
.And(x => x.ThenTheCorrectHeadersAreUsed(new HeaderDictionary
|
|
||||||
{
|
|
||||||
{"ChopSticks", "Bubbles" }
|
|
||||||
}))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_use_request_id()
|
|
||||||
{
|
|
||||||
var requestId = Guid.NewGuid().ToString();
|
|
||||||
|
|
||||||
this.Given(x => x.GivenIHaveHttpMethod("GET"))
|
|
||||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(x => x.GivenTheHttpHeadersAre(new HeaderDictionary()))
|
|
||||||
.And(x => x.GivenTheRequestIdIs(new Ocelot.RequestId.RequestId("RequestId", requestId)))
|
|
||||||
.And(x => x.GivenTheQos(true, new NoQoSProvider()))
|
|
||||||
.When(x => x.WhenICreateARequest())
|
|
||||||
.And(x => x.ThenTheCorrectHeadersAreUsed(new HeaderDictionary
|
|
||||||
{
|
|
||||||
{"RequestId", requestId }
|
|
||||||
}))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_not_use_request_if_if_already_in_headers()
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenIHaveHttpMethod("GET"))
|
|
||||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(x => x.GivenTheHttpHeadersAre(new HeaderDictionary
|
|
||||||
{
|
|
||||||
{"RequestId", "534534gv54gv45g" }
|
|
||||||
}))
|
|
||||||
.And(x => x.GivenTheRequestIdIs(new Ocelot.RequestId.RequestId("RequestId", Guid.NewGuid().ToString())))
|
|
||||||
.And(x => x.GivenTheQos(true, new NoQoSProvider()))
|
|
||||||
.When(x => x.WhenICreateARequest())
|
|
||||||
.And(x => x.ThenTheCorrectHeadersAreUsed(new HeaderDictionary
|
|
||||||
{
|
|
||||||
{"RequestId", "534534gv54gv45g" }
|
|
||||||
}))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[InlineData(null, "blahh")]
|
|
||||||
[InlineData("", "blahh")]
|
|
||||||
[InlineData("RequestId", "")]
|
|
||||||
[InlineData("RequestId", null)]
|
|
||||||
public void should_not_use_request_id(string requestIdKey, string requestIdValue)
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenIHaveHttpMethod("GET"))
|
|
||||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(x => x.GivenTheHttpHeadersAre(new HeaderDictionary()))
|
|
||||||
.And(x => x.GivenTheRequestIdIs(new Ocelot.RequestId.RequestId(requestIdKey, requestIdValue)))
|
|
||||||
.And(x => x.GivenTheQos(true, new NoQoSProvider()))
|
|
||||||
.When(x => x.WhenICreateARequest())
|
|
||||||
.And(x => x.ThenTheRequestIdIsNotInTheHeaders())
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheRequestIdIs(Ocelot.RequestId.RequestId requestId)
|
|
||||||
{
|
|
||||||
_requestId = requestId;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheQos(bool isQos, IQoSProvider qoSProvider)
|
|
||||||
{
|
|
||||||
_isQos = isQos;
|
|
||||||
_qoSProvider = qoSProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_user_query_string()
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenIHaveHttpMethod("POST"))
|
|
||||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
|
||||||
.And(x => x.GivenTheQueryStringIs(new QueryString("?jeff=1&geoff=2")))
|
|
||||||
.When(x => x.WhenICreateARequest())
|
|
||||||
.And(x => x.ThenTheCorrectQueryStringIsUsed("?jeff=1&geoff=2"))
|
|
||||||
.BDDfy();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheContentTypeIs(string contentType)
|
|
||||||
{
|
|
||||||
_contentType = contentType;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheCorrectQueryStringIsUsed(string expected)
|
|
||||||
{
|
|
||||||
_result.Data.HttpRequestMessage.RequestUri.Query.ShouldBe(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheQueryStringIs(QueryString query)
|
|
||||||
{
|
|
||||||
_query = query;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheCorrectCookiesAreUsed(IRequestCookieCollection expected)
|
|
||||||
{
|
|
||||||
/* var resultCookies = _result.Data.CookieContainer.GetCookies(new Uri(_downstreamUrl + _query));
|
|
||||||
var resultDictionary = resultCookies.Cast<Cookie>().ToDictionary(cook => cook.Name, cook => cook.Value);
|
|
||||||
|
|
||||||
foreach (var expectedCookie in expected)
|
|
||||||
{
|
|
||||||
var resultCookie = resultDictionary[expectedCookie.Key];
|
|
||||||
resultCookie.ShouldBe(expectedCookie.Value);
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheCookiesAre(IRequestCookieCollection cookies)
|
|
||||||
{
|
|
||||||
_cookies = cookies;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheRequestIdIsNotInTheHeaders()
|
|
||||||
{
|
|
||||||
_result.Data.HttpRequestMessage.Headers.ShouldNotContain(x => x.Key == "RequestId");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheCorrectHeadersAreUsed(IHeaderDictionary expected)
|
|
||||||
{
|
|
||||||
var expectedHeaders = expected.Select(x => new KeyValuePair<string, string[]>(x.Key, x.Value));
|
|
||||||
|
|
||||||
foreach (var expectedHeader in expectedHeaders)
|
|
||||||
{
|
|
||||||
_result.Data.HttpRequestMessage.Headers.ShouldContain(x => x.Key == expectedHeader.Key && x.Value.First() == expectedHeader.Value[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheCorrectContentHeadersAreUsed(IHeaderDictionary expected)
|
|
||||||
{
|
|
||||||
var expectedHeaders = expected.Select(x => new KeyValuePair<string, string[]>(x.Key, x.Value));
|
|
||||||
|
|
||||||
foreach (var expectedHeader in expectedHeaders)
|
|
||||||
{
|
|
||||||
_result.Data.HttpRequestMessage.Content.Headers.ShouldContain(x => x.Key == expectedHeader.Key
|
|
||||||
&& x.Value.First() == expectedHeader.Value[0]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheHttpHeadersAre(IHeaderDictionary headers)
|
|
||||||
{
|
|
||||||
_headers = headers;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenIHaveTheHttpContent(HttpContent content)
|
|
||||||
{
|
|
||||||
_content = content;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenIHaveHttpMethod(string httpMethod)
|
|
||||||
{
|
|
||||||
_httpMethod = httpMethod;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenIHaveDownstreamUrl(string downstreamUrl)
|
|
||||||
{
|
|
||||||
_downstreamUrl = downstreamUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WhenICreateARequest()
|
|
||||||
{
|
|
||||||
//_result = _requestCreator.Build(_httpMethod, _downstreamUrl, _content?.ReadAsStreamAsync().Result, _headers,
|
|
||||||
// _query, _contentType, _requestId,_isQos,_qoSProvider).Result;
|
|
||||||
|
|
||||||
//todo: add httprequestmessage
|
|
||||||
_result = _requestCreator.Build(null, _isQos, _qoSProvider).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void ThenTheCorrectDownstreamUrlIsUsed(string expected)
|
|
||||||
{
|
|
||||||
_result.Data.HttpRequestMessage.RequestUri.AbsoluteUri.ShouldBe(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheCorrectHttpMethodIsUsed(HttpMethod expected)
|
|
||||||
{
|
|
||||||
_result.Data.HttpRequestMessage.Method.Method.ShouldBe(expected.Method);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheCorrectContentIsUsed(HttpContent expected)
|
|
||||||
{
|
|
||||||
_result.Data.HttpRequestMessage.Content.ReadAsStringAsync().Result.ShouldBe(expected.ReadAsStringAsync().Result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,14 +8,12 @@ using Microsoft.AspNetCore.Builder;
|
|||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Configuration.Builder;
|
using Ocelot.Configuration.Builder;
|
||||||
using Ocelot.DownstreamRouteFinder;
|
using Ocelot.DownstreamRouteFinder;
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
using Ocelot.Infrastructure.RequestData;
|
using Ocelot.Infrastructure.RequestData;
|
||||||
using Ocelot.Logging;
|
using Ocelot.Logging;
|
||||||
using Ocelot.Request.Middleware;
|
|
||||||
using Ocelot.RequestId.Middleware;
|
using Ocelot.RequestId.Middleware;
|
||||||
using Ocelot.Responses;
|
using Ocelot.Responses;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
@ -27,6 +25,7 @@ namespace Ocelot.UnitTests.RequestId
|
|||||||
public class RequestIdMiddlewareTests
|
public class RequestIdMiddlewareTests
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
||||||
|
private readonly HttpRequestMessage _downstreamRequest;
|
||||||
private readonly string _url;
|
private readonly string _url;
|
||||||
private readonly TestServer _server;
|
private readonly TestServer _server;
|
||||||
private readonly HttpClient _client;
|
private readonly HttpClient _client;
|
||||||
@ -64,10 +63,16 @@ namespace Ocelot.UnitTests.RequestId
|
|||||||
|
|
||||||
_server = new TestServer(builder);
|
_server = new TestServer(builder);
|
||||||
_client = _server.CreateClient();
|
_client = _server.CreateClient();
|
||||||
|
|
||||||
|
_downstreamRequest = new HttpRequestMessage();
|
||||||
|
|
||||||
|
_scopedRepository
|
||||||
|
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||||
|
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_add_request_id_to_repository()
|
public void should_pass_down_request_id_from_upstream_request()
|
||||||
{
|
{
|
||||||
var downstreamRoute = new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(),
|
var downstreamRoute = new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(),
|
||||||
new ReRouteBuilder()
|
new ReRouteBuilder()
|
||||||
@ -86,7 +91,7 @@ namespace Ocelot.UnitTests.RequestId
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_add_trace_indentifier_to_repository()
|
public void should_add_request_id_when_not_on_upstream_request()
|
||||||
{
|
{
|
||||||
var downstreamRoute = new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(),
|
var downstreamRoute = new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(),
|
||||||
new ReRouteBuilder()
|
new ReRouteBuilder()
|
||||||
@ -101,14 +106,12 @@ namespace Ocelot.UnitTests.RequestId
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheTraceIdIsAnything()
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||||
{
|
{
|
||||||
_result.Headers.GetValues("LSRequestId").First().ShouldNotBeNullOrEmpty();
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
}
|
_scopedRepository
|
||||||
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
private void ThenTheTraceIdIs(string expected)
|
.Returns(_downstreamRoute);
|
||||||
{
|
|
||||||
_result.Headers.GetValues("LSRequestId").First().ShouldBe(expected);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheRequestIdIsAddedToTheRequest(string key, string value)
|
private void GivenTheRequestIdIsAddedToTheRequest(string key, string value)
|
||||||
@ -123,12 +126,14 @@ namespace Ocelot.UnitTests.RequestId
|
|||||||
_result = _client.GetAsync(_url).Result;
|
_result = _client.GetAsync(_url).Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
private void ThenTheTraceIdIsAnything()
|
||||||
{
|
{
|
||||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
_result.Headers.GetValues("LSRequestId").First().ShouldNotBeNullOrEmpty();
|
||||||
_scopedRepository
|
}
|
||||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
|
||||||
.Returns(_downstreamRoute);
|
private void ThenTheTraceIdIs(string expected)
|
||||||
|
{
|
||||||
|
_result.Headers.GetValues("LSRequestId").First().ShouldBe(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user