diff --git a/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs b/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs index ac786ec1..5bf848cd 100644 --- a/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs @@ -1,125 +1,87 @@ -using System; -using System.IO; -using System.Net; -using System.Net.Http; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.TestHost; -using Microsoft.Extensions.DependencyInjection; -using Moq; -using Ocelot.Errors.Middleware; -using Ocelot.Infrastructure.RequestData; -using Ocelot.Logging; -using Shouldly; -using TestStack.BDDfy; -using Xunit; -using System.Threading.Tasks; - namespace Ocelot.UnitTests.Errors { - public class ExceptionHandlerMiddlewareTests + using System; + using System.Net; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; + using Ocelot.Errors.Middleware; + using Ocelot.Logging; + using Shouldly; + using TestStack.BDDfy; + using Xunit; + using Microsoft.AspNetCore.Http; + + public class ExceptionHandlerMiddlewareTests : ServerHostedMiddlewareTest { - private readonly Mock _scopedRepository; - private readonly string _url; - private TestServer _server; - private HttpClient _client; - private HttpResponseMessage _result; + bool _shouldThrowAnException = false; public ExceptionHandlerMiddlewareTests() { - _url = "http://localhost:52879"; - _scopedRepository = new Mock(); + GivenTheTestServerIsConfigured(); } [Fact] - public void should_call_next_middleware() + public void NoDownstreamException() { - this.Given(_ => GivenASuccessfulRequest()) - .When(_ => WhenIMakeTheRequest()) + this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream()) + .When(_ => WhenICallTheMiddleware()) .Then(_ => ThenTheResponseIsOk()) .BDDfy(); } [Fact] - public void should_call_return_error() + public void DownstreamException() { - this.Given(_ => GivenAnError()) - .When(_ => WhenIMakeTheRequest()) + this.Given(_ => GivenAnExceptionWillBeThrownDownstream()) + .When(_ => WhenICallTheMiddleware()) .Then(_ => ThenTheResponseIsError()) .BDDfy(); } - private void GivenAnError() + protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services) { - var builder = new WebHostBuilder() - .ConfigureServices(x => - { - x.AddSingleton(); - x.AddLogging(); - x.AddSingleton(_scopedRepository.Object); - }) - .UseUrls(_url) - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() - .UseUrls(_url) - .Configure(app => - { - app.UseExceptionHandlerMiddleware(); - app.Use(async (context, next) => - { - await Task.CompletedTask; - throw new Exception("BOOM"); - }); - }); + services.AddSingleton(); + services.AddLogging(); + services.AddSingleton(ScopedRepository.Object); + } - _server = new TestServer(builder); - _client = _server.CreateClient(); + protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app) + { + app.UseExceptionHandlerMiddleware(); + app.Run(DownstreamExceptionSimulator); + } + + private async Task DownstreamExceptionSimulator(HttpContext context) + { + await Task.CompletedTask; + + if (_shouldThrowAnException) + { + throw new Exception("BOOM"); + } + + context.Response.StatusCode = (int)HttpStatusCode.OK; + } + + private void GivenAnExceptionWillNotBeThrownDownstream() + { + _shouldThrowAnException = false; + } + + private void GivenAnExceptionWillBeThrownDownstream() + { + _shouldThrowAnException = true; } private void ThenTheResponseIsOk() { - _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(); - 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(); - } - - } } \ No newline at end of file