using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using Heimdall.Api.Controllers;
using Xunit;
namespace Heimdall.Web.Tests.Controllers;
public sealed class UpdatesControllerRetryTests
{
[Theory]
[InlineData("
Proxy error")]
[InlineData("{not-json")]
public async Task ReadForgejoReleaseAsync_returns_null_for_non_json_responses(string body)
{
var result = await ReadReleaseAsync(new StringContent(body, Encoding.UTF8, "text/html"));
Assert.Null(result);
}
[Fact]
public async Task ReadForgejoReleaseAsync_returns_release_for_valid_json()
{
var result = await ReadReleaseAsync(new StringContent(
"{\"tag_name\":\"v1.2.3\",\"body\":\"notes\"}", Encoding.UTF8, "application/json"));
Assert.NotNull(result);
Assert.Equal("v1.2.3", result.GetType().GetProperty("TagName")?.GetValue(result));
Assert.Equal("notes", result.GetType().GetProperty("Body")?.GetValue(result));
}
[Fact]
public async Task ReadForgejoReleaseAsync_rejects_content_larger_than_limit()
{
using var content = new StringContent("{}", Encoding.UTF8, "application/json");
content.Headers.ContentLength = 1_048_577;
var result = await ReadReleaseAsync(content);
Assert.Null(result);
}
[Fact]
public async Task ReadForgejoReleaseAsync_rejects_oversized_stream_without_content_length()
{
using var content = new StringContent(new string(' ', 1_048_577), Encoding.UTF8, "application/json");
content.Headers.ContentLength = null;
var result = await ReadReleaseAsync(content);
Assert.Null(result);
}
private static async Task