using System.Net; using System.Text; namespace SmsSluzba.Api.Tests.Api; /// Stands in for the SMS gateway: returns queued responses and records every request. internal sealed class FakeGatewayHandler : HttpMessageHandler { private readonly Queue> _responses = new(); public List<(Uri Uri, string? Body)> Requests { get; } = []; public void Enqueue(string body, HttpStatusCode status = HttpStatusCode.OK) => _responses.Enqueue(() => new HttpResponseMessage(status) { Content = new StringContent(body, Encoding.UTF8, "application/xml"), }); public void EnqueueException(Exception exception) => _responses.Enqueue(() => throw exception); protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var body = request.Content is null ? null : await request.Content.ReadAsStringAsync(cancellationToken); Requests.Add((request.RequestUri!, body)); if (_responses.Count == 0) { throw new InvalidOperationException("Unexpected gateway call: " + request.RequestUri); } return _responses.Dequeue()(); } }