From 226234075437716a21d7a9369bdacc3c197189e1 Mon Sep 17 00:00:00 2001 From: luxick Date: Mon, 9 Mar 2020 17:47:49 +0100 Subject: [PATCH] Add delay to requests to simulate slow connection --- EstusShots.Server/Filters/DebugDelayFilter.cs | 22 +++++++++++++++++ EstusShots.Server/Startup.cs | 24 ++++++++----------- 2 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 EstusShots.Server/Filters/DebugDelayFilter.cs diff --git a/EstusShots.Server/Filters/DebugDelayFilter.cs b/EstusShots.Server/Filters/DebugDelayFilter.cs new file mode 100644 index 0000000..5a32789 --- /dev/null +++ b/EstusShots.Server/Filters/DebugDelayFilter.cs @@ -0,0 +1,22 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.Filters; + +namespace EstusShots.Server.Filters +{ + /// + /// Adds 2 seconds of wait time to each request + /// + public class DebugDelayFilter : IActionFilter + + { + public void OnActionExecuted(ActionExecutedContext context) + { + + } + + public void OnActionExecuting(ActionExecutingContext context) + { + Task.Delay(2000).Wait(); + } + } +} \ No newline at end of file diff --git a/EstusShots.Server/Startup.cs b/EstusShots.Server/Startup.cs index df530af..67711ca 100644 --- a/EstusShots.Server/Startup.cs +++ b/EstusShots.Server/Startup.cs @@ -1,4 +1,5 @@ using AutoMapper; +using EstusShots.Server.Filters; using EstusShots.Server.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -15,7 +16,7 @@ namespace EstusShots.Server public IConfiguration Configuration { get; } private bool IsDevelopment { get; set; } - + public Startup(IConfiguration configuration) { Configuration = configuration; @@ -31,17 +32,15 @@ namespace EstusShots.Server opt.UseSqlite(Configuration.GetConnectionString("Sqlite")); }); services.AddAutoMapper(typeof(Startup)); - services.AddControllers().AddJsonOptions(options => - { - options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; - if (IsDevelopment) + services.AddControllers(options => { options.Filters.Add(typeof(DebugDelayFilter)); }) + .AddJsonOptions(options => { - options.JsonSerializerOptions.WriteIndented = true; - } - }); + options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; + if (IsDevelopment) options.JsonSerializerOptions.WriteIndented = true; + }); services.AddSwaggerGen(options => { - options.SwaggerDoc("v1", new OpenApiInfo { Title = "Estus Shots API", Version = "v1" }); + options.SwaggerDoc("v1", new OpenApiInfo {Title = "Estus Shots API", Version = "v1"}); }); // Register business logic services @@ -58,7 +57,7 @@ namespace EstusShots.Server IsDevelopment = env.IsDevelopment(); if (IsDevelopment) app.UseDeveloperExceptionPage(); - + // Do not Redirect for now. Breaks local tests. // app.UseHttpsRedirection(); @@ -67,10 +66,7 @@ namespace EstusShots.Server // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), // specifying the Swagger JSON endpoint. - app.UseSwaggerUI(c => - { - c.SwaggerEndpoint("/swagger/v1/swagger.json", "Estus Shots API V1"); - }); + app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Estus Shots API V1"); }); app.UseRouting(); app.UseAuthorization();