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> /// <summary>
/// Unique object identifier. /// Unique object identifier.
/// The format and length of IDs may change over time.
/// </summary> /// </summary>
[JsonPropertyName("id")] [JsonPropertyName("id")]
public string Id { get; init; } = string.Empty; public string Id { get; init; } = string.Empty;
/// <summary> /// <summary>
/// Object type. /// Object type.
/// For files, this is always "file".
/// </summary> /// </summary>
[JsonPropertyName("type")] [JsonPropertyName("type")]
public string Type { get; init; } = "file"; public string Type { get; init; } = "file";
@@ -24,26 +22,26 @@ public class AnthropicFile
/// <summary> /// <summary>
/// Original filename of the uploaded file. /// Original filename of the uploaded file.
/// </summary> /// </summary>
[JsonPropertyName("file_name")] [JsonPropertyName("filename")]
public string FileName { get; init; } = string.Empty; public string Name { get; init; } = string.Empty;
/// <summary> /// <summary>
/// RFC 3339 datetime string representing when the file was created. /// Date file was created.
/// </summary> /// </summary>
[JsonPropertyName("created_at")] [JsonPropertyName("created_at")]
public string CreatedAt { get; init; } = string.Empty; public DateTimeOffset CreatedAt { get; init; }
/// <summary> /// <summary>
/// Size of the file in bytes. /// Size of the file in bytes.
/// </summary> /// </summary>
[JsonPropertyName("size_bytes")] [JsonPropertyName("size_bytes")]
public long SizeBytes { get; init; } public long Size { get; init; }
/// <summary> /// <summary>
/// MIME type of the file. /// MIME type of the file.
/// </summary> /// </summary>
[JsonPropertyName("file_type")] [JsonPropertyName("mime_type")]
public string FileType { get; init; } = string.Empty; public string MimeType { get; init; } = string.Empty;
/// <summary> /// <summary>
/// Whether the file can be downloaded. /// Whether the file can be downloaded.
@@ -1928,4 +1928,41 @@ public class AnthropicApiClientTests : IntegrationTest
result.Value.Id.Should().Be(batchId); result.Value.Id.Should().Be(batchId);
result.Value.Type.Should().Be("message_batch_deleted"); 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 CountTokensEndpoint = $"{BaseUrl}/messages/count_tokens";
private static readonly string MessageBatchesEndpoint = $"{BaseUrl}/messages/batches"; private static readonly string MessageBatchesEndpoint = $"{BaseUrl}/messages/batches";
private static readonly string ModelsEndpoint = $"{BaseUrl}/models"; private static readonly string ModelsEndpoint = $"{BaseUrl}/models";
private static readonly string FilesEndpoint = $"{BaseUrl}/files";
private static MockedRequest SetupBaseRequest( private static MockedRequest SetupBaseRequest(
this MockHttpMessageHandler mockHttpMessageHandler, this MockHttpMessageHandler mockHttpMessageHandler,
@@ -103,4 +104,10 @@ public static class MockHttpMessageHandlerExtensions
return mockHttpMessageHandler return mockHttpMessageHandler
.SetupBaseRequest(HttpMethod.Delete, $"{MessageBatchesEndpoint}/{batchId}"); .SetupBaseRequest(HttpMethod.Delete, $"{MessageBatchesEndpoint}/{batchId}");
} }
public static MockedRequest WhenCreateFileRequest(this MockHttpMessageHandler mockHttpMessageHandler)
{
return mockHttpMessageHandler
.SetupBaseRequest(HttpMethod.Post, FilesEndpoint);
}
} }