Files
Chronos/tests/Titano.Server.Tests/Http.cs
T
Claude 80b91d0585 test(server): auth, uploads, ranges, sync, jobs, render nodes, events
Covers bootstrap admin, sign-in failure, lockout and rate limit, token
revocation and expiry, 401 versus 403, CSRF on cookie sessions, the error
shape and API header, chunked upload happy path, resume, hash
short-circuit, damaged chunk, quota, Range on originals, thumbnails and
exports, path traversal, settings_version conflicts, job state
transitions with a fake handler, render node claim, heartbeat timeout and
reassignment, output upload, WebSocket progress events, the discovery
beacon, the Windows service command line and status rules, and a
PostgreSQL run (TITANO_TEST_PG).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 12:28:47 +00:00

42 lines
1.9 KiB
C#

using System.Net;
using System.Net.Http.Json;
using System.Text.Json.Serialization.Metadata;
using Titano.Protocol;
using Titano.Testing;
namespace Titano.Server.Tests;
/// <summary>Assertions and JSON helpers for API calls.</summary>
internal static class Http
{
/// <summary>The body of a successful response (an error response fails the test with its body).</summary>
public static async Task<T> ReadAsync<T>(this HttpResponseMessage response, JsonTypeInfo<T> type) where T : class
{
if (!response.IsSuccessStatusCode) Assert.Fail($"Expected success, got {(int)response.StatusCode}: {await response.Content.ReadAsStringAsync()}");
return Assert.NotNull(await response.Content.ReadFromJsonAsync(type));
}
/// <summary>Asserts the status, the <c>{ code, message, details }</c> body with <paramref name="code"/>, and the API header.</summary>
public static async Task<ApiError> ExpectErrorAsync(this HttpResponseMessage response, HttpStatusCode status, string code)
{
var body = await response.Content.ReadAsStringAsync();
Assert.Equal(status, response.StatusCode, body);
Assert.Equal("1", response.Headers.GetValues(ApiVersion.Header).Single());
var error = Assert.NotNull(System.Text.Json.JsonSerializer.Deserialize(body, ProtocolJson.Default.ApiError));
Assert.Equal(code, error.Code, body);
Assert.False(string.IsNullOrWhiteSpace(error.Message), "message is empty");
return error;
}
public static async Task BytesEqualAsync(byte[] expected, Task<byte[]> actual)
{
var bytes = await actual;
Assert.BytesEqual(expected, bytes);
}
public static async Task ExpectAsync(this HttpResponseMessage response, HttpStatusCode status)
{
if (response.StatusCode != status) Assert.Fail($"Expected {(int)status}, got {(int)response.StatusCode}: {await response.Content.ReadAsStringAsync()}");
}
}