Skip to content

Commit b3d1c7d

Browse files
EnvelopeTests
1 parent f92258e commit b3d1c7d

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/Sentry/Protocol/Envelopes/EnvelopeItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Sentry.Protocol.Envelopes;
1010
/// </summary>
1111
public sealed class EnvelopeItem : ISerializable, IDisposable
1212
{
13-
private const string TypeKey = "type";
13+
internal const string TypeKey = "type";
1414

1515
internal const string TypeValueEvent = "event";
1616
internal const string TypeValueFeedback = "feedback";

test/Sentry.Tests/Protocol/Envelopes/EnvelopeTests.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,90 @@ public async Task Roundtrip_WithUserFeedback_Success()
819819
envelopeRoundtrip.Should().BeEquivalentTo(envelope);
820820
}
821821

822+
[Fact]
823+
public async Task Roundtrip_WithFeedback_Success()
824+
{
825+
// Arrange
826+
var feedback = new SentryFeedback(
827+
"Everything is great!",
828+
829+
"Someone Nice",
830+
"fake-replay-id",
831+
"https://www.example.com",
832+
SentryId.Create()
833+
);
834+
var evt = new SentryEvent { Level = SentryLevel.Info,
835+
Contexts =
836+
{
837+
Feedback = feedback
838+
}
839+
};
840+
841+
using var envelope = Envelope.FromFeedback(evt);
842+
843+
using var stream = new MemoryStream();
844+
845+
// Act
846+
await envelope.SerializeAsync(stream, _testOutputLogger);
847+
stream.Seek(0, SeekOrigin.Begin);
848+
849+
using var envelopeRoundtrip = await Envelope.DeserializeAsync(stream);
850+
851+
// Assert
852+
envelopeRoundtrip.Should().BeEquivalentTo(envelope);
853+
}
854+
855+
[Fact]
856+
public void FromFeedback_NoFeedbackContext_Throws()
857+
{
858+
// Arrange
859+
var evt = new SentryEvent { Level = SentryLevel.Info };
860+
861+
// Act
862+
Action act = () => Envelope.FromFeedback(evt);
863+
864+
// Assert
865+
act.Should().Throw<ArgumentException>()
866+
.WithMessage("Unable to create envelope - the event does not contain any feedback.");
867+
}
868+
869+
[Fact]
870+
public void FromFeedback_MultipleAttachments_LogsWarning()
871+
{
872+
// Arrange
873+
var feedback = new SentryFeedback(
874+
"Everything is great!",
875+
876+
"Someone Nice",
877+
"fake-replay-id",
878+
"https://www.example.com",
879+
SentryId.Create()
880+
);
881+
var evt = new SentryEvent { Level = SentryLevel.Info,
882+
Contexts =
883+
{
884+
Feedback = feedback
885+
}
886+
};
887+
var logger = Substitute.For<IDiagnosticLogger>();
888+
logger.IsEnabled(Arg.Any<SentryLevel>()).Returns(true);
889+
890+
List<SentryAttachment> attachments = [
891+
AttachmentHelper.FakeAttachment("file1.txt"), AttachmentHelper.FakeAttachment("file2.txt")
892+
];
893+
894+
// Act
895+
using var envelope = Envelope.FromFeedback(evt, logger, attachments);
896+
897+
// Assert
898+
logger.Received(1).Log(
899+
SentryLevel.Warning,
900+
Arg.Is<string>(m => m.Contains("Feedback can only contain one attachment")),
901+
null,
902+
Arg.Any<object[]>());
903+
envelope.Items.Should().ContainSingle(item => item.Header[EnvelopeItem.TypeKey].ToString() == EnvelopeItem.TypeValueAttachment);
904+
}
905+
822906
[Fact]
823907
public async Task Roundtrip_WithSession_Success()
824908
{

0 commit comments

Comments
 (0)