mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
Refactor ExceptionHandlerMiddleware tests to use base class.
This commit is contained in:
parent
ca4efa5e10
commit
de5ae05bd6
@ -1,125 +1,87 @@
|
|||||||
|
namespace Ocelot.UnitTests.Errors
|
||||||
|
{
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Moq;
|
|
||||||
using Ocelot.Errors.Middleware;
|
using Ocelot.Errors.Middleware;
|
||||||
using Ocelot.Infrastructure.RequestData;
|
|
||||||
using Ocelot.Logging;
|
using Ocelot.Logging;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using System.Threading.Tasks;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Errors
|
public class ExceptionHandlerMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
public class ExceptionHandlerMiddlewareTests
|
bool _shouldThrowAnException = false;
|
||||||
{
|
|
||||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
|
||||||
private readonly string _url;
|
|
||||||
private TestServer _server;
|
|
||||||
private HttpClient _client;
|
|
||||||
private HttpResponseMessage _result;
|
|
||||||
|
|
||||||
public ExceptionHandlerMiddlewareTests()
|
public ExceptionHandlerMiddlewareTests()
|
||||||
{
|
{
|
||||||
_url = "http://localhost:52879";
|
GivenTheTestServerIsConfigured();
|
||||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_call_next_middleware()
|
public void NoDownstreamException()
|
||||||
{
|
{
|
||||||
this.Given(_ => GivenASuccessfulRequest())
|
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
|
||||||
.When(_ => WhenIMakeTheRequest())
|
.When(_ => WhenICallTheMiddleware())
|
||||||
.Then(_ => ThenTheResponseIsOk())
|
.Then(_ => ThenTheResponseIsOk())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_call_return_error()
|
public void DownstreamException()
|
||||||
{
|
{
|
||||||
this.Given(_ => GivenAnError())
|
this.Given(_ => GivenAnExceptionWillBeThrownDownstream())
|
||||||
.When(_ => WhenIMakeTheRequest())
|
.When(_ => WhenICallTheMiddleware())
|
||||||
.Then(_ => ThenTheResponseIsError())
|
.Then(_ => ThenTheResponseIsError())
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenAnError()
|
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||||
{
|
{
|
||||||
var builder = new WebHostBuilder()
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
.ConfigureServices(x =>
|
services.AddLogging();
|
||||||
{
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
}
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
{
|
||||||
app.UseExceptionHandlerMiddleware();
|
app.UseExceptionHandlerMiddleware();
|
||||||
app.Use(async (context, next) =>
|
app.Run(DownstreamExceptionSimulator);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task DownstreamExceptionSimulator(HttpContext context)
|
||||||
{
|
{
|
||||||
await Task.CompletedTask;
|
await Task.CompletedTask;
|
||||||
throw new Exception("BOOM");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
if (_shouldThrowAnException)
|
||||||
_client = _server.CreateClient();
|
{
|
||||||
|
throw new Exception("BOOM");
|
||||||
|
}
|
||||||
|
|
||||||
|
context.Response.StatusCode = (int)HttpStatusCode.OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenAnExceptionWillNotBeThrownDownstream()
|
||||||
|
{
|
||||||
|
_shouldThrowAnException = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenAnExceptionWillBeThrownDownstream()
|
||||||
|
{
|
||||||
|
_shouldThrowAnException = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheResponseIsOk()
|
private void ThenTheResponseIsOk()
|
||||||
{
|
{
|
||||||
_result.StatusCode.ShouldBe(HttpStatusCode.OK);
|
ResponseMessage.StatusCode.ShouldBe(HttpStatusCode.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheResponseIsError()
|
private void ThenTheResponseIsError()
|
||||||
{
|
{
|
||||||
_result.StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
|
ResponseMessage.StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenIMakeTheRequest()
|
|
||||||
{
|
|
||||||
_result = _client.GetAsync("/").Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenASuccessfulRequest()
|
|
||||||
{
|
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.ConfigureServices(x =>
|
|
||||||
{
|
|
||||||
x.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
|
||||||
x.AddLogging();
|
|
||||||
x.AddSingleton(_scopedRepository.Object);
|
|
||||||
})
|
|
||||||
.UseUrls(_url)
|
|
||||||
.UseKestrel()
|
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseUrls(_url)
|
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.UseExceptionHandlerMiddleware();
|
|
||||||
app.Run(async context =>
|
|
||||||
{
|
|
||||||
await Task.CompletedTask;
|
|
||||||
context.Response.StatusCode = 200;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
_server = new TestServer(builder);
|
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user