tests: add integration test for creating file

This commit is contained in:
Stevan Freeborn
2025-07-14 23:30:02 -05:00
parent f4ffcf5fbc
commit b26e663960
3 changed files with 51 additions and 9 deletions
+7 -9
View File
@@ -9,14 +9,12 @@ public class AnthropicFile
{
/// <summary>
/// Unique object identifier.
/// The format and length of IDs may change over time.
/// </summary>
[JsonPropertyName("id")]
public string Id { get; init; } = string.Empty;
/// <summary>
/// Object type.
/// For files, this is always "file".
/// </summary>
[JsonPropertyName("type")]
public string Type { get; init; } = "file";
@@ -24,26 +22,26 @@ public class AnthropicFile
/// <summary>
/// Original filename of the uploaded file.
/// </summary>
[JsonPropertyName("file_name")]
public string FileName { get; init; } = string.Empty;
[JsonPropertyName("filename")]
public string Name { get; init; } = string.Empty;
/// <summary>
/// RFC 3339 datetime string representing when the file was created.
/// Date file was created.
/// </summary>
[JsonPropertyName("created_at")]
public string CreatedAt { get; init; } = string.Empty;
public DateTimeOffset CreatedAt { get; init; }
/// <summary>
/// Size of the file in bytes.
/// </summary>
[JsonPropertyName("size_bytes")]
public long SizeBytes { get; init; }
public long Size { get; init; }
/// <summary>
/// MIME type of the file.
/// </summary>
[JsonPropertyName("file_type")]
public string FileType { get; init; } = string.Empty;
[JsonPropertyName("mime_type")]
public string MimeType { get; init; } = string.Empty;
/// <summary>
/// Whether the file can be downloaded.
@@ -1928,4 +1928,41 @@ public class AnthropicApiClientTests : IntegrationTest
result.Value.Id.Should().Be(batchId);
result.Value.Type.Should().Be("message_batch_deleted");
}
[Fact]
public async Task CreateFileAsync_WhenCalled_ItShouldReturnFile()
{
_mockHttpMessageHandler
.WhenCreateFileRequest()
.Respond(
HttpStatusCode.OK,
"application/json",
@"{
""created_at"": ""2023-11-07T05:31:56Z"",
""downloadable"": false,
""filename"": ""example.txt"",
""id"": ""file_013Zva2CMHLNnXjNJJKqJ2EF"",
""mime_type"": ""text/plain"",
""size_bytes"": 1234,
""type"": ""file""
}"
);
var fileContent = new MemoryStream(Encoding.UTF8.GetBytes("Example file content"));
var request = new CreateFileRequest(fileContent, "example.txt", "text/plain");
var result = await Client.CreateFileAsync(request);
result.IsSuccess.Should().BeTrue();
result.Value.Should().BeEquivalentTo(new AnthropicFile()
{
CreatedAt = DateTimeOffset.Parse("2023-11-07T05:31:56Z"),
Downloadable = false,
Name = "example.txt",
Id = "file_013Zva2CMHLNnXjNJJKqJ2EF",
MimeType = "text/plain",
Size = 1234,
Type = "file"
});
}
}
@@ -20,6 +20,7 @@ public static class MockHttpMessageHandlerExtensions
private static readonly string CountTokensEndpoint = $"{BaseUrl}/messages/count_tokens";
private static readonly string MessageBatchesEndpoint = $"{BaseUrl}/messages/batches";
private static readonly string ModelsEndpoint = $"{BaseUrl}/models";
private static readonly string FilesEndpoint = $"{BaseUrl}/files";
private static MockedRequest SetupBaseRequest(
this MockHttpMessageHandler mockHttpMessageHandler,
@@ -103,4 +104,10 @@ public static class MockHttpMessageHandlerExtensions
return mockHttpMessageHandler
.SetupBaseRequest(HttpMethod.Delete, $"{MessageBatchesEndpoint}/{batchId}");
}
public static MockedRequest WhenCreateFileRequest(this MockHttpMessageHandler mockHttpMessageHandler)
{
return mockHttpMessageHandler
.SetupBaseRequest(HttpMethod.Post, FilesEndpoint);
}
}