public async Task SendAsync_SentryTraceHeaderAlreadySet_NotOverwritten() { // Arrange var hub = Substitute.For <IHub>(); hub.GetTraceHeader().ReturnsForAnyArgs( SentryTraceHeader.Parse("75302ac48a024bde9a3b3734a82e36c8-1000000000000000-0") ); using var innerHandler = new RecordingHttpMessageHandler(new FakeHttpMessageHandler()); using var sentryHandler = new SentryHttpMessageHandler(innerHandler, hub); using var client = new HttpClient(sentryHandler); client.DefaultRequestHeaders.Add("sentry-trace", "foobar"); // Act await client.GetAsync("https://example.com/"); using var request = innerHandler.GetRequests().Single(); // Assert request.Headers.Should().Contain(h => h.Key == "sentry-trace" && string.Concat(h.Value) == "foobar" ); }
public async Task SendEnvelopeAsync_AttachmentTooLarge_DropsItem() { // Arrange using var httpHandler = new RecordingHttpMessageHandler( new FakeHttpMessageHandler() ); var logger = new InMemoryDiagnosticLogger(); var httpTransport = new HttpTransport( new SentryOptions { Dsn = DsnSamples.ValidDsnWithSecret, MaxAttachmentSize = 1, DiagnosticLogger = logger, Debug = true }, new HttpClient(httpHandler) ); var attachmentNormal = new Attachment( AttachmentType.Default, new StreamAttachmentContent(new MemoryStream(new byte[] { 1 })), "test1.txt", null ); var attachmentTooBig = new Attachment( AttachmentType.Default, new StreamAttachmentContent(new MemoryStream(new byte[] { 1, 2, 3, 4, 5 })), "test2.txt", null ); using var envelope = Envelope.FromEvent( new SentryEvent(), new[] { attachmentNormal, attachmentTooBig } ); // Act await httpTransport.SendEnvelopeAsync(envelope); var lastRequest = httpHandler.GetRequests().Last(); var actualEnvelopeSerialized = await lastRequest.Content.ReadAsStringAsync(); // Assert // (the envelope should have only one item) logger.Entries.Should().Contain(e => e.Message == "Attachment '{0}' dropped because it's too large ({1} bytes)." && e.Args[0].ToString() == "test2.txt" && e.Args[1].ToString() == "5" ); actualEnvelopeSerialized.Should().NotContain("test2.txt"); }
public async Task SendEnvelopeAsync_AttachmentFail_DropsItem() { // Arrange using var httpHandler = new RecordingHttpMessageHandler( new FakeHttpMessageHandler()); var logger = new InMemoryDiagnosticLogger(); var httpTransport = new HttpTransport( new SentryOptions { Dsn = DsnSamples.ValidDsnWithSecret, MaxAttachmentSize = 1, DiagnosticLogger = logger, Debug = true }, new HttpClient(httpHandler)); var attachment = new Attachment( AttachmentType.Default, new FileAttachmentContent("test1.txt"), "test1.txt", null); using var envelope = Envelope.FromEvent( new SentryEvent(), logger, new[] { attachment }); // Act await httpTransport.SendEnvelopeAsync(envelope); var lastRequest = httpHandler.GetRequests().Last(); var actualEnvelopeSerialized = await lastRequest.Content.ReadAsStringAsync(); // Assert // (the envelope should have only one item) logger.Entries.Should().Contain(e => e.Message == "Failed to add attachment: {0}." && (string)e.Args[0] == "test1.txt"); actualEnvelopeSerialized.Should().NotContain("test2.txt"); }
public async Task SendEnvelopeAsync_ItemRateLimit_DoesNotAffectNextSessionWithDifferentId() { // Arrange using var httpHandler = new RecordingHttpMessageHandler( new FakeHttpMessageHandler( () => SentryResponses.GetRateLimitResponse("1:session") ) ); var httpTransport = new HttpTransport( new SentryOptions { Dsn = DsnSamples.ValidDsnWithSecret }, new HttpClient(httpHandler) ); var session = new Session("foo", "bar", "baz"); // First request always goes through await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent())); // Send session update with init=true await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent(), null, session.CreateUpdate(true, DateTimeOffset.Now))); // Pretend the rate limit has already passed foreach (var(category, _) in httpTransport.CategoryLimitResets) { httpTransport.CategoryLimitResets[category] = DateTimeOffset.Now - TimeSpan.FromDays(1); } // Act // Send an update for different session with init=false (should NOT get promoted) var nextSession = new Session("foo2", "bar2", "baz2"); await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent(), null, nextSession.CreateUpdate(false, DateTimeOffset.Now))); var lastRequest = httpHandler.GetRequests().Last(); var actualEnvelopeSerialized = await lastRequest.Content.ReadAsStringAsync(); // Assert actualEnvelopeSerialized.Should().NotContain("\"init\":true"); }
public async Task SendEnvelopeAsync_ItemRateLimit_DropsItem() { // Arrange using var httpHandler = new RecordingHttpMessageHandler( new FakeHttpMessageHandler( () => SentryResponses.GetRateLimitResponse("1234:event, 897:transaction") )); var httpTransport = new HttpTransport( new SentryOptions { Dsn = DsnSamples.ValidDsnWithSecret }, new HttpClient(httpHandler)); // First request always goes through await httpTransport.SendEnvelopeAsync(Envelope.FromEvent(new SentryEvent())); var envelope = new Envelope( new Dictionary <string, object>(), new[] { // Should be dropped new EnvelopeItem( new Dictionary <string, object> { ["type"] = "event" }, new EmptySerializable()), new EnvelopeItem( new Dictionary <string, object> { ["type"] = "event" }, new EmptySerializable()), new EnvelopeItem( new Dictionary <string, object> { ["type"] = "transaction" }, new EmptySerializable()), // Should stay new EnvelopeItem( new Dictionary <string, object> { ["type"] = "other" }, new EmptySerializable()) }); var expectedEnvelope = new Envelope( new Dictionary <string, object>(), new[] { new EnvelopeItem( new Dictionary <string, object> { ["type"] = "other" }, new EmptySerializable()) }); var expectedEnvelopeSerialized = await expectedEnvelope.SerializeToStringAsync(); // Act await httpTransport.SendEnvelopeAsync(envelope); var lastRequest = httpHandler.GetRequests().Last(); var actualEnvelopeSerialized = await lastRequest.Content.ReadAsStringAsync(); // Assert actualEnvelopeSerialized.Should().BeEquivalentTo(expectedEnvelopeSerialized); }