< Summary

Information
Class: Elsa.Resilience.Endpoints.SimulateResponse.SimulateResponseEndpoint
Assembly: Elsa.Resilience
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Resilience/Endpoints/SimulateResponse/Endpoint.cs
Line coverage
14%
Covered lines: 4
Uncovered lines: 24
Coverable lines: 28
Total lines: 55
Line coverage: 14.2%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.cctor()100%210%
Configure()100%11100%
HandleAsync()0%7280%
GetCodes()0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Resilience/Endpoints/SimulateResponse/Endpoint.cs

#LineLine coverage
 1using System.Text.Json;
 2using FastEndpoints;
 3using Microsoft.Extensions.Caching.Memory;
 4using static Elsa.Resilience.Endpoints.SimulateResponse.StatusCodeMessageLookup;
 5
 6namespace Elsa.Resilience.Endpoints.SimulateResponse;
 7
 18public class SimulateResponseEndpoint(IMemoryCache memoryCache) : EndpointWithoutRequest<SimulatedResponse>
 9{
 010    private static readonly TimeSpan SlidingExpirationTimeSpan = TimeSpan.FromMinutes(15);
 11
 12    public override void Configure()
 13    {
 114        Get("/simulate-response");
 115        AllowAnonymous();
 116    }
 17
 18    public override async Task HandleAsync(CancellationToken ct)
 19    {
 020        var sessionId = HttpContext.Request.Query["sessionId"].FirstOrDefault() ?? "default";
 021        var codes = GetCodes();
 022        var cacheKey = $"status-simulation-session-{sessionId}";
 023        var nextIndex = memoryCache.GetOrCreate(cacheKey, entry =>
 024        {
 025            entry.SlidingExpiration = SlidingExpirationTimeSpan;
 026            return 0;
 027        });
 28
 029        var currentCode = nextIndex < codes.Length ? codes[nextIndex] : codes[^1];
 030        var message = StatusMessages.TryGetValue(currentCode, out var reason)
 031            ? reason
 032            : $"Status Code {currentCode}";
 33
 034        if (nextIndex + 1 >= codes.Length)
 35        {
 036            memoryCache.Remove(cacheKey);
 37        }
 38        else
 39        {
 040            memoryCache.Set(cacheKey, nextIndex + 1, new MemoryCacheEntryOptions
 041            {
 042                SlidingExpiration = SlidingExpirationTimeSpan
 043            });
 44        }
 45
 046        await Send.ResponseAsync(new(message), currentCode, ct);
 047    }
 48
 49    private int[] GetCodes()
 50    {
 051        var codesParam = HttpContext.Request.Query["codes"].FirstOrDefault();
 052        int[] defaultCodes = [429, 503, 200];
 053        return string.IsNullOrWhiteSpace(codesParam) ? defaultCodes : JsonSerializer.Deserialize<int[]>(codesParam)!;
 54    }
 55}