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
58 lines
2.7 KiB
C#
58 lines
2.7 KiB
C#
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using System.Security.Cryptography;
|
|
using Titano.Protocol;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Server.Tests;
|
|
|
|
/// <summary>Raw-HTTP upload helpers (the protocol client is exercised separately).</summary>
|
|
internal static class Uploads
|
|
{
|
|
public static byte[] Photo(int size, int seed)
|
|
{
|
|
var bytes = new byte[size];
|
|
new Random(seed).NextBytes(bytes);
|
|
return bytes;
|
|
}
|
|
|
|
public static string Sha(byte[] data) => Convert.ToHexStringLower(SHA256.HashData(data));
|
|
|
|
public static async Task<StartUploadResponse> StartAsync(HttpClient client, byte[] data, string fileName = "IMG_0001.CR3", Guid? sequenceId = null,
|
|
string? sessionKey = null, HttpStatusCode expected = HttpStatusCode.Created)
|
|
{
|
|
using var response = await client.PostAsJsonAsync("/api/v1/uploads",
|
|
new StartUploadRequest(sequenceId, fileName, data.Length, Sha(data), new DateTime(2026, 9, 28, 18, 2, 11), sessionKey), ProtocolJson.Default.StartUploadRequest);
|
|
await response.ExpectAsync(expected);
|
|
return await response.ReadAsync(ProtocolJson.Default.StartUploadResponse);
|
|
}
|
|
|
|
public static async Task<HttpResponseMessage> PutChunkAsync(HttpClient client, Guid uploadId, int index, ReadOnlyMemory<byte> data)
|
|
{
|
|
var content = new ByteArrayContent(data.ToArray());
|
|
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
|
|
return await client.PutAsync($"/api/v1/uploads/{uploadId}/chunks/{index}", content);
|
|
}
|
|
|
|
public static ReadOnlyMemory<byte> Chunk(byte[] data, int chunkSize, int index) =>
|
|
data.AsMemory(index * chunkSize, Math.Min(chunkSize, data.Length - index * chunkSize));
|
|
|
|
/// <summary>Start, every chunk, complete; returns the committed frame.</summary>
|
|
public static async Task<CompleteUploadResponse> UploadAsync(HttpClient client, byte[] data, string fileName = "IMG_0001.CR3", Guid? sequenceId = null,
|
|
string? sessionKey = null)
|
|
{
|
|
var start = await StartAsync(client, data, fileName, sequenceId, sessionKey);
|
|
var id = Assert.NotNull(start.UploadId);
|
|
var chunks = (data.Length + start.ChunkSize - 1) / start.ChunkSize;
|
|
for (var i = 0; i < chunks; i++)
|
|
{
|
|
using var put = await PutChunkAsync(client, id, i, Chunk(data, start.ChunkSize, i));
|
|
await put.ExpectAsync(HttpStatusCode.OK);
|
|
}
|
|
using var complete = await client.PostAsync($"/api/v1/uploads/{id}/complete", null);
|
|
await complete.ExpectAsync(HttpStatusCode.OK);
|
|
return await complete.ReadAsync(ProtocolJson.Default.CompleteUploadResponse);
|
|
}
|
|
}
|