Esempio n. 1
0
        public void AddNewMappingGeneric()
        {
            var mappings = new HttpStatusCodeMappings();

            mappings.Add <ArgumentNullException>(400);

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

            mappings.Add <NotFoundException>(500);

            Assert.Equal(500, mappings.GetStatusCode(typeof(NotFoundException)));
        }
        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]);
        }
        private async Task HandlesBaseExceptionWithStatusCode5xx()
        {
            var mappings = new HttpStatusCodeMappings();

            mappings.Add <NotFoundException>(500);
            var handler = new ExceptionHandler(mappings, _logger);

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

            await handler.HandleAsync(mockHttpContext);

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

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

            Assert.Equal(serializedError, GetResponseBodyAsString(_mockHttpResponse.Object));
            Assert.StartsWith("Error", _logger.LoggedMessages[0]);
            Assert.Contains(serializedError, _logger.LoggedMessages[0]);
        }