2026-11-06 16:02:48 +01:00
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Reflection;
|
2026-11-07 18:19:56 +01:00
|
|
|
using System.Text;
|
2026-11-06 16:02:48 +01:00
|
|
|
using Heimdall.Api.Controllers;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Web.Tests.Controllers;
|
|
|
|
|
|
|
|
|
|
public sealed class UpdatesControllerRetryTests
|
|
|
|
|
{
|
2026-11-07 18:19:56 +01:00
|
|
|
[Theory]
|
|
|
|
|
[InlineData("<html><title>Proxy error</title></html>")]
|
|
|
|
|
[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<object?> ReadReleaseAsync(HttpContent content)
|
|
|
|
|
{
|
|
|
|
|
var method = typeof(UpdatesController).GetMethod("ReadForgejoReleaseAsync",
|
|
|
|
|
BindingFlags.NonPublic | BindingFlags.Static);
|
|
|
|
|
Assert.NotNull(method);
|
|
|
|
|
var task = method.Invoke(null, [content, CancellationToken.None]);
|
|
|
|
|
Assert.NotNull(task);
|
|
|
|
|
await (Task)task;
|
|
|
|
|
return task.GetType().GetProperty("Result")?.GetValue(task);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-06 16:02:48 +01:00
|
|
|
[Theory]
|
|
|
|
|
[InlineData(HttpStatusCode.RequestTimeout, true)]
|
|
|
|
|
[InlineData(HttpStatusCode.TooManyRequests, true)]
|
|
|
|
|
[InlineData(HttpStatusCode.BadGateway, true)]
|
|
|
|
|
[InlineData(HttpStatusCode.ServiceUnavailable, true)]
|
|
|
|
|
[InlineData(HttpStatusCode.NotFound, false)]
|
|
|
|
|
[InlineData(HttpStatusCode.BadRequest, false)]
|
|
|
|
|
public void IsTransientStatusCode_classifies_only_retryable_http_responses(HttpStatusCode code, bool expected)
|
|
|
|
|
{
|
|
|
|
|
var method = typeof(UpdatesController).GetMethod("IsTransientStatusCode",
|
|
|
|
|
BindingFlags.NonPublic | BindingFlags.Static);
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(method);
|
|
|
|
|
Assert.Equal(expected, method.Invoke(null, [code]));
|
|
|
|
|
}
|
|
|
|
|
}
|