using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace MCPForUnity.Editor.Services.AssetGen.Http
{
///
/// Test double for . Records every request it is handed and
/// returns a canned response, so provider adapters can be exercised without a network.
/// Match responses either with the delegate (full control) or with
/// (first entry whose key is contained in the request URL).
///
public sealed class FakeHttpTransport : IHttpTransport
{
public List RecordedRequests { get; } = new List();
/// Highest-priority responder; return null to fall through to .
public Func Handler { get; set; }
/// Canned responses keyed by a substring expected to appear in the request URL.
public Dictionary ByUrlSubstring { get; } = new Dictionary();
public Task SendAsync(HttpRequestSpec spec, CancellationToken ct)
{
RecordedRequests.Add(spec);
HttpResult result = Handler?.Invoke(spec);
if (result == null && spec?.Url != null)
{
foreach (var kv in ByUrlSubstring)
{
if (spec.Url.IndexOf(kv.Key, StringComparison.Ordinal) >= 0)
{
result = kv.Value;
break;
}
}
}
if (result == null)
{
result = new HttpResult
{
Status = 500,
IsSuccess = false,
Text = "FakeHttpTransport: no canned response matched " + (spec?.Url ?? "")
};
}
return Task.FromResult(result);
}
}
}