|
| 1 | +using Microsoft.AspNetCore.Http; |
| 2 | +using Ocelot.Configuration.File; |
| 3 | +using Shouldly; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Net; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | + |
| 8 | +namespace Ocelot.Testing; |
| 9 | + |
| 10 | +public class TimeoutSteps : AcceptanceSteps |
| 11 | +{ |
| 12 | + public static int Ms(int seconds) => 1000 * seconds; |
| 13 | + |
| 14 | + public FileConfiguration GivenConfiguration(int port, int? routeTimeout = null, int? globalTimeout = null) |
| 15 | + { |
| 16 | + var route = GivenDefaultRoute(port); |
| 17 | + route.Timeout = routeTimeout; |
| 18 | + var configuration = GivenConfiguration(route); |
| 19 | + configuration.GlobalConfiguration.Timeout = globalTimeout; |
| 20 | + return configuration; |
| 21 | + } |
| 22 | + |
| 23 | + public virtual void GivenThereIsAServiceRunningOn(int port, HttpStatusCode statusCode, int timeout, |
| 24 | + [CallerMemberName] string response = nameof(TimeoutSteps)) |
| 25 | + { |
| 26 | + async Task MapBodyWithTimeout(HttpContext context) |
| 27 | + { |
| 28 | + await Task.Delay(timeout); |
| 29 | + context.Response.StatusCode = (int)statusCode; |
| 30 | + await context.Response.WriteAsync(response); |
| 31 | + } |
| 32 | + handler.GivenThereIsAServiceRunningOn(port, MapBodyWithTimeout); |
| 33 | + } |
| 34 | + |
| 35 | + public async Task<Stopwatch> WatchWhenIGetUrlOnTheApiGateway(string? upstream = null) |
| 36 | + { |
| 37 | + var watcher = Stopwatch.StartNew(); |
| 38 | + await WhenIGetUrlOnTheApiGateway(upstream ?? "/"); |
| 39 | + watcher.Stop(); |
| 40 | + return watcher; |
| 41 | + } |
| 42 | + |
| 43 | + public static void ThenTimeoutIsInRange(Stopwatch watcher, int lowDurationMs, int highDurationMs) |
| 44 | + { |
| 45 | + var expectedLowDuration = TimeSpan.FromMilliseconds(lowDurationMs); |
| 46 | + var expectedHighDuration = TimeSpan.FromMilliseconds(highDurationMs); |
| 47 | + watcher.Elapsed.ShouldBeGreaterThan(expectedLowDuration); |
| 48 | + watcher.Elapsed.ShouldBeLessThan(expectedHighDuration); |
| 49 | + } |
| 50 | +} |
0 commit comments