mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 20:12:51 +08:00
Merge pull request #111 from binarymash/feature/ImproveTests
Feature/improve tests
This commit is contained in:
commit
41e9dfac94
@ -16,7 +16,7 @@ var artifactsDir = Directory("artifacts");
|
|||||||
// unit testing
|
// unit testing
|
||||||
var artifactsForUnitTestsDir = artifactsDir + Directory("UnitTests");
|
var artifactsForUnitTestsDir = artifactsDir + Directory("UnitTests");
|
||||||
var unitTestAssemblies = @"./test/Ocelot.UnitTests/Ocelot.UnitTests.csproj";
|
var unitTestAssemblies = @"./test/Ocelot.UnitTests/Ocelot.UnitTests.csproj";
|
||||||
var minCodeCoverage = 75d;
|
var minCodeCoverage = 76.4d;
|
||||||
var coverallsRepoToken = "coveralls-repo-token-ocelot";
|
var coverallsRepoToken = "coveralls-repo-token-ocelot";
|
||||||
var coverallsRepo = "https://coveralls.io/github/TomPallister/Ocelot";
|
var coverallsRepo = "https://coveralls.io/github/TomPallister/Ocelot";
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ namespace Ocelot.Authentication.Middleware
|
|||||||
{
|
{
|
||||||
if (IsAuthenticatedRoute(DownstreamRoute.ReRoute))
|
if (IsAuthenticatedRoute(DownstreamRoute.ReRoute))
|
||||||
{
|
{
|
||||||
_logger.LogDebug($"{context.Request.Path} is an authenticated route. {MiddlwareName} checking if client is authenticated");
|
_logger.LogDebug($"{context.Request.Path} is an authenticated route. {MiddlewareName} checking if client is authenticated");
|
||||||
|
|
||||||
var authenticationHandler = _authHandlerFactory.Get(_app, DownstreamRoute.ReRoute.AuthenticationOptions);
|
var authenticationHandler = _authHandlerFactory.Get(_app, DownstreamRoute.ReRoute.AuthenticationOptions);
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ namespace Ocelot.DownstreamRouteFinder.Middleware
|
|||||||
|
|
||||||
if (downstreamRoute.IsError)
|
if (downstreamRoute.IsError)
|
||||||
{
|
{
|
||||||
_logger.LogError($"{MiddlwareName} setting pipeline errors. IDownstreamRouteFinder returned {downstreamRoute.Errors.ToErrorString()}");
|
_logger.LogError($"{MiddlewareName} setting pipeline errors. IDownstreamRouteFinder returned {downstreamRoute.Errors.ToErrorString()}");
|
||||||
|
|
||||||
SetPipelineError(downstreamRoute.Errors);
|
SetPipelineError(downstreamRoute.Errors);
|
||||||
return;
|
return;
|
||||||
|
@ -13,10 +13,10 @@ namespace Ocelot.Middleware
|
|||||||
protected OcelotMiddleware(IRequestScopedDataRepository requestScopedDataRepository)
|
protected OcelotMiddleware(IRequestScopedDataRepository requestScopedDataRepository)
|
||||||
{
|
{
|
||||||
_requestScopedDataRepository = requestScopedDataRepository;
|
_requestScopedDataRepository = requestScopedDataRepository;
|
||||||
MiddlwareName = this.GetType().Name;
|
MiddlewareName = this.GetType().Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string MiddlwareName { get; }
|
public string MiddlewareName { get; }
|
||||||
|
|
||||||
public bool PipelineError => _requestScopedDataRepository.Get<bool>("OcelotMiddlewareError").Data;
|
public bool PipelineError => _requestScopedDataRepository.Get<bool>("OcelotMiddlewareError").Data;
|
||||||
|
|
||||||
|
@ -62,14 +62,6 @@ namespace Ocelot.Responder
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void AddHeaderIfDoesntExist(HttpContext context, KeyValuePair<string, IEnumerable<string>> httpResponseHeader)
|
|
||||||
{
|
|
||||||
if (!context.Response.Headers.ContainsKey(httpResponseHeader.Key))
|
|
||||||
{
|
|
||||||
context.Response.Headers.Add(httpResponseHeader.Key, new StringValues(httpResponseHeader.Value.ToArray()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetErrorResponseOnContext(HttpContext context, int statusCode)
|
public void SetErrorResponseOnContext(HttpContext context, int statusCode)
|
||||||
{
|
{
|
||||||
context.Response.OnStarting(x =>
|
context.Response.OnStarting(x =>
|
||||||
@ -78,5 +70,13 @@ namespace Ocelot.Responder
|
|||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}, context);
|
}, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void AddHeaderIfDoesntExist(HttpContext context, KeyValuePair<string, IEnumerable<string>> httpResponseHeader)
|
||||||
|
{
|
||||||
|
if (!context.Response.Headers.ContainsKey(httpResponseHeader.Key))
|
||||||
|
{
|
||||||
|
context.Response.Headers.Add(httpResponseHeader.Key, new StringValues(httpResponseHeader.Value.ToArray()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -39,7 +39,7 @@ namespace Ocelot.Responder.Middleware
|
|||||||
if (PipelineError)
|
if (PipelineError)
|
||||||
{
|
{
|
||||||
var errors = PipelineErrors;
|
var errors = PipelineErrors;
|
||||||
_logger.LogError($"{errors.Count} pipeline errors found in {MiddlwareName}. Setting error response status code");
|
_logger.LogError($"{PipelineErrors.Count} pipeline errors found in {MiddlewareName}. Setting error response status code");
|
||||||
|
|
||||||
SetErrorResponse(context, errors);
|
SetErrorResponse(context, errors);
|
||||||
}
|
}
|
||||||
@ -53,7 +53,6 @@ namespace Ocelot.Responder.Middleware
|
|||||||
private void SetErrorResponse(HttpContext context, List<Error> errors)
|
private void SetErrorResponse(HttpContext context, List<Error> errors)
|
||||||
{
|
{
|
||||||
var statusCode = _codeMapper.Map(errors);
|
var statusCode = _codeMapper.Map(errors);
|
||||||
|
|
||||||
_responder.SetErrorResponseOnContext(context, statusCode);
|
_responder.SetErrorResponseOnContext(context, statusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ using TestStack.BDDfy;
|
|||||||
using Xunit;
|
using Xunit;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Threading;
|
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
namespace Ocelot.IntegrationTests
|
namespace Ocelot.IntegrationTests
|
||||||
@ -23,11 +22,9 @@ namespace Ocelot.IntegrationTests
|
|||||||
public class ThreadSafeHeadersTests : IDisposable
|
public class ThreadSafeHeadersTests : IDisposable
|
||||||
{
|
{
|
||||||
private readonly HttpClient _httpClient;
|
private readonly HttpClient _httpClient;
|
||||||
private HttpResponseMessage _response;
|
|
||||||
private IWebHost _builder;
|
private IWebHost _builder;
|
||||||
private IWebHostBuilder _webHostBuilder;
|
private IWebHostBuilder _webHostBuilder;
|
||||||
private readonly string _ocelotBaseUrl;
|
private readonly string _ocelotBaseUrl;
|
||||||
private BearerToken _token;
|
|
||||||
private IWebHost _downstreamBuilder;
|
private IWebHost _downstreamBuilder;
|
||||||
private readonly Random _random;
|
private readonly Random _random;
|
||||||
private readonly ConcurrentBag<ThreadSafeHeadersTestResult> _results;
|
private readonly ConcurrentBag<ThreadSafeHeadersTestResult> _results;
|
||||||
@ -135,7 +132,7 @@ namespace Ocelot.IntegrationTests
|
|||||||
text = File.ReadAllText(configurationPath);
|
text = File.ReadAllText(configurationPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WhenIGetUrlOnTheApiGatewayMultipleTimesWithDifferentHeaderValues(string url, int times)
|
private void WhenIGetUrlOnTheApiGatewayMultipleTimesWithDifferentHeaderValues(string url, int times)
|
||||||
{
|
{
|
||||||
var tasks = new Task[times];
|
var tasks = new Task[times];
|
||||||
|
|
||||||
|
@ -1,60 +1,55 @@
|
|||||||
using System;
|
namespace Ocelot.UnitTests.Authentication
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Authentication.Handler;
|
|
||||||
using Ocelot.Authentication.Handler.Factory;
|
|
||||||
using Ocelot.Authentication.Middleware;
|
|
||||||
using Ocelot.Cache.Middleware;
|
|
||||||
using Ocelot.Configuration;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Authentication
|
|
||||||
{
|
{
|
||||||
public class AuthenticationMiddlewareTests : IDisposable
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Authentication.Handler.Factory;
|
||||||
|
using Ocelot.Authentication.Middleware;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using Shouldly;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class AuthenticationMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly Mock<IAuthenticationHandlerFactory> _authFactory;
|
private readonly Mock<IAuthenticationHandlerFactory> _authFactory;
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private HttpResponseMessage _result;
|
|
||||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||||
|
|
||||||
public AuthenticationMiddlewareTests()
|
public AuthenticationMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
_authFactory = new Mock<IAuthenticationHandlerFactory>();
|
_authFactory = new Mock<IAuthenticationHandlerFactory>();
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
GivenTheTestServerIsConfigured();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_call_next_middleware_if_route_is_not_authenticated()
|
||||||
{
|
{
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
this.Given(x => x.GivenTheDownStreamRouteIs(
|
||||||
x.AddLogging();
|
new DownstreamRoute(
|
||||||
x.AddSingleton(_authFactory.Object);
|
new List<UrlPathPlaceholderNameAndValue>(),
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
new ReRouteBuilder().WithUpstreamHttpMethod(new List<string> { "Get" }).Build())))
|
||||||
})
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
.UseUrls(_url)
|
.Then(x => x.ThenTheUserIsAuthenticated())
|
||||||
.UseKestrel()
|
.BDDfy();
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
}
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
.Configure(app =>
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_authFactory.Object);
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
app.UseAuthenticationMiddleware();
|
app.UseAuthenticationMiddleware();
|
||||||
|
|
||||||
@ -62,47 +57,20 @@ namespace Ocelot.UnitTests.Authentication
|
|||||||
{
|
{
|
||||||
await x.Response.WriteAsync("The user is authenticated");
|
await x.Response.WriteAsync("The user is authenticated");
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void should_call_next_middleware_if_route_is_not_authenticated()
|
|
||||||
{
|
|
||||||
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), new ReRouteBuilder()
|
|
||||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
|
||||||
.Build())))
|
|
||||||
.When(x => x.WhenICallTheMiddleware())
|
|
||||||
.Then(x => x.ThenTheUserIsAuthenticated())
|
|
||||||
.BDDfy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheUserIsAuthenticated()
|
private void ThenTheUserIsAuthenticated()
|
||||||
{
|
{
|
||||||
var content = _result.Content.ReadAsStringAsync().Result;
|
var content = ResponseMessage.Content.ReadAsStringAsync().Result;
|
||||||
content.ShouldBe("The user is authenticated");
|
content.ShouldBe("The user is authenticated");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||||
{
|
{
|
||||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
.Returns(_downstreamRoute);
|
.Returns(_downstreamRoute);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_client.Dispose();
|
|
||||||
_server.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,66 +1,32 @@
|
|||||||
using System;
|
namespace Ocelot.UnitTests.Authorization
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Security.Claims;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Authorisation;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Authorization
|
|
||||||
{
|
{
|
||||||
using Authorisation.Middleware;
|
using System.Collections.Generic;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Authorisation;
|
||||||
|
using Ocelot.Authorisation.Middleware;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
public class AuthorisationMiddlewareTests : IDisposable
|
public class AuthorisationMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly Mock<IClaimsAuthoriser> _authService;
|
private readonly Mock<IClaimsAuthoriser> _authService;
|
||||||
private readonly Mock<IScopesAuthoriser> _authScopesService;
|
private readonly Mock<IScopesAuthoriser> _authScopesService;
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private HttpResponseMessage _result;
|
|
||||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||||
|
|
||||||
public AuthorisationMiddlewareTests()
|
public AuthorisationMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
_authService = new Mock<IClaimsAuthoriser>();
|
_authService = new Mock<IClaimsAuthoriser>();
|
||||||
_authScopesService = new Mock<IScopesAuthoriser>();
|
_authScopesService = new Mock<IScopesAuthoriser>();
|
||||||
|
|
||||||
var builder = new WebHostBuilder()
|
GivenTheTestServerIsConfigured();
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_authService.Object);
|
|
||||||
x.AddSingleton(_authScopesService.Object);
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseAuthorisationMiddleware();
|
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -77,6 +43,28 @@ namespace Ocelot.UnitTests.Authorization
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_authService.Object);
|
||||||
|
services.AddSingleton(_authScopesService.Object);
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseAuthorisationMiddleware();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||||
|
{
|
||||||
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
|
ScopedRepository
|
||||||
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
|
.Returns(_downstreamRoute);
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheAuthServiceReturns(Response<bool> expected)
|
private void GivenTheAuthServiceReturns(Response<bool> expected)
|
||||||
{
|
{
|
||||||
_authService
|
_authService
|
||||||
@ -90,25 +78,5 @@ namespace Ocelot.UnitTests.Authorization
|
|||||||
.Verify(x => x.Authorise(It.IsAny<ClaimsPrincipal>(),
|
.Verify(x => x.Authorise(It.IsAny<ClaimsPrincipal>(),
|
||||||
It.IsAny<Dictionary<string, string>>()), Times.Once);
|
It.IsAny<Dictionary<string, string>>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
|
||||||
{
|
|
||||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
|
||||||
_scopedRepository
|
|
||||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
|
||||||
.Returns(_downstreamRoute);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_client.Dispose();
|
|
||||||
_server.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using CacheManager.Core;
|
using CacheManager.Core;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Cache;
|
using Ocelot.Cache;
|
||||||
@ -17,7 +16,6 @@ namespace Ocelot.UnitTests.Cache
|
|||||||
private string _value;
|
private string _value;
|
||||||
private string _resultGet;
|
private string _resultGet;
|
||||||
private TimeSpan _ttlSeconds;
|
private TimeSpan _ttlSeconds;
|
||||||
private List<string> _resultKeys;
|
|
||||||
private string _region;
|
private string _region;
|
||||||
|
|
||||||
public CacheManagerCacheTests()
|
public CacheManagerCacheTests()
|
||||||
|
@ -1,67 +1,36 @@
|
|||||||
using System;
|
namespace Ocelot.UnitTests.Cache
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Cache;
|
|
||||||
using Ocelot.Cache.Middleware;
|
|
||||||
using Ocelot.Configuration;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Cache
|
|
||||||
{
|
{
|
||||||
public class OutputCacheMiddlewareTests
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Cache;
|
||||||
|
using Ocelot.Cache.Middleware;
|
||||||
|
using Ocelot.Configuration;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class OutputCacheMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IOcelotCache<HttpResponseMessage>> _cacheManager;
|
private readonly Mock<IOcelotCache<HttpResponseMessage>> _cacheManager;
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepo;
|
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private HttpResponseMessage _result;
|
|
||||||
private HttpResponseMessage _response;
|
private HttpResponseMessage _response;
|
||||||
|
|
||||||
public OutputCacheMiddlewareTests()
|
public OutputCacheMiddlewareTests()
|
||||||
{
|
{
|
||||||
_cacheManager = new Mock<IOcelotCache<HttpResponseMessage>>();
|
_cacheManager = new Mock<IOcelotCache<HttpResponseMessage>>();
|
||||||
_scopedRepo = new Mock<IRequestScopedDataRepository>();
|
|
||||||
|
|
||||||
_url = "http://localhost:51879";
|
ScopedRepository
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_cacheManager.Object);
|
|
||||||
x.AddSingleton(_scopedRepo.Object);
|
|
||||||
x.AddSingleton<IRegionCreator, RegionCreator>();
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseOutputCacheMiddleware();
|
|
||||||
});
|
|
||||||
|
|
||||||
_scopedRepo
|
|
||||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||||
.Returns(new OkResponse<HttpRequestMessage>(new HttpRequestMessage(HttpMethod.Get, "https://some.url/blah?abcd=123")));
|
.Returns(new OkResponse<HttpRequestMessage>(new HttpRequestMessage(HttpMethod.Get, "https://some.url/blah?abcd=123")));
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
GivenTheTestServerIsConfigured();
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -87,6 +56,34 @@ namespace Ocelot.UnitTests.Cache
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_cacheManager.Object);
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
services.AddSingleton<IRegionCreator, RegionCreator>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseOutputCacheMiddleware();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenThereIsACachedResponse(HttpResponseMessage response)
|
||||||
|
{
|
||||||
|
_response = response;
|
||||||
|
_cacheManager
|
||||||
|
.Setup(x => x.Get(It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns(_response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenResponseIsNotCached()
|
||||||
|
{
|
||||||
|
ScopedRepository
|
||||||
|
.Setup(x => x.Get<HttpResponseMessage>("HttpResponseMessage"))
|
||||||
|
.Returns(new OkResponse<HttpResponseMessage>(new HttpResponseMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheDownstreamRouteIs()
|
private void GivenTheDownstreamRouteIs()
|
||||||
{
|
{
|
||||||
@ -98,21 +95,21 @@ namespace Ocelot.UnitTests.Cache
|
|||||||
|
|
||||||
var downstreamRoute = new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), reRoute);
|
var downstreamRoute = new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), reRoute);
|
||||||
|
|
||||||
_scopedRepo
|
ScopedRepository
|
||||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
.Returns(new OkResponse<DownstreamRoute>(downstreamRoute));
|
.Returns(new OkResponse<DownstreamRoute>(downstreamRoute));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenThereAreNoErrors()
|
private void GivenThereAreNoErrors()
|
||||||
{
|
{
|
||||||
_scopedRepo
|
ScopedRepository
|
||||||
.Setup(x => x.Get<bool>("OcelotMiddlewareError"))
|
.Setup(x => x.Get<bool>("OcelotMiddlewareError"))
|
||||||
.Returns(new OkResponse<bool>(false));
|
.Returns(new OkResponse<bool>(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenThereIsADownstreamUrl()
|
private void GivenThereIsADownstreamUrl()
|
||||||
{
|
{
|
||||||
_scopedRepo
|
ScopedRepository
|
||||||
.Setup(x => x.Get<string>("DownstreamUrl"))
|
.Setup(x => x.Get<string>("DownstreamUrl"))
|
||||||
.Returns(new OkResponse<string>("anything"));
|
.Returns(new OkResponse<string>("anything"));
|
||||||
}
|
}
|
||||||
@ -128,25 +125,5 @@ namespace Ocelot.UnitTests.Cache
|
|||||||
_cacheManager
|
_cacheManager
|
||||||
.Verify(x => x.Add(It.IsAny<string>(), It.IsAny<HttpResponseMessage>(), It.IsAny<TimeSpan>(), It.IsAny<string>()), Times.Once);
|
.Verify(x => x.Add(It.IsAny<string>(), It.IsAny<HttpResponseMessage>(), It.IsAny<TimeSpan>(), It.IsAny<string>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenResponseIsNotCached()
|
|
||||||
{
|
|
||||||
_scopedRepo
|
|
||||||
.Setup(x => x.Get<HttpResponseMessage>("HttpResponseMessage"))
|
|
||||||
.Returns(new OkResponse<HttpResponseMessage>(new HttpResponseMessage()));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenThereIsACachedResponse(HttpResponseMessage response)
|
|
||||||
{
|
|
||||||
_response = response;
|
|
||||||
_cacheManager
|
|
||||||
.Setup(x => x.Get(It.IsAny<string>(), It.IsAny<string>()))
|
|
||||||
.Returns(_response);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,65 +1,31 @@
|
|||||||
using System;
|
namespace Ocelot.UnitTests.Claims
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Cache.Middleware;
|
|
||||||
using Ocelot.Claims;
|
|
||||||
using Ocelot.Claims.Middleware;
|
|
||||||
using Ocelot.Configuration;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Claims
|
|
||||||
{
|
{
|
||||||
public class ClaimsBuilderMiddlewareTests : IDisposable
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Claims;
|
||||||
|
using Ocelot.Claims.Middleware;
|
||||||
|
using Ocelot.Configuration;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class ClaimsBuilderMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly Mock<IAddClaimsToRequest> _addHeaders;
|
private readonly Mock<IAddClaimsToRequest> _addHeaders;
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private Response<DownstreamRoute> _downstreamRoute;
|
private Response<DownstreamRoute> _downstreamRoute;
|
||||||
private HttpResponseMessage _result;
|
|
||||||
|
|
||||||
public ClaimsBuilderMiddlewareTests()
|
public ClaimsBuilderMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
_addHeaders = new Mock<IAddClaimsToRequest>();
|
_addHeaders = new Mock<IAddClaimsToRequest>();
|
||||||
|
|
||||||
var builder = new WebHostBuilder()
|
GivenTheTestServerIsConfigured();
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_addHeaders.Object);
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseClaimsBuilderMiddleware();
|
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -82,6 +48,27 @@ namespace Ocelot.UnitTests.Claims
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_addHeaders.Object);
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseClaimsBuilderMiddleware();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||||
|
{
|
||||||
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
|
ScopedRepository
|
||||||
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
|
.Returns(_downstreamRoute);
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheAddClaimsToRequestReturns()
|
private void GivenTheAddClaimsToRequestReturns()
|
||||||
{
|
{
|
||||||
_addHeaders
|
_addHeaders
|
||||||
@ -96,24 +83,5 @@ namespace Ocelot.UnitTests.Claims
|
|||||||
.Verify(x => x.SetClaimsOnContext(It.IsAny<List<ClaimToThing>>(),
|
.Verify(x => x.SetClaimsOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||||
It.IsAny<HttpContext>()), Times.Once);
|
It.IsAny<HttpContext>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
|
||||||
{
|
|
||||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
|
||||||
_scopedRepository
|
|
||||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
|
||||||
.Returns(_downstreamRoute);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_client.Dispose();
|
|
||||||
_server.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,61 +1,29 @@
|
|||||||
using System;
|
namespace Ocelot.UnitTests.DownstreamRouteFinder
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.Finder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.DownstreamRouteFinder
|
|
||||||
{
|
{
|
||||||
public class DownstreamRouteFinderMiddlewareTests : IDisposable
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.DownstreamRouteFinder.Finder;
|
||||||
|
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||||
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class DownstreamRouteFinderMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IDownstreamRouteFinder> _downstreamRouteFinder;
|
private readonly Mock<IDownstreamRouteFinder> _downstreamRouteFinder;
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private Response<DownstreamRoute> _downstreamRoute;
|
private Response<DownstreamRoute> _downstreamRoute;
|
||||||
private HttpResponseMessage _result;
|
|
||||||
|
|
||||||
public DownstreamRouteFinderMiddlewareTests()
|
public DownstreamRouteFinderMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
|
||||||
_downstreamRouteFinder = new Mock<IDownstreamRouteFinder>();
|
_downstreamRouteFinder = new Mock<IDownstreamRouteFinder>();
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
|
|
||||||
var builder = new WebHostBuilder()
|
GivenTheTestServerIsConfigured();
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_downstreamRouteFinder.Object);
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseDownstreamRouteFinderMiddleware();
|
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -73,16 +41,17 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
|
|
||||||
{
|
{
|
||||||
_scopedRepository
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
.Verify(x => x.Add("DownstreamRoute", _downstreamRoute.Data), Times.Once());
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_downstreamRouteFinder.Object);
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
_result = _client.GetAsync(_url).Result;
|
app.UseDownstreamRouteFinderMiddleware();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenTheDownStreamRouteFinderReturns(DownstreamRoute downstreamRoute)
|
private void GivenTheDownStreamRouteFinderReturns(DownstreamRoute downstreamRoute)
|
||||||
@ -93,10 +62,10 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
|
|||||||
.ReturnsAsync(_downstreamRoute);
|
.ReturnsAsync(_downstreamRoute);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
|
||||||
{
|
{
|
||||||
_client.Dispose();
|
ScopedRepository
|
||||||
_server.Dispose();
|
.Verify(x => x.Add("DownstreamRoute", _downstreamRoute.Data), Times.Once());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,73 +1,45 @@
|
|||||||
using System;
|
namespace Ocelot.UnitTests.DownstreamUrlCreator
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
||||||
using Ocelot.DownstreamUrlCreator;
|
|
||||||
using Ocelot.DownstreamUrlCreator.Middleware;
|
|
||||||
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using Ocelot.Values;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
using Shouldly;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.DownstreamUrlCreator
|
|
||||||
{
|
{
|
||||||
public class DownstreamUrlCreatorMiddlewareTests : IDisposable
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
|
using Ocelot.DownstreamUrlCreator;
|
||||||
|
using Ocelot.DownstreamUrlCreator.Middleware;
|
||||||
|
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
|
||||||
|
using Ocelot.Infrastructure.RequestData;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using Ocelot.Values;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
using Shouldly;
|
||||||
|
|
||||||
|
public class DownstreamUrlCreatorMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IDownstreamPathPlaceholderReplacer> _downstreamUrlTemplateVariableReplacer;
|
private readonly Mock<IDownstreamPathPlaceholderReplacer> _downstreamUrlTemplateVariableReplacer;
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly Mock<IUrlBuilder> _urlBuilder;
|
private readonly Mock<IUrlBuilder> _urlBuilder;
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private Response<DownstreamRoute> _downstreamRoute;
|
private Response<DownstreamRoute> _downstreamRoute;
|
||||||
private OkResponse<DownstreamPath> _downstreamPath;
|
private OkResponse<DownstreamPath> _downstreamPath;
|
||||||
private HttpRequestMessage _downstreamRequest;
|
private HttpRequestMessage _downstreamRequest;
|
||||||
private HttpResponseMessage _result;
|
|
||||||
|
|
||||||
public DownstreamUrlCreatorMiddlewareTests()
|
public DownstreamUrlCreatorMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
|
||||||
_downstreamUrlTemplateVariableReplacer = new Mock<IDownstreamPathPlaceholderReplacer>();
|
_downstreamUrlTemplateVariableReplacer = new Mock<IDownstreamPathPlaceholderReplacer>();
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
_urlBuilder = new Mock<IUrlBuilder>();
|
_urlBuilder = new Mock<IUrlBuilder>();
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_downstreamUrlTemplateVariableReplacer.Object);
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
x.AddSingleton(_urlBuilder.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseDownstreamUrlCreatorMiddleware();
|
|
||||||
});
|
|
||||||
|
|
||||||
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "https://my.url/abc/?q=123");
|
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "https://my.url/abc/?q=123");
|
||||||
|
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
GivenTheTestServerIsConfigured();
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -88,10 +60,24 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_downstreamUrlTemplateVariableReplacer.Object);
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
services.AddSingleton(_urlBuilder.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseDownstreamUrlCreatorMiddleware();
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||||
{
|
{
|
||||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
.Returns(_downstreamRoute);
|
.Returns(_downstreamRoute);
|
||||||
}
|
}
|
||||||
@ -109,20 +95,9 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
|
|||||||
.Returns(_downstreamPath);
|
.Returns(_downstreamPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheDownstreamRequestUriIs(string expectedUri)
|
private void ThenTheDownstreamRequestUriIs(string expectedUri)
|
||||||
{
|
{
|
||||||
_downstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
|
_downstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_client.Dispose();
|
|
||||||
_server.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,125 +1,87 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamUrlCreator;
|
|
||||||
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
|
|
||||||
using Ocelot.Errors.Middleware;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using Ocelot.Values;
|
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Errors
|
namespace Ocelot.UnitTests.Errors
|
||||||
{
|
{
|
||||||
public class ExceptionHandlerMiddlewareTests
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Ocelot.Errors.Middleware;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Shouldly;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
public class ExceptionHandlerMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
bool _shouldThrowAnException = false;
|
||||||
private readonly string _url;
|
|
||||||
private TestServer _server;
|
|
||||||
private HttpClient _client;
|
|
||||||
private HttpResponseMessage _result;
|
|
||||||
|
|
||||||
public ExceptionHandlerMiddlewareTests()
|
public ExceptionHandlerMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:52879";
|
GivenTheTestServerIsConfigured();
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_call_next_middleware()
|
public void NoDownstreamException()
|
||||||
{
|
{
|
||||||
this.Given(_ => GivenASuccessfulRequest())
|
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
|
||||||
.When(_ => WhenIMakeTheRequest())
|
.When(_ => WhenICallTheMiddleware())
|
||||||
.Then(_ => ThenTheResponseIsOk())
|
.Then(_ => ThenTheResponseIsOk())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_call_return_error()
|
public void DownstreamException()
|
||||||
{
|
{
|
||||||
this.Given(_ => GivenAnError())
|
this.Given(_ => GivenAnExceptionWillBeThrownDownstream())
|
||||||
.When(_ => WhenIMakeTheRequest())
|
.When(_ => WhenICallTheMiddleware())
|
||||||
.Then(_ => ThenTheResponseIsError())
|
.Then(_ => ThenTheResponseIsError())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseExceptionHandlerMiddleware();
|
||||||
|
app.Run(DownstreamExceptionSimulator);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task DownstreamExceptionSimulator(HttpContext context)
|
||||||
|
{
|
||||||
|
await Task.CompletedTask;
|
||||||
|
|
||||||
|
if (_shouldThrowAnException)
|
||||||
|
{
|
||||||
|
throw new Exception("BOOM");
|
||||||
|
}
|
||||||
|
|
||||||
|
context.Response.StatusCode = (int)HttpStatusCode.OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenAnExceptionWillNotBeThrownDownstream()
|
||||||
|
{
|
||||||
|
_shouldThrowAnException = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenAnExceptionWillBeThrownDownstream()
|
||||||
|
{
|
||||||
|
_shouldThrowAnException = true;
|
||||||
|
}
|
||||||
|
|
||||||
private void ThenTheResponseIsOk()
|
private void ThenTheResponseIsOk()
|
||||||
{
|
{
|
||||||
_result.StatusCode.ShouldBe(HttpStatusCode.OK);
|
ResponseMessage.StatusCode.ShouldBe(HttpStatusCode.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheResponseIsError()
|
private void ThenTheResponseIsError()
|
||||||
{
|
{
|
||||||
_result.StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
|
ResponseMessage.StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
|
||||||
}
|
|
||||||
|
|
||||||
private void WhenIMakeTheRequest()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync("/").Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenASuccessfulRequest()
|
|
||||||
{
|
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseExceptionHandlerMiddleware();
|
|
||||||
app.Run(async context =>
|
|
||||||
{
|
|
||||||
context.Response.StatusCode = 200;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenAnError()
|
|
||||||
{
|
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseExceptionHandlerMiddleware();
|
|
||||||
app.Use(async (context, next) =>
|
|
||||||
{
|
|
||||||
throw new Exception("BOOM");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,69 +1,37 @@
|
|||||||
using System;
|
namespace Ocelot.UnitTests.Headers
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Configuration;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
||||||
using Ocelot.Headers;
|
|
||||||
using Ocelot.Headers.Middleware;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Headers
|
|
||||||
{
|
{
|
||||||
public class HttpRequestHeadersBuilderMiddlewareTests : IDisposable
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Configuration;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
|
using Ocelot.Headers;
|
||||||
|
using Ocelot.Headers.Middleware;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class HttpRequestHeadersBuilderMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly Mock<IAddHeadersToRequest> _addHeaders;
|
private readonly Mock<IAddHeadersToRequest> _addHeaders;
|
||||||
private readonly HttpRequestMessage _downstreamRequest;
|
private readonly HttpRequestMessage _downstreamRequest;
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private Response<DownstreamRoute> _downstreamRoute;
|
private Response<DownstreamRoute> _downstreamRoute;
|
||||||
private HttpResponseMessage _result;
|
|
||||||
|
|
||||||
public HttpRequestHeadersBuilderMiddlewareTests()
|
public HttpRequestHeadersBuilderMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
_addHeaders = new Mock<IAddHeadersToRequest>();
|
_addHeaders = new Mock<IAddHeadersToRequest>();
|
||||||
|
|
||||||
|
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_addHeaders.Object);
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseHttpRequestHeadersBuilderMiddleware();
|
|
||||||
});
|
|
||||||
|
|
||||||
_downstreamRequest = new HttpRequestMessage();
|
_downstreamRequest = new HttpRequestMessage();
|
||||||
|
ScopedRepository
|
||||||
_scopedRepository
|
|
||||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
GivenTheTestServerIsConfigured();
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -86,6 +54,27 @@ namespace Ocelot.UnitTests.Headers
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_addHeaders.Object);
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseHttpRequestHeadersBuilderMiddleware();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||||
|
{
|
||||||
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
|
ScopedRepository
|
||||||
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
|
.Returns(_downstreamRoute);
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheAddHeadersToDownstreamRequestReturnsOk()
|
private void GivenTheAddHeadersToDownstreamRequestReturnsOk()
|
||||||
{
|
{
|
||||||
_addHeaders
|
_addHeaders
|
||||||
@ -104,24 +93,5 @@ namespace Ocelot.UnitTests.Headers
|
|||||||
It.IsAny<IEnumerable<System.Security.Claims.Claim>>(),
|
It.IsAny<IEnumerable<System.Security.Claims.Claim>>(),
|
||||||
_downstreamRequest), Times.Once);
|
_downstreamRequest), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
|
||||||
{
|
|
||||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
|
||||||
_scopedRepository
|
|
||||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
|
||||||
.Returns(_downstreamRoute);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_client.Dispose();
|
|
||||||
_server.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,34 +1,26 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.Errors;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.LoadBalancer.LoadBalancers;
|
|
||||||
using Ocelot.LoadBalancer.Middleware;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using Ocelot.Values;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
using Shouldly;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.LoadBalancer
|
namespace Ocelot.UnitTests.LoadBalancer
|
||||||
{
|
{
|
||||||
public class LoadBalancerMiddlewareTests
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.Errors;
|
||||||
|
using Ocelot.LoadBalancer.LoadBalancers;
|
||||||
|
using Ocelot.LoadBalancer.Middleware;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using Ocelot.Values;
|
||||||
|
using Shouldly;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class LoadBalancerMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<ILoadBalancerHouse> _loadBalancerHouse;
|
private readonly Mock<ILoadBalancerHouse> _loadBalancerHouse;
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly Mock<ILoadBalancer> _loadBalancer;
|
private readonly Mock<ILoadBalancer> _loadBalancer;
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private HttpResponseMessage _result;
|
|
||||||
private HostAndPort _hostAndPort;
|
private HostAndPort _hostAndPort;
|
||||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||||
private ErrorResponse<ILoadBalancer> _getLoadBalancerHouseError;
|
private ErrorResponse<ILoadBalancer> _getLoadBalancerHouseError;
|
||||||
@ -37,35 +29,17 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
|
|
||||||
public LoadBalancerMiddlewareTests()
|
public LoadBalancerMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
|
||||||
_loadBalancerHouse = new Mock<ILoadBalancerHouse>();
|
_loadBalancerHouse = new Mock<ILoadBalancerHouse>();
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
_loadBalancer = new Mock<ILoadBalancer>();
|
_loadBalancer = new Mock<ILoadBalancer>();
|
||||||
_loadBalancerHouse = new Mock<ILoadBalancerHouse>();
|
_loadBalancerHouse = new Mock<ILoadBalancerHouse>();
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_loadBalancerHouse.Object);
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseLoadBalancingMiddleware();
|
|
||||||
});
|
|
||||||
|
|
||||||
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "");
|
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "");
|
||||||
_scopedRepository
|
|
||||||
|
ScopedRepository
|
||||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||||
_server = new TestServer(builder);
|
|
||||||
_client = _server.CreateClient();
|
GivenTheTestServerIsConfigured();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -118,6 +92,19 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_loadBalancerHouse.Object);
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseLoadBalancingMiddleware();
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheDownStreamUrlIs(string downstreamUrl)
|
private void GivenTheDownStreamUrlIs(string downstreamUrl)
|
||||||
{
|
{
|
||||||
_downstreamRequest.RequestUri = new System.Uri(downstreamUrl);
|
_downstreamRequest.RequestUri = new System.Uri(downstreamUrl);
|
||||||
@ -142,7 +129,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||||
{
|
{
|
||||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
.Returns(_downstreamRoute);
|
.Returns(_downstreamRoute);
|
||||||
}
|
}
|
||||||
@ -154,7 +141,6 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.Returns(new OkResponse<ILoadBalancer>(_loadBalancer.Object));
|
.Returns(new OkResponse<ILoadBalancer>(_loadBalancer.Object));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void GivenTheLoadBalancerHouseReturnsAnError()
|
private void GivenTheLoadBalancerHouseReturnsAnError()
|
||||||
{
|
{
|
||||||
_getLoadBalancerHouseError = new ErrorResponse<ILoadBalancer>(new List<Ocelot.Errors.Error>()
|
_getLoadBalancerHouseError = new ErrorResponse<ILoadBalancer>(new List<Ocelot.Errors.Error>()
|
||||||
@ -167,49 +153,36 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.Returns(_getLoadBalancerHouseError);
|
.Returns(_getLoadBalancerHouseError);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline()
|
private void ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline()
|
||||||
{
|
{
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Verify(x => x.Add("OcelotMiddlewareError", true), Times.Once);
|
.Verify(x => x.Add("OcelotMiddlewareError", true), Times.Once);
|
||||||
|
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Verify(x => x.Add("OcelotMiddlewareErrors", _getLoadBalancerHouseError.Errors), Times.Once);
|
.Verify(x => x.Add("OcelotMiddlewareErrors", _getLoadBalancerHouseError.Errors), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenAnErrorSayingReleaseFailedIsSetOnThePipeline()
|
private void ThenAnErrorSayingReleaseFailedIsSetOnThePipeline()
|
||||||
{
|
{
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Verify(x => x.Add("OcelotMiddlewareError", true), Times.Once);
|
.Verify(x => x.Add("OcelotMiddlewareError", true), Times.Once);
|
||||||
|
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Verify(x => x.Add("OcelotMiddlewareErrors", It.IsAny<List<Error>>()), Times.Once);
|
.Verify(x => x.Add("OcelotMiddlewareErrors", It.IsAny<List<Error>>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenAnErrorStatingHostAndPortCouldNotBeFoundIsSetOnPipeline()
|
private void ThenAnErrorStatingHostAndPortCouldNotBeFoundIsSetOnPipeline()
|
||||||
{
|
{
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Verify(x => x.Add("OcelotMiddlewareError", true), Times.Once);
|
.Verify(x => x.Add("OcelotMiddlewareError", true), Times.Once);
|
||||||
|
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Verify(x => x.Add("OcelotMiddlewareErrors", _getHostAndPortError.Errors), Times.Once);
|
.Verify(x => x.Add("OcelotMiddlewareErrors", _getHostAndPortError.Errors), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void ThenTheDownstreamUrlIsReplacedWith(string expectedUri)
|
private void ThenTheDownstreamUrlIsReplacedWith(string expectedUri)
|
||||||
{
|
{
|
||||||
_downstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
|
_downstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_client.Dispose();
|
|
||||||
_server.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -19,6 +19,10 @@
|
|||||||
<DebugSymbols>True</DebugSymbols>
|
<DebugSymbols>True</DebugSymbols>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Remove="Responder\HttpContextResponderTests.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj" />
|
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -1,67 +1,37 @@
|
|||||||
using System;
|
namespace Ocelot.UnitTests.QueryStrings
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Configuration;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.QueryStrings;
|
|
||||||
using Ocelot.QueryStrings.Middleware;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
using System.Security.Claims;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.QueryStrings
|
|
||||||
{
|
{
|
||||||
public class QueryStringBuilderMiddlewareTests : IDisposable
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
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.Responses;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
|
||||||
|
public class QueryStringBuilderMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly Mock<IAddQueriesToRequest> _addQueries;
|
private readonly Mock<IAddQueriesToRequest> _addQueries;
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private readonly HttpRequestMessage _downstreamRequest;
|
private readonly HttpRequestMessage _downstreamRequest;
|
||||||
private Response<DownstreamRoute> _downstreamRoute;
|
private Response<DownstreamRoute> _downstreamRoute;
|
||||||
private HttpResponseMessage _result;
|
|
||||||
|
|
||||||
public QueryStringBuilderMiddlewareTests()
|
public QueryStringBuilderMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
_addQueries = new Mock<IAddQueriesToRequest>();
|
_addQueries = new Mock<IAddQueriesToRequest>();
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_addQueries.Object);
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseQueryStringBuilderMiddleware();
|
|
||||||
});
|
|
||||||
|
|
||||||
_downstreamRequest = new HttpRequestMessage();
|
_downstreamRequest = new HttpRequestMessage();
|
||||||
|
ScopedRepository.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||||
_scopedRepository.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
|
||||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
GivenTheTestServerIsConfigured();
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -84,6 +54,19 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_addQueries.Object);
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseQueryStringBuilderMiddleware();
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheAddHeadersToRequestReturnsOk()
|
private void GivenTheAddHeadersToRequestReturnsOk()
|
||||||
{
|
{
|
||||||
_addQueries
|
_addQueries
|
||||||
@ -103,23 +86,12 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
_downstreamRequest), Times.Once);
|
_downstreamRequest), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||||
{
|
{
|
||||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
.Returns(_downstreamRoute);
|
.Returns(_downstreamRoute);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_client.Dispose();
|
|
||||||
_server.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,71 +1,33 @@
|
|||||||
using Microsoft.AspNetCore.Hosting;
|
namespace Ocelot.UnitTests.RateLimit
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.RateLimit;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using System.IO;
|
|
||||||
using Ocelot.RateLimit.Middleware;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using Xunit;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Shouldly;
|
|
||||||
using Ocelot.Configuration;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.RateLimit
|
|
||||||
{
|
{
|
||||||
public class ClientRateLimitMiddlewareTests
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Configuration;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.RateLimit;
|
||||||
|
using Ocelot.RateLimit.Middleware;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using Shouldly;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class ClientRateLimitMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||||
private int responseStatusCode;
|
private int responseStatusCode;
|
||||||
|
|
||||||
public ClientRateLimitMiddlewareTests()
|
public ClientRateLimitMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879/api/ClientRateLimit";
|
GivenTheTestServerIsConfigured();
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddMemoryCache();
|
|
||||||
x.AddSingleton<IRateLimitCounterHandler, MemoryCacheRateLimitCounterHandler>();
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseRateLimiting();
|
|
||||||
app.Run(async context =>
|
|
||||||
{
|
|
||||||
context.Response.StatusCode = 200;
|
|
||||||
await context.Response.WriteAsync("This is ratelimit test");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_call_middleware_and_ratelimiting()
|
public void should_call_middleware_and_ratelimiting()
|
||||||
{
|
{
|
||||||
@ -98,11 +60,29 @@ namespace Ocelot.UnitTests.RateLimit
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddMemoryCache();
|
||||||
|
services.AddSingleton<IRateLimitCounterHandler, MemoryCacheRateLimitCounterHandler>();
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseRateLimiting();
|
||||||
|
app.Run(async context =>
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 200;
|
||||||
|
await context.Response.WriteAsync("This is ratelimit test");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||||
{
|
{
|
||||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
.Returns(_downstreamRoute);
|
.Returns(_downstreamRoute);
|
||||||
}
|
}
|
||||||
@ -110,28 +90,27 @@ namespace Ocelot.UnitTests.RateLimit
|
|||||||
private void WhenICallTheMiddlewareMultipleTime(int times)
|
private void WhenICallTheMiddlewareMultipleTime(int times)
|
||||||
{
|
{
|
||||||
var clientId = "ocelotclient1";
|
var clientId = "ocelotclient1";
|
||||||
// Act
|
|
||||||
for (int i = 0; i < times; i++)
|
for (int i = 0; i < times; i++)
|
||||||
{
|
{
|
||||||
var request = new HttpRequestMessage(new HttpMethod("GET"), _url);
|
var request = new HttpRequestMessage(new HttpMethod("GET"), Url);
|
||||||
request.Headers.Add("ClientId", clientId);
|
request.Headers.Add("ClientId", clientId);
|
||||||
|
|
||||||
var response = _client.SendAsync(request);
|
var response = Client.SendAsync(request);
|
||||||
responseStatusCode = (int)response.Result.StatusCode;
|
responseStatusCode = (int)response.Result.StatusCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddlewareWithWhiteClient()
|
private void WhenICallTheMiddlewareWithWhiteClient()
|
||||||
{
|
{
|
||||||
var clientId = "ocelotclient2";
|
var clientId = "ocelotclient2";
|
||||||
// Act
|
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++)
|
||||||
{
|
{
|
||||||
var request = new HttpRequestMessage(new HttpMethod("GET"), _url);
|
var request = new HttpRequestMessage(new HttpMethod("GET"), Url);
|
||||||
request.Headers.Add("ClientId", clientId);
|
request.Headers.Add("ClientId", clientId);
|
||||||
|
|
||||||
var response = _client.SendAsync(request);
|
var response = Client.SendAsync(request);
|
||||||
responseStatusCode = (int)response.Result.StatusCode;
|
responseStatusCode = (int)response.Result.StatusCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,63 +1,37 @@
|
|||||||
using System;
|
namespace Ocelot.UnitTests.Request
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Request.Builder;
|
|
||||||
using Ocelot.Request.Middleware;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
using Ocelot.Requester.QoS;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Request
|
|
||||||
{
|
{
|
||||||
public class HttpRequestBuilderMiddlewareTests : IDisposable
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
|
using Ocelot.Infrastructure.RequestData;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Request.Builder;
|
||||||
|
using Ocelot.Request.Middleware;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
using Ocelot.Requester.QoS;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
|
||||||
|
public class HttpRequestBuilderMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestCreator> _requestBuilder;
|
private readonly Mock<IRequestCreator> _requestBuilder;
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
||||||
private readonly Mock<IQosProviderHouse> _qosProviderHouse;
|
private readonly Mock<IQosProviderHouse> _qosProviderHouse;
|
||||||
private readonly HttpRequestMessage _downstreamRequest;
|
private readonly HttpRequestMessage _downstreamRequest;
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private HttpResponseMessage _result;
|
|
||||||
private OkResponse<Ocelot.Request.Request> _request;
|
private OkResponse<Ocelot.Request.Request> _request;
|
||||||
private OkResponse<string> _downstreamUrl;
|
private OkResponse<string> _downstreamUrl;
|
||||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||||
|
|
||||||
public HttpRequestBuilderMiddlewareTests()
|
public HttpRequestBuilderMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
|
||||||
_qosProviderHouse = new Mock<IQosProviderHouse>();
|
_qosProviderHouse = new Mock<IQosProviderHouse>();
|
||||||
_requestBuilder = new Mock<IRequestCreator>();
|
_requestBuilder = new Mock<IRequestCreator>();
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_qosProviderHouse.Object);
|
|
||||||
x.AddSingleton(_requestBuilder.Object);
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseHttpRequestBuilderMiddleware();
|
|
||||||
});
|
|
||||||
|
|
||||||
_downstreamRequest = new HttpRequestMessage();
|
_downstreamRequest = new HttpRequestMessage();
|
||||||
|
|
||||||
@ -65,8 +39,7 @@ namespace Ocelot.UnitTests.Request
|
|||||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
GivenTheTestServerIsConfigured();
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -88,6 +61,28 @@ namespace Ocelot.UnitTests.Request
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_qosProviderHouse.Object);
|
||||||
|
services.AddSingleton(_requestBuilder.Object);
|
||||||
|
services.AddSingleton(_scopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseHttpRequestBuilderMiddleware();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheDownStreamUrlIs(string downstreamUrl)
|
||||||
|
{
|
||||||
|
_downstreamUrl = new OkResponse<string>(downstreamUrl);
|
||||||
|
_scopedRepository
|
||||||
|
.Setup(x => x.Get<string>(It.IsAny<string>()))
|
||||||
|
.Returns(_downstreamUrl);
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheQosProviderHouseReturns(Response<IQoSProvider> qosProvider)
|
private void GivenTheQosProviderHouseReturns(Response<IQoSProvider> qosProvider)
|
||||||
{
|
{
|
||||||
_qosProviderHouse
|
_qosProviderHouse
|
||||||
@ -117,24 +112,5 @@ namespace Ocelot.UnitTests.Request
|
|||||||
_scopedRepository
|
_scopedRepository
|
||||||
.Verify(x => x.Add("Request", _request.Data), Times.Once());
|
.Verify(x => x.Add("Request", _request.Data), Times.Once());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheDownStreamUrlIs(string downstreamUrl)
|
|
||||||
{
|
|
||||||
_downstreamUrl = new OkResponse<string>(downstreamUrl);
|
|
||||||
_scopedRepository
|
|
||||||
.Setup(x => x.Get<string>(It.IsAny<string>()))
|
|
||||||
.Returns(_downstreamUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_client.Dispose();
|
|
||||||
_server.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,74 +1,40 @@
|
|||||||
using System;
|
namespace Ocelot.UnitTests.RequestId
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Configuration.Builder;
|
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.RequestId.Middleware;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.RequestId
|
|
||||||
{
|
{
|
||||||
public class RequestIdMiddlewareTests
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Configuration.Builder;
|
||||||
|
using Ocelot.DownstreamRouteFinder;
|
||||||
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.RequestId.Middleware;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using Shouldly;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class RequestIdMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly HttpRequestMessage _downstreamRequest;
|
private readonly HttpRequestMessage _downstreamRequest;
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private Response<DownstreamRoute> _downstreamRoute;
|
private Response<DownstreamRoute> _downstreamRoute;
|
||||||
private HttpResponseMessage _result;
|
|
||||||
private string _value;
|
private string _value;
|
||||||
private string _key;
|
private string _key;
|
||||||
|
|
||||||
public RequestIdMiddlewareTests()
|
public RequestIdMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseRequestIdMiddleware();
|
|
||||||
|
|
||||||
app.Run(x =>
|
|
||||||
{
|
|
||||||
x.Response.Headers.Add("LSRequestId", x.TraceIdentifier);
|
|
||||||
return Task.CompletedTask;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
|
||||||
_client = _server.CreateClient();
|
|
||||||
|
|
||||||
_downstreamRequest = new HttpRequestMessage();
|
_downstreamRequest = new HttpRequestMessage();
|
||||||
|
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||||
|
|
||||||
|
GivenTheTestServerIsConfigured();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -106,10 +72,28 @@ namespace Ocelot.UnitTests.RequestId
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseRequestIdMiddleware();
|
||||||
|
|
||||||
|
app.Run(x =>
|
||||||
|
{
|
||||||
|
x.Response.Headers.Add("LSRequestId", x.TraceIdentifier);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||||
{
|
{
|
||||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
.Returns(_downstreamRoute);
|
.Returns(_downstreamRoute);
|
||||||
}
|
}
|
||||||
@ -118,28 +102,17 @@ namespace Ocelot.UnitTests.RequestId
|
|||||||
{
|
{
|
||||||
_key = key;
|
_key = key;
|
||||||
_value = value;
|
_value = value;
|
||||||
_client.DefaultRequestHeaders.TryAddWithoutValidation(_key, _value);
|
Client.DefaultRequestHeaders.TryAddWithoutValidation(_key, _value);
|
||||||
}
|
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheTraceIdIsAnything()
|
private void ThenTheTraceIdIsAnything()
|
||||||
{
|
{
|
||||||
_result.Headers.GetValues("LSRequestId").First().ShouldNotBeNullOrEmpty();
|
ResponseMessage.Headers.GetValues("LSRequestId").First().ShouldNotBeNullOrEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheTraceIdIs(string expected)
|
private void ThenTheTraceIdIs(string expected)
|
||||||
{
|
{
|
||||||
_result.Headers.GetValues("LSRequestId").First().ShouldBe(expected);
|
ResponseMessage.Headers.GetValues("LSRequestId").First().ShouldBe(expected);
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_client.Dispose();
|
|
||||||
_server.Dispose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,62 +1,28 @@
|
|||||||
using System;
|
namespace Ocelot.UnitTests.Requester
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.QueryStrings.Middleware;
|
|
||||||
using Ocelot.Requester;
|
|
||||||
using Ocelot.Requester.Middleware;
|
|
||||||
using Ocelot.Requester.QoS;
|
|
||||||
using Ocelot.Responder;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Requester
|
|
||||||
{
|
{
|
||||||
public class HttpRequesterMiddlewareTests : IDisposable
|
using System.Net.Http;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Requester;
|
||||||
|
using Ocelot.Requester.Middleware;
|
||||||
|
using Ocelot.Requester.QoS;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class HttpRequesterMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IHttpRequester> _requester;
|
private readonly Mock<IHttpRequester> _requester;
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private HttpResponseMessage _result;
|
|
||||||
private OkResponse<HttpResponseMessage> _response;
|
private OkResponse<HttpResponseMessage> _response;
|
||||||
private OkResponse<Ocelot.Request.Request> _request;
|
private OkResponse<Ocelot.Request.Request> _request;
|
||||||
|
|
||||||
public HttpRequesterMiddlewareTests()
|
public HttpRequesterMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
|
||||||
_requester = new Mock<IHttpRequester>();
|
_requester = new Mock<IHttpRequester>();
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_requester.Object);
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseHttpRequesterMiddleware();
|
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
GivenTheTestServerIsConfigured();
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -70,6 +36,27 @@ namespace Ocelot.UnitTests.Requester
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_requester.Object);
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseHttpRequesterMiddleware();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheRequestIs(Ocelot.Request.Request request)
|
||||||
|
{
|
||||||
|
_request = new OkResponse<Ocelot.Request.Request>(request);
|
||||||
|
ScopedRepository
|
||||||
|
.Setup(x => x.Get<Ocelot.Request.Request>(It.IsAny<string>()))
|
||||||
|
.Returns(_request);
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheRequesterReturns(HttpResponseMessage response)
|
private void GivenTheRequesterReturns(HttpResponseMessage response)
|
||||||
{
|
{
|
||||||
_response = new OkResponse<HttpResponseMessage>(response);
|
_response = new OkResponse<HttpResponseMessage>(response);
|
||||||
@ -80,34 +67,15 @@ namespace Ocelot.UnitTests.Requester
|
|||||||
|
|
||||||
private void GivenTheScopedRepoReturns()
|
private void GivenTheScopedRepoReturns()
|
||||||
{
|
{
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Setup(x => x.Add(It.IsAny<string>(), _response.Data))
|
.Setup(x => x.Add(It.IsAny<string>(), _response.Data))
|
||||||
.Returns(new OkResponse());
|
.Returns(new OkResponse());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheScopedRepoIsCalledCorrectly()
|
private void ThenTheScopedRepoIsCalledCorrectly()
|
||||||
{
|
{
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Verify(x => x.Add("HttpResponseMessage", _response.Data), Times.Once());
|
.Verify(x => x.Add("HttpResponseMessage", _response.Data), Times.Once());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheRequestIs(Ocelot.Request.Request request)
|
|
||||||
{
|
|
||||||
_request = new OkResponse<Ocelot.Request.Request>(request);
|
|
||||||
_scopedRepository
|
|
||||||
.Setup(x => x.Get<Ocelot.Request.Request>(It.IsAny<string>()))
|
|
||||||
.Returns(_request);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_client.Dispose();
|
|
||||||
_server.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
test/Ocelot.UnitTests/Responder/AnyError.cs
Normal file
15
test/Ocelot.UnitTests/Responder/AnyError.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using Ocelot.Errors;
|
||||||
|
|
||||||
|
namespace Ocelot.UnitTests.Responder
|
||||||
|
{
|
||||||
|
class AnyError : Error
|
||||||
|
{
|
||||||
|
public AnyError() : base("blahh", OcelotErrorCode.UnknownError)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnyError(OcelotErrorCode errorCode) : base("blah", errorCode)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
using Ocelot.Errors;
|
using Ocelot.Errors;
|
||||||
using Ocelot.Middleware;
|
|
||||||
using Ocelot.Requester;
|
|
||||||
using Ocelot.Responder;
|
using Ocelot.Responder;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
@ -21,47 +20,127 @@ namespace Ocelot.UnitTests.Responder
|
|||||||
_codeMapper = new ErrorsToHttpStatusCodeMapper();
|
_codeMapper = new ErrorsToHttpStatusCodeMapper();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Theory]
|
||||||
public void should_return_timeout()
|
[InlineData(OcelotErrorCode.UnauthenticatedError)]
|
||||||
|
public void should_return_unauthorized(OcelotErrorCode errorCode)
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereAreErrors(new List<Error>
|
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.Unauthorized);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(OcelotErrorCode.CannotFindClaimError)]
|
||||||
|
[InlineData(OcelotErrorCode.ClaimValueNotAuthorisedError)]
|
||||||
|
[InlineData(OcelotErrorCode.ScopeNotAuthorisedError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnauthorizedError)]
|
||||||
|
[InlineData(OcelotErrorCode.UserDoesNotHaveClaimError)]
|
||||||
|
public void should_return_forbidden(OcelotErrorCode errorCode)
|
||||||
{
|
{
|
||||||
new RequestTimedOutError(new Exception())
|
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.Forbidden);
|
||||||
}))
|
}
|
||||||
.When(x => x.WhenIGetErrorStatusCode())
|
|
||||||
.Then(x => x.ThenTheResponseIsStatusCodeIs(503))
|
[Theory]
|
||||||
.BDDfy();
|
[InlineData(OcelotErrorCode.RequestTimedOutError)]
|
||||||
|
public void should_return_service_unavailable(OcelotErrorCode errorCode)
|
||||||
|
{
|
||||||
|
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.ServiceUnavailable);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(OcelotErrorCode.CannotAddDataError)]
|
||||||
|
[InlineData(OcelotErrorCode.CannotFindDataError)]
|
||||||
|
[InlineData(OcelotErrorCode.DownstreamHostNullOrEmptyError)]
|
||||||
|
[InlineData(OcelotErrorCode.DownstreamPathNullOrEmptyError)]
|
||||||
|
[InlineData(OcelotErrorCode.DownstreampathTemplateAlreadyUsedError)]
|
||||||
|
[InlineData(OcelotErrorCode.DownstreamPathTemplateContainsSchemeError)]
|
||||||
|
[InlineData(OcelotErrorCode.DownstreamSchemeNullOrEmptyError)]
|
||||||
|
[InlineData(OcelotErrorCode.InstructionNotForClaimsError)]
|
||||||
|
[InlineData(OcelotErrorCode.NoInstructionsError)]
|
||||||
|
[InlineData(OcelotErrorCode.ParsingConfigurationHeaderError)]
|
||||||
|
[InlineData(OcelotErrorCode.RateLimitOptionsError)]
|
||||||
|
[InlineData(OcelotErrorCode.ServicesAreEmptyError)]
|
||||||
|
[InlineData(OcelotErrorCode.ServicesAreNullError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToCompleteRequestError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToCreateAuthenticationHandlerError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToFindDownstreamRouteError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToFindLoadBalancerError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToFindServiceDiscoveryProviderError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToFindQoSProviderError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnableToSetConfigInConsulError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnknownError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnmappableRequestError)]
|
||||||
|
[InlineData(OcelotErrorCode.UnsupportedAuthenticationProviderError)]
|
||||||
|
public void should_return_not_found(OcelotErrorCode errorCode)
|
||||||
|
{
|
||||||
|
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_create_unauthenticated_response_code()
|
public void AuthenticationErrorsHaveHighestPriority()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereAreErrors(new List<Error>
|
var errors = new List<OcelotErrorCode>
|
||||||
{
|
{
|
||||||
new UnauthenticatedError("no matter")
|
OcelotErrorCode.CannotAddDataError,
|
||||||
}))
|
OcelotErrorCode.CannotFindClaimError,
|
||||||
.When(x => x.WhenIGetErrorStatusCode())
|
OcelotErrorCode.UnauthenticatedError,
|
||||||
.Then(x => x.ThenTheResponseIsStatusCodeIs(401))
|
OcelotErrorCode.RequestTimedOutError,
|
||||||
.BDDfy();
|
};
|
||||||
|
|
||||||
|
ShouldMapErrorsToStatusCode(errors, HttpStatusCode.Unauthorized);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_create_not_found_response_response_code()
|
public void AuthorisationErrorsHaveSecondHighestPriority()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereAreErrors(new List<Error>
|
var errors = new List<OcelotErrorCode>
|
||||||
{
|
{
|
||||||
new AnyError()
|
OcelotErrorCode.CannotAddDataError,
|
||||||
}))
|
OcelotErrorCode.CannotFindClaimError,
|
||||||
.When(x => x.WhenIGetErrorStatusCode())
|
OcelotErrorCode.RequestTimedOutError
|
||||||
.Then(x => x.ThenTheResponseIsStatusCodeIs(404))
|
};
|
||||||
.BDDfy();
|
|
||||||
|
ShouldMapErrorsToStatusCode(errors, HttpStatusCode.Forbidden);
|
||||||
}
|
}
|
||||||
|
|
||||||
class AnyError : Error
|
[Fact]
|
||||||
|
public void ServiceUnavailableErrorsHaveThirdHighestPriority()
|
||||||
{
|
{
|
||||||
public AnyError() : base("blahh", OcelotErrorCode.UnknownError)
|
var errors = new List<OcelotErrorCode>
|
||||||
{
|
{
|
||||||
|
OcelotErrorCode.CannotAddDataError,
|
||||||
|
OcelotErrorCode.RequestTimedOutError
|
||||||
|
};
|
||||||
|
|
||||||
|
ShouldMapErrorsToStatusCode(errors, HttpStatusCode.ServiceUnavailable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void check_we_have_considered_all_errors_in_these_tests()
|
||||||
|
{
|
||||||
|
// If this test fails then it's because the number of error codes has changed.
|
||||||
|
// You should make the appropriate changes to the test cases here to ensure
|
||||||
|
// they cover all the error codes, and then modify this assertion.
|
||||||
|
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(30, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode)
|
||||||
|
{
|
||||||
|
ShouldMapErrorsToStatusCode(new List<OcelotErrorCode> { errorCode }, expectedHttpStatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ShouldMapErrorsToStatusCode(List<OcelotErrorCode> errorCodes, HttpStatusCode expectedHttpStatusCode)
|
||||||
|
{
|
||||||
|
var errors = new List<Error>();
|
||||||
|
|
||||||
|
foreach(var errorCode in errorCodes)
|
||||||
|
{
|
||||||
|
errors.Add(new AnyError(errorCode));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.Given(x => x.GivenThereAreErrors(errors))
|
||||||
|
.When(x => x.WhenIGetErrorStatusCode())
|
||||||
|
.Then(x => x.ThenTheResponseIsStatusCodeIs(expectedHttpStatusCode))
|
||||||
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenThereAreErrors(List<Error> errors)
|
private void GivenThereAreErrors(List<Error> errors)
|
||||||
@ -78,5 +157,10 @@ namespace Ocelot.UnitTests.Responder
|
|||||||
{
|
{
|
||||||
_result.ShouldBe(expectedCode);
|
_result.ShouldBe(expectedCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ThenTheResponseIsStatusCodeIs(HttpStatusCode expectedCode)
|
||||||
|
{
|
||||||
|
_result.ShouldBe((int)expectedCode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,60 +1,28 @@
|
|||||||
using System;
|
namespace Ocelot.UnitTests.Responder
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
|
||||||
using Ocelot.Responder;
|
|
||||||
using Ocelot.Responder.Middleware;
|
|
||||||
using Ocelot.Responses;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Responder
|
|
||||||
{
|
{
|
||||||
public class ResponderMiddlewareTests : IDisposable
|
using System.Net.Http;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responder;
|
||||||
|
using Ocelot.Responder.Middleware;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class ResponderMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IHttpResponder> _responder;
|
private readonly Mock<IHttpResponder> _responder;
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly Mock<IErrorsToHttpStatusCodeMapper> _codeMapper;
|
private readonly Mock<IErrorsToHttpStatusCodeMapper> _codeMapper;
|
||||||
private readonly string _url;
|
|
||||||
private readonly TestServer _server;
|
|
||||||
private readonly HttpClient _client;
|
|
||||||
private HttpResponseMessage _result;
|
|
||||||
private OkResponse<HttpResponseMessage> _response;
|
private OkResponse<HttpResponseMessage> _response;
|
||||||
|
|
||||||
public ResponderMiddlewareTests()
|
public ResponderMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
|
||||||
_responder = new Mock<IHttpResponder>();
|
_responder = new Mock<IHttpResponder>();
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
_codeMapper = new Mock<IErrorsToHttpStatusCodeMapper>();
|
_codeMapper = new Mock<IErrorsToHttpStatusCodeMapper>();
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_codeMapper.Object);
|
|
||||||
x.AddSingleton(_responder.Object);
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseResponderMiddleware();
|
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
GivenTheTestServerIsConfigured();
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -67,9 +35,31 @@ namespace Ocelot.UnitTests.Responder
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_codeMapper.Object);
|
||||||
|
services.AddSingleton(_responder.Object);
|
||||||
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseResponderMiddleware();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheHttpResponseMessageIs(HttpResponseMessage response)
|
||||||
|
{
|
||||||
|
_response = new OkResponse<HttpResponseMessage>(response);
|
||||||
|
ScopedRepository
|
||||||
|
.Setup(x => x.Get<HttpResponseMessage>(It.IsAny<string>()))
|
||||||
|
.Returns(_response);
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenThereAreNoPipelineErrors()
|
private void GivenThereAreNoPipelineErrors()
|
||||||
{
|
{
|
||||||
_scopedRepository
|
ScopedRepository
|
||||||
.Setup(x => x.Get<bool>(It.IsAny<string>()))
|
.Setup(x => x.Get<bool>(It.IsAny<string>()))
|
||||||
.Returns(new OkResponse<bool>(false));
|
.Returns(new OkResponse<bool>(false));
|
||||||
}
|
}
|
||||||
@ -78,24 +68,5 @@ namespace Ocelot.UnitTests.Responder
|
|||||||
{
|
{
|
||||||
//todo a better assert?
|
//todo a better assert?
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheHttpResponseMessageIs(HttpResponseMessage response)
|
|
||||||
{
|
|
||||||
_response = new OkResponse<HttpResponseMessage>(response);
|
|
||||||
_scopedRepository
|
|
||||||
.Setup(x => x.Get<HttpResponseMessage>(It.IsAny<string>()))
|
|
||||||
.Returns(_response);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_client.Dispose();
|
|
||||||
_server.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
62
test/Ocelot.UnitTests/ServerHostedMiddlewareTest.cs
Normal file
62
test/Ocelot.UnitTests/ServerHostedMiddlewareTest.cs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
namespace Ocelot.UnitTests
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Infrastructure.RequestData;
|
||||||
|
|
||||||
|
public abstract class ServerHostedMiddlewareTest : IDisposable
|
||||||
|
{
|
||||||
|
protected TestServer Server { get; private set; }
|
||||||
|
protected HttpClient Client { get; private set; }
|
||||||
|
protected string Url { get; private set; }
|
||||||
|
protected HttpResponseMessage ResponseMessage { get; private set; }
|
||||||
|
protected Mock<IRequestScopedDataRepository> ScopedRepository { get; private set; }
|
||||||
|
|
||||||
|
public ServerHostedMiddlewareTest()
|
||||||
|
{
|
||||||
|
Url = "http://localhost:51879";
|
||||||
|
ScopedRepository = new Mock<IRequestScopedDataRepository>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void GivenTheTestServerIsConfigured()
|
||||||
|
{
|
||||||
|
var builder = new WebHostBuilder()
|
||||||
|
.ConfigureServices(x => GivenTheTestServerServicesAreConfigured(x))
|
||||||
|
.UseUrls(Url)
|
||||||
|
.UseKestrel()
|
||||||
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
|
.UseIISIntegration()
|
||||||
|
.Configure(app => GivenTheTestServerPipelineIsConfigured(app));
|
||||||
|
|
||||||
|
Server = new TestServer(builder);
|
||||||
|
Client = Server.CreateClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
// override this in your test fixture to set up service dependencies
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
// override this in your test fixture to set up the test server pipeline
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void WhenICallTheMiddleware()
|
||||||
|
{
|
||||||
|
ResponseMessage = Client.GetAsync(Url).Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Client.Dispose();
|
||||||
|
Server.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -28,6 +28,7 @@ namespace Ocelot.UnitTests.ServiceDiscovery
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
public void should_lookup_service()
|
public void should_lookup_service()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenAServiceIsRegistered("product", "localhost:600", 80))
|
this.Given(x => x.GivenAServiceIsRegistered("product", "localhost:600", 80))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user