Skip to content

Commit a70f78c

Browse files
SentryClient tests
1 parent b3d1c7d commit a70f78c

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
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-
internal const string TypeKey = "type";
13+
private const string TypeKey = "type";
1414

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

src/Sentry/SentryClient.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public void CaptureFeedback(SentryFeedback feedback, Scope? scope = null, Sentry
8686
{
8787
if (string.IsNullOrEmpty(feedback.Message))
8888
{
89-
// Ignore the user feedback if EventId is empty
9089
_options.LogWarning("Feedback dropped due to empty message.");
9190
return;
9291
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ public void FromFeedback_MultipleAttachments_LogsWarning()
900900
Arg.Is<string>(m => m.Contains("Feedback can only contain one attachment")),
901901
null,
902902
Arg.Any<object[]>());
903-
envelope.Items.Should().ContainSingle(item => item.Header[EnvelopeItem.TypeKey].ToString() == EnvelopeItem.TypeValueAttachment);
903+
envelope.Items.Should().ContainSingle(item => item.TryGetType() == EnvelopeItem.TypeValueAttachment);
904904
}
905905

906906
[Fact]

test/Sentry.Tests/SentryClientTests.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ public void CaptureUserFeedback_EventIdEmpty_FeedbackIgnored()
836836
//Assert
837837
_ = sut.Worker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
838838
}
839+
839840
[Fact]
840841
public void Dispose_should_only_flush()
841842
{
@@ -849,6 +850,93 @@ public void Dispose_should_only_flush()
849850
client.CaptureEvent(new SentryEvent { Message = "Test" });
850851
}
851852

853+
[Fact]
854+
public void CaptureFeedback_DisposedClient_DoesNotThrow()
855+
{
856+
// Arrange
857+
var feedback = new SentryFeedback("Everything is great!");
858+
859+
var sut = _fixture.GetSut();
860+
sut.Dispose();
861+
862+
// Act / Assert
863+
sut.CaptureFeedback(feedback);
864+
}
865+
866+
[Fact]
867+
public void CaptureFeedback_NoMessage_FeedbackIgnored()
868+
{
869+
//Arrange
870+
var sut = _fixture.GetSut();
871+
var feedback = new SentryFeedback(string.Empty);
872+
873+
//Act
874+
sut.CaptureFeedback(feedback);
875+
876+
//Assert
877+
_ = sut.Worker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
878+
}
879+
880+
[Fact]
881+
public void CaptureFeedback_ValidUserFeedback_FeedbackRegistered()
882+
{
883+
//Arrange
884+
var sut = _fixture.GetSut();
885+
var feedback = new SentryFeedback("Everything is great!");
886+
887+
//Act
888+
sut.CaptureFeedback(feedback);
889+
890+
//Assert
891+
_ = sut.Worker.Received(1).EnqueueEnvelope(Arg.Any<Envelope>());
892+
}
893+
894+
[Fact]
895+
public void CaptureFeedback_WithScope_ScopeCopiedToEvent()
896+
{
897+
//Arrange
898+
const string expectedBreadcrumb = "test";
899+
var scope = new Scope(_fixture.SentryOptions);
900+
scope.AddBreadcrumb(expectedBreadcrumb);
901+
scope.Level = SentryLevel.Warning;
902+
var feedback = new SentryFeedback("Everything is great!");
903+
var sut = _fixture.GetSut();
904+
905+
Envelope envelope = null;
906+
sut.Worker.When(w => w.EnqueueEnvelope(Arg.Any<Envelope>()))
907+
.Do(callback => envelope = callback.Arg<Envelope>());
908+
909+
//Act
910+
sut.CaptureFeedback(feedback, scope);
911+
912+
//Assert
913+
_ = sut.Worker.Received(1).EnqueueEnvelope(Arg.Any<Envelope>());
914+
envelope.Should().NotBeNull();
915+
envelope.Items.Should().Contain(item => item.TryGetType() == EnvelopeItem.TypeValueFeedback);
916+
var item = envelope.Items.First(x => x.TryGetType() == EnvelopeItem.TypeValueFeedback);
917+
var @event = (SentryEvent)((JsonSerializable)item.Payload).Source;
918+
@event.Level.Should().Be(scope.Level);
919+
Assert.Equal(scope.Breadcrumbs, @event.Breadcrumbs);
920+
}
921+
922+
[Fact]
923+
public void CaptureFeedback_WithHint_HasHintAttachment()
924+
{
925+
//Arrange
926+
var sut = _fixture.GetSut();
927+
var feedback = new SentryFeedback("Everything is great!");
928+
var hint = new SentryHint();
929+
hint.Attachments.Add(AttachmentHelper.FakeAttachment("foo.txt"));
930+
931+
//Act
932+
sut.CaptureFeedback(feedback, null, hint);
933+
934+
//Assert
935+
_ = sut.Worker.Received(1).EnqueueEnvelope(Arg.Any<Envelope>());
936+
sut.Worker.Received(1).EnqueueEnvelope(Arg.Is<Envelope>(envelope =>
937+
envelope.Items.Count(item => item.TryGetType() == "attachment") == 1));
938+
}
939+
852940
[Fact]
853941
public void CaptureUserFeedback_DisposedClient_DoesNotThrow()
854942
{

0 commit comments

Comments
 (0)