elfisa-pharmacy/tests/Elfisa.Tests/TestHelpers.cs
Exest 3d89fac6e0
Some checks failed
Desktop tests / test (push) Has been cancelled
тесты: автотесты десктопа (xUnit) + CI
- проект tests/Elfisa.Tests: 56 юнит-тестов на Elfisa.Core + Elfisa.Avalonia:
  версии апдейтера, срок JWT-сессии, ApiClient (URL/ошибки/ретраи/парсинг),
  DTO-контракты, черновики, корзина, дрилдаун прайса (МГ/базовое имя)
- тест-швы: internal-методы через InternalsVisibleTo, ApiClient(HttpMessageHandler)
- CI .github/workflows/desktop-tests.yml — dotnet test при пуше (GitHub/Gitea Actions)
- README с описанием покрытия

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-17 11:51:50 +03:00

42 lines
1.7 KiB
C#

using System.Net;
using System.Net.Http;
using System.Text;
namespace Elfisa.Tests;
/// <summary>Мок HttpMessageHandler: отдаёт заданные ответы или бросает ошибки.</summary>
internal sealed class FakeHttpHandler : HttpMessageHandler
{
private readonly Func<HttpRequestMessage, int, HttpResponseMessage> _responder;
public int Calls { get; private set; }
public HttpRequestMessage? LastRequest { get; private set; }
public FakeHttpHandler(Func<HttpRequestMessage, int, HttpResponseMessage> responder) => _responder = responder;
public FakeHttpHandler(HttpStatusCode code, string body)
: this((_, __) => Json(code, body)) { }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
LastRequest = request;
var i = Calls++;
return Task.FromResult(_responder(request, i));
}
public static HttpResponseMessage Json(HttpStatusCode code, string body) =>
new(code) { Content = new StringContent(body, Encoding.UTF8, "application/json") };
}
/// <summary>Генератор JWT для тестов срока действия сессии.</summary>
internal static class Jwt
{
public static long Now => DateTimeOffset.UtcNow.ToUnixTimeSeconds();
private static string B64(string s) =>
Convert.ToBase64String(Encoding.UTF8.GetBytes(s)).TrimEnd('=').Replace('+', '-').Replace('/', '_');
public static string Make(long exp) => $"{B64("{\"alg\":\"HS256\"}")}.{B64($"{{\"exp\":{exp}}}")}.sig";
public static string MakeNoExp() => $"{B64("{\"alg\":\"HS256\"}")}.{B64("{\"sub\":\"x\"}")}.sig";
}