public async Task Invoke_Should_ReturnProblemDetailsResult_WhenUnauthorizedRequest() { _nextMock.Setup(n => n.Invoke(It.IsAny <HttpContext>())) .Returns((HttpContext ctx) => { ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; return(Task.CompletedTask); }); ProblemDetailsResult result = null; _actionResultExecutorMock.Setup(e => e.ExecuteAsync(It.IsAny <ActionContext>(), It.IsAny <ObjectResult>())) .Callback <ActionContext, ObjectResult>((actionContext, actionResult) => result = (ProblemDetailsResult)actionResult) .Returns(Task.CompletedTask); var services = new ServiceCollection(); services.AddTransient((_) => _actionResultExecutorMock.Object); var context = new DefaultHttpContext { RequestServices = services.BuildServiceProvider() }; await _middleware.Invoke(context); result.Should().NotBeNull(); result.StatusCode.Should().Be((int)HttpStatusCode.Unauthorized); result.Value.ShouldNotBeNull(HttpStatusCode.Unauthorized); result.Value.Title.Should().Be("Unauthorized"); }
public async Task Invoke_Should_NotLogError_WithCustomShouldLogExceptionConstraints() { var options = _optionsMock.Object.Value; options.ShouldLogException = (_) => false; _optionsMock.Setup(o => o.Value).Returns(options); _nextMock.Setup(n => n.Invoke(It.IsAny <HttpContext>())).Throws(new ApplicationException()); ProblemDetailsResult result = null; _actionResultExecutorMock.Setup(e => e.ExecuteAsync(It.IsAny <ActionContext>(), It.IsAny <ObjectResult>())) .Callback <ActionContext, ObjectResult>((actionContext, actionResult) => result = (ProblemDetailsResult)actionResult) .Returns(Task.CompletedTask); var services = new ServiceCollection(); services.AddTransient((_) => _actionResultExecutorMock.Object); var context = new DefaultHttpContext { RequestServices = services.BuildServiceProvider() }; await _middleware.Invoke(context); _loggerMock.Verify(l => l.Log( It.IsAny <LogLevel>(), It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Never); }
public void Constructor_Should_ConstructProblemDetailsResult() { var problemDetails = new ProblemDetails { Status = (int)HttpStatusCode.InternalServerError }; var problemDetailsResult = new ProblemDetailsResult(problemDetails); problemDetailsResult.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError); problemDetailsResult.DeclaredType.Should().Be(typeof(ProblemDetails)); problemDetailsResult.ContentTypes.Should().HaveCount(2); problemDetailsResult.ContentTypes.Should().Contain("application/problem+json"); problemDetailsResult.Value.Should().NotBeNull(); problemDetailsResult.Value.Should().BeOfType <ProblemDetails>(); }
public async Task Invoke_Should_ReturnProblemDetailsResult_ForApplicationException_WhenExceptionThrown() { _nextMock.Setup(n => n.Invoke(It.IsAny <HttpContext>())).Throws(new ApplicationException()); ProblemDetailsResult result = null; _actionResultExecutorMock.Setup(e => e.ExecuteAsync(It.IsAny <ActionContext>(), It.IsAny <ObjectResult>())) .Callback <ActionContext, ObjectResult>((actionContext, actionResult) => result = (ProblemDetailsResult)actionResult) .Returns(Task.CompletedTask); var services = new ServiceCollection(); services.AddTransient((_) => _actionResultExecutorMock.Object); var context = new DefaultHttpContext { RequestServices = services.BuildServiceProvider() }; await _middleware.Invoke(context); result.Should().NotBeNull(); result.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError); result.Value.ShouldNotBeNull(HttpStatusCode.InternalServerError); }