|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.AspNetCore.Builder; |
| 8 | +using Microsoft.AspNetCore.Http; |
| 9 | +using Microsoft.Extensions.Logging.Abstractions; |
| 10 | +using Microsoft.Extensions.Options; |
| 11 | +using Moq; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace Microsoft.AspNetCore.Diagnostics |
| 15 | +{ |
| 16 | + public class ExceptionHandlerMiddlewareTest |
| 17 | + { |
| 18 | + [Fact] |
| 19 | + public async Task Invoke_ExceptionThrownResultsInClearedRouteValuesAndEndpoint() |
| 20 | + { |
| 21 | + // Arrange |
| 22 | + var httpContext = CreateHttpContext(); |
| 23 | + httpContext.SetEndpoint(new Endpoint((_) => Task.CompletedTask, new EndpointMetadataCollection(), "Test")); |
| 24 | + httpContext.Request.RouteValues["John"] = "Doe"; |
| 25 | + |
| 26 | + var optionsAccessor = CreateOptionsAccessor( |
| 27 | + exceptionHandler: context => |
| 28 | + { |
| 29 | + Assert.Empty(context.Request.RouteValues); |
| 30 | + Assert.Null(context.GetEndpoint()); |
| 31 | + return Task.CompletedTask; |
| 32 | + }); |
| 33 | + var middleware = CreateMiddleware(_ => throw new InvalidOperationException(), optionsAccessor); |
| 34 | + |
| 35 | + // Act & Assert |
| 36 | + await middleware.Invoke(httpContext); |
| 37 | + } |
| 38 | + |
| 39 | + private HttpContext CreateHttpContext() |
| 40 | + { |
| 41 | + var httpContext = new DefaultHttpContext |
| 42 | + { |
| 43 | + RequestServices = new TestServiceProvider() |
| 44 | + }; |
| 45 | + |
| 46 | + return httpContext; |
| 47 | + } |
| 48 | + |
| 49 | + private IOptions<ExceptionHandlerOptions> CreateOptionsAccessor( |
| 50 | + RequestDelegate exceptionHandler = null, |
| 51 | + string exceptionHandlingPath = null) |
| 52 | + { |
| 53 | + exceptionHandler ??= c => Task.CompletedTask; |
| 54 | + var options = new ExceptionHandlerOptions() |
| 55 | + { |
| 56 | + ExceptionHandler = exceptionHandler, |
| 57 | + ExceptionHandlingPath = exceptionHandlingPath, |
| 58 | + }; |
| 59 | + var optionsAccessor = Mock.Of<IOptions<ExceptionHandlerOptions>>(o => o.Value == options); |
| 60 | + return optionsAccessor; |
| 61 | + } |
| 62 | + |
| 63 | + private ExceptionHandlerMiddleware CreateMiddleware( |
| 64 | + RequestDelegate next, |
| 65 | + IOptions<ExceptionHandlerOptions> options) |
| 66 | + { |
| 67 | + next ??= c => Task.CompletedTask; |
| 68 | + var listener = new DiagnosticListener("Microsoft.AspNetCore"); |
| 69 | + |
| 70 | + var middleware = new ExceptionHandlerMiddleware( |
| 71 | + next, |
| 72 | + NullLoggerFactory.Instance, |
| 73 | + options, |
| 74 | + listener); |
| 75 | + |
| 76 | + return middleware; |
| 77 | + } |
| 78 | + |
| 79 | + private class TestServiceProvider : IServiceProvider |
| 80 | + { |
| 81 | + public object GetService(Type serviceType) |
| 82 | + { |
| 83 | + throw new NotImplementedException(); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments