Beispiel #1
0
        public void AddNewMappingGeneric()
        {
            var mappings = new HttpStatusCodeMappings();

            mappings.Add <ArgumentNullException>(400);

            Assert.Equal(400, mappings.GetStatusCode(typeof(ArgumentNullException)));
        }
Beispiel #2
0
        public void OverrideExistingGeneric()
        {
            var mappings = new HttpStatusCodeMappings();

            mappings.Add <NotFoundException>(500);

            Assert.Equal(500, mappings.GetStatusCode(typeof(NotFoundException)));
        }
Beispiel #3
0
        public void InitializeDefaults()
        {
            var mappings = new HttpStatusCodeMappings();

            Assert.Equal(404, mappings.GetStatusCode(typeof(NotFoundException)));
            Assert.Equal(400, mappings.GetStatusCode(typeof(ValidationException)));
            Assert.Equal(403, mappings.GetStatusCode(typeof(UnauthorizedException)));
        }
        private async Task LogsExceptionsOccurredInHandler()
        {
            var mappings = new HttpStatusCodeMappings();
            var handler  = new ExceptionHandler(mappings, _logger);

            var mockHttpContext = new Mock <HttpContext>();

            mockHttpContext.SetupGet(c => c.Features).Throws <Exception>();

            await handler.HandleAsync(mockHttpContext.Object);

            Assert.StartsWith("Error", _logger.LoggedMessages[0]);
        }
        private async Task HandlesNonBaseExceptionWithCustomStatusCode()
        {
            var mappings = new HttpStatusCodeMappings();

            mappings.Add <NullReferenceException>(400);
            var handler = new ExceptionHandler(mappings, _logger);

            var exception       = new NullReferenceException();
            var mockHttpContext = CreateMockHttpContext(exception);

            await handler.HandleAsync(mockHttpContext);

            Assert.Equal(400, _mockHttpResponse.Object.StatusCode);
            Assert.StartsWith("Debug", _logger.LoggedMessages[0]);
        }
Beispiel #6
0
        public void AddNewMappingsRange()
        {
            var newRange = new Dictionary <Type, int>()
            {
                { typeof(ArgumentNullException), 400 },
                { typeof(InvalidOperationException), 500 }
            };

            var mappings = new HttpStatusCodeMappings();

            mappings.AddRange(newRange);

            Assert.Equal(400, mappings.GetStatusCode(typeof(ArgumentNullException)));
            Assert.Equal(500, mappings.GetStatusCode(typeof(InvalidOperationException)));
        }
        public static IApplicationBuilder UseExceptionHandling(this IApplicationBuilder app, Action <HttpStatusCodeMappings> setupAction)
        {
            var logger = app.ApplicationServices.GetService <ILogger <ExceptionHandler> >();

            var mappings = new HttpStatusCodeMappings();

            setupAction.Invoke(mappings);

            var handler = new ExceptionHandler(mappings, logger);

            return(app.UseExceptionHandler(appBuilder =>
            {
                appBuilder.Run(async context => await handler.HandleAsync(context));
            }));
        }
        private async Task HandlesNonBaseException()
        {
            var mappings = new HttpStatusCodeMappings();
            var handler  = new ExceptionHandler(mappings, _logger);

            var exception       = new ArgumentNullException();
            var mockHttpContext = CreateMockHttpContext(exception);

            await handler.HandleAsync(mockHttpContext);

            Assert.Equal(500, _mockHttpResponse.Object.StatusCode);

            var returnedError = GetResponseBodyAsError(_mockHttpResponse.Object);

            Assert.NotNull(returnedError);
            Assert.Equal("Exception of type System.ArgumentNullException occurred. Check logs for more info.", returnedError.Messages.First().Message);

            Assert.StartsWith("Error", _logger.LoggedMessages[0]);
        }
        private async Task HandlesBaseExceptionWithStatusCode4xx()
        {
            var mappings = new HttpStatusCodeMappings();
            var handler  = new ExceptionHandler(mappings, _logger);

            var exception       = new NotFoundException();
            var mockHttpContext = CreateMockHttpContext(exception);

            await handler.HandleAsync(mockHttpContext);

            Assert.Equal(404, _mockHttpResponse.Object.StatusCode);
            Assert.Equal("application/json", _mockHttpResponse.Object.ContentType);

            var serializedError = JsonConvert.SerializeObject(exception.Error);

            Assert.Equal(serializedError, GetResponseBodyAsString(_mockHttpResponse.Object));
            Assert.StartsWith("Debug", _logger.LoggedMessages[0]);
            Assert.Contains(serializedError, _logger.LoggedMessages[0]);
        }
Beispiel #10
0
        public void ContainsKeyReturnsFalse()
        {
            var mappings = new HttpStatusCodeMappings();

            Assert.False(mappings.ContainsKey(typeof(ArgumentNullException)));
        }
Beispiel #11
0
        public void ContainsKeyReturnsTrue()
        {
            var mappings = new HttpStatusCodeMappings();

            Assert.True(mappings.ContainsKey(typeof(NotFoundException)));
        }