Validate mimeType in ImageContentBlock.FromBytes and AudioContentBlock.FromBytes (#1321)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
This commit is contained in:
parent
3727dc4aaa
commit
539481f999
2 changed files with 26 additions and 2 deletions
|
|
@ -425,9 +425,12 @@ public sealed class ImageContentBlock : ContentBlock
|
|||
/// <remarks>
|
||||
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
|
||||
/// </remarks>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="mimeType"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentException"><paramref name="mimeType"/> is empty or composed entirely of whitespace.</exception>
|
||||
public static ImageContentBlock FromBytes(ReadOnlyMemory<byte> bytes, string mimeType)
|
||||
{
|
||||
Throw.IfNullOrWhiteSpace(mimeType);
|
||||
|
||||
ReadOnlyMemory<byte> data = EncodingUtilities.EncodeToBase64Utf8(bytes);
|
||||
|
||||
return new()
|
||||
|
|
@ -510,9 +513,12 @@ public sealed class AudioContentBlock : ContentBlock
|
|||
/// <remarks>
|
||||
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
|
||||
/// </remarks>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="mimeType"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentException"><paramref name="mimeType"/> is empty or composed entirely of whitespace.</exception>
|
||||
public static AudioContentBlock FromBytes(ReadOnlyMemory<byte> bytes, string mimeType)
|
||||
{
|
||||
Throw.IfNullOrWhiteSpace(mimeType);
|
||||
|
||||
ReadOnlyMemory<byte> data = EncodingUtilities.EncodeToBase64Utf8(bytes);
|
||||
|
||||
return new()
|
||||
|
|
|
|||
|
|
@ -272,4 +272,22 @@ public class ContentBlockTests
|
|||
Assert.Equal("Paris", result.Input.GetProperty("city").GetString());
|
||||
Assert.Equal("metric", result.Input.GetProperty("units").GetString());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void ImageContentBlock_FromBytes_ThrowsForNullOrWhiteSpaceMimeType(string? mimeType)
|
||||
{
|
||||
Assert.ThrowsAny<ArgumentException>(() => ImageContentBlock.FromBytes((byte[])[1, 2, 3], mimeType!));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void AudioContentBlock_FromBytes_ThrowsForNullOrWhiteSpaceMimeType(string? mimeType)
|
||||
{
|
||||
Assert.ThrowsAny<ArgumentException>(() => AudioContentBlock.FromBytes((byte[])[1, 2, 3], mimeType!));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue