mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
Moved common middleare test setup into a base class
This commit is contained in:
parent
b0c12431d6
commit
8042bbab2c
@ -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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,87 +1,70 @@
|
|||||||
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.Infrastructure.RequestData;
|
||||||
|
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<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>();
|
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
||||||
_authFactory = new Mock<IAuthenticationHandlerFactory>();
|
_authFactory = new Mock<IAuthenticationHandlerFactory>();
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_authFactory.Object);
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseAuthenticationMiddleware();
|
|
||||||
|
|
||||||
app.Run(async x =>
|
GivenTheTestServerIsConfigured();
|
||||||
{
|
|
||||||
await x.Response.WriteAsync("The user is authenticated");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_call_next_middleware_if_route_is_not_authenticated()
|
public void should_call_next_middleware_if_route_is_not_authenticated()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(), new ReRouteBuilder()
|
this.Given(x => x.GivenTheDownStreamRouteIs(
|
||||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
new DownstreamRoute(
|
||||||
.Build())))
|
new List<UrlPathPlaceholderNameAndValue>(),
|
||||||
|
new ReRouteBuilder().WithUpstreamHttpMethod(new List<string> { "Get" }).Build())))
|
||||||
.When(x => x.WhenICallTheMiddleware())
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
.Then(x => x.ThenTheUserIsAuthenticated())
|
.Then(x => x.ThenTheUserIsAuthenticated())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_authFactory.Object);
|
||||||
|
services.AddSingleton(_scopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseAuthenticationMiddleware();
|
||||||
|
|
||||||
|
app.Run(async x =>
|
||||||
|
{
|
||||||
|
await x.Response.WriteAsync("The user is authenticated");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,17 +75,5 @@ namespace Ocelot.UnitTests.Authentication
|
|||||||
.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,35 @@
|
|||||||
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.Infrastructure.RequestData;
|
||||||
|
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<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>();
|
_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 +46,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 +81,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,34 +1,27 @@
|
|||||||
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.Infrastructure.RequestData;
|
||||||
|
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 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()
|
||||||
@ -36,32 +29,11 @@ namespace Ocelot.UnitTests.Cache
|
|||||||
_cacheManager = new Mock<IOcelotCache<HttpResponseMessage>>();
|
_cacheManager = new Mock<IOcelotCache<HttpResponseMessage>>();
|
||||||
_scopedRepo = new Mock<IRequestScopedDataRepository>();
|
_scopedRepo = new Mock<IRequestScopedDataRepository>();
|
||||||
|
|
||||||
_url = "http://localhost:51879";
|
|
||||||
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
|
_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 +59,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(_scopedRepo.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()
|
||||||
|
{
|
||||||
|
_scopedRepo
|
||||||
|
.Setup(x => x.Get<HttpResponseMessage>("HttpResponseMessage"))
|
||||||
|
.Returns(new OkResponse<HttpResponseMessage>(new HttpResponseMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenTheDownstreamRouteIs()
|
private void GivenTheDownstreamRouteIs()
|
||||||
{
|
{
|
||||||
@ -128,25 +128,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,34 @@
|
|||||||
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.Infrastructure.RequestData;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class ClaimsBuilderMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
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>();
|
_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 +51,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 +86,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,32 @@
|
|||||||
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.Infrastructure.RequestData;
|
||||||
|
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 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>();
|
_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 +44,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 +65,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,64 +1,39 @@
|
|||||||
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<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>();
|
_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");
|
||||||
|
|
||||||
@ -66,8 +41,7 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
|
|||||||
.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 +62,20 @@ 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);
|
||||||
@ -109,20 +97,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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,14 +7,9 @@ using Microsoft.AspNetCore.Hosting;
|
|||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.DownstreamRouteFinder;
|
|
||||||
using Ocelot.DownstreamUrlCreator;
|
|
||||||
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
|
|
||||||
using Ocelot.Errors.Middleware;
|
using Ocelot.Errors.Middleware;
|
||||||
using Ocelot.Infrastructure.RequestData;
|
using Ocelot.Infrastructure.RequestData;
|
||||||
using Ocelot.Logging;
|
using Ocelot.Logging;
|
||||||
using Ocelot.Responses;
|
|
||||||
using Ocelot.Values;
|
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
@ -53,6 +48,33 @@ namespace Ocelot.UnitTests.Errors
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
private void ThenTheResponseIsOk()
|
private void ThenTheResponseIsOk()
|
||||||
{
|
{
|
||||||
_result.StatusCode.ShouldBe(HttpStatusCode.OK);
|
_result.StatusCode.ShouldBe(HttpStatusCode.OK);
|
||||||
@ -95,31 +117,6 @@ namespace Ocelot.UnitTests.Errors
|
|||||||
_client = _server.CreateClient();
|
_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,40 @@
|
|||||||
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.Infrastructure.RequestData;
|
||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class HttpRequestHeadersBuilderMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
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>();
|
_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 +57,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 +96,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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
58
test/Ocelot.UnitTests/HostedMiddlewareTest.cs
Normal file
58
test/Ocelot.UnitTests/HostedMiddlewareTest.cs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
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; }
|
||||||
|
|
||||||
|
public ServerHostedMiddlewareTest()
|
||||||
|
{
|
||||||
|
Url = "http://localhost:51879";
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,34 +1,28 @@
|
|||||||
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.Infrastructure.RequestData;
|
||||||
|
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<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 +31,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>();
|
_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 +94,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);
|
||||||
@ -154,7 +143,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,11 +155,6 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.Returns(_getLoadBalancerHouseError);
|
.Returns(_getLoadBalancerHouseError);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline()
|
private void ThenAnErrorStatingLoadBalancerCouldNotBeFoundIsSetOnPipeline()
|
||||||
{
|
{
|
||||||
_scopedRepository
|
_scopedRepository
|
||||||
@ -181,7 +164,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.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);
|
||||||
@ -190,7 +173,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.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);
|
||||||
@ -199,17 +182,9 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
.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,40 @@
|
|||||||
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.Infrastructure.RequestData;
|
||||||
|
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<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>();
|
_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 +57,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,11 +89,6 @@ 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);
|
||||||
@ -115,11 +96,5 @@ namespace Ocelot.UnitTests.QueryStrings
|
|||||||
.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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,48 +24,19 @@ using Ocelot.Configuration;
|
|||||||
|
|
||||||
namespace Ocelot.UnitTests.RateLimit
|
namespace Ocelot.UnitTests.RateLimit
|
||||||
{
|
{
|
||||||
public class ClientRateLimitMiddlewareTests
|
public class ClientRateLimitMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
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";
|
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
_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);
|
GivenTheTestServerIsConfigured();
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_call_middleware_and_ratelimiting()
|
public void should_call_middleware_and_ratelimiting()
|
||||||
{
|
{
|
||||||
@ -98,6 +69,24 @@ 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)
|
||||||
{
|
{
|
||||||
@ -110,28 +99,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,43 @@
|
|||||||
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.Infrastructure.RequestData;
|
||||||
|
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 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 = new Mock<IRequestScopedDataRepository>();
|
||||||
_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,6 +75,24 @@ 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);
|
||||||
@ -118,28 +105,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,31 @@
|
|||||||
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.Infrastructure.RequestData;
|
||||||
|
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 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>();
|
_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 +39,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);
|
||||||
@ -90,24 +80,5 @@ namespace Ocelot.UnitTests.Requester
|
|||||||
_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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
98
test/Ocelot.UnitTests/Responder/HttpContextResponderTests.cs
Normal file
98
test/Ocelot.UnitTests/Responder/HttpContextResponderTests.cs
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Moq;
|
||||||
|
using Ocelot.Headers;
|
||||||
|
using Ocelot.Responder;
|
||||||
|
using Shouldly;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Ocelot.UnitTests.Responder
|
||||||
|
{
|
||||||
|
public class HttpContextResponderTests
|
||||||
|
{
|
||||||
|
readonly HttpContextResponder _responder;
|
||||||
|
|
||||||
|
readonly Mock<IRemoveOutputHeaders> _removeOutputHeaders;
|
||||||
|
|
||||||
|
HttpContext _httpContext;
|
||||||
|
|
||||||
|
HttpResponseMessage _httpResponseMessage;
|
||||||
|
|
||||||
|
public HttpContextResponderTests()
|
||||||
|
{
|
||||||
|
_removeOutputHeaders = new Mock<IRemoveOutputHeaders>();
|
||||||
|
_httpContext = new DefaultHttpContext();
|
||||||
|
_httpResponseMessage = new HttpResponseMessage();
|
||||||
|
_httpResponseMessage.Content = new MyHttpContent();
|
||||||
|
_responder = new HttpContextResponder(_removeOutputHeaders.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DoSomething()
|
||||||
|
{
|
||||||
|
this.Given(_ => GivenTheHttpResponseMessageHasHeader("abc","123"))
|
||||||
|
.And(_ => GivenTheHttpResponseMessageHasHeader("def", new[] { "456", "789" }))
|
||||||
|
.And(_ => GivenTheContextResponseHasHeader("abc", "123"))
|
||||||
|
.When(_ => WhenWeSetTheResponseOnAnHttpContext())
|
||||||
|
.Then(_ => ThenSupportedHeadersAreAddedToTheContextResponse())
|
||||||
|
.And(_ => ThenUnsupportedHeadersAreNotAddedToTheResponse())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void GivenTheHttpResponseMessageHasHeader(string name, string value)
|
||||||
|
{
|
||||||
|
_httpResponseMessage.Headers.Add(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheHttpResponseMessageHasHeader(string name, IEnumerable<string> values)
|
||||||
|
{
|
||||||
|
_httpResponseMessage.Headers.Add(name, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheContextResponseHasHeader(string name, string value)
|
||||||
|
{
|
||||||
|
_httpContext.Response.Headers.Add(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenWeSetTheResponseOnAnHttpContext()
|
||||||
|
{
|
||||||
|
_responder.SetResponseOnHttpContext(_httpContext, _httpResponseMessage).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenSupportedHeadersAreAddedToTheContextResponse()
|
||||||
|
{
|
||||||
|
_httpContext.Response.Headers.Count.ShouldBe(2);
|
||||||
|
_httpContext.Response.Headers.ShouldContain(h => h.Key == "abc" && h.Value == "123");
|
||||||
|
_httpContext.Response.Headers.ShouldContain(h => h.Key == "def" && h.Value == "456, 789");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenUnsupportedHeadersAreNotAddedToTheResponse()
|
||||||
|
{
|
||||||
|
_removeOutputHeaders.Verify(roh => roh.Remove(_httpResponseMessage.Headers), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class MyHttpContent : HttpContent
|
||||||
|
{
|
||||||
|
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
|
||||||
|
{
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override bool TryComputeLength(out long length)
|
||||||
|
{
|
||||||
|
length = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,6 @@
|
|||||||
using System;
|
using System.Net.Http;
|
||||||
using System.IO;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Infrastructure.RequestData;
|
using Ocelot.Infrastructure.RequestData;
|
||||||
using Ocelot.Logging;
|
using Ocelot.Logging;
|
||||||
@ -14,77 +9,109 @@ using Ocelot.Responder.Middleware;
|
|||||||
using Ocelot.Responses;
|
using Ocelot.Responses;
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Shouldly;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Ocelot.Errors;
|
||||||
|
using System.Net;
|
||||||
|
using Ocelot.Headers;
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Responder
|
namespace Ocelot.UnitTests.Responder
|
||||||
{
|
{
|
||||||
public class ResponderMiddlewareTests : IDisposable
|
public class ResponderMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IHttpResponder> _responder;
|
private readonly IHttpResponder _responder;
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
||||||
private readonly Mock<IErrorsToHttpStatusCodeMapper> _codeMapper;
|
private readonly Mock<IErrorsToHttpStatusCodeMapper> _codeMapper;
|
||||||
private readonly string _url;
|
private readonly Mock<IRemoveOutputHeaders> _outputHeaderRemover;
|
||||||
private readonly TestServer _server;
|
private HttpStatusCode _httpStatusFromController;
|
||||||
private readonly HttpClient _client;
|
private string _contentFromController;
|
||||||
private HttpResponseMessage _result;
|
|
||||||
private OkResponse<HttpResponseMessage> _response;
|
private OkResponse<HttpResponseMessage> _response;
|
||||||
|
private List<Error> _pipelineErrors;
|
||||||
|
|
||||||
public ResponderMiddlewareTests()
|
public ResponderMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:51879";
|
_outputHeaderRemover = new Mock<IRemoveOutputHeaders>();
|
||||||
_responder = new Mock<IHttpResponder>();
|
_responder = new HttpContextResponder(_outputHeaderRemover.Object);
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
_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]
|
||||||
public void should_not_return_any_errors()
|
public void PipelineErrors()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenTheHttpResponseMessageIs(new HttpResponseMessage()))
|
var responseMessage = new HttpResponseMessage(System.Net.HttpStatusCode.Continue);
|
||||||
|
|
||||||
|
this.Given(x => x.GivenTheIncomingHttpResponseMessageIs(new HttpResponseMessage()))
|
||||||
|
.And(x => x.GivenThereArePipelineErrors())
|
||||||
|
.And(x => x.GivenTheErrorWillBeMappedToAnHttpStatus())
|
||||||
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
|
.Then(x => x.ThenThereAreErrors())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NoPipelineErrors()
|
||||||
|
{
|
||||||
|
this.Given(x => x.GivenTheIncomingHttpResponseMessageIs(new HttpResponseMessage()))
|
||||||
.And(x => x.GivenThereAreNoPipelineErrors())
|
.And(x => x.GivenThereAreNoPipelineErrors())
|
||||||
.When(x => x.WhenICallTheMiddleware())
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
.Then(x => x.ThenThereAreNoErrors())
|
.Then(x => x.ThenThereAreNoErrors())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
|
services.AddLogging();
|
||||||
|
services.AddSingleton(_codeMapper.Object);
|
||||||
|
services.AddSingleton(_responder);
|
||||||
|
services.AddSingleton(_scopedRepository.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseResponderMiddleware();
|
||||||
|
app.Run(SetControllerResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SetControllerResponse(HttpContext context)
|
||||||
|
{
|
||||||
|
_httpStatusFromController = HttpStatusCode.OK;
|
||||||
|
_contentFromController = "test response";
|
||||||
|
context.Response.StatusCode = (int)_httpStatusFromController;
|
||||||
|
await context.Response.WriteAsync(_contentFromController);
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenThereAreNoPipelineErrors()
|
private void GivenThereAreNoPipelineErrors()
|
||||||
{
|
{
|
||||||
|
GivenThereArePipelineErrors(new List<Error>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenThereArePipelineErrors()
|
||||||
|
{
|
||||||
|
GivenThereArePipelineErrors(new List<Error>() { new AnyError() });
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenThereArePipelineErrors(List<Error> pipelineErrors)
|
||||||
|
{
|
||||||
|
_pipelineErrors = pipelineErrors;
|
||||||
|
|
||||||
_scopedRepository
|
_scopedRepository
|
||||||
.Setup(x => x.Get<bool>(It.IsAny<string>()))
|
.Setup(x => x.Get<bool>("OcelotMiddlewareError"))
|
||||||
.Returns(new OkResponse<bool>(false));
|
.Returns(new OkResponse<bool>(_pipelineErrors.Count != 0));
|
||||||
|
|
||||||
|
_scopedRepository
|
||||||
|
.Setup(sr => sr.Get<List<Error>>("OcelotMiddlewareErrors"))
|
||||||
|
.Returns(new OkResponse<List<Error>>(_pipelineErrors));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenThereAreNoErrors()
|
private void GivenTheIncomingHttpResponseMessageIs(HttpResponseMessage response)
|
||||||
{
|
|
||||||
//todo a better assert?
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WhenICallTheMiddleware()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync(_url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheHttpResponseMessageIs(HttpResponseMessage response)
|
|
||||||
{
|
{
|
||||||
_response = new OkResponse<HttpResponseMessage>(response);
|
_response = new OkResponse<HttpResponseMessage>(response);
|
||||||
_scopedRepository
|
_scopedRepository
|
||||||
@ -92,10 +119,21 @@ namespace Ocelot.UnitTests.Responder
|
|||||||
.Returns(_response);
|
.Returns(_response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
private void GivenTheErrorWillBeMappedToAnHttpStatus()
|
||||||
{
|
{
|
||||||
_client.Dispose();
|
_codeMapper.Setup(cm => cm.Map(_pipelineErrors))
|
||||||
_server.Dispose();
|
.Returns((int)HttpStatusCode.InternalServerError);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenThereAreNoErrors()
|
||||||
|
{
|
||||||
|
ResponseMessage.StatusCode.ShouldBe(_httpStatusFromController);
|
||||||
|
ResponseMessage.Content.ReadAsStringAsync().Result.ShouldBe(_contentFromController);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenThereAreErrors()
|
||||||
|
{
|
||||||
|
ResponseMessage.StatusCode.ShouldBe(System.Net.HttpStatusCode.BadRequest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ namespace Ocelot.UnitTests.Responder
|
|||||||
{
|
{
|
||||||
public class ResponderMiddlewareTestsV2
|
public class ResponderMiddlewareTestsV2
|
||||||
{
|
{
|
||||||
|
private readonly ResponderMiddleware _middleware;
|
||||||
private readonly Mock<IHttpResponder> _responder;
|
private readonly Mock<IHttpResponder> _responder;
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
||||||
private readonly Mock<IErrorsToHttpStatusCodeMapper> _codeMapper;
|
private readonly Mock<IErrorsToHttpStatusCodeMapper> _codeMapper;
|
||||||
@ -22,7 +23,6 @@ namespace Ocelot.UnitTests.Responder
|
|||||||
private readonly Mock<IOcelotLoggerFactory> _loggerFactory;
|
private readonly Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||||
private readonly Mock<IOcelotLogger> _logger;
|
private readonly Mock<IOcelotLogger> _logger;
|
||||||
private readonly Mock<HttpContext> _httpContext;
|
private readonly Mock<HttpContext> _httpContext;
|
||||||
private ResponderMiddleware _middleware;
|
|
||||||
private OkResponse<HttpResponseMessage> _response;
|
private OkResponse<HttpResponseMessage> _response;
|
||||||
private int _mappedStatusCode;
|
private int _mappedStatusCode;
|
||||||
private List<Error> _pipelineErrors;
|
private List<Error> _pipelineErrors;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user