Example #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Bookstore API");
            });

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            ExceptionMiddlewareExtensions.ConfigureExceptionHandler(app);

            app.UseHttpsRedirection();

            app.UseRouting();
            app.UseStaticFiles();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerManager logger)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            //Exception Handling
            ExceptionMiddlewareExtensions.ConfigureExceptionHandler(app, logger);

            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Cars");
            });

            app.UseHttpsRedirection();

            app.UseMvc(routes => routes.MapRoute(
                           name: "default",
                           template: "{controller=Home}/{action=Index}/{id?}"));
        }
 public void WriteException_GivenException_ThrowsNoException()
 {
     try
     {
         ExceptionMiddlewareExtensions.WriteException(new Exception("Test Exception"));
     }
     catch (Exception ex)
     {
         Assert.True(false, $"Executing WriteException method should not return an exception. Exception Type {ex.GetType().ToString()} with message '{ex.Message}' caught.");
     }
 }
Example #4
0
        public async Task HandleExceptionsTestWithHandlerButNoExceptionWritesResponse()
        {
            // Act
            await ExceptionMiddlewareExtensions.HandleExceptions(_httpContext, _mockLogger.Object)
            .ConfigureAwait(false);

            // Assert
            await VerifyResponse().ConfigureAwait(false);

            _mockLogger.VerifyExact(LogLevel.Error, "Request failed.", Times.Once());
        }
Example #5
0
        public async Task HandleExceptionsTestNoExceptionHandlerWritesResponse()
        {
            // Arrange
            _httpContext.Features.Set <IExceptionHandlerFeature>(null);

            // Act
            await ExceptionMiddlewareExtensions.HandleExceptions(_httpContext, _mockLogger.Object)
            .ConfigureAwait(false);

            // Assert
            await VerifyResponse().ConfigureAwait(false);
        }
        public void ConfigureExceptionHandler_Executed_ThrowsNoException()
        {
            var mockApplicationBuilder = Substitute.For <IApplicationBuilder>();

            try
            {
                ExceptionMiddlewareExtensions.ConfigureExceptionHandler(mockApplicationBuilder);
            }
            catch (Exception ex)
            {
                Assert.True(false, $"Executing ConfigureExceptionHandler method should not return an exception. Exception Type {ex.GetType().ToString()} with message '{ex.Message}' caught.");
            }
        }
        public void WriteException_GivenNullException_ThrowsNoException()
        {
            var mockConfiguration = Substitute.For <IConfiguration>();

            try
            {
                ExceptionMiddlewareExtensions.WriteException(null);
            }
            catch (Exception ex)
            {
                Assert.True(false, $"Executing WriteException method with null exception should not return an exception. Exception Type {ex.GetType().ToString()} with message '{ex.Message}' caught.");
            }

            Assert.True(true, $"Executing WriteException method with null exception should not return an exception.");
        }
Example #8
0
        public async Task HandleExceptionsTestWithHandlerWithExceptionWritesResponse()
        {
            // Arrange
            var exMessage = "This is an exception";
            var exception = new System.Exception(exMessage);

            _mockExHandlerFeature.SetupGet(x => x.Error).Returns(exception);

            // Act
            await ExceptionMiddlewareExtensions.HandleExceptions(_httpContext, _mockLogger.Object)
            .ConfigureAwait(false);

            // Assert
            await VerifyResponse(exMessage).ConfigureAwait(false);

            _mockLogger.VerifyContains(LogLevel.Error, "Request failed.", Times.Once());
        }