Esempio n. 1
0
        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");
        }
Esempio n. 2
0
        public async Task SendEnvelopeAsync_ItemRateLimit_DropsItem()
        {
            // Arrange
            using var httpHandler = new FakeHttpClientHandler(
                      _ => 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);
        }