Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Hints now accept byte[] as attachment #3352

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/Sentry/SentryHint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public SentryHint(string key, object? value)
internal void AddAttachmentsFromScope(Scope scope) => _attachments.AddRange(scope.Attachments);

/// <summary>
/// Creates a new Hint with one or more attachments.
/// Takes a path and adds the file as an attachment to the hint.
/// </summary>
/// <param name="filePath">The path to the file to attach.</param>
/// <param name="type">The type of attachment.</param>
Expand All @@ -80,6 +80,30 @@ public SentryHint(string key, object? value)
}
}

/// <summary>
/// Adds a 'byte[]' as attachment to the hind.
/// </summary>
/// <param name="data">The byte array to be attached</param>
/// <param name="fileName">The filename for the attachment</param>
/// <param name="type">The type of attachment.</param>
/// <param name="contentType">The content type of the attachment.</param>
public void AddAttachment(
byte[] data,
string fileName,
AttachmentType type = AttachmentType.Default,
string? contentType = null)
{
if (_options is not null)
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
{
_attachments.Add(
new SentryAttachment(
type,
new ByteAttachmentContent(data),
fileName,
contentType));
}
}

/// <summary>
/// Creates a new Hint with one or more attachments.
/// </summary>
Expand Down
20 changes: 19 additions & 1 deletion test/Sentry.Tests/HintTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public HintTests()
}

[Fact]
public void AddAttachment_AddsToHint()
public void AddAttachment_Files_AddsToHint()
{
// Arrange
var attachmentPath1 = Path.Combine(_testDirectory, Path.GetTempFileName());
Expand All @@ -29,6 +29,24 @@ public void AddAttachment_AddsToHint()
Assert.Equal(2, hint.Attachments.Count);
}

[Fact]
public void AddAttachment_ByteArray_AddsToHint()
{
// Arrange
var byteArray1 = new byte[5];
var byteArray2 = new byte[10];


var hint = new SentryHint(new SentryOptions());

// Act
hint.AddAttachment(byteArray1, "byteArray1");
hint.AddAttachment(byteArray2, "byteArray2");

// Assert
Assert.Equal(2, hint.Attachments.Count);
}

[Fact]
public void Clear_WithEntries_ClearsHintEntries()
{
Expand Down
Loading